显示标签为“Develop”的博文。显示所有博文
显示标签为“Develop”的博文。显示所有博文

2009/12/14

通过虚拟机进行 Windows Kernel Debug

文介绍了硬件方式,这次来个更快的,通过虚拟机进行 Windows Kernel Debug。现在计算机硬件如此强大,我们完全可以把Target运行在一个虚拟机内,然后通过宿主机来调试。

首先,target——也就是虚拟机,也要进行同样的启动配置,让它进入debug模式。

其次,配置虚拟机程序,让它把target的COM端口 映射到一个命名管道。


以Virtual PC2007为例子,需要把Target的端口映射成一个 命名管道。其他的虚拟及程序如 VMWare 以及 VirtualBox也都有相应的设置。

然后,修改 windbg的参数,通过命名管道连接target
windbg -k com:pipe,port=\\.\pipe\com2,resets=0,reconnect
连接搞定!!!

效果和使用一个 单独的target计算机一样 :D

Windows Kernel Debug

Debug 对于程序开发来说是非常非常重要的手段。一个良好的Debug开发环境会让开发工作无往而不利。 对于 Microsoft Windows这样的庞然大物,内核开发的调试工作是非常有挑战性的。Windows 驱动程序开发,这些都需要进行内核级别的调试。当然,Microsoft的同志们,感触肯定比我们要多,因为他们每天都要在上面工作。
Microsoft实际上也已经作了大量的工作,对于Windows内核开发而言,内核的调试其实还是很简单的。

1, 硬件环境准备
进行windows内核调试,原则上需要两台计算机:一个运行测试代码叫做target;另一个运行debugger 叫做 Host。在Target与Host之间需要 通讯连接。目前比较常见的有三种: 串口,USB, 和1394。 串口是最常见的,下面就以串口为例。

Figure 1 Target and Host.
(from http://www.wd-3.com/archive/RemoteDbg.htm, Author: Jolyon Wright)
可以把 Host COM1 连接 Target COM1;也可以 Host COM2 连 Target COM1。
连接的时候不需要 一一对应。

2,Target 设置
所谓Target设置,其实就是打开Target计算机 Windows操作系统的调试选项。Windows都是内置内核调试功能的, 但是缺省关闭的。需要通过修改启动参数 (boot option) 打开。

对于Windows server 2003/XP/2000 操作系统,可以修改 Boot.ini 在启动项的后面加上
“/debug /debugport=COM2 /baudrate=115200”
看起来就象下面这样:
multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Windows XP Debug1" /fastdetect /debug /debugport=COM1 /baudrate=115200
这里 debugport 可以用COM1/2, Baudrate也可以作相应的调整 115200是不错的选择。

对于Windows Vista/2008/7 操作系统,需要使用 bcdedit来设置。这里需要两步:
第一: 打开debug
bcdedit /debug [on | off]
第二: 设置debug参数
bcdedit /dbgsettings SERIAL DEBUGPORT:2 BAUDRATE:115200
第二步其实可以跳过,系统缺省使用串口COM1, 速率:115200。

然后重新启动Target,进入调试模式。 其实在调试模式下,如果没有连上Debugger,系统运行起来就跟普通的系统差不多,可能会慢一些。


3, Host调试软件
Host 需要安装 Debugging Tools for Windows (http://www.microsoft.com/whdc/devtools/downloads.mspx)
安装完毕后,就可以使用Windbg连接 target。建议使用 6.11或更高的版本。

运行参数:
windbg -k com:port=Com1,baud=115200

Windbg启动后应该显示:
Microsoft (R) Windows Debugger Version 6.1.0017.2
Copyright (c) Microsoft Corporation. All rights reserved.
Opened \\.\com1
Waiting to reconnect...

Target连接成功后应该显示:
Connected to Windows XP 2600 x86 compatible target, ptr64 FALSE
Kernel Debugger connection established.
Symbol search path is: \\Sysphus\softshared\symbols\KillerApp\sym;SRV*\\Sysphus\softshared\symbols\SymStore*http://msdl.microsoft.com/download/symbols
Executable search path is:
Windows XP Kernel Version 2600 MP (1 procs) Checked x86 compatible
Built by: 2600.xpsp1.020828-1920

显示的内容会随着Target运行软件的版本而有些不同。
在windbg 通过 ctrl+break键 来中断 target,进入调试交互状态,这时候Target就像死了一样,别紧张,这是因为你再通过debugger来 控制它。

4, Host 配置Symbol Path
调试中有一个很重的步骤就是 Symbol,没有Symbol 在调试中寸步难行,我们只能面对一堆二进制代码。微软提供的Symbol支持方案配置很灵活,可以有本地symbol 也可以通过Symbol server, symbol cache。 关于symbol 在 Debugging Tools for Windows的 帮助文件中 专门有一个章节介绍: Debuggers >> Symbols
比较快捷的办法:下载 Windows Symbol Files;同时配置 Microsoft的symbol server。
A, 下载相应版本的 Windows Symbol Files http://www.microsoft.com/whdc/devtools/debugging/symbolpkg.mspx
安装到 一个位置:如 C:\WinSymbols
B, 在Windbg中设置 Symbol paths: File--> Symbol File Path 快捷方式(Ctrl+S)
设置path 为:
C:\WinSymbols;d:\OtherPath\To\the\build\pdb;srv*D:\Symbols*http://msdl.microsoft.com/download/symbols
这里D:\Symbols 是用来保存从 Microsoft symbol服务器下载回来的 pdb。

剩下的工作就是学习如何使用 windbg了。 :)

Goood luck~~~

2008/08/07

How to Write a Good Technical Documentation

Writing good documentation always hard! But you can make it better.

DO:

  • Fully understand the situation, and know the 5 W's:
    "Who, What, Where, When, and Why".

  • KISS: Keep It Simple and Stupid
  • Write agenda before you start

  • Keep the technical integrity
  • Add reference links for all the shared knowledge

  • Use diagram to show the complex logical
  • Use table to show the structured information
  • Compare the different design alternatives
  • Use bulletin and numbered list to express the idea

  • Write a good looking document
  • Have a good template
  • Use named style to control the format
  • Always do spelling/grammar check
  • Tidy your documentation

  • Ask for comments from others
  • Track changes between the reversions

DON'T:
  • Write long sentence or paragraph
  • Be afraid to delete some pieces
  • Be afraid to reconstruct sections
  • Be afraid to spend more time

Miscellanea:
  • Ask for others' help, and help others
  • Eats your own dog food
    Read it once more when you feel it were finished

2007/01/17

Hack the IDLE.DLL of Yahoo Messenger

Yahoo Messenger is very nice IM software. But it used the very old window HOOK functions to detect the user idle time through IDLE.DLL. The idle.dll is a 6k DLL file which created with Microsoft VC 7.1 and linked with MSVCR71.DLL. In this way, the IDLE.DLL will be injected to all the user applications, and hook the message queue of all applications, and it will also injects the MSVCR71.DLL into the user applications. Please refer MSDN for details about the Windows HOOK function.

Yahoo Messenger 是一个很不错的IM软件。但是它是用了一个非常陈旧的Windows Hook功能,因此制造了一个idle.dll 用来来检测用户的发呆时间。这个IDLE.DLL 文件看起来并不大,只有6k,其实它是一个用VC7.1编译产生的动态链接库并且动态链接到MSVCR71.DLL。 由于HOOK函数需要把 IDLE.DLL 注入到所有的用户程序中,MSVCR71.DLL 也会被注入到所有的用户程序中。 而且这个IDLE.DLL 会进入所有用户程序的消息循环。在所有的程序中都有这个 idle.dll 实在是让人不爽。 ;)

