saferApp / androguard

Automatically exported from code.google.com/p/androguard
0 stars 0 forks source link

Add jython compatability for apk.py #88

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Currently, I am trying to run apk.py (latest version) from Java using Jython 
2.7.2a (latest release) by calling either apk.py directly or some wrapper 
script like this:

Python:

(..)
from androguard.core.bytecodes import apk
a = apk.APK("examples//APKILTests.apk")

Java (pseudocode):

(..)
interpreter = new PythonInterpreter(null, new PySystemState());
interpreter.execfile(new FileInputStream("apk.py"));
PyObject apk = interpreter.get("APK");
apk = apk.__call__(new PyString("examples//APKILTests.apk"));

Error (with any apk):

Traceback (most recent call last):
  File "<iostream>", line 174, in __init__
  File "<iostream>", line 948, in __init__
  File "<iostream>", line 820, in getName
  File "<iostream>", line 627, in getRaw
  File "<iostream>", line 638, in getShort
IndexError: index out of range: XXXL <-- on any apk fails here

The error is really deep down, so I am having trouble detecting the exact 
issue. Unzipping works, it seems like a problem with the AXMLParser. If you 
could check it out that would be great... 

Original issue reported on code.google.com by Miller....@googlemail.com on 14 Nov 2012 at 10:27

GoogleCodeExporter commented 8 years ago
Ok, have you try without java code ? only with jython ?

Original comment by anthony....@gmail.com on 16 Nov 2012 at 6:53

GoogleCodeExporter commented 8 years ago
well, this is the same issue. maybe it's jythons fault, afterall their 2.72 
port is just alpha status, but would be great, if you could check it out 
nevertheless, since it would enable ppl to integrate your great library in 
Java. maybe we will find bug for jythons team, too. 

Original comment by Miller....@googlemail.com on 16 Nov 2012 at 8:01

GoogleCodeExporter commented 8 years ago
I just tested parsing with Android4Me (uses same code base as yours, see 
"apk-reader" project) and it also crashes. I could "fix" the issue by simply 
breaking the getString for-loop on an ArrayIndexOutOfBoundsException, but could 
extract better debug info:

For the sample APK attached to this comment. At this point:

public String getString(int index) {
        if (index<0 ||
            m_stringOffsets==null ||
            index>=m_stringOffsets.length)
        {
            return null;
        }
        int offset=m_stringOffsets[index];
        int length=getShort(m_strings,offset);
        StringBuilder result=new StringBuilder(length);
        for (;length!=0;length-=1) {
            offset+=2;
            try {
                result.append((char)getShort(m_strings,offset));
            } catch(ArrayIndexOutOfBoundsException exc) {
                break; <-- HERE!!!
            }
        }
        return result.toString();
    }

The "m_strings" contains:

[1919879947, 1953391977, 1869182049, 202113134, 1870225772, 2002744437, 
1752458345, 1812794624, 1970239841, 1701338996, 1952999273, 1627850496, 
1869767790, 704668777, 1953785898, 791624304, 1701340019, 779313517, 
1919184481, 778332527, 795701091, 795570273, 796091762, 1919184481, 6580591, 
201326592, 1852394508, 1282564453, 1970239841, 116]

The "m_stringOffsets" contains:

[0, 14, 29, 45, 55, 100, 103]

.. and offset was 121 (/4 = 30.25, rounded to 30). Since "m_strings" only has 
29 elements, this is what is causing the crash. I'm assuming this is some weird 
unicode-parsing error.

Maybe this helps you a bit more... 

Original comment by Miller....@googlemail.com on 16 Nov 2012 at 11:15

Attachments:

GoogleCodeExporter commented 8 years ago
Ok thx for the feedback.

I will check that as soon as possible !

Original comment by anthony....@gmail.com on 17 Nov 2012 at 4:43

GoogleCodeExporter commented 8 years ago

Original comment by anthony....@gmail.com on 25 Nov 2012 at 10:56

GoogleCodeExporter commented 8 years ago
Could you try with the new version ?

Original comment by anthony....@gmail.com on 28 Nov 2012 at 8:40

GoogleCodeExporter commented 8 years ago
Okay great, really happy you updated so quickly. I'll give you feedback 
tomorrow. 

Original comment by Miller....@googlemail.com on 28 Nov 2012 at 8:45

GoogleCodeExporter commented 8 years ago
no news ?

Original comment by anthony....@gmail.com on 13 Dec 2012 at 6:59

GoogleCodeExporter commented 8 years ago
Sorry for the long wait, was very busy at work. I just tested the new 1.9 
version. This time around, I was able to Initialize an "APK" class instance 
successfully. Trying to get the "min_sdk_version" returned a bad error though:

