您的位置:首页 > 其它

获取apk中的证书信息

2016-05-24 23:01 260 查看

模块目标

从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()函数一个一个匹配。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: