Friday, March 19, 2010

To remove old kernel from Ubuntu

To remove old kernels from ubuntu OS:

First check the current kernel version:
$ uname -r

Do not remove current version.

To remove old kernel version use command:
$sudo apt-get purge linux-image-2.6.XX-XX-generic


Then remove header of that version:
$sudo apt-get purge linux-headers-2.6.XX-XX

Tuesday, March 16, 2010

Cannot Update Ubuntu

Recently, When I was trying to update ubuntu, I was getting following error message:
E: Could not get lock /var/lib/apt/lists/lock - open (11 Resource temporarily unavailable)
E: Unable to lock the list directory

After searching on Ubuntu forums I found the solution to solve this Problem.

Use following command to see is their any synaptic pkg manager running?
      $ ps -e | grep apt

If yes, then kill all those processes using 
  $ sudo kill -9 processID

Then remove lock file from ur system:
  $  sudo rm /var/lib/apt/lists/lock

Now try to update ur system. It should work. Atleast it worked for me.



DOT Graphs

DOT (filename.dot) is a file format to draw graphs including directed graphs.
DOT writes graphs in .ps, .pdf, .gif, .png formats.

For example,

$ dot -Tps src.dot -o dest.ps

$ dot -Tpdf src.dot -o dest.pdf

Dot file  format is as follows:

digraph graphName {
 "Node 1"  -> "Node 2"  ;
 "Node 1" -> "Node 2" -> "Node 3";
}

Each line is terminated by semicolon (;), and arrow (->) is used to show directed arc.

digraph means directed graph whereas graph means undirected graph.

Within a main graph a subgraph define a subset of graph.
For example,
digraph GraphName {
"Node 1" -> "Node 2";
 subgraph SubGraphName{
   "Node 3" -> "Node 1";
   "Node 4" -> Node 3";
  }
}

Tuesday, March 2, 2010

JavaScript Injection

JavaScript Injection is a technique that allows you alter the content of current web page without actually leaving the current web page.  It is extremely useful when you want to spoof the contents that are sent to server using Forms. 

