Giter Club home page Giter Club logo

pyaxmlparser's Introduction

axmlparser

A simple parser to parse Android XML file.

Usage

➜ apkinfo --help
Usage: apkinfo [OPTIONS] FILENAME

Options:
-s, --silent  Don't print any debug or warning logs
--help        Show this message and exit.

CLI :

$ apkinfo ~/Downloads/com.hardcodedjoy.roboremo.15.apk
APK: /home/chillaranand/Downloads/com.hardcodedjoy.roboremo.15.apk
App name: RoboRemo
Package: com.hardcodedjoy.roboremo
Version name: 2.0.0
Version code: 15
Is it Signed: True
Is it Signed with v1 Signatures: True
Is it Signed with v2 Signatures: True
Is it Signed with v3 Signatures: False

Python package :

from pyaxmlparser import APK


apk = APK('/foo/bar.apk')
print(apk.package)
print(apk.version_name)
print(apk.version_code)
print(apk.icon_info)
print(apk.icon_data)
print(apk.application)

pyaxmlparser's People

Contributors

arist0v avatar chenrui333 avatar chillaranand avatar cosmosgenius avatar dhilipsiva avatar dilinger avatar doctaweeks avatar jflbr avatar jtjisgod avatar mhils avatar praseetha-kr avatar rbdixon avatar subho007 avatar supercilex avatar viren-nadkarni avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyaxmlparser's Issues

_get_res_string_value parsing error

image
I parsed some string data from public xml and i tried bringing text value with _get_res_string_value("@string/text"). but it dosen't work! So, i found a mistake code from your core.py. "@string/" is 8, but you are slicing at 9. so, string_key is to be ext not text.

"IndexError: list index out of range" in calling get_arsc_info

Running get_arsc_info function from pyaxmlparser.arscutil on resources.arsc from repo at:
https://github.com/appknox/pyaxmlparser/blob/master/tests/test_apk/resources.arsc
will return:

