ijd8.COM

A simple blog for an old Ma Nong.

用Python分析apk文件里的AndroidManifest.xml 文件获取包信息

Permalink

要自动提取包里的Android apk包的信息,如版本号、版本名、包名等信息。网上找了好久,无论是php还是python,大多是调用java 接口。这显然不是我想要的,特别是在线服务,一个python 环境还要整个java,这个庞然大物耗不起,我要用python,人生简短。

apk文件其实可以调用系统的unzip 命令解压,然后分析 AndroidManifest.xml 信息即可。

Python: 解压apk包
1
os.system('unzip yourapp.apk -d yourapp')

直接用下面代码是不行的

Python: AndroidManifest.xml解析
1
2
3
4
5
6
import os
from xml.dom.minidom import parse
dom1 = parse("AndroidManifest.xml")

f = os.open("AndroidManifest.xml", os.O_RDWR)
os.write( f, dom1.toxml() )

用记事本打开 AndroidManifest.xml 就知道了。

关键的一步是把binary xml格式转为 string xml。

Python: axmlprinter
1
2
3
4
5
6
7
8
9
10
import axmlprinter
from xml.dom import minidom

def main():
  ap = axmlprinter.AXMLPrinter(open('example/binary/AndroidManifest.xml', 'rb').read())
  buff = minidom.parseString(ap.getBuff()).toxml()
  print(buff)

if __name__ == "__main__":
  main()

相关模块 https://github.com/antitree/AxmlParserPY

AndroidManifest.xml 文件结构

XML: AndroidManifest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xmlversion="1.0"encoding="utf-8"?>
<manifest>
    <application>
       <activity>
           <intent-filter>
               <action/>
               <category/>
           </intent-filter>
      </activity>
       <activity-alias>
           <intent-filter></intent-filter>
           <meta-data/>
      </activity-alias>
       <service>
           <intent-filter></intent-filter>
           <meta-data/>
       </service>
       <receiver>
           <intent-filter></intent-filter>
           <meta-data/>
       </receiver>
       <provider>
           <grant-uri-permission/>
           <meta-data/>
       </provider>
       <uses-library/>
    </application>
    <uses-permission/>
    <permission/>
    <permission-tree/>
    <permission-group/>
    <instrumentation/>
    <uses-sdk/>
    <uses-configuration/> 
    <uses-feature/> 
    <supports-screens/>
</manifest>

Write a Comment

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.7 Processed in 0ms