Tuesday, 7 October 2014

How to view your web-page source code in android device



First create a project with the name WebsiteAnalysisTools and have a main activity with following code:

package com.clearpath.websiteanalysistools;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

   EditText url_text;
   TextView textView;  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        url_text = (EditText) findViewById(R.id.address);
        textView = (TextView) findViewById(R.id.tv);
   }
    
    public void myButtonClickHandler(View view) {
        new GrabURL().execute();
    }
    
    private class GrabURL extends AsyncTask<String, Void, Void> {
        private final HttpClient Client = new DefaultHttpClient();
        private String response_str;
        private String Error = null;
        private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
        
        protected void onPreExecute() {
        Dialog.setMessage("Processing..");
            Dialog.show();
      }
  protected Void doInBackground(String... urls) {
            try {
             HttpGet request = new HttpGet(url_text.getText().toString());
                  // Get the response
                  ResponseHandler<String> responseHandler = new BasicResponseHandler();
                  response_str = Client.execute(request, responseHandler);
                  Log.d("HTML:", response_str);
            } catch (ClientProtocolException e) {
                Error = e.getMessage();
                cancel(true);
            } catch (IOException e) {
                Error = e.getMessage();
                cancel(true);
            }
            
            return null;
        }
        protected void onPostExecute(Void unused) {
            Dialog.dismiss();
           textView.setText(response_str);
            if (Error != null) {
                Toast.makeText(MainActivity.this, Error, Toast.LENGTH_LONG).show();
            } 
        }
        
    }

}


The main layout file is presented here which is activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <EditText android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/address"
        android:hint="http://www.google.com"
        android:singleLine="true">
    </EditText>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Read Webpage"
        android:id="@+id/ReadWebPage"
        android:onClick="myButtonClickHandler">
    </Button>

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/tv"
            android:scrollbars="vertical">
        </TextView>
    </ScrollView>

</LinearLayout>

The manifest should have following permissions:

<uses-permission android:name="android.permission.INTERNET" />


Screenshot of the output:



Thursday, 25 September 2014

Decoding Android apk file (Reverse Engineering)

Procedure for decoding .apk files, step-by-step method:

Step 1:
Make a new folder and put .apk file in it (which you want to decode). Now rename the extension of this .apk file to .zip (eg.: rename from filename.apk to filename.apk.zip) and save it. Now you get classes.dex files, etc. At this stage you are able to see drawable but not xml and java files, so continue.

Step 2:
Now extract this zip apk file in the same folder (or NEW FOLDER). Now download dex2jar from this link
 http://code.google.com/p/dex2jar/  and extract it to the same folder (or NEW FOLDER). Now open command prompt and change directory to that folder (or NEW FOLDER). Then write dex2jar classes.dex and press enter. Now you get classes.dex.dex2jar file in the same folder. Then download java decompiler from  http://java.decompiler.free.fr/?q=jdgui  or from here and now double click on jd-gui  or AndroChef Java Decompiler 1.0 shortcut created on your desktop and click on open file. Then open classes.dex.dex2jar file from that folder. Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name. At this stage you get java source but the xml files are still unreadable, so continue.

Step 3:
Now open another new folder and put these files
1.  put .apk file which you want to decode
2.  download apktool v1.x AND apktool install window (both can be downloaded at the same location) and put in the same folder
3.  download framework-res.apk file and put in the same folder (Not all apk file need framework-res.apk file)
4.  Open a command window
5.  Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk
6.  apktool d "fname".apk ("fname" denotes filename which you want to decode)
now you get a file folder in that folder and now you can easily read xml files also.