Untitled
unknown
plain_text
a year ago
2.0 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#define ERROR 1
#define PROC_INDEX 0
#define PATH_INDEX 1
#define SIGN_INDEX 2
int isElf(FILE* file);
int checkInfected(FILE* file,char* sign);
void findSign(char* path, char* sign);
char* getFullPath(char* parent,char* child);
int main(int argc, char* argv[])
{
int errorInFiles = 0;
if(argc != 3)
{
printf("Usage: %s <dir path> <virus signature file>\n",argv[PROC_INDEX]);
return ERROR;
}
FILE* virusSign = fopen(argv[SIGN_INDEX],"rb");
if(!virusSign)
{
printf("Error opening signature file!\n");
errorInFiles = ERROR;
}
DIR* path = opendir(argv[PATH_INDEX]);
if(!path)
{
printf("Error opening path directory file!\n");
errorInFiles = ERROR;
}
if(errorInFiles) return ERROR;
findSign(path,virusSign);
fclose(virusSign);
closedir(path);
return 0;
}
int isElf(FILE* file)
{
char* header = malloc(strlen("ELF"));
fread(header,sizeof(unsigned char), strlen("ELF"), file);
if(!strcmp(header,"ELF")) return 1;
return 0;
}
int checkInfected(FILE* file,char* sign)
{
int streak = 0,flag = 0;
FILE* fp = fopen(file,"rb");
if(!fp)
{
printf("Error opening inner file!\n");
return 0;
}
while(
}
void findSign(char* path, char* sign)
{
struct dirent* entry;
DIR* currDir = opendir(path);
DIR* dir;
FILE* fp;
char* entryPath;
while((entry = readdir(currDir)) != 0)
{
entryPath = getFullPath(path,entry->d_name);
if((dir = opendir(entryPath))
{
closedir(dir);
findSign(entryPath,sign);
}
else if((fp = fopen(entryPath,"rb"))
{
if(isElf(fp))
{
if(checkInfected(fp,sign)) printf("%s is infected!\n",entryPath);
}
}
free(entryPath);
}
closedir(currDir);
}
char* getFullPath(char* parent,char* child)
{
char* fullName = malloc(strlen(parent) + strlen(child) + 1);
memset(fullName,0,strlen(parent) + strlen(child) + 1);
strncpy(fullName,parent,strlen(parent));
strncat(fullName,"/",1);
strncat(fullName,child,strlen(child));
return fullName;
}
Editor is loading...
Leave a Comment