Here is a "code snippit" from my Java side

//interpreter is a new PythonInterpreter(null, new PySystemState());
//and it parsed the file "core/bytecodes/apk.py"
//then ...
PyObject apk = interpreter.get("APK");
            apk = apk.__call__(new PyString(apkFile.getAbsolutePath())); //still works
            PyObject min_sdk_version = apk.invoke("get_min_sdk_version"); //fails

This happens with different types of APK files.

Traceback (most recent call last):
  File "<iostream>", line 514, in get_min_sdk_version
  File "<iostream>", line 406, in get_element
AttributeError: 'NoneType' object has no attribute 'getElementsByTagName'

    at org.python.core.Py.AttributeError(Py.java:179)
    at org.python.core.PyObject.noAttributeError(PyObject.java:946)
    at org.python.core.PyObject.__getattr__(PyObject.java:941)
    at org.python.pycode._pyx0.get_element$27(<iostream>:411)
    at org.python.pycode._pyx0.call_function(<iostream>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:166)
    at org.python.core.PyFunction.__call__(PyFunction.java:368)
    at org.python.core.PyMethod.__call__(PyMethod.java:139)
    at org.python.pycode._pyx0.get_min_sdk_version$36(<iostream>:514)
    at org.python.pycode._pyx0.call_function(<iostream>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
    at org.python.core.PyFunction.__call__(PyFunction.java:347)
    at org.python.core.PyFunction.__call__(PyFunction.java:342)
    at org.python.core.PyInstance.invoke(PyInstance.java:203)

Original comment by Miller....@googlemail.com on 13 Dec 2012 at 9:53

GoogleCodeExporter commented 8 years ago
Happy new year. :) Anything new on this bug? I'm back at trying to integrate 
androguard and stuck at the same issue.

I'm thinking maybe the bug has to do with the raw XML manifest not having a 
version field set?

See this crash:

2013-01-11 18:01:48,186 ERROR 189 [mobie] - <Failed to analyze APK with 
androguard. Trying apkreader from now on ...>
Traceback (most recent call last):
  File "<iostream>", line 514, in get_min_sdk_version
  File "<iostream>", line 406, in get_element
AttributeError: 'NoneType' object has no attribute 'getElementsByTagName'

    at org.python.core.Py.AttributeError(Py.java:179)
    at org.python.core.PyObject.noAttributeError(PyObject.java:946)
    at org.python.core.PyObject.__getattr__(PyObject.java:941)
    at org.python.pycode._pyx0.get_element$27(<iostream>:411)
    at org.python.pycode._pyx0.call_function(<iostream>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:166)
    at org.python.core.PyFunction.__call__(PyFunction.java:368)
    at org.python.core.PyMethod.__call__(PyMethod.java:139)
    at org.python.pycode._pyx0.get_min_sdk_version$36(<iostream>:514)
    at org.python.pycode._pyx0.call_function(<iostream>)
    at org.python.core.PyTableCode.call(PyTableCode.java:165)
    at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
    at org.python.core.PyFunction.__call__(PyFunction.java:347)
    at org.python.core.PyFunction.__call__(PyFunction.java:342)
    at org.python.core.PyInstance.invoke(PyInstance.java:203)

Manifest file (sample 60b1c98fc6ca2b86fbd7c772dc08a73e.apkxx) is:

<?xml version="1.0" encoding="UTF-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.service"
    >
    <uses-permission
        android:name="android.permission.GET_ACCOUNTS"
        >
    </uses-permission>
    <uses-permission
        android:name="android.permission.INTERNET"
        >
    </uses-permission>
    <uses-permission
        android:name="android.permission.ACCESS_FINE_LOCATION"
        >
    </uses-permission>
    <uses-permission
        android:name="android.permission.RECEIVE_BOOT_COMPLETED"
        >
    </uses-permission>
    <application
        android:icon="@7F020000"
        >
        <activity
            android:label="@7F050000"
            android:name=".Main"
            >
            <intent-filter
                >
                <action
                    android:name="android.intent.action.MAIN"
                    >
                </action>
                <category
                    android:name="android.intent.category.LAUNCHER"
                    >
                </category>
            </intent-filter>
        </activity>
        <activity
            android:name=".ReceiverActivity"
            android:launchMode="3"
            >
        </activity>
        <receiver
            android:name=".SampleReceiver"
            >
            <intent-filter
                >
                <action
                    android:name="android.intent.action.BOOT_COMPLETED"
                    >
                </action>
            </intent-filter>
        </receiver>
        <service
            android:name=".KitchenTimerService"
            android:enabled="true"
            >
        </service>
    </application>
</manifest>

Original comment by Miller....@googlemail.com on 11 Jan 2013 at 5:06