获取apk中的证书信息

来源:互联网 发布:淘宝侵权处罚标准 编辑:程序博客网 时间:2024/04/28 02:51

模块目标

从apk解压出的文件夹中找到所有后缀名为cer|pem|cert|crt|pub|key|pfx|p12的文件路径

Git

这个项目的所有模块位于github:https://github.com/LeeHDsniper/APKAnalyserModules

代码要点

要找寻文件路径,在python中很容易,使用os.walk(‘path’)就能实现,但是在C++中,就需要自己编写函数了

头文件

#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
在linux下,文件夹的操作在zlib解压缩那篇文章中已经说过
这里需要注意第一个头文件dirent.h,这个头文件中定义了一个结构提dirent,这个结构体保存了当前文件/文件夹的信息。
例如dirent->d_name就保存了文件名,dirent->d_type 保存了文件类型(文件or文件夹)等等。具体的信息可百度。

函数设计

我们需要编写一个函数,这个函数将递归地遍历初始路径下的所有文件夹和文件。
在当前文件类型为8即为文件时,我们获取这个文件的文件名,获取文件名后缀,匹配cer|pem|cert|crt|pub|key|pfx|p12,如果是,则返回当前文件的全路径。

上代码

#include <iostream>#include <dirent.h>#include <sys/types.h>#include <sys/stat.h>#include <string>#include <cstring>using namespace std;void GetHardcodedCert( const char *path, int &sum, char **file_Path ){  struct dirent * ent = NULL;  DIR   *pDir;  pDir = opendir( path );  if ( pDir == NULL )  {    return;  }  while ( (ent = readdir( pDir ) ) != NULL )  {    if ( ent->d_type == 8 )    {      char *p, *suffix;      p = ent->d_name;      while ( (*p) != '\0' )      {        if ( ( (*p) == '.') )          suffix = p + 1;        p++;      }      if ( (strcmp( suffix, "cer" ) == 0) || (strcmp( suffix, "pem" ) == 0) ||           (strcmp( suffix, "cert" ) == 0) || (strcmp( suffix, "crt" ) == 0) ||           (strcmp( suffix, "pub" ) == 0) || (strcmp( suffix, "key" ) == 0) ||           (strcmp( suffix, "pfx" ) == 0) || (strcmp( suffix, "p12" ) == 0) )      {        int len = (int) strlen( path ) + (int) strlen( ent->d_name ) + 1;        file_Path[sum] = new char[len];        strcpy( file_Path[sum], path );        file_Path[sum][(int) strlen( path )] = '/';        strcat( file_Path[sum], ent->d_name );        sum++;      }    }else  {      if ( strcmp( ent->d_name, "." ) == 0 || strcmp( ent->d_name, ".." ) == 0 )      {        continue;      }      /* directory */      string  _path( path );      string  _dirName( ent->d_name );      string  fullDirPath = _path + "/" + _dirName;      GetHardcodedCert( fullDirPath.c_str(), sum, file_Path );    }  }}

在这里我没有使用正则表达式,因为C++的正则确实不是很好用,所以直接使用使用strcmp()函数一个一个匹配。

0 0
原创粉丝点击