我想遍历给定的目录并查找给定的文件签名是否存在于该目录的任何常规文件中。
这是我的代码:
char* given_signature = "981d0000ec33fffffb06000000460e10";
int file_sign(char* path){
FILE* file = fopen(path, "rb");
if(!file){
printf("error with file opening");
return -1;
}
fseek(file, 0, SEEK_END);
long filelen = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = malloc(filelen);
if(buffer)
fread(buffer, 1, filelen, file);
fclose(file);
for(int i = 0; i < filelen - 16; i++){
if(memcmp(buffer + i, given_signature, 16) == 0){
printf("Signature found in %s\n", path);
}
}
free(buffer);
return 0;
}
void traverse_dirs(char* base_path){
char path[_MAX_LINE_];
struct dirent* dp;
DIR* dir = opendir(base_path);
if(!dir)
return;
while((dp = readdir(dir)) != NULL){
if(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
continue;
strcpy(path, base_path);
strcat(path, "/");
strcat(path, dp->d_name);
if(dp->d_type == DT_REG){
file_sign(path);
}
traverse_dirs(path);
}
closedir(dir);
}
遍历正确完成,因为它适用于其他一些函数。所以问题出在
file_sign()
函数中,但我找不到我做错了什么。
我是否错误地实现了给定的签名?例如:我可以这样做吗:
char* given_signature[] = {"98", "1d", "00", "00", "ec", "33", "ff", "ff", "fb", "06", "00", "00", "00", "46", "0e", "10"};
并逐字节解析文件?如果是的话我该怎么做?
有什么想法吗?
您显然对字符串和二进制数据感到困惑。您显示的字符串长度为 33 个字节(包括空终止符),并且由纯十六进制数字组成。这些与您尝试表示的字节无关。
你想要的是这样的:
char* given_signature[] = {
0x98, 0x1d, 0x00, 0x00,
0xec, 0x33, 0xff, 0xff,
0xfb, 0x06, 0x00, 0x00,
0x00, 0x46, 0x0e, 0x10
};