通过WMI获取据盘符获取U盘物理序列号.docx

上传人:b****3 文档编号:3696579 上传时间:2022-11-24 格式:DOCX 页数:36 大小:185.34KB
下载 相关 举报
通过WMI获取据盘符获取U盘物理序列号.docx_第1页
第1页 / 共36页
通过WMI获取据盘符获取U盘物理序列号.docx_第2页
第2页 / 共36页
通过WMI获取据盘符获取U盘物理序列号.docx_第3页
第3页 / 共36页
通过WMI获取据盘符获取U盘物理序列号.docx_第4页
第4页 / 共36页
通过WMI获取据盘符获取U盘物理序列号.docx_第5页
第5页 / 共36页
点击查看更多>>
下载资源
资源描述

通过WMI获取据盘符获取U盘物理序列号.docx

《通过WMI获取据盘符获取U盘物理序列号.docx》由会员分享,可在线阅读,更多相关《通过WMI获取据盘符获取U盘物理序列号.docx(36页珍藏版)》请在冰豆网上搜索。

通过WMI获取据盘符获取U盘物理序列号.docx

通过WMI获取据盘符获取U盘物理序列号

通过WMI获取据盘符获取U盘物理序列号

 

 

————————————————————————————————作者:

————————————————————————————————日期:

 

JavaScript通过WMI获取根据盘符获取U盘物理序列号

一:

达到的效果

根据UKeyTool生成的U盘文件xxxx_private.ukey内的md5与U盘物理序列号+xxxx_uKey+界面读取文件自由串(50*10)计算的md5对比,如果一样,则允许登录,否则不允许登录

二:

加密工具

1原理

U盘序列号信息格式:

vid_0537&pid_a703#192b0c00048a,其中192b0c00048a为U盘的物理序列号(唯一);

U盘中的验证文件名为xxxx_private.ukey;将U盘的物理序列号加工后保存在文件xxxx_private.ukey中,使U盘和文件一一对应。

2加密步骤

1.把U盘的物理序列号中的所有字母转换成大写,如192B0C00048A,标为串1;

2.固定串xxxx_uKey,标为串2;

3.随机生成50*10个的可见字符:

ASCII范围33-126,标为串3;

4.串1、串2、串3顺序合并,计算32位MD5(字母大写);

5.创建文件ipbc_private.ukey,写入字符串:

File:

xxxx_V3USBPrivatekey;

6.换行,写入版本号,格式:

Version:

1.0.0.0

7.换行,写入MD5值;

8.换行,写入串3,每50个字符换行;

三:

JavaScript解密

1解密步骤

1.把U盘的物理序列号中的所有字母转换成大写,如192B0C00048A,标为串1;

2.固定串xxxx_uKey,标为串2;

3.打开文件xxxx_private.ukey,读入50*10个可见字符,标为串3;

4.串1、串2、串3顺序合并,计算32位MD5(字母大写);

5.打开文件xxxx_private.ukey,读取MD5值;

6.比较计算的和读取的MD5值是否相同,相同则通过验证,否则失败。

2JavaScript读取U盘物理序列号(推荐方案3)

准备

1读取U盘盘符

/**

*判断是否有U盘并获取U盘盘符,只适用于IE?

*@returns{string}返回U盘盘符

*@constructor

*/

functionCheckUsb(){

vare,x;

varForReading=1,

RorWriting=2;

varbfResult='';

try{

varfso=newActiveXObject("Scripting.FileSystemObject");

}catch(err){

returnbfResult;

}

e=newEnumerator(fso.Drives);

for(;!

e.atEnd();e.moveNext()){

x=e.item();

if(x.DriveType==1){

if(x.Path!

="A:

"){

bfResult+=x.Path+',';

}

}

}

returnbfResult;

}

方案1:

根据WMI的Win32_USBHub读取U盘物理序列号

缺陷:

无法根据盘符一对一获取U盘物理序列号

缺陷说明:

此方法获取的intCount,及U盘迭代index跟注册表中的键对应

注册表键地址:

"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Disk\\Enum\\"+(intCount+1)

次部分键对应的值的注册表地址为:

"HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Enum\\USBSTOR\\"+值

在此处可以查看U盘的物理序列号,可验证获取的是否正确。

点击物理序列号会出现右侧:

其键ParentIdPrefix对应的即盘符信息。

盘符信息注册表地址:

"HKEY_LOCAL_MACHINE\\SYSTEM\\MountedDevices"

需要将ParentIdPrefix转换成十六进制,并在此处与值查询,获得的键即盘符信息。

参考地址:

 

出错场景:

当有多个U盘,第一次可能会通过,比如有G,I,H三个盘,此时根据注册顺序分别为1,2,3;如果在验证过程中将G盘拔出,此时I,H盘的顺序为1,2;再插入G盘进行验证,此时G盘顺序为3;

但是准备部分获取的U盘盘符顺序为G,I,H,此时迭代的盘符index或获取到I盘的物理序列号,导致验证失败。

读取U盘物理序列号

