资源简介
VC++源码 一个使用detours来HookAPI的简单例子
DeviceIoControl获取硬盘串号
GetAdaptersInfo获取MAC
Hook这两个API改变获取的硬盘串号和MAC地址
HDHook工程 生成dll
GetHDDSN工程 载入生成的dll 获取硬盘串号和MAC地址
需要自行安装detours
代码片段和文件信息
// GetHDDSN.cpp : 定义控制台应用程序的入口点。
//Notic:虽然使用了_T的写法,但是不能开启UNICODE
#include “stdafx.h“
#include
#include
#include
//#include
#include
#pragma comment ( lib “iphlpapi.lib“ )
BOOL GetPhyDriveSerial();
void ToLittleEndian(PUSHORT pWords int nFirstIndex int nLastIndex LPTSTR pBuf);
void TrimStart(LPTSTR pBuf);
void GetMAC();
int _tmain(int argc _TCHAR* argv[])
{
LoadLibrary(_T(“C:\\HDHook.dll“));
GetPhyDriveSerial();
GetMAC();
getchar();
return 0;
}
//
// Model Number: 40 ASCII Chars
// SerialNumber: 20 ASCII Chars
//
BOOL GetPhyDriveSerial()
{
TCHAR szModelNo[41]szSerialNo[21]szSerialNo2[21];
PBYTE pSerial=nullptr;
ZeroMemory(szModelNo41);
ZeroMemory(szSerialNo21);
ZeroMemory(szSerialNo221);
//-1是因为 SENDCMDOUTPARAMS 的结尾是 BYTE bBuffer[1];
BYTE IdentifyResult[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
BYTE pBuffer[sizeof(STORAGE_DEVICE_DEscriptOR)+512];
DWORD dwBytesReturned;
GETVERSIONINPARAMS get_version;
SENDCMDINPARAMS send_cmd = { 0 };
HANDLE hFile = CreateFile(_T(“\\\\.\\PHYSICALDRIVE0“) GENERIC_READ | GENERIC_WRITE
FILE_SHARE_READ | FILE_SHARE_WRITE NULL OPEN_EXISTING 0 NULL);
if(hFile == INVALID_HANDLE_VALUE)
return FALSE;
//get version
DeviceIoControl(hFile SMART_GET_VERSION NULL 0
&get_version sizeof(get_version) &dwBytesReturned NULL);
//identify device
send_cmd.irDriveRegs.bCommandReg = (get_version.bIDEDeviceMap & 0x10)? ATAPI_ID_CMD : ID_CMD;
DeviceIoControl(hFile SMART_RCV_DRIVE_DATA &send_cmd sizeof(SENDCMDINPARAMS) - 1
IdentifyResult sizeof(IdentifyResult) &dwBytesReturned NULL);
DeviceIoControl(hFile IOCTL_STORAGE_QUERY_PROPERTY &send_cmd sizeof(SENDCMDINPARAMS) - 1
pBuffer sizeof(pBuffer) &dwBytesReturned NULL);
CloseHandle(hFile);
//for (int j=0;j // printf(“%d:%d %c\n“jpBuffer[j]pBuffer[j]);
pSerial=pBuffer+((PSTORAGE_DEVICE_DEscriptOR)pBuffer)->SerialNumberOffset;
if (((PSTORAGE_DEVICE_DEscriptOR)pBuffer)->SerialNumberOffset&&dwBytesReturned-((PSTORAGE_DEVICE_DEscriptOR)pBuffer)->SerialNumberOffset>1)
for (int i=0;i<19;i++)
{
szSerialNo2[i+1]=(pSerial[2*i]<<4)+(pSerial[2*i+1]&0x0F)+((pSerial[2*i+1]>>6)*0x09);
i++;
szSerialNo2[i-1]=(pSerial[2*i]<<4)+(pSerial[2*i+1]&0x0F)+((pSerial[2*i+1]>>6)*0x09);
}
//adjust the byte order
PUSHORT pWords = (USHORT*)(((SENDCMDOUTPARAMS*)IdentifyResult)->bBuffer);
ToLittleEndian(pWords 27 46 szModelNo);
ToLittleEndian(pWords 10 19 szSerialNo);
TrimStart(szSerialNo);
TrimStart(szSerialNo2);
_tprintf(_T(“Model Number:%s\n“)szModelNo);
_tprintf(_T(“SMART_RCV_DRIVE_DATA Serial:%s\n“)szSerialNo);
_tprintf(_T(“IOCTL_STORAGE_QUERY_PROPERTY Serial:%s\n“)szSerialNo2);
return TRUE;
}
//把WORD数组调整字节序为little-endian,并滤除字符串结尾的空格。
void ToLittleEndian(PUSHORT pWords int nFirstIndex int nLastIndex LPTS
评论
共有 条评论