获取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()函数一个一个匹配。
相关文章推荐
- 梦断代码阅读笔记03
- Tire树
- java 获取当前天之后或之前7天(任意)日期
- Qt5+MinGW 调用ffmpeg
- Jenkins+Git+Maven+Shell+Tomcat持续集成 经典教程
- C++第六次作业
- 关于java的线程池与同步之间的关系
- 调试日志输出假想
- HorizontalScrollView的简单用法
- java cpu占用过大
- 用AD组策略--彻低禁止USB存储设备
- 欢迎使用CSDN-markdown编辑器
- linux内核栈和用户栈
- Kafka技术内幕-日志压缩
- APP开发实战38-Cookie介绍
- 排序算法之快速排序
- 关于浏览器和网络的20项须知-什么是互联网?
- native binder相关类
- 四种AD安装的情况以及配置方法
- Linux的防火墙–Iptables