Saturday, 18 May 2013

Secure Your Android Application from Hackers


Do you know following things about Android?
Who can see application's code?
If not properly protected – just about anybody can! Android applications are run client-side, which means that the actual code for the application is downloaded onto the user's mobile device.

What they can see if not protected?
If not properly protected, decompiled code can potentially reveal usernames and passwords to back-end databases. This means that anyone could have access to your sensitive data.

How did they do it?
Through a process called 'de-compilation', hackers can use simple tools to reverse engineer your app, reproducing the original source code.

Mobile app development and IT services company RIIS has released a new security tool designed to prevent Android de-compilation by hackers on mobile devices

HoseDex2Jar is similar to HoseMocha which appeared in the late 1990's. Like HoseMocha, HoseDex2Jar is a simple tool that adds some instructions to the classes.dex file that Dex2Jar can not process. HoseDex2Jar is currently available on http://www.decompilingandroid.com.

Developers can take steps such as using tools like ProGuard to obfuscate their code, but up until now, it has been impossible to prevent someone from decompiling an app,” said Godfrey Nolan, RIIS president. ”We realized if there was a way to stop Dex2Jar, we would stop all Android de-compilation. HoseDex2Jar does just that.  It stops Dex2Jar by inserting harmless code in an Android APK that confuses and disables Dex2Jar and protects the code from de-compilation.

“To crack an Android app, hackers can download the app on another machine (e.g., Mac) and run a tool (e.g., apktool) to unpackage the app and disassemble its Dalvik bytecode,” the report notes. Dalvik is the process virtual machine in Google Android's operating system.

“They analyze the disassembled code or use tools (e.g., dex2jar and a Java de-compiler) to decompile Dalvik bytecode to Java source code and analyze the source code,” the report continues. “They can make changes to disable license checks (or other modifications) and repackage the app and resign it.”
According to RIIS, the new tool represents a step forward in protecting applications from this kind of behavior.


Download source code at GitHub.

Friday, 3 May 2013

Resizable Rectangle Overlay on Touch android

