[Tools-004] How to extract androidmanifest.xml from APK and extract the appropriate information

Source: Internet
Author: User

As with the previous article, we also need to verify some of the apk , such as Umengkey,adkey,talkingdata , then we also need to unzip the apk file, and then extract One of the androidmanifest.xml. Then parse the XML to analyze and contrast the content.

1. Unzip the APK file

if (Path.getextension (FilePath).   Equals (". apk")) {//Get the app name String appName = path.getfilenamewithoutextension (FilePath);   Export directory String Outpath = "tempandroid\\" + appName;   Create an extract stream zipinputstream s = new Zipinputstream (File.openread (FilePath));   String androidmanifestname = "Androidmanifest.xml";   ZipEntry Theentry;   BOOL found = false;      while ((Theentry = S.getnextentry ()) = null) {Console.WriteLine (theentry.name);      Gets the extracted filename string filename = Path.getfilename (theentry.name);           Traverse Lookup configuration file if (Androidmanifestname! = null) {if (Filename.equals (Androidmanifestname)) {            Found = true;            if (Outpath.length > 0) {directory.createdirectory (Outpath);              } using (FileStream StreamWriter = file.create (outpath + "\ \" + androidmanifestname)) {                int size = 2048;                byte[] data = new byte[2048]; while (true) {                  Size = s.read (data, 0, data.                    Length);                    if (Size > 0) {streamwriter.write (data, 0, size);                    } else {break;                 }} streamwriter.flush ();                 StreamWriter.Close (); The decryption operation is performed because the signed XML must be decrypted, otherwise the binary file String execstring = "Java-jar" + Decodexmljar + "" + Outpath + "\ \" + A                 Ndroidmanifestname + ">" + Outpath + "\\AndroidManifest2.xml";                 RunCommand (execstring);                 Perform a file replacement operation Thread.Sleep (3000);                 File.delete (Outpath + "\" + androidmanifestname);            File.move (Outpath + "\\AndroidManifest2.xml", Outpath + "\ \" + androidmanifestname);         } break;  }}} s.close ();   if (found = = False) {Logappend (appName + "-------Invalid", false, false); LogaPpend (Environment.NewLine, False, false); }}

2. The extracted XML file is a binary file that must be decrypted, using the Axmlprinter2.jar, as follows

private string Decodexmljar = "Axmlprinter2.jar";//Perform decryption operation String execstring = "Java-jar" + Decodexmljar + "" + Outpath + "\ \" + Androidmanifestname + ">" + Outpath + "\\AndroidManifest2.xml"; RunCommand (execstring);//Perform file substitution operation Thread.slee P (3000); File.delete (Outpath + "\" + androidmanifestname); File.move (Outpath + "\\AndroidManifest2.xml", Outpath + "\ \" + androidmanifestname);/** * Run command * */private void RunCommand (String command)  {Process p = new process ();  p.StartInfo.FileName = "cmd.exe";  P.startinfo.useshellexecute = false;  P.startinfo.redirectstandardinput = true;  P.startinfo.redirectstandardoutput = true;  P.startinfo.redirectstandarderror = true;  P.startinfo.createnowindow = true;    P.startinfo.workingdirectory = Application.startuppath;    try {p.start ();    Console.WriteLine ("Command:" + COMMAND + "&exit");    P.standardinput.writeline (command);    P.standardoutput.close ();  P.close (); } catch (Exception E1) {Console.WriteLine ("Error" + E1. Message); }}

3. After decrypting the file, we can use the XML read to process, here we refer to the package is system.xml,c# own

XmlDocument doc = new XmlDocument ();//load XML file Doc. Load (pathInfo);//Get root node XmlElement Rootelem = Doc. documentelement;//Gets the person child node collection  xmlnodelist metadatanodes = Rootelem.getelementsbytagname ("Meta-data"); String AppKey = Rootelem.getattribute ("package"); String Mangguokey = ""; String talkingData = ""; String Umengkey = ""; String Qihukey = ""; foreach (XmlNode metadatanode in metadatanodes) {if (Metadatanode.    NodeType = = xmlnodetype.element) {XmlElement nodeelement = (XmlElement) Metadatanode; String name = Nodeelement.    GetAttribute ("Android:name"); if ("Umeng_appkey"). Equals (name)) {Umengkey = nodeelement.    GetAttribute ("Android:value"); } else if ("td_app_id". Equals (name)) {TalkingData = nodeelement.    GetAttribute ("Android:value"); } else if ("mango_id". Equals (name)) {Mangguokey = nodeelement.    GetAttribute ("Android:value"); } else if ("qh_360_id". Equals (name)) {Qihukey = nodeelement.    GetAttribute ("Android:value"); } }} 

Combining the above three steps, we can easily extract the information from the XML to do the comparison.

Conclusion

    • Benefited and learned to extract the information from the Androidmanifest.xml in apk

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )

Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4528708.html

[Tools-004] How to extract androidmanifest.xml from the APK and extract the appropriate information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.