[工具-004]如何從apk中提取AndroidManifest.xml並提取相應信息


  跟上一篇類似,我們也需要對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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM