Translate

Showing posts with label command line. Show all posts
Showing posts with label command line. Show all posts

Tuesday

Converting Dot files into Control Flow Graphs using GraphViz

Generate Control Flow Graph Using Graphviz

To convert android apk into dot files and do analysis by using soot click here. 

To generate control flow graph using graphviz, you need dot files which is generated from all methods of android apks. you can find them on sootOutput directory, now you need to go to command line and type following commands if you have already set graph viz path. 

To convert into pdf:

> dot Tpdf -o Example.pdf Example.dot

To convert into png:

> dot Tpng -o Example.png Example.dot

Note: if you haven't set path you need to set path (to know how to set path see installation post) or go to bin folder of graph viz installation folder and find dot file from command line and type above commands.

commands:

>dot : executable dot file (also MS word old file format for graph)

> -Tpdf, -Tpng: converting file format

> -o: Optimization

> Example.pdf, .png: Name of Control flow graph you wanna give after conversion.

>Example.dot : Name of dot file you wanna convert


Note: Graphviz not only use dot file format and executable files you can convert various different files into control flow graph. This post is on the context of program analysis of smart phone apps i.e. android apps using Soot as analysis tool.


Monday

Graphviz - Graph Visualization Software

Installation 

For installation, download latest version of "Graphviz"  from here. You can either download executable file directly and install or zip file and extract zip and put on command line directory.

To use any executable extension like dot from Graphviz you need to set the PATH after installation. For windows

Control panel -> System-> Advance System Setting -> Environment Variables -> PATH

Normally, file will install in Graphviz directory inside program files so path will looks like :


PATH   ->   C:\Program Files\Graphviz2.38\bin\


To test the installation type following from command line :

> dot -help

If you haven't the set path yet go to graphviz bin directory from command line and type following :

> dot -help

Friday

How to Convert any Android apk file into dalvik bytecode using soot

How to Convert any Android apk file into dalvik bytecode using soot

Lets look at command to convert any android apk file into Dalvik. 
For conversion, you need to:

> Download any .apk file
> Download android sdk from android developer site or download android jars
> Download Soot's nightly build version from Soot- Github


Before applied command, you need to know right path for android sdk -platforms directory, .apk file located directory and nightly build version of soot at right directory

Set CLASSPATH of Soot.jar or go to the directory that contains Soot.jar file then type following Command:

> java -Xmx2g -jar soot-trunk.jar soot.Main -w -allow-phantom-refs       -android-jars "D:\sdk\platform" -src-prec apk -output-format dex         -process-dir "C:\apkpath\example.apk"

If you have already set CLASSPATH for jar then type following

>java -Xmx2g soot.Main -w -allow-phantom-refs -android-jars "D:\sdk\platform" -src-prec apk -output-format dex -process-dir "C:\apkpath\example.apk"


Details: -Xmx2g it represents memory of 2GB which is normally sufficient for any apk process.

-jar can be replaced by -cp which means CLASSPATH or it represent command prompt's current directory.

-W stands for writing

-allow-phantom-refs creates phantom classes for missing or unprocessed classes

-android-jars leads to path for android sdk or jars to process apks

-src-prec represents the file format you are trying to process

-output-format dex is responsible for converting all the classes from apk into dex format
 
-process-dir shows the path for processing directory or apk located directory



Creating Control Flow Diagram from Java Class files Using Soot.

Creating Control Flow Diagram from Java Class files Using Soot.

In order to create control flow diagram you need to convert Java class file into dot files by using control flow viewer soot tool. You can get HelloWorld.java file from previous posts(here). Convert it in to class file and type following commands on command prompt inside soot directory where HelloWorld is located:

>Java soot.tools.CFGViewer HelloWorld

And if you do not set classpath then type following inside soot directory where HelloWorld  and soot jars are located:

>Java -cp soot-2.5.0.jar soot.tools.CFGViewer HelloWorld

you will see following if it succeed:


It will create graphical representation of control flow graph of dot file format for each method from the class files. 

Java.lang.String[] Method:

digraph "void main(java.lang.String[])" {
    label="void main(java.lang.String[])";
    node [shape=box];
    "0" [style=filled,fillcolor=gray,label="r0 := @parameter0",];
    "1" [label="$r1 = new HelloWorld",];
    "0"->"1";
    "2" [label="specialinvoke $r1.<init>()",];
    "1"->"2";
    "3" [label="HelloWorld.helloObj = $r1",];
    "2"->"3";
    "4" [label="$r2 = java.lang.System.out",];
    "3"->"4";
    "5" [label="$r3 = HelloWorld.helloObj",];
    "4"->"5";
    "6" [label="$r4 = $r3.printMessage()",];
    "5"->"6";
    "7" [label="$r2.println($r4)",];
    "6"->"7";
    "8" [style=filled,fillcolor=lightgray,label="return",];
    "7"->"8";
}
 

Print Message() Method:

digraph "java.lang.String printMessage()" {
    label="java.lang.String printMessage()";
    node [shape=box];
    "0" [style=filled,fillcolor=gray,label="r0 := @this",];
    "1" [label="$r1 = r0.output",];
    "0"->"1";
    "2" [style=filled,fillcolor=lightgray,label="return $r1",];
    "1"->"2";
}