Basics of JS Injection:
JS injection means inserting or executing a script. You can execute a script from the URL bar of the web page which you want to alter.  To execute JS code, you must first clear the URL bar (Note: Don't press enter yet), that is, no http:// or anything else. 
Javascript can be executed from URL using javascript: protocol.
Try following code in the URL bar of the web page to display your message. 

              javascript:alert("Hello World!");

If you saw a window pop-up and saying Hello World, then congrats, you successfully did a JS injection test.
Cookie Editing:
This time we will try penetrate one level deeper and we will try to modify server state. 
One of the mechanism used to represent server state is using Cookies. Server identifies client state and authorization using Cookies. Therefore, it is worth to learn cookie alteration using JS injection technique. 

To check the cookies set by web site, use following script at URL bar:
         javascript:alert(document.cookie);

Above script will show you cookies set by web site. To modify any key=value pair, use following syntax:
        javascript:void(document.cookie="Key=Value");

 Above command can either alter existing Key=Value pair or add new Key=Value pair if it doesn't exists.  To edit or alter information we use void( ) function of JavaScript.

 For example, server set Authorization=no in Cookie and you want to modify this Key=value pair.  Then you can use script given below:
        javascript:void(document.cookie="Autorization=yes");

It is also useful to try an alert(document.cookie); script at the end of the same line to see what effect your altering had.
    
Form Modifications:
One way to edit values sent to web server from client using a Form is to store a web page on a local disk and modify its Form field values with whatever values you want and then submit the form to the server.

For example:
  Following HTML code snippet shows that hidden field is submitted when a submit button is clicked on Form. If we want to modify email address to get data sent by email to webmaster.

 
<form action="/missions/basic/process.php" method="post">
<input type="hidden" name="to" value="webmaster@mywebsite.com" />
<input type="submit" value="Click to Submit" />
</form>

First, we need to store this web page on local disk, and then modify it as shown below.

<form action="http://mywebsite.com/missions/basic/process.php" method="post">
<input type="hidden" name="to" value="altered@emailaddress.com" />
<input type="submit" value="Click to Submit" />
</form>

However, sometimes the website checks to see if you actually submitted it from the website  or not. To get around this, we can just edit the form using from javascript Injection.

Every form on a given webpage (unless named otherwise) is stored in the forms[x] array... where "x" is the number, in order from top to bottom, of all the forms in a page. Note that the forms start at 0, so the first form on the page would actually be 0, and the second would be 1 and so on.

Lets consider our previous form example:
<form action="/missions/basic/process.php" method="post">
<input type="hidden" name="to" value="webmaster@mywebsite.com" />
<input type="submit" value="Click to Submit" />
</form>
Note:Since this is the first form on the page, it is forms[0].

To check the value using JS, use following command:
      javascript:alert(document.forms[0].to.value)

In this case, It would pop up an alert that says "webmaster@mywebsite.com"

So here's how to Inject your email into it. You can use the same technique as shown earlier in the cookies editing :
   javascript:void(document.forms[0].to.value="altered@emailaddress.com");

Above script would change email address to altered@emailaddress.com. You can use alert( ) JavaScript function to check your work.

These are the most basic things you need to know about JS injection and useful in many cases.


    




Tuesday, February 23, 2010

How Download accelerator Works?

In this post I will explain the basic principle of Download accelerators (such as DAP, wxDownload Fast, etc) used to download files. 
How it speeds up the downloading. 

First Let me explain the difference between normal downloading of a file and downloading of a file using download accelerator.  If you use regular browser to download a file then it creates only one connection with the server to download the file, whereas if download accelerators is used to download file then it creates multiple connection with the server and downloads a file in chunks and upon completion of download it joins those chunks.  Number of chunks created by download accelerators is depend on its configuration. I used wxDownload Fast and configured it to create 3 chunks of a file. That is, it creates 3 connections with the server to download any file. 

Lets consider an example. 
I used wxDownload Fast as download accelerator and downloaded a file ymsgr8us.exe (Yahoo messenger) which is of size 9.9 MB.
The initial request sent by wxDownload Fast to server was ordinary request to retrieve file, as given below:


Hypertext Transfer Protocol
GET /dl/9073e1f8a8d00eb735874cd9d3b6769c/4b824b4c/30%2Fymsgr8us.exe HTTP/1.1\r\n
Request Method: GET
Request Version: HTTP/1.1
HOST: us.download.soft32.com\r\n
User-Agent: wxDownload Fast\r\n
Range: bytes=0-\r\n

In HTTP response, from Content-Length header field wxDownload Fast learned the actual size of file.  Once the file size is know to wxDownload Fast (in general, to download accelerators), depending on number of connection (chunks) to create it decides how big should be the chunk size. In our case, I configured it to create three chunks, therefore, it divides Content-Length value by 3 to create three chunks.

Then it sends another HTTP request to the server, by creating another connection. The HTTP request made by wxDownload to the server was as follows:

Hypertext Transfer Protocol
GET /download/63-164279-1/ymsgr8us.exe HTTP/1.1\r\n
Request Method: GET
Request URI: /download/63-164279-1/ymsgr8us.exe
Request Version: HTTP/1.1
HOST: www.soft32.com\r\n
User-Agent: wxDownload Fast\r\n
Range: bytes=3474600-\r\n

Note the Range field in HTTP request.  The range header field was
                     Range: bytes=3474600- 


It instructs the server to return a file from 3474600th byte onwards.  Although, download manager requested for entire file in its first connection, it would however terminate that connection as soon as it will receive upto bytes 3474599. Hence it would not wast resources and download duplicate byte streams. 


As now you can image what would be the third HTTP request. It is given below. observe the Range HTTP header field.   

Hypertext Transfer Protocol
GET /download/63-164279-1/ymsgr8us.exe HTTP/1.1\r\n
Request Method: GET
Request URI: /download/63-164279-1/ymsgr8us.exe
Request Version: HTTP/1.1
HOST: www.soft32.com\r\n
User-Agent: wxDownload Fast\r\n
Range: bytes=6949200-\r\n

This is the basic principle followed by download accelerators (such as wxDownload Fast, etc)  to download file quickly than normal browser download.


Friday, February 19, 2010

Heritrix and HTMLUnit

Hi folks, In this post I will explain how to build Heritrix from its source code and how to Integrate HTMLUnit into Heritrix.

First question comes to mind is, What is Heritrix? and why do we need HTMLUnit to Integrate into Heritrix?

Well, Heritrix is a open-source, Web crawler. Heritrix does not include web page level DOM model and JavaScript Interpreter. Therefore, if you want to crawl the web to look for malicious scripts or obfuscated JS, then you need a JS interpreter. Hence. HTMLUnit comes into play. HTMLUnit is a headless browser, which has got JS interpreter.

Steps to Build Heritrix:
  1. Download latest version of JDK rpm from sun website and install it. 
  2. Set JAVA_HOME and PATH environment to .bashrc file (~/.bashrc)
                 export JAVA_HOME=/usr/java/jdk1.6.x.x
                 export PATH=$JAVA_HOME/bin:$PATH
     Now JDK is ready to be used by Heritirx and Maven
     3.  We need maven 1.0.2 to build heritix.  Note: We need src of heritix so that we can modify it in future. Therefore do not use heritix binaries available on Ineternet. Build heritirx from src. Also Note the version of maven. it is very very important. Do not try with latest version of maven. It may not work. 
     4. Download binary of maven 1.0.2 and extract it somewhere on disk. Now set MAVEN_HOME
         environment for it as mention below.
            Edit (/etc/profile) file to insert following lines before unset i and unset pathmunge commands at the end of file.
            export MAVEN_HOME=/path_of_Maven_directory   
            pathmunge $MAVEN_HOME/bin before 

     Now logout and login again to reflect environment variable changes done above to be get reflected.

    5. Run maven -v command to test maven is running properly.
    6. Run maven jar command. this will create /root/.maven/repository directory.
    7. Now go into heritirx directory and run command maven dist
    8. This will create subdirectory target, and many other subdirectories inside target directory.
         target/distribution directory holds heritirx build version.
         It there is failure due to any dependency jar file then download that file from Internet and store it in either /root/.maven/cache or /root/.maven/repository/.../jar/ directory.
   9. Heritirx is build Successfully. Extract build version and test heritirx.
  10.  Launch heritrix by using command:
           $ HERITRIX_HOME/bin/heritrix --admin=LOGIN:PASSWORD
                 where $HERITRIX_HOME is the location of your untarred heritrix.?.?.?.tar.gz.

Integrating HTMLUNIT into Heritrix:

This is little bit tricky. You are at this point means you already have heritrix, sun JDK and maven.

Follow the steps given below:
Step 1: Download HTMLUnit (I used HTMLUnit 2.5). We don't need source code of HTMLUnit therefore download binary of HTMLUnit. We only need its JAR files.

Step 2: Copy all JAR files in HTMLUnit into lib sub-directory of heritrix folder. Do not replace files, which are already there, if you replace them, then you need to modify project.properties file. Only add those files which are not there.

Step 3: Edit project.xml file in heritrix directory. Bcoz we want to tell heritrix where HTMLUnit classes can be found. Add tag for each JAR file of HTMLUnit.

Sample of dependency tag is given below:
    <dependency>
       <id>htmlunit</id>
        <version>2.5</version>
         <url>http://htmlunit.sourceforge.net/ </url>
            <properties>
             <war.bundle>true </war.bundle  >
              <ear.bundle>true</ear.bundle>
                <ear.bundle.dir>APP-INF/lib</ear.bundle.dir>
                <description>
                Use to handle JS obfuscation. It is a headless browser.
                </description>
                <license>Apache 2.0
                    http://www.apache.org/licenses/LICENSE-2.0 </license>
             </properties> 
     </dependency>
Add this dependency tag for all JAR files of HTMLUnit.

Step 4: Edit project.properties file in heritirx directory to instruct maven that, do not try to download those dependency files from Internet, rather look into local directory. Syntax to do this can be easily found in project. properties file, simply make use of it.

For example:
            maven.jar.htmlunit = ${basedir}/lib/htmlunit-2.5.jar

Add a entry for each JAR file (that is, each dependency entry done in Step 3 ) of HTMLUnit.

Step 5. Done. Now build Heritrix again.

Monday, November 16, 2009

How to Reset Sansa Mp3 Player when it Freezes

I got 2GB Sansa Mp3 player, as a complementary accessaries with Nikon CoolPix L20.
My Mp3 Player freezes, when I am charging it and accedentally unplug it, witout ejecting it. Some time it hungs while plying Song.

After couple of try I found few solutions that can reboot Sansa MP3 Player, and it works with my Sansa MP3 player.

1.  Turn ON the Hold switch. (move hold button to Hold Position)
2.  Press and hold Menu and Rec buttons at the same time until the Sansa MP3 player reboots.

Alternate Solution is :
Hold Menu (Power) button for atleast 20 seconds. Sometimes this trick works.

Hard Reset
To perform hardware reset on the Sansa Express, press and hold the "Select" button while pressing the Volume UP (+) buttons simultaneously once.

How to use ClamAv Antivirus on Ubuntu?

This tutorial will explain you, how to Install and use ClamAv Antivirus on Ubuntu. ClamAv antivirus is a free antivirus, available in ubuntu repository.

To install ClamAv antivirus and GUI to manage it on ubuntu 9.10,  use Ubuntu Software Center to install "KlamAV".  KlamAV is an anti-virus manager for the KDE desktop. It allows virus scanning, software/update databases, etc.

I am using "KlamAV" as a GUI tool to manage and use ClamAV antivirus.  You can also use "clamtk" as a GUI to use CLAMAV in Gnome or KDE.
Use following command to install "clamtk"
sudo apt-get install clamtk

Once it is installed on your machine,  then install anti-virus updater, so that you can get  latest update of ClamAV from Internet.
sudo apt-get install clamav-freshclam

Use command "sudo freshclam" to update ClamAV database. 
To check the version of ClamAV use following Command:
clamdscan -V


To do command line scanning:
1.  To scan specific folder:
     sudo clamscan -r /home/directoryname/subdirectory
     Above command will display the name of files in subdirectory on the screen. You can use -i parameter to display only infected files. 
2.  To scan all files on your computer
      sudo clamscan  -r   --bell  -i   /
     Above command will display only infected files and will bell the ring when virus found.
3. To remove infected file from your computer
    sudo clamscan -r -remove /









Friday, November 13, 2009

Lambda Calculus Type inference examples

1] \ x -> x
 Above function takes one argument and returns the same argument as result, therefore type of argument and return value must be same.

type: a - > a

2] \ x y -> x
Above function takes two arguments and returns the first argument as result, therefore type of first argument and return value must be same.

type: a -> b -> a

3] \f g -> g (f g)
Above function takes two parameter f and g.

To solve this, my algorithm is as follows:

First look at function body from right-to-left. Function body is g(f g)

Assume type of g is t1. That is,  ( g = t1).

Now, take next variable from function body, which is ff is a function that take g as argument and return value of any type, say return type is a. So far, there is no restriction of return value of f.  assume type of f is t1-> a. That is,  (f = t1 -> a).

Now, take next variable, which is gg is a function that takes return value of function f as a argument (a), and returns value of any type, say return type is b. So far there is no restriction of type of value return by g.  But we already assume g is of type t1.  Now, we found that g is function so, t1 is a - > b.
That is, (g = gt1 = a -> b).

Now replace t1 in f with (a -> b). So f becomes f = (a -> b ) -> a

we have done with function body. Now look at function declaration syntax. It takes two arguments f and g, and f is first argument, g is second argument, and return value is of same type of return value of g.

So type of function \f g -> g (f g) is as follows:

((a -> b) -> a) -> ( a -> b) -> b


4] \ x f g -> f g (x g)

 Above function accepts three arguments x, f and g
 Assume g is of type a. that is, (g = a).
 Now, x takes g as a argument and returns any type of value. So far there is no restriction on type of value returned by x, say return type of value is b
