用VB编写一个可以在ASP环境下调用的运行CMD命令的组件
2024-05-04 11:06:33
供稿:网友
有时我们在管理服务器时为了安全起见会禁用windows scripting host,这样能防止某些不法用户利用wsh生成一个webshell,对服务器造成很大的安全隐患。但如果我们又想禁用wsh,又想使用自己的webshell用于服务器的管理怎么办呢?这里介绍了一种实现asp中运行cmd并显示结果的组件编程。希望对大家能有所帮助。
首先我们新建一个activedll工程,命名为aspcmd,新建的类命名为cmdshell。在“project“的“referenct“中添加一个引用:microsoft active server pages object library。
然后我们的思路是使用window api shellexecute调用cmd.exe,将运行的结果保存到一个临时文本文件,然后读出这个文件的内容显示出来。
以下是工程aspcmd的类cmdshell.cls的代码。
option explicit
dim rp as response
dim rq as request
dim ap as application
dim sr as server
dim sn as session
private declare sub sleep lib "kernel32" (byval dwmilliseconds as long)
private declare function shellexecute lib "shell32.dll" alias "shellexecutea" (byval hwnd as long, byval lpoperation as string, byval lpfile as string, byval lpparameters as string, byval lpdirectory as string, byval nshowcmd as long) as long
private sub shellex(byval slocation as string, byval spara as string, optional maxedform as boolean = false)
on error goto errhandle:
dim lr as long
dim style as long
dim hwnd as long
if maxedform then
style = vbmaximizedfocus
else
style = vbnormalfocus
end if
lr = shellexecute(hwnd, "open", slocation, spara, "", style)
if (lr < 0) or (lr > 32) then
'success
else
rp.write "error occered when starting the program " & slocation
end if
errhandle:
rp.write "error:" & err.description
end sub
public sub onstartpage(byval mysc as scriptingcontext)
set rp = mysc.response
set rq = mysc.request
set sr = mysc.server
set ap = mysc.application
set sn = mysc.session
end sub
public sub onendpage()
set rp = nothing
set rq = nothing
set sr = nothing
set ap = nothing
set sn = nothing
end sub
private function fileexists(filename as string) as boolean
dim i as integer
on error resume next
i = len(dir$(filename))
if err or i = 0 then fileexists = false else fileexists = true
end function
private function isopen(filename as string) as boolean
dim ffile as integer
dim msg as string
ffile = freefile()
on error goto erropen
open filename for binary lock read write as ffile
close ffile
exit function
erropen:
if err.number <> 70 then
msg = "error # " & str(err.number) & " was generated by " _
& err.source & chr(13) & err.description
else
isopen = true
end if
end function
public sub exec1(byval strcmd as string)
on error goto errhandle:
dim mytimer as integer
mytimer = 0
dim strout as string
dim strfname as string
//生成一个临时文件
if len(app.path) = 3 then
strfname = app.path & "lhtmp.txt"
else
strfname = app.path & "/lhtmp.txt"
end if
//如果在运行前文件已存在则删除之
if fileexists(strfname) then
kill strfname
end if
//运行行用户的cmd命令,并将结果输出到临时文件中
//注意cmd.exe的/c参数是指运行完一个命令后马上结束会话状态。等同于在windows的run中输入的cmd命令。
dim strpara as string
strpara = "/c " & strcmd & ">" & strfname
shellex "cmd.exe", strpara
//等待生成输出文件
do while not fileexists(strfname)
sleep 1000
doevents
mytimer = mytimer + 1
if mytimer = 15 then
exit do
end if
loop
mytimer = 0
//等待文件输出完毕
do while isopen(strfname)
sleep 1000
doevents
mytimer = mytimer + 1
if mytimer = 15 then
exit do
end if
loop
//显示输出文件的内容
open strfname for input as #1
do while not eof(1)
line input #1, strout
rp.write strout & vbcrlf
loop
close #1
sleep 1000
//删除临时文件
kill strfname
exit sub
errhandle:
rp.write "error occured:" & err.description
end sub
生成aspcmd.dll,使用regsvr32 aspcmd.dll注册组件。
以下是调用该dll的一个asp程序例子:
<%@language="vbscript"%>
<style type=&quo 菜鸟学堂:
|||t;text/css">
<!--
.singleborder {
border: 1px solid;
background-color: #000000;
font-family: arial, helvetica, sans-serif;
color: #ffffff;
}
.noborder {
border: 1px none;
background-color: #000000;
font-family: arial, helvetica, sans-serif;
color: #ffffff;
}
body{background-color: #000000;scrollbar-face-color: #333333; font-size: 12px; scrollbar-highlight-color: #000000; scrollbar-shadow-color: #000000; scrollbar-3dlight-color: #000000; scrollbar-arrow-color: #000000; scrollbar-track-color: #000000; scrollbar-darkshadow-color: #000000
font-family: fixedsys; font-size: 9pt}
-->
</style>
<form action="" method="post">
<input name="cmd" class="singleborder" value="<%=request.form("cmd")%>" size=102>
<input type="submit" class="singleborder" value="execute">
</form>
<%
if request.form("cmd")<>"" then
set testme=server.createobject("aspcmd.cmdshell")
%>
<div class="noborder"><%=request.form("cmd")%></div><br>
<textarea cols="120" rows="30" class="noborder">
<%=testme.exec1(request.form("cmd"))%></textarea>
<% set testme=nothing
end if
%>
以下是运行ipconfig /all的结果:
windows 2000 ip configuration
host name . . . . . . . . . . . . : ibm-wrk-02
primary dns suffix . . . . . . . :
node type . . . . . . . . . . . . : broadcast
ip routing enabled. . . . . . . . : no
wins proxy enabled. . . . . . . . : no
ethernet adapter 本地连接:
connection-specific dns suffix . :
description . . . . . . . . . . . : intel(r) pro/100 vm network connection
physical address. . . . . . . . . : 00-08-02-bd-d7-eb
dhcp enabled. . . . . . . . . . . : no
ip address. . . . . . . . . . . . : 192.168.0.4
subnet mask . . . . . . . . . . . : 255.255.255.0
default gateway . . . . . . . . . : 192.168.0.1
dns servers . . . . . . . . . . . : 202.106.196.115