zoukankan      html  css  js  c++  java
  • [工具-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

  • 相关阅读:
    apache 错误日志
    搭建服务器
    vim配置
    临时表增加查询速度
    如何清空$_POST or $_GET
    hdu 2084
    快速幂
    zjut 1176
    rwkj 1091
    zjut 1090 --------同余定理的应用
  • 原文地址:https://www.cnblogs.com/superdo/p/4528708.html
Copyright © 2011-2022 走看看