Therefore, x = a -> b


Now, f takes two argument g and return type of x. Return type of f has no restriction, so say it is of type c
Therefore, f = a -> b -> c

(Note: For simplicity as assume g as a variable. And is of type a. However, you can consider it g as a function then you have to unify a = d->e and replace all occurrences of a with d->e)

  Return type of f is the return type of this function.



Now look at function definition and arrange its type accordingly.


\ x f g -> f g (x g) type is as follows:

(a -> b) -> (a -> b -> c) -> a - > c 




5] \ f g x -> f ( g x)

Above function takes three arguments, namely, f , g ,and x.

Assume x is of type a., That is ( x = a)

Now g takes x as argument and returns a value. Assume return type of g is b.
Therefore, (g = a -> b)

Now f takes return value of g as argument (that is, b) and returns a value.
Therefore, ( f = b -> c)


So type of \f g x -> f (g x) is:
Now, look at function declaration: \f g x

(b -> c) -> (a -> b) -> a ->c


6] \x y f -> f (x (\w -> f w)) (y f x)

Above function takes three arguments, x y and f.

Now look at Function body, and assume type for each variable.
Let w = a
       x = t1
       y = t2
       f  = t3

 Observe the function body, f will be applied to on first argument (x (\w -> f x)),  and then it will be applied on second argument (y f x).

Go inside \w function,

 f = t3 = a - > b

Therefore, x = t1 = (a -> b) -> c

Return type of x is c, which will become the argument of f, therefore unification is possible here, and c = a;

Hence, x = t1 = (a->b) ->a

Now we examine ( y f x) part,

y = t2 = (a -> b) -> (a->b->a) -> d

d is the return type of y.

After applying the first argument (x(\w->f w)), f returns b.

Therefore, d is parameter of b.

b should take d as argument and return a value, That is, b = d -> e

Now replace each occurrence of b with d -> e

f = a -> b = a -> d -> e

x = (a ->b -> a ) = ((a -> d -> e)- > a)

y = (a ->d -> e) -> ( a -> d -> e) -> a) -> d

Hence type of entire expression is, (arrenge according to function definition:

((a -> d -> e)- > a) ->
 (a ->d -> e) -> ( a -> d -> e) -> a) -> d ->
(a -> d -> e) -> e

e is the return type of entire expression which is also the return type of f.











 

Wednesday, November 4, 2009

Plugins Vs Extension

Extension and plugins often confused people. And Mostly people believe both are same.
However, there is subtle difference between Extension and Plugin.  Extensions are mostly written in JavaScript and/or XUL and relies on XPCOM APIs to access resources. Whereas, plugins are binary applications and they make use of operating system and can do interaction with operating system or access events from OS.

Securing plugins is more complex than extension.  Extension are individual identity so one extension needs one set of privileges, whereas plugins are more complicated and needs to identify privileges for each application runs plugins rather than a whole plugin.