Let's check the IDLE.DLL
下面就让我们看看这 IDLE.DLL


C:\Program Files\Yahoo!\Messenger>dumpbin /exports idle.dll
Microsoft (R) COFF/PE Dumper Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file idle.dll

File Type: DLL

Section contains the following exports for idle.dll

00000000 characteristics
450867C8 time date stamp Thu Sep 14 04:19:20 2006
0.00 version
1 ordinal base
3 number of functions
3 number of names

ordinal hint RVA name

1 0 00001016 ?IdleUIGetLastInputTime@@YAKXZ
2 1 000010C3 ?IdleUIInit@@YAHXZ
3 2 0000111C ?IdleUITerm@@YAXXZ

Summary

1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
1000 .text
1000 Y_IDLE


It has 3 C++ functions defined, and a customized data section "Y_IDLE", I think it is the shared data section which used to store the latest user active time stamp. While what's the function protocol for these 3 function? I found a very good documentation for the C++ name mangling of different compilers at URL http://www.agner.org/optimize/ Calling conventions for different C++ compilers and operating systems
它定义了三个输出函数,同时自定义了一个程序段 Y_IDLE 相比这应该是一个数据段,用来记录从各个应用程序收集到的最新的用户消息发出的时间。 而这三个函数应该是用C++的命名规则输出的,但是这三个函数的原型是什么呢?幸好发现了一个非常好的文档,详细地描述了各种编译器的 C++ name mangling 也就是符号标的转换规则。参见 http://www.agner.org/optimize/ Calling conventions for different C++ compilers and operating systems


In fact, from the Windows 2000, WIN_VER>=0x500 there is a new function GetLastInputInfo in user32.dll, it will provide the Last user input time stamp quickly.
其实从Window2000开始,微软在 User32.dll 就提供了一个新的函数 GetLastInputInfo 它就可以用来返回用户的最后输入的时间。



/**
* This is used to hack the Yahoo Messenger.
* compiled with VC7.1 as:
* cl -W3 -O1sy -LD -MD -D_WIN32_WINNT=0x0500 idle.cpp -Feidle.dll User32.lib
*/

#include

#pragma section("Y_IDLE",read,write)
__declspec(allocate("Y_IDLE"))
int Y_IDLE = 0;

#define DllExport __declspec( dllexport )

DllExport DWORD IdleUIGetLastInputTime()
{
LASTINPUTINFO lii;
lii.cbSize = sizeof(lii);
GetLastInputInfo(&lii);
return lii.dwTime;
}

DllExport INT IdleUIInit()
{
return TRUE;
}

DllExport VOID IdleUITerm()
{
return;
}



This will be able to create a new IDLE.DLL
这样就可以知道一个新的 idle.dll , 而且它也再也不会被注入到其他的程序中了。
替换原来的 IDLE.DLL 还真不错~~~~ 工作正常。


写在后面


在找到 IDLE.DLL 输出函数的原型还真花了些时间。后来才发现其实微软已经给我们提供了一个很方便的工具,那就是随Platform SDK 分发的 Dependency Walker (depends.exe) 里面有一个 Undecorate C++ functions 的功能,我们只需要用Dependency Walker 打开 IDLE.DLL 就可以看到输出函数的原型了。


其实

BlockChain 相关电子书

@copyright of Sam Chadwick   - https://thehub.thomsonreuters.com/groups/bitcoin/blog/2017/09/10/blockchain-paper Blockchain Papers A c...