標籤:
跟上一篇類似,我們也需要對APK的一些諸如umengkey,ADkey,TalkingData進行驗證,那麼我們同樣需要解壓apk檔案,然後提取其中的AndroidManifest.xml。然後解析xml對內容進行分析對比。
1.解壓apk檔案
if (Path.GetExtension(filePath).Equals(".apk")){ // 擷取應用程式名稱 String appName = Path.GetFileNameWithoutExtension(filePath); // 匯出目錄 String outPath = "tempandroid\\" + appName; // 建立解壓流 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); // 擷取解壓檔案名稱 string fileName = Path.GetFileName(theEntry.Name); // 遍曆尋找設定檔 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(); // 執行解密操作,由於簽名的xml必須解密,不然是二進位檔案 String execString = "java -jar " + decodeXmljar + " " + outPath + "\\" + AndroidManifestName + " > " + outPath + "\\AndroidManifest2.xml"; runcommand(execString); // 執行檔案替換操作 Thread.Sleep(3000); File.Delete(outPath + "\\" + AndroidManifestName); File.Move(outPath + "\\AndroidManifest2.xml", outPath + "\\" + AndroidManifestName); } break; } } } s.Close(); if (found == false) { logAppend(appName + "------- 無效", false, false); logAppend(Environment.NewLine, false, false); }}
2.解壓出來的xml檔案是二進位檔案,必須要解密,用的是AXMLPrinter2.jar,具體實現如下
private String decodeXmljar = "AXMLPrinter2.jar";// 執行解密操作String execString = "java -jar " + decodeXmljar + " " + outPath + "\\" + AndroidManifestName + " > " + outPath + "\\AndroidManifest2.xml";runcommand(execString);//執行檔案替換操作Thread.Sleep(3000);File.Delete(outPath + "\\" + AndroidManifestName);File.Move(outPath + "\\AndroidManifest2.xml", outPath + "\\" + AndroidManifestName);/** * 運行命令 * */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.解密檔案後,我們就可以使用xml讀取去處理,這邊我們引用的包是System.Xml,C#內建
XmlDocument doc = new XmlDocument();// 載入Xml檔案 doc.Load(pathInfo);// 擷取根節點 XmlElement rootElem = doc.DocumentElement;// 擷取person子節點集合 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"); } }}
綜合以上三步,我們可以很簡單的提取到xml中的資訊進行比對。
結語
- 受益,學會了提取apk中的AndroidManifest.xml中的資訊
本站文章為 寶寶巴士 SD.Team 原創,轉載務必在明顯處註明:(作者官方網站: 寶寶巴士 )
轉載自【寶寶巴士SuperDo團隊】 原文連結: http://www.cnblogs.com/superdo/p/4528708.html
[工具-004]如何從apk中提取AndroidManifest.xml並提取相應資訊