示例演练包含在 msvc++exp 中编译并在 32 位机器中打包的几行代码
int _tmain(int argc, _TCHAR* argv[]) {
WSADATA wsaData;
in_addr addr;
hostent *myhost;
if ((WSAStartup(MAKEWORD(2, 2), &wsaData)) == 0)
if ( ( myhost = gethostbyname("www.google.com") ) != NULL) {
printf("host name = %s\n",myhost->h_name);
if (myhost->h_addrtype == AF_INET) {
addr.s_addr = *(u_long *) myhost->h_addr_list[0];
printf("IPv4 Addr = %s\n",inet_ntoa(addr));
}
}
return 0;
}
使用条件断点在windbg中运行编译的exe,如下解释
break when ws2_32!gethostbyname is called
gu is to go up back to caller (eax will hold * to hostent structure)
display the structure pointed by eax with a c++ expression evaluator ?? and quit
.
:\>cdb -c "bp ws2_32!gethostbyname \"gu ; r eax; ?? (hostent *) @eax;q\";g" ghostbyname.exe
0:000> cdb: Reading initial command 'bp ws2_32!gethostbyname "gu ; r eax; ?? (ho
stent *) @eax;q";g'
eax=0016c3b8
struct hostent * 0x0016c3b8
+0x000 h_name : 0x0016c3f8 "www.google.com"
+0x004 h_aliases : 0x0016c3c8 -> (null)
+0x008 h_addrtype : 0n2
+0x00a h_length : 0n4
+0x00c h_addr_list : 0x0016c3cc -> 0x0016c3e4 "J}???"
quit:
:\>
windbg 也可用于确定单个成员的大小
0:000> ?? sizeof(((hostent *) @eax)->h_name)
unsigned int 4
0:000> ?? sizeof(((hostent *) @eax)->h_aliases)
unsigned int 4
0:000> ?? sizeof(((hostent *) @eax)->h_addrtype)
unsigned int 2
0:000> ?? sizeof(((hostent *) @eax)->h_length)
unsigned int 2
0:000> ?? sizeof(((hostent *) @eax)->h_addr_list)
unsigned int 4
0:000> ?? sizeof(hostent)
unsigned int 0x10
收集大小,因此以原始格式转储它变得很容易在未知的二进制文件中编写脚本
r $t0 = ${$arg1}
.printf /D "<b>eax \t%p </b>\n",@$t0
.printf /D "<b>hostent * \t%p </b>\n",poi(@$t0)
.printf /D "<b>->h_name \t%ma</b>\n",poi(@$t0)
.printf /D "<b>->h_alias \t%p </b>\n",poi(poi(@$t0+0x4))
.printf /D "<b>->h_addrtype \t%p </b>\n",low(poi(@$t0+0x8))
.printf /D "<b>->h_length \t%p </b>\n",hi(poi(@$t0+0x8))
.printf /D "<b>->h_addrlist \t%p </b>\n",poi(poi(@$t0+0xc))
.printf /D "<b>->h_addrlist contains ip address in network byte order</b>\n"
db poi(poi(@$t0+0xc)) l10
.printf /D "<b>Ipv4 Address of google.com %d.%d.%d.%d\n" ,
by(poi(poi(@$t0+0xc))),by(poi(poi(@$t0+0xc))+1),by(poi(poi(@$t0+0xc))+2),by(poi(poi(@$t0+0xc))+3)
结果以原始格式转储,不支持未知二进制文件中的符号
0:000> $$>a< ghostbyname.txt @eax
eax 0016c3b8
hostent * 0016c3f8
->h_name www.google.com
->h_alias 00000000
->h_addrtype 00000002
->h_length 00000004
->h_addrlist 0016c3e4
->h_addrlist contains ip address in network byte order
0016c3e4 4a 7d ec d0 4a 7d ec d4-4a 7d ec d3 4a 7d ec d1 J}..J}..J}..J}..
Ipv4 Address of google.com 74.125.236.208