==================================
Following code is to draw rectangle on touch base.
==================================
    import java.util.ArrayList;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Point;
    import android.graphics.drawable.BitmapDrawable;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    import com.common.Utils;
    import com.example.rectangleoverlay.R;

    public class DrawView extends View {

        Point point1, point3;
        Point point2, point4;

        /**
         * point1 and point 3 are of same group and same as point 2 and point4
         */
        int groupId = -1;
        private ArrayList<ColorBall> colorballs = new ArrayList<ColorBall>();
        // array that holds the balls
        private int balID = 0;
        // variable to know what ball is being dragged
        Paint paint;
        Canvas canvas;

        public DrawView(Context context) {
            super(context);
            paint = new Paint();
            setFocusable(true); // necessary for getting the touch events
            canvas = new Canvas();
            // setting the start point for the balls
            point1 = new Point();
            point1.x = 50;
            point1.y = 20;

            point2 = new Point();
            point2.x = 150;
            point2.y = 20;

            point3 = new Point();
            point3.x = 150;
            point3.y = 120;

            point4 = new Point();
            point4.x = 50;
            point4.y = 120;

            // declare each ball with the ColorBall class
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point1));
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point2));
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point3));
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point4));

        }

        public DrawView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public DrawView(Context context, AttributeSet attrs) {
            super(context, attrs);
            paint = new Paint();
            setFocusable(true); // necessary for getting the touch events
            canvas = new Canvas();
            // setting the start point for the balls
            point1 = new Point();
            point1.x = 50;
            point1.y = 20;

            point2 = new Point();
            point2.x = 150;
            point2.y = 20;

            point3 = new Point();
            point3.x = 150;
            point3.y = 120;

            point4 = new Point();
            point4.x = 50;
            point4.y = 120;

            // declare each ball with the ColorBall class
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point1));
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point2));
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point3));
            colorballs.add(new ColorBall(context, R.drawable.gray_circle, point4));

        }

        // the method that draws the balls
        @Override
        protected void onDraw(Canvas canvas) {
            // canvas.drawColor(0xFFCCCCCC); //if you want another background color

            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setColor(Color.parseColor("#55000000"));
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeJoin(Paint.Join.ROUND);
            // mPaint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeWidth(5);

            canvas.drawPaint(paint);
            paint.setColor(Color.parseColor("#55FFFFFF"));

            if (groupId == 1) {
                canvas.drawRect(point1.x + colorballs.get(0).getWidthOfBall() / 2,
                        point3.y + colorballs.get(2).getWidthOfBall() / 2, point3.x
                                + colorballs.get(2).getWidthOfBall() / 2, point1.y
                                + colorballs.get(0).getWidthOfBall() / 2, paint);
            } else {
                canvas.drawRect(point2.x + colorballs.get(1).getWidthOfBall() / 2,
                        point4.y + colorballs.get(3).getWidthOfBall() / 2, point4.x
                                + colorballs.get(3).getWidthOfBall() / 2, point2.y
                                + colorballs.get(1).getWidthOfBall() / 2, paint);
            }
            BitmapDrawable mBitmap;
            mBitmap = new BitmapDrawable();

            // draw the balls on the canvas
            for (ColorBall ball : colorballs) {
                canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(),
                        new Paint());
            }
        }

        // events when touching the screen
        public boolean onTouchEvent(MotionEvent event) {
            int eventaction = event.getAction();

            int X = (int) event.getX();
            int Y = (int) event.getY();

            switch (eventaction) {

            case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
                                            // a ball
                balID = -1;
                groupId = -1;
                for (ColorBall ball : colorballs) {
                    // check if inside the bounds of the ball (circle)
                    // get the center for the ball
                    Utils.logd("Id : " + ball.getID());
                    Utils.logd("getX : " + ball.getX() + " getY() : " + ball.getY());
                    int centerX = ball.getX() + ball.getWidthOfBall();
                    int centerY = ball.getY() + ball.getHeightOfBall();
                    paint.setColor(Color.CYAN);
                    // calculate the radius from the touch to the center of the ball
                    double radCircle = Math
                            .sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y)
                                    * (centerY - Y)));

                    Utils.logd("X : " + X + " Y : " + Y + " centerX : " + centerX
                            + " CenterY : " + centerY + " radCircle : " + radCircle);

                    if (radCircle < ball.getWidthOfBall()) {

                        balID = ball.getID();
                        Utils.logd("Selected ball : " + balID);
                        if (balID == 1 || balID == 3) {
                            groupId = 2;
                            canvas.drawRect(point1.x, point3.y, point3.x, point1.y,
                                    paint);
                        } else {
                            groupId = 1;
                            canvas.drawRect(point2.x, point4.y, point4.x, point2.y,
                                    paint);
                        }
                        invalidate();
                        break;
                    }
                    invalidate();
                }

                break;

            case MotionEvent.ACTION_MOVE: // touch drag with the ball
                // move the balls the same as the finger
                if (balID > -1) {
                    Utils.logd("Moving Ball : " + balID);

                    colorballs.get(balID).setX(X);
                    colorballs.get(balID).setY(Y);

                    paint.setColor(Color.CYAN);

                    if (groupId == 1) {
                        colorballs.get(1).setX(colorballs.get(0).getX());
                        colorballs.get(1).setY(colorballs.get(2).getY());
                        colorballs.get(3).setX(colorballs.get(2).getX());
                        colorballs.get(3).setY(colorballs.get(0).getY());
                        canvas.drawRect(point1.x, point3.y, point3.x, point1.y,
                                paint);
                    } else {
                        colorballs.get(0).setX(colorballs.get(1).getX());
                        colorballs.get(0).setY(colorballs.get(3).getY());
                        colorballs.get(2).setX(colorballs.get(3).getX());
                        colorballs.get(2).setY(colorballs.get(1).getY());
                        canvas.drawRect(point2.x, point4.y, point4.x, point2.y,
                                paint);
                    }

                    invalidate();
                }

                break;

            case MotionEvent.ACTION_UP:
                // touch drop - just do things here after dropping

                break;
            }
            // redraw the canvas
            invalidate();
            return true;

        }

        public void shade_region_between_points() {
            canvas.drawRect(point1.x, point3.y, point3.x, point1.y, paint);
        }
    }
============================
Following class is used to store objects
============================
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Point;

    public class ColorBall {

        Bitmap bitmap;
        Context mContext;
        Point point;
        int id;
        static int count = 0;

        public ColorBall(Context context, int resourceId, Point point) {
            this.id = count++;
            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    resourceId);
            mContext = context;
            this.point = point;
        }

        public int getWidthOfBall() {
            return bitmap.getWidth();
        }

        public int getHeightOfBall() {
            return bitmap.getHeight();
        }

        public Bitmap getBitmap() {
            return bitmap;
        }

        public int getX() {
            return point.x;
        }

        public int getY() {
            return point.y;
        }

        public int getID() {
            return id;
        }

        public void setX(int x) {
            point.x = x;
        }

        public void setY(int y) {
            point.y = y;
        }
    }