W e S D
0 1

[工具-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

posted @ 2015-05-25 19:51  SD.Team  阅读(4939)  评论(0编辑  收藏  举报