迭代准备部分获取的U盘盘符,并将盘符和迭代index传入此方法,获取物理序列号

/**

*根据盘符获取物理序列号

*@paramdrivepath盘符

*@paramintCount轮询获取次数

*@returns{*}返回盘符与物理序列号字符串,格式:

G:

XXXXXXXXXXXXXX

*@constructor

*/

functionReadUsbSnEach(drivepath,intCount){

varproperties=service.ExecQuery("SELECT*FROMWin32_USBHub");

//Win32_USBHub所有属性说明:

vare=newEnumerator(properties);

varcount=0;

for(;!

e.atEnd();e.moveNext()){

varp=e.item();

varsn=p.DeviceID;

//1:

GenericUSBHub;2:

USBRootHub;3:

USBMassStorageDeviceUSB大容量存储设备

//ConfigManagerErrorCode=0Deviceisworkingproperly.

//因每个盘的Name不同,这里只判断英文和中文两种,超过的U盘将不被验证通过

if(p.Name=="USBMassStorageDevice"||p.Name=="USB大容量存储设备"){

count=count+1;

}

if(count==intCount+1){

//&p.ConfigManagerErrorCode==0

if((p.Name=="USBMassStorageDevice"||p.Name=="USB大容量存储设备")){

strSN+=GetUsbSN(sn);

//alert(drivepath+"------"+count+"-----"+p.Name+"-----"+strSN);

returnstrSN;

}

}

}

}

Win32参考地址:

物理序列号处理:

/**

*分解通过注册表获取的值或其他方式获取同样的值,只取物理序列号即可

*@paramsn通过注册表获取的值或其他方式获取的同格式的值

*@returns{*}物理序列号

*@constructor

*/

functionGetUsbSNByReg(sn)

{

varUsbSN;

varstrSN=sn;

vararraySN=newArray();

if(strSN.indexOf("&")>0){

arraySN=strSN.split("\\");

if(arraySN.length>2){

UsbSN=arraySN[2].split(("&"))[0];

}else{

UsbSN="";

}

}else{

UsbSN="";

}

returnUsbSN;

}

方案3:

根据注册表读取U盘物理序列号

缺陷:

无法根据盘符一对一获取U盘物理序列号

缺陷说明:

参考方案1缺陷对应部分

参考地址:

参考方案1缺陷对应部分

出错场景:

参考方案1缺陷对应部分

读取U盘物理序列号

迭代准备部分获取的U盘盘符,并将盘符和迭代index传入此方法,获取物理序列号

/**

*通过注册表获取物理序列号

*@paramdrivepath盘符

*@paramintCount盘符index

*@returns{string}

*@constructor

*/

functionReadUsbSnEachByReg(drivepath,intCount){

varsn="";

varobj=newActiveXObject("WScript.Shell");

try{

vars="HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Disk\\Enum\\"+(intCount+1);

varsNic=obj.RegRead(s);//USBSTOR\Disk&Ven_Kingston&Prod_DT_101_G2&Rev_5.00\1401051502351655498409&0

sn=drivepath+GetUsbSNByReg(sNic);

}catch(e){

}

returnsn;

}

物理序列号处理:

参考方案1缺陷对应部分

方案2:

根据WMI的其他Win32方法读取U盘物理序列号

其他Win32方法

(Win32_LogicalDisk,Win32_LogicalDiskToPartition,Win32_DiskDriveToDiskPartition,Win32_DiskDrive)

读取U盘物理序列号

/**

*根据盘符获取物理序列号

*@paramdrivepath盘符

*@paramintCount轮询获取次数目前无用,保留,注释的Win32_USBHub方式获取有使用到

*@returns{*}返回盘符与物理序列号字符串,格式:

G:

XXXXXXXXXXXXXX

*@constructor

*/

