主题:[讨论]删除本文件夹及子文件夹中所有ASP文件中的一段代码
jscag
[专家分:0] 发布于 2009-08-13 07:15:00
我的网站经常被注码,ASP文件莫名其妙的就会多出一段恶意代码,我常用备份来覆盖,或用手工打开每个文件,一个个地删除这一段代码。这种做法太烦,请大师指点如何编程来清除这一段代码。[em2]
回复列表 (共1个回复)
沙发
一江秋水 [专家分:9680] 发布于 2009-08-13 09:42:00
可以用批量替换的办法,把恶意代码替换为空。不过,要求所有ASP文件中的恶意代码都是相同的。
新建一个窗体,在窗体中添加:
一个文本框,设置:MultiLine=True,ScrollBars=2
一个命令按纽,标题是:批量替换
Option Explicit
Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Type BrowseInfo
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfnCallback As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Private Sub Command1_Click()
If BatchWork() Then MsgBox "批量替换完毕" Else MsgBox "批量替换失败"
End Sub
Private Function BatchWork() As Boolean
On Error GoTo InErr
Dim PName As String '去除路径后的所有文件名
Dim PName1 As String '从PName中提取的单个文件名
Dim PName2 As String '保存时使用的文件名
Dim Ppath As String '路径
Dim St As String, z1 As String, z2 As String, i As Integer
Ppath = GetPath(): If Len(Ppath) = 0 Then Exit Function
PName1 = Dir(Ppath, 7) '找寻第一项
Do While Len(PName1) '开始循环
If UCase(Right(PName1, 3)) = "ASP" Then PName = PName & PName1 & "`"
PName1 = Dir '查找下一个文件
Loop
z1 = Text1 '查找字符串
z2 = "" '替换字符串
Do While Len(PName)
i = InStr(PName, "`")
PName1 = Ppath & Left(PName, i - 1)
PName = Mid(PName, i + 1)
GoSub 100
St = Replace(St, z1, z2, , -1)
PName2 = PName1
GoSub 200
Loop
InErr:
Close
If Err.Number = 0 Then BatchWork = True
Exit Function
100
Open PName1 For Binary As #1
St = Space(FileLen(PName1))
Get #1, , St
Close
Return
200
Open PName2 For Output As #1
Print #1, St
Close
St = ""
Return
End Function
Private Function GetPath() As String
Dim tBrowseInfo As BrowseInfo, lID As Long '文件夹列表对话框句柄
Dim sPath As String
With tBrowseInfo
.hOwner = Me.hWnd
.lpszTitle = "请选择文件夹"
.ulFlags = 3
End With
lID = SHBrowseForFolder(tBrowseInfo) '显示文件夹对话框,返回其句柄
If lID Then
sPath = Space(300)
SHGetPathFromIDList lID, sPath '获取文件夹路径
CoTaskMemFree lID '回收系统资源
sPath = Left$(sPath, InStr(sPath, vbNullChar) - 1)
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
GetPath = sPath
End If
End Function
使用方法:
把恶意代码复制到文本框,然后点击按纽,定位到ASP文件所在的文件夹,即可
我来回复