Traceback (most recent call last):
  File "/tmp/arsctest.py", line 12, in <module>
    print(get_arsc_info(rsc))
  File "/usr/lib/python3/dist-packages/pyaxmlparser/arscutil.py", line 636, in get_arsc_info
    tmp_buff = getattr(arscobj, "get_" + ttype + "_resources")(
  File "/usr/lib/python3/dist-packages/pyaxmlparser/arscparser.py", line 439, in get_bool_resources
    buff += '<bool name="%s">%s</bool>\n' % (i[0], i[1])
IndexError: list index out of range

code to reproduce:

from pyaxmlparser.arscparser import ARSCParser
from pyaxmlparser.arscutil   import get_arsc_info
rsc_file = 'resources.arsc'
rsc = ARSCParser(open(rsc_file, 'rb').read())
print('rsc=\n')
print(rsc)
print('rsc.get_strings_resources()=\n')
print(rsc.get_strings_resources())
print('get_arsc_info(rsc)=\n')
print(get_arsc_info(rsc))

ResParserError: res1 must be zero!

The parsing of the files stop when res1 is zero on some apks, which causes the analysis to completely stop.
A patch was implemented for androguard to make the analysis of the particular chunk stop, and log a warning instead:
https://github.com/androguard/androguard/pull/1008/files

I would suggest the replacement of the two assert statements at line in 129 pyaxmlparser/arscutil.py with the following:

res_error = False
if self.res0 != 0:
    log.warning("res0 is not zero!")
    res_error = True
if self.res1 != 0:
    log.warning("res1 is not zero!")
    res_error = True

if not res_error:  # Skips processing attempt if there was an error
    self.entryCount = unpack("<I", buff.read(4))[0]

    self.typespec_entries = []
    for i in range(0, self.entryCount):
        self.typespec_entries.append(unpack("<I", buff.read(4))[0])

And replace the assert statement at line 587 with the following:

if self.res0 != 0:
    log.warning("res0 is not zero!")
else:
    self.data_type = unpack('<B', buff.read(1))[0]
    self.data = unpack('<I', buff.read(4))[0]

Many errors on some APKs

"invalid decoded string length" errors on apk parsing...

F.e. https://alfamobile-web.servicecdn.ru/latest/alfa-bank.apk

apkinfo alfa-bank.apk
APK: alfa-bank.apk
invalid decoded string length
invalid decoded string length
invalid decoded string length
invalid decoded string length
invalid decoded string length
invalid decoded string length
App name: Альфа-Банк
Package: ru.alfabank.mobile.android
Version name: 12.07.04
Version code: 2042070400
Is it Signed: True
Is it Signed with v1 Signatures: False
Is it Signed with v2 Signatures: True
Is it Signed with v3 Signatures: False

And some more:
https://cdn.sberbank.ru/appdistr/SberbankOnline.apk
https://mobile-apps-prod.cdn-tinkoff.ru/tinkoff-bank-6-27-0-8011.apk

Lots of warnings pollute output

Hi,

gplaycli uses pyaxmlparser.

Starting from version 0.3.9, pyaxmlparser generates lots and lots of warning, such as described here: matlink/gplaycli#188

It seems that there are unnecessary warnings in code. Can you please have a look

Unexpected behaviour is observed if a large APK file is loaded

Currently the lib always tries to fully load the content of an APK file into the memory because of

self.__raw = bytearray(filename)
/ https://github.com/appknox/pyaxmlparser/blob/master/pyaxmlparser/core.py#L102, which is slow and dangerous in case of larger package files. In k8s environment the supervisor simply kills the POD running the script because of unacceptably high memory usage while working with bigger APKs (some games might be 1GB and more in size because of resources).

Is there a way to lower the overall memory usage and prevent the full content of the source APK to be loaded into RAM?

Python Click 7

A python-click upgrade to version 7.0 breaks pyaxmlparser which specifically requires python-click 6.7.

I didn't test but perhaps requiring 6.7 or 7.0 would do?

commandline support

add support to run the same via commandline directly

$name <ABC.apk>

or

$name <ABC.apk> <value_needed>

Option to make apkinfo faster

Can you add option to only get info about apk and nothing more? On a slow systems there are time lag at the beginning. And at the end too. Maybe skip sign check or what is after getting info.

Infinite loop when analyzing SettingsIntelligence.apk

When analyzing a certain app it gets in an infinite loop.
Probably related to #78
Apk file can be provided if needed.

artzert@DESKTOP-EED68NC:~/slimmer$ apkinfo d/system/product/priv-app/SettingsIntelligence/SettingsIntelligence.apk
APK: d/system/product/priv-app/SettingsIntelligence/SettingsIntelligence.apk
res1 is not zero!
res0 is not zero!
res0 is not zero!
res1 is not zero!
res1 is not zero!
res0 is not zero!
res0 is not zero!
res0 is not zero!
...
[100's of lines of the same stuff]
...
res0 is not zero!
res0 is not zero!
Traceback (most recent call last):
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/core.py", line 1478, in get_android_resources
    return self.arsc["resources.arsc"]
KeyError: 'resources.arsc'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/artzert/.local/bin/apkinfo", line 8, in <module>
    sys.exit(main())
  File "/home/artzert/.local/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/home/artzert/.local/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/home/artzert/.local/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/artzert/.local/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/cli.py", line 34, in main
    click.echo('App name: {}'.format(apk.application))
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/core.py", line 2061, in application
    return self.get_app_name()
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/core.py", line 496, in get_app_name
    res_parser = self.get_android_resources()
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/core.py", line 1484, in get_android_resources
    self.arsc["resources.arsc"] = ARSCParser(self.zip.read("resources.arsc"))
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/arscparser.py", line 138, in __init__
    ate = ARSCResTableEntry(self.buff, res_id, pc)
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/arscutil.py", line 534, in __init__
    self.item = ARSCComplex(buff, parent)
  File "/home/artzert/.local/lib/python3.10/site-packages/pyaxmlparser/arscutil.py", line 580, in __init__
    self.items.append((unpack('<I', buff.read(4))[0],
struct.error: unpack requires a buffer of 4 bytes

pyaxmlparser broken by Click 7.0

Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/pkg_resources/init.py", line 578, in _build_master
ws.require(requires)
File "/usr/lib/python3.7/site-packages/pkg_resources/init.py", line 895, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python3.7/site-packages/pkg_resources/init.py", line 786, in resolve
raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.ContextualVersionConflict: (Click 7.0 (/usr/lib/python3.7/site-packages), Requirement.parse('click==6.7'), {'pyaxmlparser'})

zipfile.BadZipFile with com.textra

Python version: 3.6.3
OS: archlinux
pyaxmlparser version: 0.3.6

Downloading the application com.textra result in the following error

>>> from pyaxmlparser import APK
>>> a = APK('./com.textra.apk')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nomore/workspace/nogoogle/lib/python3.6/site-packages/pyaxmlparser/core.py", line 10, in __init__
    self.zip_file = get_zip_file(apk)
  File "/home/nomore/workspace/nogoogle/lib/python3.6/site-packages/pyaxmlparser/utils.py", line 29, in get_zip_file
    return ZipFile(resource)
  File "/usr/lib64/python3.6/zipfile.py", line 1108, in __init__
    self._RealGetContents()
  File "/usr/lib64/python3.6/zipfile.py", line 1175, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Apk data should be valid since I can install it on my android phone succesfully. Let me know if I can provide some more debug information.

tag or release versions

Please consider tagging or releasing stable versions, this would make it much easier to keep track of updates.
Thank you.

"SyntaxWarning: invalid escape sequence" error with python 3.12

I noticed today that apkinfo pops this error. It probably has to do with python 3.12, like many other similar errors I have come accross lately, so I am asking for a fix. It is from debian unstable, which is on python 3.12 since late June, but it still has apkinfo 0.3.28 in the repos.

$ apkinfo Downloads/wattz-1.19.apk 
/usr/lib/python3/dist-packages/pyaxmlparser/core.py:789: SyntaxWarning: invalid escape sequence '\d'
  dexre = re.compile("classes(\d*).dex")
/usr/lib/python3/dist-packages/pyaxmlparser/core.py:807: SyntaxWarning: invalid escape sequence '\d'
  dexre = re.compile("^classes(\d+)?.dex$")
/usr/lib/python3/dist-packages/pyaxmlparser/core.py:1006: SyntaxWarning: invalid escape sequence '\>'
  """
APK: Downloads/wattz-1.19.apk
App name: Wattz
Package: dubrowgn.wattz
Version name: 1.19
Version code: 19
Is it Signed: True
Is it Signed with v1 Signatures: False
Is it Signed with v2 Signatures: True
Is it Signed with v3 Signatures: False

"x509 is not defined" in calling apk.get_certificates()

In "pyaxmlparser/core.py", you use "x509" and "cms", but they haven't be imported.
And i read "requirements.txt", but i can't find anything about "x509" and "cms".
After i search in google, find "asn1crypto".

I think you should add "asn1crypto" in "requirements.txt" and import them in "pyaxmlparser/core.py"

v0.3.14 package misses public.xml

On Debian/stretch, I do pip3 install gplaycli which installs pyaxmlparser v0.3.14. Then it fails with this error:

    Traceback (most recent call last):
      File "/usr/local/bin/gplaycli", line 7, in <module>
        from gplaycli.gplaycli import main
      File "/usr/local/lib/python3.5/dist-packages/gplaycli/gplaycli.py", line 37, in <module>
        from pyaxmlparser import APK
      File "/usr/local/lib/python3.5/dist-packages/pyaxmlparser/__init__.py", line 3, in <module>
        from pyaxmlparser.core import APK
      File "/usr/local/lib/python3.5/dist-packages/pyaxmlparser/core.py", line 9, in <module>
        from pyaxmlparser.axmlprinter import AXMLPrinter
      File "/usr/local/lib/python3.5/dist-packages/pyaxmlparser/axmlprinter.py", line 23, in <module>
        from pyaxmlparser.axmlparser import AXMLParser
      File "/usr/local/lib/python3.5/dist-packages/pyaxmlparser/axmlparser.py", line 24, in <module>
        from pyaxmlparser.resources import public
      File "/usr/local/lib/python3.5/dist-packages/pyaxmlparser/resources/public.py", line 21, in <module>
        raise Exception("need to copy the sdk/platforms/android-?/data/res/values/public.xml here")

License clarifications

hello,

the top level file of this project says MIT license, but several files inside the pyaxmlparser directory have a very clear Apache license as they were imported from Androguard (Apache 2 licensed). Could you please clarify this, as right now it seems that the entire project is under MIT, which is not true.

Implement functions for `with` statement and `close()`

I noticed that the ZipFile will stay open (OS prevents it from being modified or removed) and it is currently not possible to use it in a with statement. So I'd suggest implementing __enter__ , __exit__ and a close() function.

At the moment you will have to call apk.zip.close() manually, to figure this out you need to look at the code.

Encountered a problem when analyzing MIUIEmail.apk

I encountered a problem when analyzing MIUIEmail.apk. The error message is as follows:Traceback (most recent call last):

File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyaxmlparser\core.py", line 440, in _get_permission_maxsdk
maxSdkVersion = int(self.get_value_from_tag(item, "maxSdkVersion"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '@7F0B003A'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 198, in _run_module_as_main
File "", line 88, in run_code
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Scripts\apkinfo.exe_main
.py", line 7, in
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1157, in call
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\click\core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyaxmlparser\cli.py", line 31, in main
apk = APK(filename)
^^^^^^^^^^^^^
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyaxmlparser\core.py", line 310, in init
self._apk_analysis()
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyaxmlparser\core.py", line 366, in _apk_analysis
self._get_permission_maxsdk(uses_permission)
File "C:\Users\Horatio\AppData\Local\Programs\Python\Python312\Lib\site-packages\pyaxmlparser\core.py", line 442, in _get_permission_maxsdk
log.warning(self.get_max_sdk_version() + 'is not a valid value for maxSdkVersion')
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

How to deal with it?

Error with the application's Icon

When i trying to get the app's icon with 'apk.icon_data', I get the following error:


Out of range dimension unit index for 65038.00399593472: 9
Out of range dimension unit index for 65038.00399593472: 10
Out of range dimension unit index for 32051.19728172544: 10
Out of range dimension unit index for 0.488437427482368: 8
Out of range dimension unit index for 32051.19728172544: 10
Out of range dimension unit index for 0.488437427482368: 8
Out of range dimension unit index for 0.488437427482368: 8
Out of range dimension unit index for 4082892.0: 13
Out of range dimension unit index for 4060610.0: 15
Out of range dimension unit index for 32133.11916175872: 12
Out of range dimension unit index for 123.12000590919679: 9
Out of range dimension unit index for 0.9924011500961792: 9

How to get icon

icon_binary_data = b'\x03...'
with open("app_icon.png", "wb") as fp:
  fp.write(icon_binary_data)

It won't open

Exception when parsing packages without a version name

When trying to parse the 'com.android.chrome' APK, the following exception occurs:

File "/python/lib/python3.6/site-packages/pyaxmlparser/core.py", line 142, in version_name
    if not version_name.startswith("@"):
AttributeError: 'NoneType' object has no attribute 'startswith'

implement __repr__

Suggestion:

implementing a repr listing all the possible value could be usefull

i created a pull request to fix this

pr: #70

Free apk file after manipulation

The Code :

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        apk = APK(apk_file)
        # print(apk.package)
        # print(apk.version_name)
        apk_version_name = apk.version_name
        # print(apk.version_code)
        apk_version_code = apk.version_code
        # print(apk.icon_info)
        # print(apk.icon_data)
        apk_name=apk.application
        # print('Apk Name : '+apk_name)
        print(apk_name+' '+apk_version_name+'_'+apk_version_code+'.apk')
        # os.rename(apk_file, apk_name+' '+apk_version_name+'_'+apk_version_code+'.apk')
        # copyfile(apk_file, apk_name+' '+apk_version_name+'_'+apk_version_code+'.apk')
        # os.remove(apk_file)
        shutil.move(apk_file, apk_name+' '+apk_version_name+'_'+apk_version_code+'.apk')

Execution :

USBDebug 1.0_1.apk
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\shutil.py", line 557, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'me.tennkia.apr.apk' -> 'USBDebug 1.0_1.apk'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "apkdl_downloader.py", line 70, in <module>
    shutil.move(apk_file, apk_name+' '+apk_version_name+'_'+apk_version_code+'.apk')
  File "C:\Program Files\Python37\lib\shutil.py", line 572, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'me.tennkia.apr.apk'

show "Failed to get the attribute with namespace" when try to decode HKnews.apk

win 10 pro 1903
python 3.7.4
pyaxmlparser-0.3.20
the apk please see to
HKNews2.zip

from pyaxmlparser import APK
apk=APK(r"G:\MyPython\Studty\1.3处理apk获取apk的xml格式的图标\HKNews2.apk")
Failed to get the attribute with namespace
Failed to get the attribute with namespace
Failed to get the attribute with namespace
Failed to get the attribute with namespace
Failed to get the attribute with namespace

os.chdir(r"G:\MyPython\Studty\1.3处理apk获取apk的xml格式的图标")
apk=APK("HKNews2.apk")
Failed to get the attribute with namespace
Failed to get the attribute with namespace
Failed to get the attribute with namespace
Failed to get the attribute with namespace
Failed to get the attribute with namespace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.