functionReadUsbSnEach(drivepath,intCount){

varstrSN=drivepath;//存放序列号

varlocator=newActiveXObject("WbemScripting.SWbemLocator");

varservice=locator.ConnectServer(".");

varlogical_disk_query="selectDeviceId,CreationClassName,SystemNamefromWin32_LogicalDiskwhereCaption='"+drivepath+"'";

varlogical_disk_property=service.ExecQuery(logical_disk_query);

varlogical_disk_Enum=newEnumerator(logical_disk_property);

for(;!

logical_disk_Enum.atEnd();logical_disk_Enum.moveNext()){

varlogical_disk_item=logical_disk_Enum.item();

vardependent="//"+logical_disk_item.SystemName+"/root/cimv2:

"+logical_disk_item.CreationClassName+".DeviceID='"+logical_disk_item.DeviceId+"'";

varlogical_disk_to_part_query="selectAntecedent,DependentfromWin32_LogicalDiskToPartitionwhereDependent=\""+dependent+"\"";

varlogical_disk_to_part_property=service.ExecQuery(logical_disk_to_part_query);

varlogical_disk_to_part_Enum=newEnumerator(logical_disk_to_part_property);

for(;!

logical_disk_to_part_Enum.atEnd();logical_disk_to_part_Enum.moveNext()){

varlogical_disk_to_part_item=logical_disk_to_part_Enum.item();

vardependent=logical_disk_to_part_item.Antecedent.replace(/\\/g,"/");

vardisk_drive_to_part_query="selectAntecedent,DependentfromWin32_DiskDriveToDiskPartitionwhereDependent='"+dependent+"'";

vardisk_drive_to_part_property=service.ExecQuery(disk_drive_to_part_query);

vardisk_drive_to_part_Enum=newEnumerator(disk_drive_to_part_property);

for(;!

disk_drive_to_part_Enum.atEnd();disk_drive_to_part_Enum.moveNext()){

vardisk_drive_to_part_item=disk_drive_to_part_Enum.item();

vardisk_drive_query="selectSystemName,CreationClassName,DeviceID,PNPDeviceIDfromWin32_DiskDrivewhereinterfaceType='USB'";

vardisk_drive_property=service.ExecQuery(disk_drive_query);

vardisk_drive_Enum=newEnumerator(disk_drive_property);

for(;!

disk_drive_Enum.atEnd();disk_drive_Enum.moveNext()){

vardisk_drive_item=disk_drive_Enum.item();

vardependent="\\\\"+disk_drive_item.SystemName+"\\root\\cimv2:

"+disk_drive_item.CreationClassName+".DeviceID=\""+disk_drive_item.DeviceId.replace(/\\/g,"\\\\")+"\"";

if(disk_drive_to_part_item.Antecedent==dependent){

varsn=GetUsbSNByReg(disk_drive_item.PNPDeviceID);

returnstrSN+sn;

}

}

}

}

}

returnstrSN;

}

代码解析:

此处使用《WMI资源浏览器.exe》显示查看个方法获取的数据

下载地址:

步骤1:

1通过盘符在Win32_LogicalDisk中获取三个字段DeviceId,CreationClassName,SystemName

"selectDeviceId,CreationClassName,SystemNamefromWin32_LogicalDiskwhereCaption='"+drivepath+"'";

2处理此三个字段为符合Win32_LogicalDiskToPartition格式的串

"//"+logical_disk_item.SystemName+"/root/cimv2:

"+logical_disk_item.CreationClassName+".DeviceID='"+logical_disk_item.DeviceId+"'";

步骤2:

1将步骤1中处理的串当成步骤2Dependent的值查询两个字段Antecedent,Dependent

"selectAntecedent,DependentfromWin32_LogicalDiskToPartitionwhereDependent=\""+dependent+"\""

2处理字段Antecedent为Win32_DiskDriveToDiskPartition合适的串

logical_disk_to_part_item.Antecedent.replace(/\\/g,"/");

步骤3:

1将步骤2中处理的串当成步骤3Dependent的值查询两个字段Antecedent,Dependent

"selectAntecedent,DependentfromWin32_DiskDriveToDiskPartitionwhereDependent='"+dependent+"'"

步骤4:

1查询Win32_DiskDrive出四个字段SystemName,CreationClassName,DeviceID,PNPDeviceID

"selectSystemName,CreationClassName,DeviceID,PNPDeviceIDfromWin32_DiskDrivewhereinterfaceType='USB'"

2将此四个字段按照步骤3中Antecedent的值的格式拼接

"\\\\"+disk_drive_item.SystemName+"\\root\\cimv2:

"+disk_drive_item.CreationClassName+".DeviceID=\""+disk_drive_item.DeviceId.replace(/\\/g,"\\\\")+"\""

3如果两个值相同,则取出步骤3中的Antecedent的值进行处理,处理方法见方案1中的物理序列号处理

4处理后的即根据盘符得到的U盘物理序列号

3按照加密工具加密的步骤进行加密

4与加密工具加密读取出的MD5值比较

5验证成功或失败

四:

完整的JavaScript代码

functioninclude(path){

vara=document.createElement("script");

a.type="text/javascript";

a.src=path;

varhead=document.getElementsByTagName("head")[0];

head.appendChild(a);

}

//include("./jquery1.3.2.js");

document.write('');

/**

*判断是否有U盘并获取U盘盘符,只适用于IE?

*@returns{string}返回U盘盘符

*@constructor

*/

functionCheckUsb(){

vare,x;

varForReading=1,

RorWriting=2;

varbfResult='';

try{

varfso=newActiveXObject("Scripting.FileSystemObject");

}catch(err){

returnbfResult;

}

e=newEnumerator(fso.Drives);

for(;!

e.atEnd();e.moveNext()){

x=e.item();

if(x.DriveType==1){

if(x.Path!

="A:

"){

bfResult+=x.Path+',';

}

}

}

returnbfResult;

}

/**

*读取U盘内文件ipbc_private.ukey内的MD5值和随机串FF存在问题

*@parampathfile文件路径

*@returns{*}返回content_array数组

*[0]为md5值

*[1]为50*10随机串

*/

functionread(pathfile){

varcontent_array=newArray();

if(typeofwindow.ActiveXObject!

='undefined'){

try{

varfso=newActiv

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1