<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer, Electron and Technology &#187; server</title>
	<atom:link href="http://www.donevii.com/post/tag/server/feed" rel="self" type="application/rss+xml" />
	<link>http://www.donevii.com</link>
	<description>关注技术、移动互联网以及一切 GEEK &#38; NERD 的事情</description>
	<lastBuildDate>Wed, 21 Dec 2011 10:49:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Intel® I/O Acceleration Technology (Intel® I/OAT)</title>
		<link>http://www.donevii.com/post/689.html</link>
		<comments>http://www.donevii.com/post/689.html#comments</comments>
		<pubDate>Tue, 16 Dec 2008 06:09:58 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/689.html</guid>
		<description><![CDATA[Intel® I/O Acceleration Technology (Intel® I/OAT), with Intel® QuickData Technology, moves data more efficiently through Intel® Xeon® processor-based servers for fast, scalable, and reliable network performance. Performance A primary benefit of Inte... ]]></description>
			<content:encoded><![CDATA[<p class="intro">Intel® I/O Acceleration Technology (Intel® I/OAT),<br />
with Intel® QuickData Technology, moves data more efficiently through<br />
Intel® Xeon® processor-based servers for fast, scalable, and reliable<br />
network performance.</p>
<p>		<!-- /product-overview-intro --></p>
<h2>Performance</h2>
<p>A<br />
primary benefit of Intel I/OAT is its ability to significantly reduce<br />
CPU overhead, freeing resources for more critical tasks. Intel I/OAT<br />
uses the <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">server</a>&#8217;s processors more efficiently by leveraging<br />
architectural improvements within the CPU, chipset, network controller,<br />
and firmware to minimize performance-limiting bottlenecks. Intel I/OAT<br />
accelerates TCP/IP processing, delivers data-movement efficiencies<br />
across the entire server platform, and minimizes system overhead.</p>
<h2>Scalability</h2>
<p>Intel<br />
I/OAT provides network acceleration that scales seamlessly across<br />
multiple Gigabit Ethernet (GbE) ports. It cost-effectively scales up to<br />
eight GbE ports and up to 10GbE, with power and thermal characteristics<br />
similar to those of a standard gigabit network adapter. TCP Offload<br />
Engine (TOE) solutions, in contrast, require a separate TOE card for<br />
each port, resulting in significant cost and thermal challenges for<br />
server platforms.</p>
<h2>Reliability</h2>
<p>Intel I/OAT<br />
is a safe and flexible choice because it is tightly integrated into<br />
popular operating systems such as Microsoft <a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">Windows</a> Server* 2003 and<br />
<a href="http://www.donevii.com/post/tag/linux" class="st_tag internal_tag" rel="tag" title="Posts tagged with linux">Linux</a>*, avoiding support risks associated with relying on third-party<br />
hardware vendors for network stack updates. Intel I/OAT also preserves<br />
critical network configurations such as teaming and failover, by<br />
maintaining control of the network stack processing within the<br />
CPU-where it belongs. This results in reduced support risks for IT<br />
departments.</p>
<p></p>
<p>via <a target="_blank" href="http://www.intel.com/technology/ioacceleration/">Intel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/689.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mac OS X Update 更新</title>
		<link>http://www.donevii.com/post/686.html</link>
		<comments>http://www.donevii.com/post/686.html#comments</comments>
		<pubDate>Tue, 16 Dec 2008 03:38:54 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/686.html</guid>
		<description><![CDATA[地址簿 增强了地址簿与 iPhone、MobileMe 和其他设备及应用程序同步时的可靠性。 AirPort 增强了 AirPort 连接的可靠性，包括使用基于 Intel 的 Mac 在大型无线网络中漫游时的功能改进。 客户端管理 ... ]]></description>
			<content:encoded><![CDATA[<p><b>地址簿</b></p>
<ul>
<li>增强了地址簿与 iPhone、MobileMe 和其他设备及应用程序同步时的可靠性。</li>
</ul>
<hr />
<p><b>AirPort</b></p>
<ul>
<li>增强了 AirPort 连接的可靠性，包括使用基于 Intel 的 Mac 在大型无线网络中漫游时的功能改进。</li>
</ul>
<hr />
<p><b>客户端管理</b></p>
<ul>
<li>增强了在便携式个人目录上同步文件的可靠性。</li>
<li>修复了 Mac OS X 10.5.4 和 10.5.5 中的一个问题，即受管理的用户可能看不到使用普通 PPD 的打印机。</li>
<li>现在，使用基于 UUID 的 ByHost 偏好设置的客户端电脑遵循受管理的屏幕保护程序设置。</li>
</ul>
<hr />
<p><b>iChat</b></p>
<ul>
<li>解决了可能导致在“聊天”窗口中出现加密警告的问题。</li>
<li>通过 AppleScript 将您的 iChat 状态设置为“隐身”时不会再让您注销 iChat。</li>
<li>解决了从 Microsoft Office 文稿中粘贴文本时可能会插入图像而非文本的问题。</li>
</ul>
<hr />
<p><b>图形</b></p>
<ul>
<li><span>包含游戏性能的一般功能改进。<br />    </span></li>
<li>包含 iChat、Cover Flow、Aperture 和 iTunes 的图形功能改进。</li>
<li><span>包含对某些 ATI 图形卡可能出现的图形变形问题的修复。<br />    </span></li>
</ul>
<hr />
<p><b>邮件</b></p>
<ul>
<li><span>包含整体性能和可靠性修复。</span></li>
<li>增强了“连接诊断”的精确性。</li>
<li>解决了可能导致已被识别为垃圾的邮件保留在收件箱中的问题。</li>
<li>解决了可能导致邮件向附件的文件扩展名追加字符的问题。</li>
<li>解决了可能阻止邮件退出的问题。</li>
<li>增强了打印 PDF 附件时的可靠性。</li>
</ul>
<hr />
<p><b>MobileMe</b></p>
<ul>
<li>通过缩短与 MobileMe 自动同步更改所用的时间，增强了同步通讯录、日历和 Safari 书签的性能。</li>
</ul>
<hr />
<p><b>联网</b></p>
<ul>
<li>增强了“Apple 文件服务”的性能，特别是使用在 AFP 服务器上受托管的个人目录时的性能。<b>重要信息</b>：如果是使用 Mac OS X 10.5.6（客户端）连接至基于 Mac OS X <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">Server</a> 10.4 的服务器，强烈建议您将服务器更新为 Mac OS X <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">Server</a> v10.4.11。</li>
<li>增强了 TCP 连接的性能和可靠性。</li>
<li>增强了 AT&amp;T 3G 卡的可靠性和性能。</li>
<li>更新了 ssh 终端命令，使之可与更多的 ssh 服务器兼容。</li>
</ul>
<hr />
<p><b>打印</b></p>
<ul>
<li>增强了 Adobe CS3 应用程序套装的打印功能。</li>
<li>增强了基于 USB 的 Brother 和 Canon 打印机的打印功能。</li>
</ul>
<hr />
<p><b>家长控制</b></p>
<ul>
<li>解决了家长控制帐户可能无法访问“iTunes 音乐商店”的问题。</li>
<li><span>		</span>包含时间限制的通用修复。</li>
<li><span>		</span>解决了阻止通过拖放从 Safari 添加允许的网站的问题。</li>
</ul>
<hr />
<p><b>Time Machine</b></p>
<ul>
<li>解决了可能导致 Time Machine 提示备份宗卷无法找到的问题。</li>
<li>使用 Time Capsule 提高了 Time Machine 的可靠性。</li>
</ul>
<hr />
<p><b>Safari</b></p>
<ul>
<li>增强了与 <a href="http://www.donevii.com/post/tag/web" class="st_tag internal_tag" rel="tag" title="Posts tagged with web">Web</a> 代理服务器的兼容性。<br />    <span><br />    </span></li>
</ul>
<hr />
<p><b>通用</b></p>
<ul>
<li>包含 Mac OS X 的安全性功能改进。有关更多信息，请参阅<a onclick='s_objectID="http://support.apple.com/kb/HT1222?viewlocale=zh_CN_1";return this.s_oc?this.s_oc(e):true' href="http://support.apple.com/kb/HT1222?viewlocale=zh_CN"><span>该网站</span></a>。</li>
<li>解决了将 Mac OS X 语言设置为德语或瑞士德语时计算器不精确的问题。</li>
<li>增强了 Chess 的性能和可靠性。</li>
<li>增强了 DVD 播放器的性能和可靠性。</li>
<li>包含<a onclick='s_objectID="http://support.apple.com/kb%20HT1475?viewlocale=zh_CN_1";return this.s_oc?this.s_oc(e):true' href="http://support.apple.com/kb%20HT1475?viewlocale=zh_CN">更多摄像机</a>的数码相机 RAW 格式支持。</li>
<li>包含 iCal 的性能改善。</li>
<li>解决了将新 iCal 事件 Automator 操作作为 applet 运行时的问题。</li>
<li>为某些便携式 Mac 添加了“触控板系统便好设置”面板。</li>
<li>增强了与智能卡（如美国国防部通用存取卡 (Common Access Card)）的兼容性。</li>
<li>更新了多个国家/地区的时区数据和“夏令时”规则。</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/686.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>google背后的分布式架构</title>
		<link>http://www.donevii.com/post/504.html</link>
		<comments>http://www.donevii.com/post/504.html#comments</comments>
		<pubDate>Tue, 23 Sep 2008 08:48:29 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[平台]]></category>
		<category><![CDATA[开发]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[芯片]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/504.html</guid>
		<description><![CDATA[　　Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用 (比如那个大堂里的地球模型)，更在于其有别常规的IT策略…… 　　加利福尼亚州山景城(Mountain View)Google公司(Google... ]]></description>
			<content:encoded><![CDATA[<p>　　Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用  (比如那个大堂里的地球模型)，更在于其有别常规的IT策略……</p>
<p>　　加利福尼亚州山景城(Mountain  View)Google公司(Google，下称Google)总部有一个43号大楼，该建筑的中央大屏幕上显示着一个与Google地球(Google  Earth)相仿的世界地图，一个转动的地球上不停地闪动着五颜六色的光点，恍如罗马宫廷的千万烛灯，每一次闪动标志着地球的这个角落一名Google用  户发起了一次新的搜索。</p>
<p>　　这同时意味着Google又一次满足了人们对未知信息的好奇与渴望。</p>
<p>　　Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用  (比如那个大堂里的地球模型)，更在于其有别常规的IT策略。从人们的常理来看，简单的硬件商品和免费软件是无法构建出一个帝国的，但是Google做到  了。在性能调整后，Google把它们变成一个无可比拟的分布式计算平台，该平台能够支持大规模的搜索和不断涌现的新兴应用。我们原本认为这些应用都是个  人消费级别的，但是Google改变了这一切。现在商业世界也在使用它们，这就令这家搜索公司显得那么与众不同。</p>
<p>　　GoogleWeb  服务背后的IT架构对无数使用搜索引擎的用户来说也许并不是非常重要，但它是Google几百位致力于把全球信息组织起来，实现“随处可达，随时可用”目  标的工程师们的最核心工作。这就需要一个在覆盖范围和野心上都与Google的商业愿景完全相符的IT蓝图作为支撑。</p>
<p>　　Google  的经理们一直对公司的IT策略话题保持沉默，他们厌恶谈及特定的厂商或者产品，当被问到他们的服务器和数据中心时，他们总是闭口不谈。但与几位  Google的IT领导一起呆了一天后，我们最终得以揭示该公司的IT是如何运作的，那可不仅仅是一个运行在无数服务器集群上的、表面看来非常简单的搜索  引擎。在其简单的外表下，蕴涵着许多内部研发软件、定制硬件、人工智能，以及对性能的执着追求和打破常规的人力管理模式。</p>
<p>　　IT理念方面，Google对同行有一条建议：尽量避免那些人人都在使用的系统和软件，以自己的方式做事会更有独特的竞争优势。</p>
<p>　　“企业文化决定了你的做事方式。”道格拉斯&#8221;美林(Douglas  Merrill)，这位Google工程副总裁和事实上的首席信息官(CIO)  指出，“到了我们这样的发展阶段，企业观念和文化非常与众不同，这也反过来鞭策我们必须要采用与众不同的方式来运行那些他人看来很常规的系统。”<br />
　　Google  最大的IT优势在于它能建造出既富于性价比(并非廉价)又能承受极高负载的高性能系统。因此IT顾问史蒂芬&#8221;阿诺德(Stephen  Arnold)指出，Google与竞争对手，如亚马逊网站(Amazon)、电子港湾公司(eBay)、微软公司(Microsoft，下称微软)和雅 虎公司  (Yahoo，下称雅虎)等公司相比，具有更大的成本优势。Google程序员的效率比其他Web公司同行们高出50%～100%，原因是Google已  经开发出了一整套专用于支持大规模并行系统编程的定制软件库。据他估算，其他竞争公司可能要花上四倍的时间才能获得同等的效果。</p>
<p>　　打造服务器</p>
<p>　　Google  究竟是怎样做到这点的呢?其中一个手段，美林认为，“是因为我们自己动手打造硬件。”Google并不制造计算机系统，但它根据自己的参数定制硬件，然后  像MTV的节目“靓车打造”(Pimp My Ride)那样自己安装和调整硬件系统。开源程序经理克里斯&#8221;迪博纳(Chris  DiBona)评论道：“我们很善于购买商业服务器，并且改造他们为我们所用，最后把性能压榨和发挥到极致，以致有时候他们热得像要融化了似的。”</p>
<p>　　这种亲手打造的方式，来源于Google从车库诞生时与生俱来的节俭风格，更与Google那超大型的系统规模息息相关，良好的习惯一直延续至  今。据说 Google在65个数据中心拥有20万～45万台服务器—这个数目会有偏差(取决于你如何定义服务器和由谁来做这项统计)。但是，不变的是持续上升的趋势。</p>
<p>　　Google不会去讨论这些资产，因为它认为保密也是一种竞争优势。事实上，Google之所以喜欢开源软件也是因为它的私密性。“如果我们购  买了软件许可或代码许可，人们只要对号入座，就可以猜出Google的IT基础架构。”迪博纳分析说，  “使用开源软件，就使我们多了一条把握自己命运的途径。”</p>
<p>　　Google喜欢规模化的服务器运行方式。当有成百上千台机器时，定制服务器的优势也会成倍增加，效果也会更趋明显。Google正在俄勒冈州  哥伦比亚河边的达勒斯市建造一个占地30亩的数据中心，在那儿它可以获得运算和降温需要的低价水力电力能源(参见边栏《Google数据中心自有一套》)。</p>
<p>　　Google以“单元”(Cell)的形式组织这些运行  Linux操作系统的服务器，迪博纳把这种形式比喻成互联网服务的“磁盘驱动器”(但别和一直谣传的Google存储服务Gdrive混淆了，“并没有  Gdrive这回事。”一位Google女发言人明确表示。)，公司的软件程序都驻扎在这些并不昂贵的电脑机箱里，由程序员决定它们的冗余工作量。这种由  很多单元组成的文件系统代替了商业存储设备;迪博纳表示Google这些单元设备更易于建造和维护，他还暗示他们能处理更大规模的数据。</p>
<p>　　Google  不会漏过对任何技术细节的关注。多年来，公司的工程师就在研究微处理器的内部工作机制，随着Google规模的持续壮大，必然会用到特别定制和调节过的芯  片。知名工程师路易斯&#8221;巴罗索(Luiz  Barroso)去年在一篇发表在工业杂志上的论文中证实，近年来Google的主要负荷都由单核设计的系统承担着。但许多服务器端的应用，如  Google搜索索引服务，所需的并行计算在单核芯片的指令级别上执行得并不好。</p>
<p>　　曾在数据设备公司(Digital  Equipment)和康柏公司(Compaq)当过芯片设计师的巴罗索认为，随着AMD公司、英特尔公司(Intel)、太阳计算机系统公司(Sun)开始制造多核芯片，必将会出现越来越多芯片级别的并行计算。</p>
<p>　　Google  也曾考虑过自己制造计算机芯片，但从业界潮流来看，这个冒险的举动似乎不是很必要。“微处理器的设计非常复杂而且成本昂贵，”运营高级副总裁乌尔斯&#8221;霍尔 茨勒(Urs  Holzle)表示。Google宁愿与芯片制造商合作，让他们去理解自己的应用并设计适合的芯片。这是一种客户建议式的设计，其关注点在于总体吞吐量、  效能，以及耗电比，而不是看单线程的峰值性能。霍尔茨勒表示，“这也是最近多核CPU的设计潮流与未来方向。”<br />
裁缝般地定制软件</p>
<p>　　为了能尽量压榨硬件性能，Google开发了相当数量的定制软件。创新产品主要包括用于简化处理和创建大规模数据集的编程模型  MapReduce;用于存储和管理大规模数据的系统BigTable;分析分布式运算环境中大规模数据集的解释编程语言Sawzall;用于数据密集型  应用的分布式文件系统的 “Google文件系统”(Google File  System);还有为处理分布式系统队列分组和任务调度的“Google工作队列”(Google  Workqueue)。</p>
<p>　　正是从Sawzall这些工具里体现出Google对计算效率的执著关注。并不是每家公司都能从底层去解决效率问题，但是对Google来说，  为常规关系型数据库无法容纳的大规模数据集专门设计一种编程语言是完全合理的。即使其他编程工具可以解决问题，Google的工程师们仍然会为了追求效率  而另外开发一套定制方案。Google工程师认为，Sawzall能与C++中的MapReduce相媲美，而且它更容易编写一些。</p>
<p>　　Google  对效率的关注使它不可能对标准Linux内核感到满意;Google会根据自己的需要运行修改过的内核版本。通过调整Linux的底层性能，Google  工程师们在提高了整体系统可靠性的基础上，还一并解决了数据损坏和数据瓶颈等一系列棘手问题。对内核的修改也使Google的计算机集群系统因为通信效率  的提高而运行得更快。</p>
<p>　　当然，Google偶尔也会出现系统故障，情况一旦发生，无数的用户就会受到影响了。三年前一次持续30分钟的系统故障使20%的搜索流量受到影响。</p>
<p>　　Google  开发了自己的网站服务器却没有使用开源的Apache服务器，尽管它在网站服务器的市场占有率超过60%。迪博纳认为，Google的网站服务器可以运行  在更多数量的主机上，对Google站点上内容庞大又彼此互相依赖的应用程序来说，这种服务器的负载均衡能力远比Apache的能力更高。同时，在用标准  公共网关接口(CGI)访问数据库动态网页方面，Google服务器的编程难度要比  Apache更高，但是最终运行速度却更快。“如果我们能够压榨出10%～20%的性能，我们就可以节省出更多系统资源、电量和人力了。”迪博纳在总结中指出。</p>
<p>　　Google还设计了自己的客户关系管理(CRM)系统用于支持自己基于竞价和点击的互联网广告收费业务。但对是否需要设计自己的工具，Google的态度也不是一成不变的。比如在财会软件上，它就使用了甲骨文公司(Oracle)的Financials软件。</p>
<p>　　美林拿着一只叉子举例说明现成的产品也可以带来价值。但在有些场合现成的软件产品就不一定适用了。“我们的文化在各个层面对我们的运作都有深远影响，”他表示，“所以我们不想让购买所得的工具改变我们的工作方式和文化层面。”<br />
Google&#8217;s  BigTable 原理 （翻译）</p>
<p>    题记：google 的成功除了一个个出色的创意外，还因为有 Jeff Dean  这样的软件架构天才。<br />
                                                  &#8212;&#8212; 编者</p>
<p>官方的  Google Reader <a href="http://www.donevii.com/post/tag/blog" class="st_tag internal_tag" rel="tag" title="Posts tagged with blog">blog</a> 中有对BigTable 的解释。这是Google 内部开发的一个用来处理大数据量的系统。这种系统适合处理半结构化的数据比如  RSS 数据源。 以下发言  是 Andrew Hitchcock  在 2005 年10月18号 基于： Google 的工程师 Jeff Dean  在华盛顿大学的一次谈话 (Creative Commons License).</p>
<p>首先，BigTable 从 2004  年初就开始研发了，到现在为止已经用了将近8个月。（2005年2月）目前大概有100个左右的服务使用BigTable，比如： Print,Search  History,Maps和 Orkut。根据Google的一贯做法，内部开发的BigTable是为跑在廉价的PC机上设计的。BigTable  让Google在提供新服务时的运行成本降低，最大限度地利用了计算能力。</p>
<p>BigTable 是建立在 GFS ，Scheduler ，Lock Service 和  MapReduce 之上的。</p>
<p>每个Table都是一个多维的稀疏图 sparse map。Table 由行和列组成，并且每个存储单元 cell  都有一个时间戳。在不同的时间对同一个存储单元cell有多份拷贝，这样就可以记录数据的变动情况。在他的例子中，行是URLs  ，列可以定义一个名字，比如：contents。Contents  字段就可以存储文件的数据。或者列名是：”language”，可以存储一个“EN”的语言代码字符串。</p>
<p>为了管理巨大的Table，把Table根据行分割，这些分割后的数据统称为：Tablets。每  个Tablets大概有 100-200 MB，每个机器存储100个左右的  Tablets。底层的架构是：GFS。由于GFS是一种分布式的文件系统，采用Tablets的机制后，可以获得很好的负载均衡。比如：可以把经常响应  的表移动到其他空闲机器上，然后快速重建。</p>
<p>Tablets在系统中的存储方式是不可修改的 immutable  的SSTables，一台机器一个日志文件。当系统的内存满后，系统会压缩一些Tablets。由于Jeff在论述这点的时候说的很快，所以我没有时间把听到的都记录下来，因此下面是一个大概的说明：</p>
<p>压缩分为：主要和次要的两部分。次要的压缩仅仅包括几个Tablets，而主要的压缩时关于整个系统的压缩。主压缩有回收硬盘空间的功能。Tablets的位置实际上是存储在几个特殊的BigTable的存储单元cell中。看起来这是一个三层的系统。<br />
客户端有一个指向METAO的Tablets的指针。如果METAO的Tablets被频繁使用，那个这台机器就会放弃其他的tablets专门支持  METAO这个Tablets。METAO tablets  保持着所有的META1的tablets的记录。这些tablets中包含着查找tablets的实际位置。（老实说翻译到这里，我也不太明白。）在这个系统中不存在大的瓶颈，因为被频繁调用的数据已经被提前获得并进行了缓存。</p>
<p>     现在我们返回到对列的说明：列是类似下面的形式：  family:optional_qualifier。在他的例子中，行：www.search-analysis.com   也许有列：”contents:其中包含html页面的代码。 “ anchor:cnn.com/news” 中包含着  相对应的url，”anchor:www.search-analysis.com/” 包含着链接的文字部分。列中包含着类型信息。<br />
     (翻译到这里我要插一句，以前我看过一个关于万能数据库的文章，当时很激动，就联系了作者，现在回想起来，或许google的 bigtable  才是更好的方案，切不说分布式的特性，就是这种建华的表结构就很有用处。)</p>
<p>     注意这里说的是列信息，而不是列类型。列的信息是如下信息，一般是：属性/规则。 比如：保存n份数据的拷贝或者保存数据n天长等等。当 tablets  重新建立的时候，就运用上面的规则，剔出不符合条件的记录。由于设计上的原因，列本身的创建是很容易的，但是跟列相关的功能确实非常复杂的，比如上文提到 的  类型和规则信息等。为了优化读取速度，列的功能被分割然后以组的方式存储在所建索引的机器上。这些被分割后的组作用于 列 ,然后被分割成不同的  SSTables。这种方式可以提高系统的性能，因为小的，频繁读取的列可以被单独存储，和那些大的不经常访问的列隔离开来。</p>
<p>在一台机器上的所有的  tablets  共享一个log，在一个包含1亿的tablets的集群中，这将会导致非常多的文件被打开和写操作。新的log块经常被创建，一般是64M大小，这个GFS的块大小相等。当一个机器down掉后，控制机器就会重新发布他的log块到其他机器上继续进行处理。这台机器重建tablets然后询问控制机器处理结构的存储位置，然后直接对重建后的数据进行处理。这个系统中有很多冗余数据，因此在系统中大量使用了压缩技术。</p>
<p>     Dean 对压缩的部分说的很快，我没有完全记下来，所以我还是说个大概吧：压缩前先寻找相似的 \行，列，和时间数据。</p>
<p>    他们使用不同版本的：  BMDiff 和 Zippy <a href="http://www.donevii.com/post/tag/%e6%8a%80%e6%9c%af" class="st_tag internal_tag" rel="tag" title="Posts tagged with 技术">技术</a>。</p>
<p>   BMDiff 提供给他们非常快的写速度： 100MB/s – 1000MB/s 。Zippy 是和 LZW  类似的。Zippy 并不像 LZW 或者 gzip 那样压缩比高，但是他处理速度非常快。</p>
<p>    Dean 还给了一个关于压缩 <a href="http://www.donevii.com/post/tag/web" class="st_tag internal_tag" rel="tag" title="Posts tagged with web">web</a>  蜘蛛数据的例子。这个例子的蜘蛛 包含 2.1B 的页面，行按照以下的方式命名：“com.cnn.www/index.html:http”.在未压缩前的web  page 页面大小是：45.1 TB ，压缩后的大小是：4.2 TB ， 只是原来的 9.2%。Links 数据压缩到原来的 13.9% ,  链接文本数据压缩到原来的 12.7%。<br />
<span id="more-504"></span><br />
Google 还有很多没有添加但是已经考虑的功能。<br />
    1.   数据操作表达式，这样可以把脚本发送到客户端来提供修改数据的功能。<br />
    2. 多行数据的事物支持。<br />
    3.   提高大数据存储单元的效率。<br />
    4. BigTable 作为服务运行。<br />
    好像：每个服务比如： maps 和 search history  历史搜索记录都有他们自己的集群运行 BigTable。<br />
    他们还考虑运行一个全局的 BigTable  系统，但这需要比较公平的分割资源和计算时间。</p>
<p>大表(Bigtable):结构化数据的分布存储系统</p>
<p>http://labs.google.com/papers/bigtable-osdi06.pdf</p>
<p>｛中是译者评论,程序除外｝<br />
{本文的翻译可能有不准确的地方,详细资料请参考原文.}</p>
<p>摘要<br />
bigtable是设计来分布存储大规模结构化数据的，从设计上它可以扩展到上２^50字节，分布存储在几千个普通服务器上．google的很多项目使用  bt来存储数据，包括网页查询，google  earth和google金融．这些应用程序对bt的要求各不相同：数据大小（从URL到网页到卫星图象）不同，反应速度不同（从后端的大批处理到实时数  据服务）．对于不同的要求，bt都成功的提供了灵活高效的服务．在本文中，我们将描述bt的数据模型．这个数据模型让用户动态的控制数据的分布和结构．我  们还将描述BT的设计和实现．<br />
１．介绍<br />
在过去两年半里，我们设计，实现并部署了BT．BT是用来分布存储和管理结构化数据的．BT的设计使它能够管理2^50  bytes(petabytes)数据，并可以部署到上千台机器上．BT完成了以下目标：应用广泛，可扩展，高性能和高可用性（high availability）.  包括google analytics, google finance, orkut, personalized search, writely和google  earth在内的60多个项目都使用BT.这些应用对BT的要求各不相同，有的需要高吞吐量的批处理，有的需要快速反应给用户数据．它们使用的BT集群也各不相同，有的只有几台机器，有的有上千台，能够存储2^40字节(terabytes)数据．<br />
BT在很多地方和数据库很类似：它使用了很多数据库的实现策略．并行数据库[14]和内存数据库[13]有可扩展性和高性能，但是BT的界面不同．BT不支持完全的关系数据模型；而是为客户提供了简单的数据模型，让客户来动态控制数据的分布和格式{就是只存储字串，格式由客户来解释}，并允许客户推断底层存储数据的局部性｛以提高访问速度｝．数据下标是行和列的名字，数据本身可以是任何字串．BT的数据是字串，没有解释｛类型等｝．客户会在把各种结构或者半结构化的数据串行化｛比如说日期串｝到数据中．通过仔细选择数据表示，客户可以控制数据的局部化．最后，可以使用BT模式来控制数据是放在内存里还是在硬盘上．｛就是说用模式，你可以把数据放在离应用最近的地方．毕竟程序在一个时间只用到一块数据．在体系结构里，就是：locality,  locality,  locality｝<br />
第二节描述数据模型细节．第三节关于客户API概述．第四节简介BT依赖的google框架．第五节描述BT的实现关键部分．第6节叙述提高BT性  能的一些调整．第7节提供BT性能的数据．在第8节，我们提供BT的几个使用例子，第9节是经验教训．在第10节，我们列出相关研究．最后是我们的结论．<br />
２．数据模型<br />
BT是一个稀疏的，长期存储的｛存在硬盘上｝，多维度的，排序的映射表．这张表的索引是行关键字，列关键字和时间戳．每个值是一个不解释的字符数组．｛数据都是字符串，没类型，客户要解释就自力更生吧｝．<br />
(row:string,  column:string,time:int64)-&gt;string  {能编程序的都能读懂，不翻译了}<br />
我们仔细查看过好些类似bigtable的系统之后定下了这个数据模型。举一个具体例子（它促使我们做出某些设计决定），  比如我们想要存储大量网页及相关信息，以用于很多不同的项目；我们姑且叫它Webtable。在Webtable里，我们将用URL作为行关键字，用网页  的某些属性作为列名，把网页内容存在contents:列中并用获取该网页的时间戳作为标识，如图一所示。<br />
图一：一个存储Web网页的范例列表片断。行名是一个反向URL｛即com.cnn.www｝。contents列族｛原文用  family，译为族，详见列族｝ 存放网页内容，anchor列族存放引用该网页的锚链接文本。CNN的主页被Sports  Illustrater｛即所谓SI，CNN的王牌体育节目｝和MY-look的主页引用，因此该行包含了名叫“anchor:cnnsi.com”和  “anchhor:my.look.ca”的列。每个锚链接只有一个版本｛由时间戳标识，如t9，t8｝；而contents列则有三个版本，分别由时间  戳t3，t5，和t6标识。<br />
行<br />
表中的行关键字可以是任意字符串（目前支持最多64KB，多数情况下10－100字节足够了）。在一个行关键字下的每一个读写操作都是原子操作（不管读写这一行里多少个不同列），这是一个设计决定，这样在对同一行进行并发操作时，用户对于系统行为更容易理解和掌控。<br />
Bigtable通过行关键字的字典序来维护数据。一张表可以动态划分成多个连续行。连续行在这里叫做“子表”｛tablet｝，是数据分布和负载  均衡的单位。这样一来，读较少的连续行就比较有效率，通常只需要较少机器之间的通信即可。用户可以利用这个属性来选择行关键字，从而达到较好数据访问地域  性｛locality｝。举例来说，在Webtable里，通过反转URL中主机名的方式，可以把同一个域名下的网页组织成连续行。具体来说，可以把  maps.google.com/index.html中的数据存放在关键字com.google.maps/index.html下。按照相同或属性相  近的域名来存放网页可以让基于主机和基于域名的分析更加有效。<br />
列族<br />
一组列关键字组成了“列族”，这是访问控制的基本单位。同一列族下存放的所有数据通常都是同一类型（同一列族下的数据可压缩在一起）。列族必须先创  建，然后在能在其中的列关键字下存放数据；列族创建后，族中任何一个列关键字均可使用。我们希望，一张表中的不同列族不能太多（最多几百个），并且列族在  运作中绝少改变。作为对比，一张表可以有无限列。<br />
列关键字用如下语法命名：列族：限定词。  列族名必须是看得懂｛printable｝的字串，而限定词可以是任意字符串。比如，Webtable可以有个列族叫language，存放撰写网页的语  言。我们在language列族中只用一个列关键字，用来存放每个网页的语言标识符。该表的另一个有用的列族是anchor；给列族的每一个列关键字代表  一个锚链接，如图一所示。而这里的限定词则是引用该网页的站点名；表中一个表项存放的是链接文本。<br />
访问控制，磁盘使用统计，内存使用统计，均可在列族这个层面进行。在Webtable举例中，我们可以用这些控制来管理不同应用：有的应用添加新的基本数据，有的读取基本数据并创建引申的列族，有的则只能浏览数据（甚至可能因为隐私权原因不能浏览所有数据）。<br />
时间戳<br />
Bigtable表中每一个表项都可以包含同一数据的多个版本，由时间戳来索引。Bigtable的时间戳是64位整型。可以由Bigtable来  赋值，表示准确到毫秒的“实时”；或者由用户应用程序来赋值。需要避免冲突的应用程序必须自己产生具有唯一性的时间戳。不同版本的表项内容按时间戳倒序排  列，即最新的排在前面。<br />
为了简化对于不同数据版本的数据的管理，我们对每一个列族支持两个设定，以便于Bigtable对表项的版本自动进行垃圾清除。用户可以指明只保留表项的最后n个版本，或者只保留足够新的版本（比如，只保留最近7天的内容）。<br />
在Webtable举例中，我们在contents:列中存放确切爬行一个网页的时间戳。如上所述的垃圾清除机制可以让我们只保留每个网页的最近三个版本。<br />
3.API<br />
BT的API提供了建立和删除表和列族的函数．还提供了函数来修改集群，表和列族的元数据，比如说访问权限．<br />
//  Open the table<br />
Table *T = OpenOrDie(”/bigtable/web/webtable”);<br />
// Write a  new anchor and delete an old anchor<br />
RowMutation r1(T,  “com.cnn.www”);<br />
r1.Set(”anchor:www.c-span.org”,  “CNN”);<br />
r1.Delete(”anchor:www.abc.com”);<br />
Operation op;<br />
Apply(&amp;op,  &amp;r1);<br />
图 2:  写入Bigtable.<br />
在BT中，客户应用可以写或者删除值，从每个行中找值，或者遍历一个表中的数据子集．图2的c++代码是使用RowMutation抽象表示来进行一系列的更新（为保证代码精简，没有包括无关的细节）．调用Apply函数，就对Ｗebtable进行了一个原子修改：它为http://www.cnn.com/增加了一个锚点，并删除了另外一个锚点．<br />
Scanner  scanner(T);<br />
ScanStream *stream;<br />
stream =  scanner.FetchColumnFamily(”anchor”);<br />
stream-&gt;SetReturnAllVersions();<br />
scanner.Lookup(”com.cnn.www”);<br />
for  (; !stream-&gt;Done(); stream-&gt;Next()) {<br />
printf(”%s %s %lld  %s\n”,<br />
scanner.RowName(),<br />
stream-&gt;ColumnName(),<br />
stream-&gt;MicroTimestamp(),<br />
stream-&gt;Value());<br />
}<br />
图3:  从Bigtable读数据.<br />
图3的C++代码是使用Scanner抽象来遍历一个行内的所有锚点．客户可以遍历多个列族．有很多方法可以限制一次扫描中产生的行，列和时间戳．  例如，我们可以限制上面的扫描，让它只找到那些匹配正则表达式*.cnn.com的锚点，或者那些时间戳在当前时间前10天的锚点．<br />
BT还支持其他一些更复杂的处理数据的功能．首先，BT支持单行处理．这个功能可以用来对存储在一个行关键字下的数据进行原子的读-修改-写操作．  BT目前不支持跨行关键字的处理，但是它有一个界面，可以用来让客户进行批量的跨行关键字处理操作．其次，BT允许把每个表项用做整数记数器．最后，BT  支持在服务器的地址空间内执行客户端提供的脚本程序．脚本程序的语言是google开发的Sawzall[28]数据处理语言．目前，我们基于的  Sawzall的API还不允许客户脚本程序向BT内写数据，但是它允许多种形式的数据变换，基于任何表达式的过滤和通过多种操作符的摘要．<br />
BT可以和MapReduce[12]一起使用．MapReduce是google开发的大规模并行计算框架．我们为编写了一套外层程序，使BT可以作为MapReduce处理的数据源头和输出结果．<br />
4.建立BT的基本单元<br />
BT是建立在其他数个google框架单元上的．BT使用google分布式文件系统(GFS)[17]来存储日志和数据文件{yeah,  right, what else can it use,  FAT32?}．一个BT集群通常在一个共享的机器池中工作，池中的机器还运行其他的分布式应用{虽然机器便宜的跟白菜似的，可是一样要运行多个程序，命苦的象小白菜}，BT和其他程序共享机器｛BT的瓶颈是ＩＯ/内存，可以和CPU要求高的程序并存｝．BT依赖集群管理系统来安排工作，在共享的机器上管理资源，处理失效机器并监视机器状态｛典型的server  farm结构，BT是上面的应用之一｝．<br />
BT内部存储数据的格式是google  SSTable格式．一个SSTable提供一个从关键字到值的映射，关键字和值都可以是任意字符串．映射是排序的，存储的｛不会因为掉电而丢失｝，不可改写的．可以进行以下操作：查询和一个关键字相关的值；或者根据给出的关键字范围遍历所有的关键字和值．在内部，每个SSTable包含一列数据块（通常每个块的大小是64KB,但是大小是可以配置的｛索引大小是16  bits，应该是比较好的一个数｝）．块索引（存储在SSTable的最后）用来定位数据块；当打开SSTable的时候，索引被读入内存｛性能｝．每次查找都可以用一个硬盘搜索完成｛根据索引算出数据在哪个道上，一个块应该不会跨两个道，没必要省那么点空间｝：首先在内存中的索引里进行二分查找找到数据块的位置，然后再从硬盘读去数据块．最佳情况是：整个SSTable可以被放在内存里，这样一来就不必访问硬盘了．｛想的美，前面是谁口口声声说要跟别人共享机器来着？你把内存占满了别人上哪睡去？｝<br />
BT还依赖一个高度可用的，存储的分布式数据锁服务Chubby[8]｛看你怎么把这个high  performance给说圆喽｝．一个Chubby服务由5个活的备份｛机器｝构成，其中一个被这些备份选成主备份，并且处理请求．这个服务只有在大多数备份都活着并且互相通信的时候才是活的｛绕口令？去看原文吧，是在有出错的前提下的冗余算法｝．当有机器失效的时候，Chubby使用Paxos算法[9,23]来保证备份的一致性｛这个问题还是比较复杂的，建议去看引文了解一下问题本身｝．Chubby提供了一个名字空间，里面包括了目录和小文件｛万变不离其宗｝．每个目录或者文件可以当成一个锁来用，读写文件操作都是原子化的．Chubby客户端的程序库提供了对Chubby文件的一致性缓存｛究竟是提高性能还是降低性能？如果访问是分布的，就是提高性能｝．每个Chubby客户维护一个和Chubby服务的会话．如果一个客户不能在一定时间内更新它的会话，这个会话就过期失效了｛还是针对大server  farm里机器失效的频率设计的｝．当一个会话失效时，其拥有的锁和打开的文件句柄都失效｛根本设计原则：失效时回到安全状态｝．Chubby客户可以在文件和目录上登记回调函数，以获得改变或者会话过期的通知．｛翻到这里，有没有人闻到java的味道了？｝<br />
BT使用Chubby来做以下几个任务：保证任何时间最多只有一个活跃的主备份；来存储BT数据的启动位置（参考5.1节）；发现小表  （tablet）服务器，并完成tablet服务器消亡的善后（5.2节）；存储BT数据的模式信息（每张表的列信息）；以及存储访问权限列表．如果有相当长的时间Chubby不能访问，BT就也不能访问了｛任何系统都有其弱点｝．最近我们在使用11个Chubby服务实例的14个BT集群中度量了这个效果，由于Chubby不能访问而导致BT中部分数据不能访问的平均百分比是0.0047%,这里Chubby不能访问的原因是Chubby本身失效或者网络问题．单个集群里，受影响最大的百分比是0.0326%｛基于文件系统的Chubby还是很稳定的｝.<br />
GFS是一个可扩展的分布式文件系统，用于大型的、分布式的、对大量数据进行访问的应用。它运行于廉价的普通硬件上，但可以提供容错功能。它可以给大量的用户提供总体性能较高的服务。<br />
出处：http://labs.google.com/papers/gfs.html<br />
1、设计概览<br />
（1）设计想定<br />
GFS与过去的分布式文件系统有很多相同的目标，但GFS的设计受到了当前及预期的应用方面的工作量及技术环境的驱动，这反映了它与早期的文件系统明显不同的设想。这就需要对传统的选择进行重新检验并进行完全不同的设计观点的探索。<br />
GFS与以往的文件系统的不同的观点如下：<br />
1、部件错误不再被当作异常，而是将其作为常见的情况加以处理。因为文件系统由成百上千个用于存储的机器构成，而这  些机器是由廉价的普通部件组成并被大量的客户机访问。部件的数量和质量使得一些机器随时都有可能无法工作并且有一部分还可能无法恢复。所以实时地监控、错  误检测、容错、自动恢复对系统来说必不可少。<br />
2、按照传统的标准，文件都非常大。长度达几个GB的文件是很平常的。每个文件通常包含很多应用对象。当经常要处理  快速增长的、包含数以万计的对象、长度达TB的数据集时，我们很难管理成千上万的KB规模的文件块，即使底层文件系统提供支持。因此，设计中操作的参数、  块的大小必须要重新考虑。对大型的文件的管理一定要能做到高效，对小型的文件也必须支持，但不必优化。<br />
3、大部分文件的更新是通过添加  新数据完成的，而不是改变已存在的数据。在一个文件中随机的操作在实践中几乎不存在。一旦写完，文件就只可读，很多数据都有这些特性。一些数据可能组成一  个大仓库以供数据分析程序扫描。有些是运行中的程序连续产生的数据流。有些是档案性质的数据，有些是在某个机器上产生、在另外一个机器上处理的中间数据。  由于这些对大型文件的访问方式，添加操作成为性能优化和原子性保证的焦点。而在客户机中缓存数据块则失去了吸引力。<br />
4、工作量主要由两种读操作构成：对大量数据的流方式的读操作和对少量数据的随机方式的读操作。在前一种读操作中，  可能要读几百KB，通常达  1MB和更多。来自同一个客户的连续操作通常会读文件的一个连续的区域。随机的读操作通常在一个随机的偏移处读几个KB。性能敏感的应用程序通常将对少量  数据的读操作进行分类并进行批处理以使得读操作稳定地向前推进，而不要让它来来回回的读。<br />
5、工作量还包含许多对大量数据进行的、连续的、向文件添加数据的写操作。所写的数据的规模和读相似。一旦写完，文件很少改动。在随机位置对少量数据的写操作也支持，但不必非常高效。<br />
6、系统必须高效地实现定义完好的大量客户同时向同一个文件的添加操作的语义。<br />
（2）系统接口<br />
GFS提供了一个相似地文件系统界面，虽然它没有向POSIX那样实现标准的API。文件在目录中按层次组织起来并由路径名标识。<br />
（3）体系结构：<br />
一个GFS集群由一个master和大量的chunkserver构成，并被许多客户（Client）访问。如图1  所示。Master和  chunkserver通常是运行用户层服务进程的Linux机器。只要资源和可靠性允许，chunkserver和client可以运行在同一个机器  上。<br />
文件被分成固定大小的块。每个块由一个不变的、全局唯一的64位的chunk－handle标识，chunk－ handle是在块创建时由  master分配的。ChunkServer将块当作Linux文件存储在本地磁盘并可以读和写由chunk－handle和位区间指定的数据。出于可靠  性考虑，每一个块被复制到多个chunkserver上。默认情况下，保存3个副本，但这可以由用户指定。<br />
Master维护文件系统所以的元数据（metadata），包括名字空间、访问控制信息、从文件到块的映射以及块  的当前位置。它也控制系统范围的活动，如块租约（lease）管理，孤儿块的垃圾收集，chunkserver间的块迁移。Master定期通过  HeartBeat消息与每一个  chunkserver通信，给chunkserver传递指令并收集它的状态。<br />
与每个应用相联的GFS客户代码实现了文件系统的API并与master和chunkserver通信以代表应用程序读和写数据。客户与master的交换只限于对元数据（metadata）的操作，所有数据方面的通信都直接和chunkserver联系。<br />
客户和chunkserver都不缓存文件数据。因为用户缓存的益处微乎其微，这是由于数据太多或工作集太大而无法  缓存。不缓存数据简化了客户程序和整个系统，因为不必考虑缓存的一致性问题。但用户缓存元数据（metadata）。Chunkserver也不必缓存文  件，因为块时作为本地文件存储的。<br />
（4）单master。<br />
只有一个master也极大的简化了设计并使得master可以根据全局情况作出先进的块放置和复制决定。但是我们  必须要将master对读和写的参与减至最少，这样它才不会成为系统的瓶颈。Client从来不会从master读和写文件数据。Client只是询问  master它应该和哪个  chunkserver联系。Client在一段限定的时间内将这些信息缓存，在后续的操作中Client直接和chunkserver交互。<br />
以图1解释一下一个简单的读操作的交互。<br />
1、client使用固定的块大小将应用程序指定的文件名和字节偏移转换成文件的一个块索引（chunk  index）。<br />
2、给master发送一个包含文件名和块索引的请求。<br />
3、master回应对应的chunk  handle和副本的位置（多个副本）。<br />
4、client以文件名和块索引为键缓存这些信息。（handle和副本的位置）。<br />
5、Client  向其中一个副本发送一个请求，很可能是最近的一个副本。请求指定了chunk handle（chunkserver以chunk  handle标识chunk）和块内的一个字节区间。<br />
6、除非缓存的信息不再有效（<a href="http://www.donevii.com/post/tag/cache" class="st_tag internal_tag" rel="tag" title="Posts tagged with cache">cache</a> for a limited  time）或文件被重新打开，否则以后对同一个块的读操作不再需要client和master间的交互。<br />
通常Client可以在一个请求中询问多个chunk的地址，而master也可以很快回应这些请求。<br />
（5）块规模：<br />
块规模是设计中的一个关键参数。我们选择的是64MB，这比一般的文件系统的块规模要大的多。每个块的副本作为一个普通的Linux文件存储，在需要的时候可以扩展。<br />
块规模较大的好处有：<br />
1、减少client和master之间的交互。因为读写同一个块只是要在开始时向master请求块位置信息。对于读写大型文件这种减少尤为重要。即使对于访问少量数据的随机读操作也可以很方便的为一个规模达几个TB的工作集缓缓存块位置信息。<br />
2、Client在一个给定的块上很可能执行多个操作，和一个chunkserver保持较长时间的TCP连接可以减少网络负载。<br />
3、这减少了master上保存的元数据（metadata）的规模，从而使得可以将metadata放在内存中。这又会带来一些别的好处。<br />
不利的一面：<br />
一个小文件可能只包含一个块，如果很多Client访问改文件的话，存储这些块的chunkserver将成为访问的热点。但在实际应用中，应用程序通常顺序地读包含多个块的文件，所以这不是一个主要问题。<br />
（6）元数据（metadata）：<br />
master存储了三中类型的metadata：文件的名字空间和块的名字空间，从文件到块的映射，块的副本的位  置。所有的metadata都放在内存中。前两种类型的metadata通过向操作日志登记修改而保持不变，操作日志存储在master的本地磁盘并在几  个远程机器上留有副本。使用日志使得我们可以很简单地、可靠地更新master的状态，即使在master崩溃的情况下也不会有不一致的问题。相反，  mater在每次启动以及当有  chuankserver加入的时候询问每个chunkserver的所拥有的块的情况。<br />
A、内存数据结构：<br />
因为metadata存储在内存中，所以master的操作很快。进一步，master可以轻易而且高效地定期在后台扫描它的整个状态。这种定期地扫描被用于实现块垃圾收集、chunkserver出现故障时的副本复制、为平衡负载和磁盘空间而进行的块迁移。<br />
这种方法的一个潜在的问题就是块的数量也即整个系统的容量是否受限与master的内存。实际上，这并不是一个严重  的问题。Master为每个  64MB的块维护的metadata不足64个字节。除了最后一块，文件所有的块都是满的。类似的，每个文件的名字空间数据也不足64个字节，因为文件名  是以一种事先确定的压缩方式存储的.如果要支持更大的文件系统，那么增加一些内存的方法对于我们将元数据（metadata）保存在内存种所获得的简单  性、可靠性、高性能和灵活性来说，这只是一个很小的代价。<br />
B、块位置：<br />
master并不为chunkserver所拥有的块的副本的保存一个不变的记录。它在启动时通过简单的查询来获得这些信息。Master可以保持这些信息的更新，因为它控制所有块的放置并通过HeartBeat消息来监控chunkserver的状态。<br />
这样做的好处：因为chunkserver可能加入或离开集群、改变路径名、崩溃、重启等，一个集群重有成百个server，这些事件经常发生，这种方法就排除了master与chunkserver之间的同步问题。<br />
另一个原因是：只有chunkserver才能确定它自己到底有哪些块，由于错误，chunkserver中的一些块可能会很自然的消失，这样在master中就没有必要为此保存一个不变的记录。<br />
C、操作日志：<br />
操作日志包含了对metadata所作的修改的历史记录。它作为逻辑时间线定义了并发操作的执行顺序。文件、块以及它们的版本号都由它们被创建时的逻辑时间而唯一地、永久地被标识。<br />
操作日志是如此的重要，我们必须要将它可靠地保存起来，并且只有在metadata的改变固定下来之后才将变化呈现给用户。所以我们将操作日志复制到数个远程的机器上，并且只有在将相应的日志记录写到本地和远程的磁盘上之后才回答用户的请求。<br />
Master可以用操作日志来恢复它的文件系统的状态。为了将启动时间减至最小，日志就必须要比较小。每当日志的长度增长到超过一定的规模后，master就要检查它的状态，它可以从本地磁盘装入最近的检查点来恢复状态。<br />
创建一个检查点比较费时，master的内部状态是以一种在创建一个检查点时并不耽误即将到来的修改操作的方式来组  织的。Master切换到一个新的日子文件并在一个单独的线程中创建检查点。这个新的检查点记录了切换前所有的修改。在一个有数十万文件的集群中用一分钟  左右就能完成。创建完后，将它写入本地和远程的磁盘。<br />
（7）数据完整性<br />
名字空间的修改必须是原子性的，它们只能有master处理：名字空间锁保证了操作的原子性和正确性，而master的操作日志在全局范围内定义了这些操作的顺序。<br />
文  件区间的状态在修改之后依赖于修改的类型，不论操作成功还是失败，也不论是不是并发操作。如果不论从哪个副本上读，所有的客户都看到同样的数据，那么文件  的这个区域就是一致的。如果文件的区域是一致的并且用户可以看到修改操作所写的数据，那么它就是已定义的。如果修改是在没有并发写操作的影响下完成的，那  么受影响的区域是已定义的，所有的client都能看到写的内容。成功的并发写操作是未定义但却是一致的。失败的修改将使区间处于不一致的状态。<br />
Write操作在应用程序指定的偏移处写入数据，而record  append操作使得数据（记录）即使在有并发修改操作的情况下也至少原子性的被加到GFS指定的偏移处，偏移地址被返回给用户。<br />
在一系列成功的修改操作后，最后的修改操作保证文件区域是已定义的。GFS通过对所有的副本执行同样顺序的修改操作并且使用块版本号检测过时的副本（由于chunkserver退出而导致丢失修改）来做到这一点。<br />
因为用户缓存了会位置信息，所以在更新缓存之前有可能从一个过时的副本中读取数据。但这有缓存的截止时间和文件的重新打开而受到限制。<br />
在修改操作成功后，部件故障仍可以是数据受到破坏。GFS通过master和chunkserver间定期的handshake，借助校验和来检测对数据的破坏。一旦检测到，就从一个有效的副本尽快重新存储。只有在GFS检测前，所有的副本都失效，这个块才会丢失。<br />
2、系统交互<br />
（1）租约（lease）和修改顺序<br />
（2）数据流<br />
我们的目标是充分利用每个机器的网络带宽，避免网络瓶颈和延迟<br />
为了有效的利用网络，我们将数据流和控制流分离。数据是以流水线的方式在选定的chunkerserver链上线性的传递的。每个机器的整个对外带宽都被用作传递数据。为避免瓶颈，每个机器在收到数据后，将它收到数据尽快传递给离它最近的机器。<br />
（3）原子性的record  Append：<br />
GFS提供了一个原子性的添加操作：record  append。在传统的写操作中，client指定被写数据的偏移位置，向同一个区间的并发的写操作是不连续的：区间有可能包含来自多个client的数  据碎片。在record append中，  client只是指定数据。GFS在其选定的偏移出将数据至少原子性的加入文件一次，并将偏移返回给client。<br />
在分布式的应用中，不同机器上的许多client可能会同时向一个文件执行添加操作，添加操作被频繁使用。如果用传  统的write操作，可能需要额外的、复杂的、开销较大的同步，例如通过分布式锁管理。在我们的工作量中，这些文件通常以多个生产者单个消费者队列的方式  或包含从多个不同 client的综合结果。<br />
Record  append和前面讲的write操作的控制流差不多，只是在primary上多了一些逻辑判断。首先，client将数据发送到文件最后一块的所有副本  上。然后向primary发送请求。Primary检查添加操作是否会导致该块超过最大的规模（64M）。如果这样，它将该块扩充到最大规模，并告诉其它  副本做同样的事，同时通知client该操作需要在下一个块上重新尝试。如果记录满足最大规模的要求，primary就会将数据添加到它的副本上，并告诉  其它的副本在在同样的偏移处写数据，最后primary向client报告写操作成功。如果在任何一个副本上record  append操作失败，client将重新尝试该操作。这时候，同一个块的副本可能包含不同的数据，因为有的可能复制了全部的数据，有的可能只复制了部  分。GFS不能保证所有的副本每个字节都是一样的。它只保证每个数据作为一个原子单元被写过至少一次。这个是这样得出的：操作要是成功，数据必须在所有的  副本上的同样的偏移处被写过。进一步，从这以后，所有的副本至少和记录一样长，所以后续的记录将被指定到更高的偏移处或者一个不同的块上，即使另一个副本  成了primary。根据一致性保证，成功的record  append操作的区间是已定义的。而受到干扰的区间是不一致的。<br />
（4）快照（snapshot）<br />
快照操作几乎在瞬间构造一个文件和目录树的副本，同时将正在进行的其他修改操作对它的影响减至最小。<br />
我们使用copy-on-write技术来实现snapshot。当master受到一个snapshot请求时，  它首先将要snapshot的文件上块上的lease。这使得任何一个向这些块写数据的操作都必须和master交互以找到拥有lease的副本。这就给  master一个创建这个块的副本的机会。<br />
副本被撤销或终止后，master在磁盘上登记执行的操作，然后复制源文件或目录树的metadata以对它的内存状态实施登记的操作。这个新创建的snapshot文件和源文件（其metadata）指向相同的块（chunk）。<br />
Snapshot之后，客户第一次向chunk  c写的时候，它发一个请求给master以找到拥有lease的副本。Master注意到chunk c的引用记数比1大，它延迟对用户的响应，选择一个chunk  handle C’,然后要求每一有chunk c的副本的chunkserver创建一个块C’。每个chunkserver在本地创建chunk  C’避免了网络开销。从这以后和对别的块的操作没有什么区别。<br />
3、MASTER操作<br />
MASTER执行所有名字空间的操作，除此之外，他还在系统范围管理数据块的复制：决定数据块的放置方案，产生新数据块并将其备份，和其他系统范围的操作协同来确保数据备份的完整性，在所有的数据块服务器之间平衡负载并收回没有使用的存储空间。<br />
3.1  名字空间管理和加锁<br />
与传统文件系统不同的是，GFS没有与每个目录相关的能列出其所有文件的数据结构，它也不支持别名（unix中的硬连接或符号连接），不管是对文件或是目录。GFS的名字空间逻辑上是从文件元数据到路径名映射的一个查用表。<br />
MASTER在执行某个操作前都要获得一系列锁，例如，它要对/d1/d2…/dn/leaf执行操作，则它必须获  得/d1，/d1/d2，…，  /d1/d2/…/dn的读锁，/d1/d2…/dn/leaf的读锁或写锁（其中leaf可以使文件也可以是目录）。MASTER操作的并行性和数据的  一致性就是通过这些锁来实现的。<br />
3.2  备份存储放置策略<br />
一个GFS集群文件系统可能是多层分布的。一般情况下是成千上万个文件块服务器分布于不同的机架上，而这些文件块服  务器又被分布于不同机架上的客户来访问。因此，不同机架上的两台机器之间的通信可能通过一个或多个交换机。数据块冗余配置策略要达到连个目的：最大的数据  可靠性和可用性，最大的网络带宽利用率。因此，如果仅仅把数据的拷贝置于不同的机器上很难满足这两个要求，必须在不同的机架上进行数据备份。这样即使整个  机架被毁或是掉线，也能确保数据的正常使用。这也使数据传输，尤其是读数据，可以充分利用带宽，访问到多个机架，而写操作，则不得不涉及到更多的机架。<br />
3.3  产生、重复制、重平衡数据块<br />
当MASTER产生新的数据块时，如何放置新数据块，要考虑如下几个因素：（1）尽量放置在磁盘利用率低的数据块服  务器上，这样，慢慢地各服务器的磁盘利用率就会达到平衡。（2）尽量控制在一个服务器上的“新创建”的次数。（3）由于上一小节讨论的原因，我们需要把数  据块放置于不同的机架上。<br />
MASTER在可用的数据块备份低于用户设定的数目时需要进行重复制。这种情况源于多种原因：服务器不可用，数据被  破坏，磁盘被破坏，或者备份数目被修改。每个被需要重复制的数据块的优先级根据以下几项确定：第一是现在的数目距目标的距离，对于能阻塞用户程序的数据  块，我们也提高它的优先级。最后，  MASTER按照产生数据块的原则复制数据块，并把它们放到不同的机架内的服务器上。<br />
MASTER周期性的平衡各服务器上的负载：它检查chunk分布和负载平衡，通过这种方式来填充一个新的服务器而  不是把其他的内容统统放置到它上面带来大量的写数据。数据块放置的原则与上面讨论的相同，此外，MASTER还决定那些数据块要被移除，原则上他会清除那  些空闲空间低于平均值的那些服务器。<br />
3.4  垃圾收集<br />
在一个文件被删除之后，GFS并不立即收回磁盘空间，而是等到垃圾收集程序在文件和数据块级的的检查中收回。<br />
当一个文件被应用程序删除之后，MASTER会立即记录下这些变化，但文件所占用的资源却不会被立即收回，而是重新  给文件命了一个隐藏的名字，并附上了删除的时间戳。在MASTER定期检查名字空间时，它删除超过三天（可以设定）的隐藏的文件。在此之前，可以以一个新  的名字来读文件，还可以以前的名字恢复。当隐藏的文件在名字空间中被删除以后，它在内存中的元数据即被擦除，这就有效地切断了他和所有数据块的联系。<br />
在一个相似的定期的名字空间检查中，MASTER确认孤儿数据块（不属于任何文件）并擦除他的元数据，在和MASTER的心跳信息交换中，每个服务器报告他所拥有的数据块，MASTER返回元数据不在内存的数据块，服务器即可以删除这些数据块。<br />
3.5  过时数据的探测<br />
在数据更新时如果服务器停机了，那么他所保存的数据备份就会过时。对每个数据块，MASTER设置了一个版本号来区别更新过的数据块和过时的数据块。<br />
当MASTER授权一个新的lease时，他会增加数据块的版本号并会通知更新数据备份。MASTER和备份都会记  录下当前的版本号，如果一个备份当时不可用，那么他的版本号不可能提高，当ChunkServer重新启动并向MASTER报告他的数据块集时，  MASTER就会发现过时的数据。<br />
MASTER在定期的垃圾收集程序中清除过时的备份，在此以前，处于效率考虑，在各客户及英大使，他会认为根本不存  在过时的数据。作为另一个安全措施，  MASTER在给客户及关于数据块的应答或是另外一个读取数据的服务器数据是都会带上版本信息，在操作前客户机和服务器会验证版本信息以确保得到的是最新  的数据。<br />
4、容错和诊断<br />
4.1 高可靠性<br />
4.1.1  快速恢复<br />
不管如何终止服务，MASTER和数据块服务器都会在几秒钟内恢复状态和运行。实际上，我们不对正常终止和不正常终止进行区分，服务器进程都会被切断而终止。客户机和其他的服务器会经历一个小小的中断，然后它们的特定请求超时，重新连接重启的服务器，重新请求。<br />
4.1.2  数据块备份<br />
如上文所讨论的，每个数据块都会被备份到放到不同机架上的不同服务器上。对不同的名字空间，用户可以设置不同的备份级别。在数据块服务器掉线或是数据被破坏时，MASTER会按照需要来复制数据块。<br />
4.1.3  MASTER备份<br />
为确保可靠性，MASTER的状态、操作记录和检查点都在多台机器上进行了备份。一个操作只有在数据块服务器硬盘上  刷新并被记录在MASTER和其备份的上之后才算是成功的。如果MASTER或是硬盘失败，系统监视器会发现并通过改变域名启动它的一个备份机，而客户机  则仅仅是使用规范的名称来访问，并不会发现MASTER的改变。<br />
4.2  数据完整性<br />
每个数据块服务器都利用校验和来检验存储数据的完整性。原因：每个服务器随时都有发生崩溃的可能性，并且在两个服务器间比较数据块也是不现实的，同时，在两台服务器间拷贝数据并不能保证数据的一致性。<br />
每个Chunk按64kB的大小分成块，每个块有32位的校验和，校验和和日志存储在一起，和用户数据分开。<br />
在读数据时，服务器首先检查与被读内容相关部分的校验和，因此，服务器不会传播错误的数据。如果所检查的内容和校验  和不符，服务器就会给数据请求者返回一个错误的信息，并把这个情况报告给MASTER。客户机就会读其他的服务器来获取数据，而MASTER则会从其他的  拷贝来复制数据，等到一个新的拷贝完成时，MASTER就会通知报告错误的服务器删除出错的数据块。<br />
附加写数据时的校验和计算优化了，因为这是主要的写操作。我们只是更新增加部分的校验和，即使末尾部分的校验和数据已被损坏而我们没有检查出来，新的校验和与数据会不相符，这种冲突在下次使用时将会被检查出来。<br />
相反，如果是覆盖现有数据的写，在写以前，我们必须检查第一和最后一个数据块，然后才能执行写操作，最后计算和记录校验和。如果我们在覆盖以前不先检查首位数据块，计算出的校验和则会因为没被覆盖的数据而产生错误。<br />
在空闲时间，服务器会检查不活跃的数据块的校验和，这样可以检查出不经常读的数据的错误。一旦错误被检查出来，服务器会拷贝一个正确的数据块来代替错误的。<br />
4.3  诊断工具<br />
广泛而细致的诊断日志以微小的代价换取了在问题隔离、诊断、性能分析方面起到了重大的作用。GFS服务器用日志来记  录显著的事件（例如服务器停机和启动）和远程的应答。远程日志记录机器之间的请求和应答，通过收集不同机器上的日志记录，并对它们进行分析恢复，我们可以  完整地重现活动的场景，并用此来进行错误分析。<br />
5 测量<br />
5.1  测试环境<br />
一台主控机，两台主控机备份，16台数据块服务器，16台客户机。<br />
每台机器：2块PIII1.4G处理器，2G内存，2块80G5400rpm的硬盘，1块100Mbps全双工网卡<br />
19台服务器连接到一个HP2524交换机上，16台客户机俩接到领外一台交换机上，两台交换机通过1G的链路相连。</p>
<p>原文转自：http://bigweb.group.javaeye.com/group/topic/5749</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/504.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP:set_error_handler &#8230;&#8230;need more</title>
		<link>http://www.donevii.com/post/350.html</link>
		<comments>http://www.donevii.com/post/350.html#comments</comments>
		<pubDate>Sat, 29 Sep 2007 10:17:41 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=350</guid>
		<description><![CDATA[本来想自己写个 error 处理的 logger 结果发现通过 set_error_handler 没办法捕获到 fatal error &#38; parse error 唉，真愁人呐。 在 php.net 上也没有找到办法，后来反到是在 zend.com 上找到了解决 catch fatal err... ]]></description>
			<content:encoded><![CDATA[<p>本来想自己写个 <a href="http://www.donevii.com/post/tag/error" class="st_tag internal_tag" rel="tag" title="Posts tagged with error">error</a> 处理的 logger 结果发现通过 set_<a href="http://www.donevii.com/post/tag/error" class="st_tag internal_tag" rel="tag" title="Posts tagged with error">error</a>_handler 没办法捕获到 fatal <a href="http://www.donevii.com/post/tag/error" class="st_tag internal_tag" rel="tag" title="Posts tagged with error">error</a> &amp; parse <a href="http://www.donevii.com/post/tag/error" class="st_tag internal_tag" rel="tag" title="Posts tagged with error">error</a> 唉，真愁人呐。</p>
<p>在 <a href="http://www.donevii.com/post/tag/php" class="st_tag internal_tag" rel="tag" title="Posts tagged with php">php</a>.net 上也没有找到办法，后来反到是在 zend.com 上找到了解决 catch fatal error 的办法就是在 auto_prepend_file 和 auto_append_file 上做手脚。</p>
<p>prepend 的文件里面有一个 string 里面是个 error page 的 <a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">html</a> 包括一个 script 可以把错误信息发送到 <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">server</a> 的一个 api 上。</p>
<p>而在 append 的文件里通过 ob_get_contents() 来把那个 string 给去掉，如果能去掉就说明程序中间的执行流程正确，无错，如果有 fatal error 则没有办法到达 append 文件这步，所以会显示那个 html 页面。</p>
<p>方法还可以，但是因为每个 php 都要 prepend &amp; append 可能效率会不行，和 Xuanyan 讨论了一小会暂时还是没有什么好办法来捕获 fatal &amp; parse error 。</p>
<p>:(</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/350.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XCache &amp; XDebug on road</title>
		<link>http://www.donevii.com/post/349.html</link>
		<comments>http://www.donevii.com/post/349.html#comments</comments>
		<pubDate>Fri, 28 Sep 2007 08:03:37 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=349</guid>
		<description><![CDATA[终于配置上 XCache 和 XDebug 了，可惜的是 php-java-bridge 一直没搞好，只有双击运行 JavaBridge 后才行，唉，要是能内置到 PHP 里就好了。 继续研究 FastCGI &#38; Lighttpd 如果说之前在 UUSee 是向上研究，... ]]></description>
			<content:encoded><![CDATA[<p>终于配置上 XCache 和 XDebug 了，可惜的是 <a href="http://www.donevii.com/post/tag/php" class="st_tag internal_tag" rel="tag" title="Posts tagged with php">php</a>-<a href="http://www.donevii.com/post/tag/java" class="st_tag internal_tag" rel="tag" title="Posts tagged with java">java</a>-bridge 一直没搞好，只有双击运行 JavaBridge 后才行，唉，要是能内置到 PHP 里就好了。</p>
<p>继续研究 <a href="http://www.donevii.com/post/tag/fastcgi" class="st_tag internal_tag" rel="tag" title="Posts tagged with fastcgi">FastCGI</a> &amp; <a href="http://www.donevii.com/post/tag/lighttpd" class="st_tag internal_tag" rel="tag" title="Posts tagged with lighttpd">Lighttpd</a></p>
<p>如果说之前在 UUSee 是向上研究，既“抽象”、“架构”的话，那么来 IMobile 之后研究方向则是向下，研究底层，研究以前没注意到的更细节的地方了。</p>
<p>:)</p>
<p>Good days, good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/349.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>redhad下的openssl(安装和卸载)</title>
		<link>http://www.donevii.com/post/348.html</link>
		<comments>http://www.donevii.com/post/348.html#comments</comments>
		<pubDate>Fri, 21 Sep 2007 10:47:46 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[openssl]]></category>
		<category><![CDATA[redhat]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=348</guid>
		<description><![CDATA[转至: http://blog.csdn.net/baitianhai/archive/2004/10/27/155461.aspx 最近在鼓捣redhat linux，想自己以源代码方式安装软件，不想用rpm方式安装。 首先从httpd开始，先卸载在安装倒是比较容易，不过后来像添加... ]]></description>
			<content:encoded><![CDATA[<p>转至: http://<a href="http://www.donevii.com/post/tag/blog" class="st_tag internal_tag" rel="tag" title="Posts tagged with blog">blog</a>.csdn.net/baitianhai/archive/2004/10/27/155461.aspx</p>
<p>最近在鼓捣redhat <a href="http://www.donevii.com/post/tag/linux" class="st_tag internal_tag" rel="tag" title="Posts tagged with linux">linux</a>，想自己以源代码方式安装软件，不想用rpm方式安装。</p>
<p>首先从httpd开始，先卸载在安装倒是比较容易，不过后来像添加ssl功能，发现编译的时候需要用openssl的安装目录，本人比较愚笨，一顿好找也没有找到，于是就想把openssl也以源代码方式安装。先卸载，此时出现问题，系统好多东西依赖于openssl的库，我查了好多资料也没找到什么办法，于是我最后一狠心，用rpm -e &#8211;nodeps给卸载了，然后手动安装了openssl，然后重新启动，这下坏了，好多服务都起不来了，smb,ssh等等，图形模式也起不来了，我欲哭无泪。</p>
<p>因为我是在虚拟机上安装的，smb起不来了，我只能重新安装系统了。这次安装我大多数东西都没选择，一路安装完毕，结果在文本方式发现vi编辑没有颜色了，哎，也不知道是少装了那个东西弄得（各位谁知道麻烦告诉告诉我一下），只能按照猜测重新安装了又添加了一些东西。不过幸运的vi高亮显示功能又有了，遗憾的是具体是那个软件我还是不清楚。有了上次的教训我不敢轻易卸掉系统原来的openssl了，我从网上搜索到了一篇安装openssl的英文文章，地址在 http://www.devside.net/<a href="http://www.donevii.com/post/tag/web" class="st_tag internal_tag" rel="tag" title="Posts tagged with web">web</a>/<a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">server</a>/linux/<a href="http://www.donevii.com/post/tag/openssl" class="st_tag internal_tag" rel="tag" title="Posts tagged with openssl">openssl</a> 我按照上面说的安装了zlib,<a href="http://www.donevii.com/post/tag/openssl" class="st_tag internal_tag" rel="tag" title="Posts tagged with openssl">openssl</a>。步骤简介如下（怕以后忘了）</p>
<p>安装zlib</p>
<p>Home : http://www.gzip.org/zlib/</p>
<p>Package(linux source) : http://www.gzip.org/zlib/</p>
<p>Our Configuration</p>
<p>Install to : /usr/local</p>
<p>Module types : dynamically and staticly loaded modules, *.so and *.a</p>
<p>Build Instructions</p>
<p>zlib library files are placed into /usr/local/lib and zlib header files are placed into /usr/local/include, by default.</p>
<p>Build static libraries</p>
<p>&#8230;/zlib-1.2.1]# ./configure</p>
<p>&#8230;/zlib-1.2.1]# make test</p>
<p>&#8230;/zlib-1.2.1]# make install</p>
<p>Build shared libraries</p>
<p>&#8230;/zlib-1.2.1]# make clean</p>
<p>&#8230;/zlib-1.2.1]# ./configure &#8211;shared</p>
<p>&#8230;/zlib-1.2.1]# make test</p>
<p>&#8230;/zlib-1.2.1]# make install</p>
<p>&#8230;/zlib-1.2.1]# cp zutil.h /usr/local/include</p>
<p>&#8230;/zlib-1.2.1]# cp zutil.c /usr/local/include</p>
<p>/usr/local/lib should now contain&#8230;</p>
<p>libz.a</p>
<p>libz.so -&gt; libz.so.1.2.1</p>
<p>libz.so.1 -&gt; libz.so.1.2.1</p>
<p>libz.so.1.2.1</p>
<p>/usr/local/include should now contain&#8230;</p>
<p>zconf.h</p>
<p>zlib.h</p>
<p>zutil.h</p>
<p>[Optional] Instructions for non-standard placement of zlib</p>
<p>Create the directory that will contain zlib</p>
<p>&#8230;/zlib-1.2.1]# mkdir /usr/local/zlib</p>
<p>Follow the given procedure above, except</p>
<p>&#8230;/zlib-1.2.1]# ./configure &#8211;prefix=/usr/local/zlib</p>
<p>Update the Run-Time Linker</p>
<p>/etc/ld.so.<a href="http://www.donevii.com/post/tag/cache" class="st_tag internal_tag" rel="tag" title="Posts tagged with cache">cache</a> will need to be updated with the new zlib shared lib: libz.so.1.2.1</p>
<p>For standard zlib installation&#8230;</p>
<p>Add /usr/local/lib to /etc/ld.so.conf, if specified path is not present</p>
<p>/etc]# ldconfig</p>
<p>If zlib was installed with a prefix&#8230;</p>
<p>Add /usr/local/zlib/lib to /etc/ld.so.conf</p>
<p>/etc]# ldconfig</p>
<p>安装openssl</p>
<p>Download</p>
<p>Home : http://www.openssl.org/</p>
<p>Package(source) : openssl-0.9.7d.tar.gz</p>
<p>Our Configuration</p>
<p>install to : /usr/local/<a href="http://www.donevii.com/post/tag/ssl" class="st_tag internal_tag" rel="tag" title="Posts tagged with ssl">ssl</a></p>
<p>module types : dynamically and staticly loaded modules, *.so *.a</p>
<p>Build Instructions</p>
<p>&#8230;/openssl-0.9.7d]# ./config</p>
<p>&#8211;prefix=/usr/local/ssl</p>
<p>[default location]</p>
<p>shared</p>
<p>[in addition to the usual static libraries, create shared libraries]</p>
<p>zlib-dynamic</p>
<p>[like "zlib", but has OpenSSL load the zlib library dynamically when needed]</p>
<p>&#8230;/openssl-0.9.7d]# ./config -t</p>
<p>[display guess on system made by ./config]</p>
<p>&#8230;/openssl-0.9.7d]# make</p>
<p>&#8230;/openssl-0.9.7d]# make test</p>
<p>&#8230;/openssl-0.9.7d]# make install</p>
<p>Update the Run-time Linker</p>
<p>ld.so.cache will need to be updated with the location of the new OpenSSL shared libs: libcrypto.so.0.9.7 and libssl.so.0.9.7</p>
<p>Sometimes it is sufficient to just add these two files to /lib, but we recommend you follow these instructions instead.</p>
<p>Edit /etc/ld.so.conf</p>
<p>Add /usr/local/ssl/lib to the bottom.</p>
<p>&#8230;]# ldconfig</p>
<p>Update the PATH</p>
<p>Edit /root/.bash_profile</p>
<p>Add /usr/local/ssl/bin to the PATH variable.</p>
<p>Re-login</p>
<p>Testing</p>
<p>&#8230;]# openssl version</p>
<p>Should display OpenSSL 0.9.7d 17 Mar 2004</p>
<p>If an older version is shown, your system contains a previously installed OpenSSL.</p>
<p>Repeate the steps in Update the PATH, except place the specified location at the start of the PATH variable.</p>
<p>[the older openssl, on most systems, is located under /usr/bin]</p>
<p>[the command 'which openssl' should display the path of the openssl that your system is using]</p>
<p>/usr/local/ssl/bin]# ./openssl version should display the correct version.</p>
<p>但是我最后没有得到想要的结果，系统原来的openssl还是没能卸载掉，我该怎么做那？我继续搜索资料，哈，幸运的我找了，在一个国内论坛上是这么说的</p>
<p>cd /usr/local/ssl/lib</p>
<p>ln -s libcrypto.so.0.9.7 libcrypto.so.2</p>
<p>ln -s libssl.so.0.9.7 libssl.so.2</p>
<p>//最后要刷新系统的动态连接库配置</p>
<p>echo /usr/local/ssl/lib &gt;&gt; /etc/ld.so.conf</p>
<p>ldconfig -v</p>
<p>这下子我豁然开朗，原来依赖的那2个文件是个软链接啊，我把它修改为我现在真正的openssl库文件不是就行了吗？于是一顿忙碌后，我终于执行了 rpm -e -nodeps ，然后重新启动系统，一路运行下去，全是绿灯。一时间感觉自己好幸福啊</p>
<p>为了这个问题我查了国内的几个比较大的unix/linux网站都没找到资料，不过从这里http://bbs.netbuddy.org/<a href="http://www.donevii.com/post/tag/unix" class="st_tag internal_tag" rel="tag" title="Posts tagged with unix">unix</a>/737.html还是找到了（国外的E文大概意思能看懂，但是查找起来还是没找到，也不知道这方面好点的网站），</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/348.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenSSL相关命令（for Linux）详细介绍</title>
		<link>http://www.donevii.com/post/347.html</link>
		<comments>http://www.donevii.com/post/347.html#comments</comments>
		<pubDate>Fri, 21 Sep 2007 08:19:49 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=347</guid>
		<description><![CDATA[转至: http://blog.ixpub.net/8400463 加密算法： 对称加密算法： DES、IDEA、RC2、RC4、AES、Skipjack &#8230;&#8230; 非对称加密算法： RSA、DSA、DiffieHellman、PKCS、PGP &#8230;&#8230; 单向的HASH算法属于报文摘要算法... ]]></description>
			<content:encoded><![CDATA[<p>转至: http://<a href="http://www.donevii.com/post/tag/blog" class="st_tag internal_tag" rel="tag" title="Posts tagged with blog">blog</a>.ixpub.net/8400463</p>
<p>加密算法：</p>
<p>对称加密算法：</p>
<p>DES、IDEA、RC2、RC4、AES、Skipjack &#8230;&#8230;</p>
<p>非对称加密算法：</p>
<p>RSA、DSA、DiffieHellman、PKCS、PGP &#8230;&#8230;</p>
<p>单向的HASH算法属于报文摘要算法，虽然有些也出自OpenSSL库。<br />
命令操作：</p>
<p>1、生成普通私钥：<br />
[weigw@TEST src]$ <a href="http://www.donevii.com/post/tag/openssl" class="st_tag internal_tag" rel="tag" title="Posts tagged with openssl">openssl</a> genrsa -out privatekey.key 1024</p>
<p>Generating RSA private key, 1024 bit long modulus &#8230;.++++++ &#8230;&#8230;.++++++ e is 65537 (0&#215;10001)</p>
<p>2、生成带加密口令的密钥：</p>
<p>[weigw@TEST src]$ openssl genrsa -des3 -out privatekey.key 1024</p>
<p>Generating RSA private key, 1024 bit long modulus &#8230;&#8230;&#8230;&#8230;++++++ &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;++++++ e is 65537 (0&#215;10001) Enter pass phrase for privatekey.key: Verifying &#8211; Enter pass phrase for privatekey.key:</p>
<p>在生成带加密口令的密钥时需要自己去输入密码。对于为密钥加密现在提供了一下几种算法：<br />
-des encrypt the generated key with DES in cbc mode</p>
<p>-des3 encrypt the generated key with DES in ede cbc mode (168 bit key)</p>
<p>-aes128, -aes192, -aes256 encrypt PEM output with cbc aes</p>
<p>去除密钥的口令：<br />
[weigw@TEST src]$ openssl rsa -in privatekey.key -out</p>
<p>privatekey.key Enter pass phrase for privatekey.key: writing RSA key</p>
<p>通过生成的私钥去生成证书：</p>
<p>[weigw@TEST src]$ openssl req -new -x509 -key privatekey.key -out cacert.crt -days 1095</p>
<p>You are about to be asked to enter information that will be incorporated into your certificate request.</p>
<p>What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter &#8216;.&#8217;, the field will be left blank.</p>
<p>&#8212;&#8211;</p>
<p>Country Name (2 letter code) [GB]:CN</p>
<p>State or Province Name (full name) [Berkshire]:beijing</p>
<p>Locality Name (eg, city) [Newbury]:beijing</p>
<p>Organization Name (eg, company) [My Company Ltd]:wondersoft</p>
<p>Organizational Unit Name (eg, section) []:develop</p>
<p>Common Name (eg, your name or your <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">server</a>&#8217;s hostname) []:WeiGW</p>
<p>Email Address []:weigongwan@sina.com</p>
<p>在生成证书的时候需要按照提示输入一些个人信息。</p>
<p>通过私钥生成公钥：</p>
<p>[weigw@TEST src]$ openssl rsa -in privatekey.key -pubout -out pubkey.key writing RSA key</p>
<p>格式转换：（证书、私钥、公钥）（PEM &lt;&#8212;&#8211;&gt;DER）</p>
<p>[weigw@TEST src]$ openssl x509 -in cacert.crt -inform. PEM -out cacert.der -outform. DER</p>
<p>[weigw@TEST src]$</p>
<p>[weigw@TEST src]$ openssl rsa -in privatekey.key -inform. PEM -out privatekey.der -outform. DER</p>
<p>writing RSA key</p>
<p>[weigw@TEST src]$ openssl rsa -pubin -in pubkey.key -inform. PEM -pubout -out pubkey.der -outform. DER</p>
<p>writing RSA key</p>
<p>从DER格式转换成PEM格式一样，就是把inform的格式改成DERoutform的格式改成PEM即可。</p>
<p>下面是一个服务器和客户端认证的证书、私钥生成方法：（server.crt、client.crt、ca.crt）</p>
<p>第一步： 生成私钥</p>
<p>[weigw@TEST bin]$ openssl genrsa -out server.key 1024</p>
<p>Generating RSA private key, 1024 bit long modulus .++++++ ..<br />
&#8230;&#8230;&#8230;++++++ e is 65537 (0&#215;10001)</p>
<p>[weigw@TEST bin]$ openssl genrsa -out client.key 1024</p>
<p>Generating RSA private key, 1024 bit long modulus &#8230;++++++ &#8230;&#8230;<br />
&#8230;&#8230;&#8230;.++++++ e is 65537 (0&#215;10001)</p>
<p>[weigw@TEST bin]$ openssl genrsa -out ca.key 1024</p>
<p>Generating RSA private key, 1024 bit long modulus &#8230;&#8230;.<br />
..++++++ &#8230;&#8230;&#8230;++++++ e is 65537 (0&#215;10001)</p>
<p>[weigw@TEST bin]$</p>
<p>第三步： 申请证书（为请求文件签名）</p>
<p>[weigw@TEST bin]$ openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key</p>
<p>[weigw@TEST bin]$ openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key</p>
<p>如果在这步出现错误信息：</p>
<p>[weigw@TEST bin]$ openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key</p>
<p>Using configuration from /usr/share/<a href="http://www.donevii.com/post/tag/ssl" class="st_tag internal_tag" rel="tag" title="Posts tagged with ssl">ssl</a>/openssl.cnf I am unable to access the ./demoCA/newcerts directory ./demoCA/newcerts: No such file or directory</p>
<p>[weigw@TEST bin]$</p>
<p>自己手动创建一个CA目录结构：<br />
[weigw@TEST bin]$ mkdir ./demoCA<br />
[weigw@TEST bin]$ mkdir demoCA/newcerts<br />
创建个空文件：<br />
[weigw@TEST bin]$ vi demoCA/index.txt<br />
向文件中写入01：<br />
[weigw@TEST bin]$ vi demoCA/serial</p>
<p>合并证书文件（crt）和私钥文件（key）：</p>
<p>[weigw@TEST bin]$ cat client.crt client.key &gt; client.pem [weigw@TEST bin]$ cat server.crt server.key &gt; server.pem</p>
<p>合并成pfx证书：</p>
<p>[weigw@TEST bin]$ openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12</p>
<p>Enter Export Password:</p>
<p>Verifying &#8211; Enter Export Password:</p>
<p>[weigw@TEST bin]$openssl pkcs12 -export -clcerts -in server.crt -inkey server.key -out server.p12<br />
Enter Export Password:<br />
Verifying &#8211; Enter Export Password:</p>
<p>文本化证书：</p>
<p>[weigw@TEST bin]$ openssl pkcs12 -in client.p12 -out client.txt Enter Import Password:</p>
<p>MAC verified OK</p>
<p>Enter PEM pass phrase: Verifying &#8211; Enter PEM pass phrase:</p>
<p>[weigw@TEST bin]$openssl pkcs12 -in server.p12 -out server.txt</p>
<p>Enter Import Password:</p>
<p>MAC verified OK</p>
<p>Enter PEM pass phrase: Verifying &#8211; Enter PEM pass phrase:</p>
<p>屏幕模式显式：（证书、私钥、公钥）</p>
<p>[weigw@TEST bin]$ openssl x509 -in client.crt -noout -text -modulus</p>
<p>[weigw@TEST bin]$ openssl rsa -in server.key -noout -text -modulus</p>
<p>[weigw@TEST bin]$ openssl rsa -in server.pub -noout -text -modulus</p>
<p>得到DH：</p>
<p>[weigw@TEST bin]$ openssl dhparam -out dh1024.pem 1024</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/347.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[收藏] Asian NTP Server pool</title>
		<link>http://www.donevii.com/post/332.html</link>
		<comments>http://www.donevii.com/post/332.html#comments</comments>
		<pubDate>Fri, 25 May 2007 02:06:10 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[ntp]]></category>
		<category><![CDATA[pool]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=332</guid>
		<description><![CDATA[Philippines &#8212; ph.pool.ntp.org (1)Malaysia &#8212; my.pool.ntp.org (4)Turkey &#8212; tr.pool.ntp.org (1)Singapore &#8212; sg.pool.ntp.org (2)India &#8212; in.pool.ntp.org (1)Hong Kong &#8212; hk.pool.ntp.org (1)United Arab Emirates &#8212; ae.pool.n... ]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pool.ntp.org/zone/ph">Philippines</a> &mdash; ph.<a href="http://www.donevii.com/post/tag/pool" class="st_tag internal_tag" rel="tag" title="Posts tagged with pool">pool</a>.<a href="http://www.donevii.com/post/tag/ntp" class="st_tag internal_tag" rel="tag" title="Posts tagged with ntp">ntp</a>.org (1)<br /><a href="http://www.pool.ntp.org/zone/my">Malaysia</a> &mdash; my.pool.ntp.org (4)<br /><a href="http://www.pool.ntp.org/zone/tr">Turkey</a> &mdash; tr.pool.ntp.org (1)<br /><a href="http://www.pool.ntp.org/zone/sg">Singapore</a> &mdash; sg.pool.ntp.org (2)<br /><a href="http://www.pool.ntp.org/zone/in">India</a> &mdash; in.pool.ntp.org (1)<br /><a href="http://www.pool.ntp.org/zone/hk">Hong Kong</a> &mdash; hk.pool.ntp.org (1)<br /><a href="http://www.pool.ntp.org/zone/ae">United Arab Emirates</a> &mdash; ae.pool.ntp.org (0)<br /><a href="http://www.pool.ntp.org/zone/jp">Japan</a> &mdash; jp.pool.ntp.org (6)<br /><a href="http://www.pool.ntp.org/zone/bd">Bangladesh</a> &mdash; bd.pool.ntp.org (0)<br /><a href="http://www.pool.ntp.org/zone/il">Israel</a> &mdash; il.pool.ntp.org (3)<br /><a href="http://www.pool.ntp.org/zone/kr">Korea</a> &mdash; kr.pool.ntp.org (4)<br /><a href="http://www.pool.ntp.org/zone/th">Thailand</a> &mdash; th.pool.ntp.org (1)<br /><a href="http://www.pool.ntp.org/zone/ir">Iran</a> &mdash; ir.pool.ntp.org (0)<br /><a href="http://www.pool.ntp.org/zone/tw">Taiwan</a> &mdash; tw.pool.ntp.org (15)<br /><a href="http://www.pool.ntp.org/zone/cn">China</a> &mdash; cn.pool.ntp.org (6)<br /><a href="http://www.pool.ntp.org/zone/id">Indonesia</a> &mdash; id.pool.ntp.org (3)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/332.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[超长篇] Inject Your Code to a Portable Executable File</title>
		<link>http://www.donevii.com/post/330.html</link>
		<comments>http://www.donevii.com/post/330.html#comments</comments>
		<pubDate>Thu, 24 May 2007 07:50:01 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=330</guid>
		<description><![CDATA[转至: http://www.codeguru.com/cpp/w-p/system/misc/article.php/c11393 Downloads pemaker1.zip &#8211; pemaker2.zip &#8211; pemaker3.zip &#8211; pemaker4.zip &#8211; pemaker5.zip &#8211; peviewer.zip &#8211; test1.zip &#8211; Windows NT 3.51 (I mean, Win3... ]]></description>
			<content:encoded><![CDATA[<p>转至: <a href="http://www.codeguru.com/cpp/w-p/system/misc/article.php/c11393">http://www.codeguru.com/cpp/w-p/system/misc/article.php/c11393</a></p>
<p><strong>Downloads</strong></p>
<li><a href="http://www.codeguru.com/dbfiles/get_file/pemaker1.zip?id=11393&amp;lbl=PEMAKER1_ZIP&amp;ds=20060302">pemaker1.zip</a> &#8211; </li>
<li><a href="http://www.codeguru.com/dbfiles/get_file/pemaker2.zip?id=11393&amp;lbl=PEMAKER2_ZIP&amp;ds=20060302">pemaker2.zip</a> &#8211; </li>
<li><a href="http://www.codeguru.com/dbfiles/get_file/pemaker3.zip?id=11393&amp;lbl=PEMAKER3_ZIP&amp;ds=20060302">pemaker3.zip</a> &#8211; </li>
<li><a href="http://www.codeguru.com/dbfiles/get_file/pemaker4.zip?id=11393&amp;lbl=PEMAKER4_ZIP&amp;ds=20060302">pemaker4.zip</a> &#8211; </li>
<li><a href="http://www.codeguru.com/dbfiles/get_file/pemaker5.zip?id=11393&amp;lbl=PEMAKER5_ZIP&amp;ds=20060302">pemaker5.zip</a> &#8211; </li>
<li><a href="http://www.codeguru.com/dbfiles/get_file/peviewer.zip?id=11393&amp;lbl=PEVIEWER_ZIP&amp;ds=20060302">peviewer.zip</a> &#8211; </li>
<li><a href="http://www.codeguru.com/dbfiles/get_file/test1.zip?id=11393&amp;lbl=TEST1_ZIP&amp;ds=20060302">test1.zip</a> &#8211; </li>
<p><a name="more"><font color="#000000"></font></a><a href="http://en.wikipedia.org/wiki/Windows_NT_3.51" target="new">Windows NT 3.51</a> (I mean, <a href="http://en.wikipedia.org/wiki/Windows_3.1" target="new">Win3.1</a>, <a href="http://en.wikipedia.org/wiki/Windows_95" target="new">Win95</a>, <a href="http://en.wikipedia.org/wiki/Windows_98" target="new">Win98</a> were not perfect <a href="http://en.wikipedia.org/wiki/Operating_System" target="new">OS</a>s). The MS-DOS data causes that your executable file to have the performance inside MS-DOS and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_core_.2f.stub.asp" target="new">the MS-DOS Stub program</a> lets it display: <strong>&quot;This program can not be run in MS-DOS mode&quot;</strong> or <strong>&quot;This program can be run only in <a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">Windows</a> mode&quot;</strong>, or some things like these comments when you try to run a Windows EXE file inside <a href="http://en.wikipedia.org/wiki/MS-DOS" target="new">MS-DOS 6.0</a>, where there is no footstep of Windows. Thus, this data is reserved for the code to indicate these comments in the <a href="http://en.wikipedia.org/wiki/MS-DOS" target="new">MS-DOS</a> <a href="http://en.wikipedia.org/wiki/Operating_System" target="new">operating system</a>. The most interesting part of the <a href="http://en.wikipedia.org/wiki/MS-DOS" target="new">MS-DOS</a> data is &quot;<strong>MZ</strong>&quot;! Can you believe, it refers to the name of &quot;<a href="http://en.wikipedia.org/wiki/Mark_Zbikowski" target="new">Mark Zbikowski</a>&quot;, one of the first Microsoft programmers?</p>
<p><font color="#000000"><img height="175" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEMAKER_GIF&amp;ds=20060302" width="452" alt="" /></font></p>
<h3>0 Preface</h3>
<p>You might demand to comprehend the ways a virus program injects its procedure into the interior of a portable executable file and corrupts it, or you are interested in implementing a packer or a protector to encrypt the data of your portable executable (PE) file. This article is committed to represent a brief discussion to realize the performance that is accomplished by EXE tools or some kinds of mal-ware.</p>
<p>You can employ this article&#8217;s source code to create your custom EXE builder. It could be used to make an EXE protector in the right way, or with the wrong intention, to spread a virus. However, my purpose of writing this article has been the first application, so I will not be responsible for the immoral usage of these methods.</p>
<h3>1 Prerequisites</h3>
<p>There are no specific mandatory prerequisites to follow the topics in this article. If you are familiar with a debugger and also the portable file format, I suggest you to drop to Sections 2 and 3; the whole of these sections has been made for people who don&#8217;t have any knowledge regarding the EXE file format or debuggers.</p>
<h3>2 Portable Executable File Format</h3>
<p>The Portable Executable file format was defined to provide the best way for the Windows Operating System to execute code and also to store the essential data that is needed to run a program&mdash;for example constant data, variable data, import library links, and resource data. It consists of MS-DOS file information, Windows NT file information, Section Headers, and Section images, as shown in Table 1.</p>
<h4>2.1 The MS-DOS data</h4>
<p>These data let you remember the first days of developing the Windows Operating System. You were at the beginning of a way to achieve a complete Operating System such as </p>
<p>To me, only the offset of the PE signature in the <a href="http://en.wikipedia.org/wiki/MS-DOS" target="new">MS-DOS</a> data is important, so I can use it to find the position of the <a href="http://en.wikipedia.org/wiki/Windows_NT" target="new">Windows NT</a> data. I just recommend that you take a look at Table 1, and then observe the structure of <tt>IMAGE_DOS_HEADER</tt> in the <em>&lt;winnt.h&gt;</em> header in the <em>&lt;Microsoft Visual Studio .net path&gt;\VC7\PlatformSDK\include\</em> folder or the <em>&lt;Microsoft Visual Studio 6.0 path&gt;\VC98\include\</em> folder. I do not know why the Microsoft team has forgotten to provide some comment about this structure in the <a href="http://msdn.microsoft.com/" target="new">MSDN</a> library!</p>
<pre><span class="codeKeyword">typedef</span> <span class="codeKeyword">struct</span> _IMAGE_DOS_HEADER { <span class="codeComment">// DOS .EXE header &quot;MZ&quot;</span>    WORD   e_magic;                <span class="codeComment">// Magic number</span>    WORD   e_cblp;                 <span class="codeComment">// Bytes on last page of file</span>    WORD   e_cp;                   <span class="codeComment">// Pages in file</span>    WORD   e_crlc;                 <span class="codeComment">// Relocations</span>    WORD   e_cparhdr;              <span class="codeComment">// Size of header in</span>                                   <span class="codeComment">// paragraphs</span>    WORD   e_minalloc;             <span class="codeComment">// Minimum extra paragraphs</span>                                   <span class="codeComment">// needed</span>    WORD   e_maxalloc;             <span class="codeComment">// Maximum extra paragraphs</span>                                   <span class="codeComment">// needed</span>    WORD   e_ss;                   <span class="codeComment">// Initial (relative) SS</span>                                   <span class="codeComment">// value</span>    WORD   e_sp;                   <span class="codeComment">// Initial SP value</span>    WORD   e_csum;                 <span class="codeComment">// Checksum</span>    WORD   e_ip;                   <span class="codeComment">// Initial IP value</span>    WORD   e_cs;                   <span class="codeComment">// Initial (relative) CS</span>                                   <span class="codeComment">// value</span>    WORD   e_lfarlc;               <span class="codeComment">// File address of relocation</span>                                   <span class="codeComment">// table</span>    WORD   e_ovno;                 <span class="codeComment">// Overlay number</span>    WORD   e_res[4];               <span class="codeComment">// Reserved words</span>    WORD   e_oemid;                <span class="codeComment">// OEM identifier</span>                                   <span class="codeComment">// (for e_oeminfo)</span>    WORD   e_oeminfo;              <span class="codeComment">// OEM information;</span>                                   <span class="codeComment">// e_oemid specific</span>    WORD   e_res2[10];             <span class="codeComment">// Reserved words</span>    LONG   <font color="#ff0000">e_lfanew</font>;               <span class="codeComment">// File address of the new</span>                                   <span class="codeComment">// exe header</span>  } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;</pre>
<p><tt>e_lfanew</tt> is the offset that refers to the position of the Windows NT data. I have provided a program to obtain the header information from an EXE file and to display it to you. To use the program, just try:</p>
<h4>PE Viewer</h4>
<p><img height="314" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEVIEWER1_GIF&amp;ds=20060302" width="491" alt="" /></p>
<p><img height="363" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEVIEWER2_GIF&amp;ds=20060302" width="500" alt="" /><br />(<a href="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEVIEWER2_GIF&amp;ds=20060302" target="_blank">Full Size Image</a>)</p>
<p><img height="313" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEVIEWER3_GIF&amp;ds=20060302" width="500" alt="" /><br />(<a href="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEVIEWER3_GIF&amp;ds=20060302" target="_blank">Full Size Image</a>)</p>
<p>This sample is useful for the whole of this article.</p>
<p><strong>Table 1:</strong> Portable Executable file format structure</p>
<p>
<table cellspacing="2" cellpadding="2" border="2">
<tbody>
<tr valign="top">
<td rowspan="17">MS-DOS <br />            information</td>
<td rowspan="16"><tt>IMAGE_DOS_<br />            HEADER</tt></td>
<td>DOS EXE Signature</td>
<td rowspan="16">
<pre lang="text">00000000  ASCII <font color="#008000">&quot;MZ&quot;</font>00000002  DW 009000000004  DW 000300000006  DW 000000000008  DW 00040000000A  DW 00000000000C  DW FFFF0000000E  DW 000000000010  DW 00B800000012  DW 000000000014  DW 000000000016  DW 000000000018  DW 00400000001A  DW 00000000001C  DB 00b&amp;b&amp;0000003B  DB 000000003C  DD <font color="#ff0000">000000F0</font></pre>
</td>
</tr>
<tr valign="top">
<td><tt>DOS_PartPag</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_PageCnt</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_ReloCnt</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_HdrSize</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_MinMem</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_MaxMem</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_ReloSS</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_ExeSP</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_ChkSum</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_ExeIPP</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_ReloCS</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_TablOff</tt></td>
</tr>
<tr valign="top">
<td><tt>DOS_Overlay</tt></td>
</tr>
<tr valign="top">
<td><tt>b&amp;<br />            </tt>Reserved words<tt><br />            b&amp;</tt></td>
</tr>
<tr valign="top">
<td>Offset to PE signature</td>
</tr>
<tr valign="top">
<td>MS-DOS Stub <br />            Program</td>
<td colspan="2">
<pre lang="text">00000040  ..B:..B4.C!B8\LC!<font color="#008000">This program canno</font>00000060  <font color="#008000">t be run in DOS mode.</font>...$.......</pre>
</td>
</tr>
<tr valign="top">
<td rowspan="54">Windows NT <br />            information
<p><tt>IMAGE_<br />            NT_HEADERS</tt></p>
</td>
<td>Signature</td>
<td>PE signature (PE)</td>
<td>
<pre lang="text"><font color="#ff0000">000000F0</font>  ASCII <font color="#008000">&quot;PE&quot;</font></pre>
</td>
</tr>
<tr valign="top">
<td rowspan="7"><tt>IMAGE_<br />            FILE_HEADER</tt></td>
<td><tt>Machine</tt></td>
<td rowspan="7">
<pre lang="text">000000F4  DW 014C000000F6  DW 0003000000F8  DD 3B7D8410000000FC  DD 0000000000000100  DD 0000000000000104  DW 00E000000106  DW 010F</pre>
</td>
</tr>
<tr valign="top">
<td><tt>NumberOfSections</tt></td>
</tr>
<tr valign="top">
<td><tt>TimeDateStamp</tt></td>
</tr>
<tr valign="top">
<td><tt>PointerToSymbolTable</tt></td>
</tr>
<tr valign="top">
<td><tt>NumberOfSymbols</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfOptionalHeader</tt></td>
</tr>
<tr valign="top">
<td><tt>Characteristics</tt></td>
</tr>
<tr valign="top">
<td rowspan="46"><tt>IMAGE_<br />            OPTIONAL_<br />            HEADER32</tt></td>
<td><tt>MagicNumber</tt></td>
<td rowspan="30">
<pre lang="text">00000108  DW 010B0000010A  DB 070000010B  DB 000000010C  DD 0001280000000110  DD 00009C0000000114  DD 0000000000000118  DD 000124750000011C  DD 0000100000000120  DD 0001400000000124  DD 0100000000000128  DD 000010000000012C  DD 0000020000000130  DW 000500000132  DW 000100000134  DW 000500000136  DW 000100000138  DW 00040000013A  DW 00000000013C  DD 0000000000000140  DD 0001F00000000144  DD 0000040000000148  DD 0001D7FC0000014C  DW 00020000014E  DW 800000000150  DD 0004000000000154  DD 0000100000000158  DD 001000000000015C  DD 0000100000000160  DD 0000000000000164  DD 00000010</pre>
</td>
</tr>
<tr valign="top">
<td><tt>MajorLinkerVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>MinorLinkerVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfCode</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfInitializedData</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfUninitializedData</tt></td>
</tr>
<tr valign="top">
<td><tt>AddressOfEntryPoint</tt></td>
</tr>
<tr valign="top">
<td><tt>BaseOfCode</tt></td>
</tr>
<tr valign="top">
<td><tt>BaseOfData</tt></td>
</tr>
<tr valign="top">
<td><tt>ImageBase</tt></td>
</tr>
<tr valign="top">
<td><tt>SectionAlignment</tt></td>
</tr>
<tr valign="top">
<td><tt>FileAlignment</tt></td>
</tr>
<tr valign="top">
<td><tt>MajorOSVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>MinorOSVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>MajorImageVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>MinorImageVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>MajorSubsystemVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>MinorSubsystemVersion</tt></td>
</tr>
<tr valign="top">
<td><tt>Reserved</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfImage</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfHeaders</tt></td>
</tr>
<tr valign="top">
<td><tt>CheckSum</tt></td>
</tr>
<tr valign="top">
<td><tt>Subsystem</tt></td>
</tr>
<tr valign="top">
<td><tt>DLLCharacteristics</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfStackReserve</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfStackCommit</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfHeapReserve</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfHeapCommit</tt></td>
</tr>
<tr valign="top">
<td><tt>LoaderFlags</tt></td>
</tr>
<tr valign="top">
<td><tt>NumberOfRvaAndSizes</tt></td>
</tr>
<tr valign="top">
<td rowspan="16"><tt>IMAGE_<br />            DATA_DIRECTORY[16]</tt></td>
<td>Export Table</td>
</tr>
<tr valign="top">
<td>Import Table</td>
</tr>
<tr valign="top">
<td>Resource Table</td>
</tr>
<tr valign="top">
<td>Exception Table</td>
</tr>
<tr valign="top">
<td>Certificate File</td>
</tr>
<tr valign="top">
<td>Relocation Table</td>
</tr>
<tr valign="top">
<td><a href="http://www.donevii.com/post/tag/debug" class="st_tag internal_tag" rel="tag" title="Posts tagged with debug">Debug</a> Data</td>
</tr>
<tr valign="top">
<td>Architecture Data</td>
</tr>
<tr valign="top">
<td>Global Ptr</td>
</tr>
<tr valign="top">
<td>TLS Table</td>
</tr>
<tr valign="top">
<td>Load Config Table</td>
</tr>
<tr valign="top">
<td>Bound Import Table</td>
</tr>
<tr valign="top">
<td>Import Address Table</td>
</tr>
<tr valign="top">
<td>Delay Import Descriptor</td>
</tr>
<tr valign="top">
<td>COM+ Runtime Header</td>
</tr>
<tr valign="top">
<td>Reserved</td>
</tr>
<tr valign="top">
<td rowspan="13">Sections <br />            information</td>
<td rowspan="10"><tt>IMAGE_<br />            SECTION_<br />            HEADER[0]</tt></td>
<td><tt>Name[8]</tt></td>
<td rowspan="10">
<pre lang="text">000001E8  ASCII<font color="#008000">&quot;.text&quot;</font>000001F0  DD 000126B0000001F4  DD 00001000000001F8  DD 00012800000001FC  DD 0000040000000200  DD 0000000000000204  DD 0000000000000208  DW 00000000020A  DW 00000000020C  DD 60000020    CODE|EXECUTE|READ</pre>
</td>
</tr>
<tr valign="top">
<td><tt>VirtualSize</tt></td>
</tr>
<tr valign="top">
<td><tt>VirtualAddress</tt></td>
</tr>
<tr valign="top">
<td><tt>SizeOfRawData</tt></td>
</tr>
<tr valign="top">
<td><tt>PointerToRawData</tt></td>
</tr>
<tr valign="top">
<td><tt>PointerToRelocations</tt></td>
</tr>
<tr valign="top">
<td><tt>PointerToLineNumbers</tt></td>
</tr>
<tr valign="top">
<td><tt>NumberOfRelocations</tt></td>
</tr>
<tr valign="top">
<td><tt>NumberOfLineNumbers</tt></td>
</tr>
<tr valign="top">
<td><tt>Characteristics</tt></td>
</tr>
<tr valign="top">
<td><tt>b&amp;<br />            b&amp;<br />            b&amp;<br />            IMAGE_<br />            SECTION_<br />            HEADER[n]</tt></td>
<td colspan="2">
<pre lang="text">00000210  ASCII<font color="#008000">&quot;.data&quot;</font>; SECTION00000218  DD 0000101C ; VirtualSize = 0x101C0000021C  DD 00014000 ; VirtualAddress = 0x1400000000220  DD 00000A00 ; SizeOfRawData = 0xA0000000224  DD 00012C00 ; PointerToRawData = 0x12C0000000228  DD 00000000 ; PointerToRelocations = 0x00000022C  DD 00000000 ; PointerToLineNumbers = 0x000000230  DW 0000     ; NumberOfRelocations = 0x000000232  DW 0000     ; NumberOfLineNumbers = 0x000000234  DD C0000040 ; Characteristics =                        INITIALIZED_DATA|READ|WRITE00000238  ASCII<font color="#008000">&quot;.rsrc&quot;</font>; SECTION00000240  DD 00008960 ; VirtualSize = 0x896000000244  DD 00016000 ; VirtualAddress = 0x1600000000248  DD 00008A00 ; SizeOfRawData = 0x8A000000024C  DD 00013600 ; PointerToRawData = 0x1360000000250  DD 00000000 ; PointerToRelocations = 0x000000254  DD 00000000 ; PointerToLineNumbers = 0x000000258  DW 0000     ; NumberOfRelocations = 0x00000025A  DW 0000     ; NumberOfLineNumbers = 0x00000025C  DD 40000040 ; Characteristics =                        INITIALIZED_DATA|READ</pre>
</td>
</tr>
<tr valign="top">
<td><tt>SECTION[0]</tt></td>
<td colspan="2">
<pre lang="text">00000400  EA 22 DD 77 D7 23 DD 77  C*&quot;C.wC.#C.w00000408  9A 18 DD 77 00 00 00 00  E!.C.w....00000410  2E 1E C7 77 83 1D C7 77  ..C.wF..C.w00000418  FF 1E C7 77 00 00 00 00  C?.C.w....00000420  93 9F E7 77 D8 05 E8 77  b.E8C'wC..C(w00000428  FD A5 E7 77 AD A9 E9 77  C=B%C'w&amp;shy;B)C)w00000430  A3 36 E7 77 03 38 E7 77  B#6C'w.8C'w00000438  41 E3 E6 77 60 8D E7 77  AC#C&amp;w`BC'w00000440  E6 1B E6 77 2B 2A E7 77  C&amp;.C&amp;w+*C'w00000448  7A 17 E6 77 79 C8 E6 77  z.C&amp;wyC.C&amp;w00000450  14 1B E7 77 C1 30 E7 77  ..C'wC.0C'wb&amp;</pre>
</td>
</tr>
<tr valign="top">
<td><tt>b&amp;<br />            b&amp;<br />            b&amp;<br />            SECTION[n]</tt></td>
<td colspan="2">
<pre lang="text">b&amp;0001BF00  63 00 2E 00 63 00 68 00  c...c.h.0001BF08  6D 00 0A 00 43 00 61 00  m...C.a.0001BF10  6C 00 63 00 75 00 6C 00  l.c.u.l.0001BF18  61 00 74 00 6F 00 72 00  a.t.o.r.0001BF20  11 00 4E 00 6F 00 74 00  ..N.o.t.0001BF28  20 00 45 00 6E 00 6F 00   .E.n.o.0001BF30  75 00 67 00 68 00 20 00  u.g.h. .0001BF38  4D 00 65 00 6D 00 6F 00  M.e.m.o.0001BF40  72 00 79 00 00 00 00 00  r.y.....0001BF48  00 00 00 00 00 00 00 00  ........0001BF50  00 00 00 00 00 00 00 00  ........0001BF58  00 00 00 00 00 00 00 00  ........0001BF60  00 00 00 00 00 00 00 00  ........0001BF68  00 00 00 00 00 00 00 00  ........0001BF70  00 00 00 00 00 00 00 00  ........0001BF78  00 00 00 00 00 00 00 00  ........</pre>
</td>
</tr>
</tbody>
</table>
<h4>2.2 The Windows NT data</h4>
<p>As mentioned in the preceding section, <tt>e_lfanew</tt> storage in the MS-DOS data structure refers to the location of the Windows NT information. Hence, if you assume that the <tt>pMem</tt> pointer relates the start point of the memory space for a selected portable executable file, you can retrieve the MS-DOS header and also the Windows NT headers by the following lines, which you also can perceive in the PE viewer sample (<em>pelib.cpp</em>, <tt>PEStructure::OpenFileName()</tt>):</p>
<pre>IMAGE_DOS_HEADER        image_dos_header;IMAGE_NT_HEADERS        image_nt_headers;PCHAR pMem;b&amp;memcpy(&amp;image_dos_header, pMem,       <span class="codeKeyword">sizeof</span>(IMAGE_DOS_HEADER));memcpy(&amp;image_nt_headers,       pMem+image_dos_header.e_lfanew,       <span class="codeKeyword">sizeof</span>(IMAGE_NT_HEADERS));</pre>
<p><a name="more"><font color="#000000"></font></a><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_nt_headers_str.asp" target="new"><tt>IMAGE_NT_HEADERS</tt></a> structure definition. It makes it possible to grasp what the image NT header maintains to execute a code inside the Windows NT OS. Now, you are conversant with the Windows NT structure; it consists of the <font color="#008000">&quot;PE&quot;</font> Signature, the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_file_header_str.asp" target="new">File Header</a>, and the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_optional_header_str.asp" target="new">Optional Header</a>. Do not forget to take a glimpse at their comments in the <a href="http://msdn.microsoft.com/" target="new">MSDN</a> Library and in Table 1.</p>
<p>It seems to be very simple, the retrieval of the headers information. I recommend inspecting the MSDN library regarding the </p>
<p>One the whole, I consider merely, in most circumstances, the following cells of the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_nt_headers_str.asp" target="new"><tt>IMAGE_NT_HEADERS</tt></a> structure:</p>
<pre>FileHeader-&gt;NumberOfSectionsOptionalHeader-&gt;AddressOfEntryPointOptionalHeader-&gt;ImageBaseOptionalHeader-&gt;SectionAlignmentOptionalHeader-&gt;FileAlignmentOptionalHeader-&gt;SizeOfImageOptionalHeader-&gt;DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]              -&gt;VirtualAddressOptionalHeader-&gt;DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]              -&gt;Size</pre>
<p>You can observe the main purpose of these values clearly, and their role when the internal virtual memory space allocated for an EXE file by the Windows task manager if you pay attention to their explanations in <a href="http://msdn.microsoft.com/" target="new">MSDN</a> library, so I am not going to repeat the MSDN annotations here.</p>
<p>I should make a brief comment regarding the PE data directories, or <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_optional_header_str.asp" target="new"><tt>OptionalHeader</tt></a>-&gt; <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_data_directory_str.asp" target="new"><tt>DataDirectory[]</tt></a>, because I think there are a few aspects of interest concerning them. When you come to survey the Optional header through the Windows NT information, you will find that there are <em>16</em> directories at the end of the Optional Header, where you can find the consecutive directories, including their Relative Virtual Address and Size. I just mention here the notes from <em>&lt;winnt.h&gt;</em> to clarify these information:</p>
<pre><span class="codeComment">// Export Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_EXPORT          0<span class="codeComment">// Import Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_IMPORT          1<span class="codeComment">// Resource Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_RESOURCE        2<span class="codeComment">// Exception Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_EXCEPTION       3<span class="codeComment">// Security Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_SECURITY        4<span class="codeComment">// Base Relocation Table</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_BASERELOC       5<span class="codeComment">// Debug Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_DEBUG           6<span class="codeComment">// Architecture Specific Data</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_ARCHITECTURE    7<span class="codeComment">// RVA of GP</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_GLOBALPTR       8<span class="codeComment">// TLS Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_TLS             9<span class="codeComment">// Load Configuration Directory</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG    10<span class="codeComment">// Bound Import Directory in headers</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT   11<span class="codeComment">// Import Address Table</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_IAT            12<span class="codeComment">// Delay Load Import Descriptors</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT   13<span class="codeComment">// COM Runtime descriptor</span><span class="codeKeyword">#define</span> IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14</pre>
<p>The last one (15) was reserved for use in the future; I have not yet seen any purpose for it, even in PE64.</p>
<p>For instance, if you want to perceive the relative virtual address (RVA) and the size of the resource data, it is enough to retrieve them by:</p>
<pre>DWORD dwRVA  = image_nt_headers.OptionalHeader-&gt;   DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE]-&gt;VirtualAddress;DWORD dwSize = image_nt_headers.OptionalHeader-&gt;   DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE]-&gt;Size;</pre>
<p>To comprehend more regarding the significance of data directories, I forward you to Section 3.4.3 of the <a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx" target="new">Microsoft Portable Executable and the Common Object File Format Specification</a> document by Microsoft, and furthermore Section 6 of this document, where you discern the various types of sections and their applications. You will see the section&#8217;s advantage subsequently.</p>
<h4>2.3 The Section Headers and Sections</h4>
<p>You currently observe how the portable executable files declare the location and the size of a section on a disk storage file and inside the virtual memory space allocated for the program with <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_nt_headers_str.asp" target="new"><tt>IMAGE_NT_HEADERS</tt></a>-&gt; <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_optional_header_str.asp" target="new"><tt>OptionalHeader</tt></a>-&gt;<tt>SizeOfImage</tt> by the Windows task manager, as well the characteristics to demonstrate the type of the section. To better understand the Section header as my previous declaration, I suggest having a brief look at the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_section_header_str.asp" target="new"><tt>IMAGE_SECTION_HEADER</tt></a> structure definition in the MSDN library. For an EXE packer developer, <tt>VirtualSize</tt>, <tt>VirtualAddress</tt>, <tt>SizeOfRawData</tt>, <tt>PointerToRawData</tt>, and <tt>Characteristics</tt> cells have significant rules. When developing an EXE packer, you should be clever enough to play with them. There are somet hings to note when you modify them; you should take care to align the <tt>VirtualSize</tt> and <tt>VirtualAddress</tt> according to <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_optional_header_str.asp" target="new"><tt>OptionalHeader</tt></a>-&gt;<tt>SectionAlignment</tt>, as well as <tt>SizeOfRawData</tt> and <tt>PointerToRawData</tt> in line with <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_optional_header_str.asp" target="new"><tt>OptionalHeader</tt></a>-&gt;<tt>FileAlignment</tt>. Otherwise, you will corrupt your target EXE file and it will never run. Regarding <tt>Characteristics</tt>, I pay attention mostly to establish a section by <tt>IMAGE_SCN_MEM_READ</tt> | <tt>IMAGE_SCN_MEM_WRITE</tt> | <tt>IMAGE_SCN_CNT_INITIALIZED_DATA</tt>, I prefer that my new section has the ability to initialize such data during the running process, such as import table; besides, I need it to be able to modify itself by the loader with my settings in the section characteristics to read- and writeable.</p>
<p>Moreover, you should pay attention to the section names; you can know the purpose of each section by its name. I will just forward you to Section 6 of the <a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx" target="new">Microsoft Portable Executable and the Common Object File Format Specification</a> documents. I believe it represents the totality of sections by their names; this is also included in Table 2.</p>
<p><strong>Table 2:</strong> Section names</p>
<p>
<table cellspacing="2" cellpadding="2" border="2">
<tbody>
<tr>
<td><font color="#008000">&quot;.text&quot;</font></td>
<td>Code Section</td>
</tr>
<tr>
<td><font color="#008000">&quot;CODE&quot;</font></td>
<td>Code Section of file linked by Borland Delphi or Borland Pascal</td>
</tr>
<tr>
<td><font color="#008000">&quot;.data&quot;</font></td>
<td>Data Section</td>
</tr>
<tr>
<td><font color="#008000">&quot;DATA&quot;</font></td>
<td>Data Section of file linked by Borland Delphi or Borland Pascal</td>
</tr>
<tr>
<td><font color="#008000">&quot;.rdata&quot;</font></td>
<td>Section for Constant Data </td>
</tr>
<tr>
<td><font color="#008000">&quot;.idata&quot;</font></td>
<td>Import Table</td>
</tr>
<tr>
<td><font color="#008000">&quot;.edata&quot; </font></td>
<td>Export Table</td>
</tr>
<tr>
<td><font color="#008000">&quot;.tls&quot;</font></td>
<td>TLS Table</td>
</tr>
<tr>
<td><font color="#008000">&quot;.reloc&quot;</font></td>
<td>Relocation Information</td>
</tr>
<tr>
<td><font color="#008000">&quot;.rsrc&quot;</font></td>
<td>Resource Information</td>
</tr>
</tbody>
</table>
<p>To comprehend the section headers and also the sections, you can run the sample PE viewer. With this PE viewer, you can realize only the application of the section headers in a file image, so to observe the main significance in the Virtual Memory, you should try to load a PE file by a debugger. The next section represents the main idea of using the virtual address and size in the virtual memory by using a debugger. The last note is about <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_nt_headers_str.asp" target="new"><tt>IMAGE_NT_HEADERS</tt></a>-&gt; <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/image_file_header_str.asp" target="new"><tt>FileHeader</tt></a>-&gt;<tt>NumberOfSections</tt>, that provides a number of sections in a PE file. Do not forget to adjust it whenever you remove or add some sections to a PE file. I am talking about section injection!</p>
<h3>3 Debugger, Disassembler and some Useful Tools</h3>
<p>In this part, you will become familiar with the necessary and essential equipment to develop your PE tools.</p>
<h4>3.1 Debuggers</h4>
<p>The first essential prerequisite to become a PE tools developer is to have enough experience with bug tracer tools. Furthermore, you should know most of the assembly instructions. To me, the Intel documents are the best references. You can obtain them from the Intel site for IA-32, and on top of that IA-64; the future belongs to IA-64 CPUs, Windows XP 64-bit, and also PE64!</p>
<ul>
<li><a href="http://www.intel.com/design/pentium4/manuals/index_new.htm#1" target="new">IA-32 Intel Architecture Software Developer&#8217;s Manuals</a> </li>
<li><a href="http://www.intel.com/software/products/compilers/docs/linux/ref/asm_lan_lx.htm#cover.htm" target="new">Intel Itanium Architecture Assembly Language Reference Guide</a> </li>
<li><a href="http://www.intel.com/cd/ids/developer/asmo-na/eng/19415.htm" target="new">The Intel Itanium Processor Developer Resource Guide</a> </li>
</ul>
<p>To trace a PE file, <a href="http://en.wikipedia.org/wiki/SoftICE" target="new">SoftICE</a> by <a href="http://www.compuware.com/" target="new">Compuware Corporation</a>, I knew it also as named <a href="http://en.wikipedia.org/wiki/Numega" target="new">NuMega</a> when I was at high school, is the best <a href="http://en.wikipedia.org/wiki/Debugger" target="new">debugger</a> in the world. It implements process tracing by using the <a href="http://en.wikipedia.org/wiki/Kernel_mode" target="new">kernel mode</a> method debugging without applying Windows debugging <a href="http://en.wikipedia.org/wiki/Application_programming_interface" target="new">application programming interface</a> (API) functions. In addition, I will introduce one perfect debugger in <a href="http://en.wikipedia.org/wiki/User_mode" target="new">user mode</a> level. It utilizes the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debugging_reference.asp" target="new">Windows debugging API</a> to trace a PE file and also attaches itself to an active <a href="http://en.wikipedia.org/wiki/Computer_process" target="new">process</a>. These <a href="http://en.wikipedia.org/wiki/Application_programming_interface" target="new">API</a> functions have been provided by Microsoft teams, inside the Windows Kernel32 library, to trace a specific process, by using Microsoft tools, or perhaps, to make your own debugger! Some of those <a href="http://en.wikipedia.org/wiki/Application_programming_interface" target="new">API</a> functions inlude:</p>
<ul><tt>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createthread.asp" target="new">CreateThread()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp" target="new">CreateProcess()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/openprocess.asp" target="new">OpenProcess()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debugactiveprocess.asp" target="new">DebugActiveProcess()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/getthreadcontext.asp" target="new">GetThreadContext()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/setthreadcontext.asp" target="new">SetThreadContext()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/continuedebugevent.asp" target="new">ContinueDebugEvent()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debugbreak.asp" target="new">DebugBreak()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/readprocessmemory.asp" target="new">ReadProcessMemory()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/writeprocessmemory.asp" target="new">WriteProcessMemory()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/suspendthread.asp" target="new">SuspendThread()</a> </li>
<li><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/resumethread.asp" target="new">ResumeThread()</a> </li>
<p>    </tt></ul>
<h5>3.1.1 SoftICE</h5>
<p>It was in 1987; Frank Grossman and Jim Moskun decided to establish a company called <a href="http://en.wikipedia.org/wiki/Numega" target="new">NuMega Technologies</a> in Nashua, NH, to develop some equipment to trace and test the reliability of Microsoft Windows software programs. Now, it is a part of <a href="http://en.wikipedia.org/wiki/Compuware" target="new">Compuware Corporation</a> and its product has participated to accelerate the reliability in Windows software, and additionally in Windows driver developments. Currently, everyone knows the Compuware DriverStudio that is used to establish an environment for implementing the elaboration of a kernel driver or a system file by aiding the <a href="http://www.microsoft.com/whdc/ddk/winddk.mspx" target="new">Windows Driver Development Kit (DDK)</a>. It bypasses the involvement of DDK to implement a portable executable file of kernel level for a Windows system software developer. For us, only one instrument of DriverStudio is important, <a href="http://en.wikipedia.org/wiki/SoftICE" target="new">SoftICE</a>; this debugger can be used to trace every portable executable file, a PE file for user mode level or a PE file for kernel mode level.</p>
<p><strong>Figure 1:</strong> SoftICE Window</p>
<p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody bgcolor="#000000" color="gray">
<tr>
<td><font color="#808080"><font color="#00ccff">EAX=00000000</font>EBX=7FFDD000<font color="#00ccff"> ECX=0007FFB0 EDX=7C90EB94</font> ESI=FFFFFFFF EDI=7C919738 <font color="#00ccff">EBP=0007FFF0 ESP=0007FFC4 EIP=010119E0</font> o d i s <font color="#00ccff">z </font>a <font color="#00ccff">p</font> c<br />                CS=0008 DS=0023 SS=0010 ES=0023 FS=0030 GS=0000</font> <font color="#00ccff">SS:0007FFC4=87C816D4F</font></td>
</tr>
<tr>
<td><font color="#808080">0023:01013000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;. 0023:01013010 01 00 00 00 20 00 00 00-0A 00 00 00 0A 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;. 0023:01013020 20 00 00 00 00 00 00 00-53 63 69 43 61 6C 63 00 &#8230;&#8230;..SciCalc. 0023:01013030 00 00 00 00 00 00 00 00-62 61 63 6B 67 72 6F 75 &#8230;&#8230;..backgrou 0023:01013040 6E 64 00 00 00 00 00 00-2E 00 00 00 00 00 00 00 nd&#8230;&#8230;&#8230;&#8230;..</font></td>
</tr>
<tr>
<td><font color="#808080">0010:0007FFC4 4F 6D 81 7C 38 07 91 7C-FF FF FF FF 00 90 FD 7F Om |8 b.| . 0010:0007FFD4 ED A6 54 80 C8 FF 07 00-E8 B4 F5 81 FF FF FF FF T . 0010:0007FFE4 F3 99 83 7C 58 6D 81 7C-00 00 00 00 00 00 00 00 Xm |&#8230;&#8230;.. 0010:0007FFF4 00 00 00 00 E0 19 01 01-00 00 00 00 00 00 00 00 &#8230;. &#8230;.</font></td>
</tr>
<tr>
<td><font color="#808080"><font color="#00ccff">010119E0 PUSH EBP</font> 010119E1 MOV EBP,ESP 010119E3 PUSH -1 010119E5 PUSH 01001570 010119EA PUSH 01011D60 010119EF MOV EAX,DWORD PTR FS:[0] 010119F5 PUSH EAX 010119F6 MOV DWORD PTR FS:[0],ESP 010119FD ADD ESP,-68 01011A00 PUSH EBX 01011A01 PUSH ESI 01011A02 PUSH EDI 01011A03 MOV DWORD PTR SS:[EBP-18],ESP 01011A06 MOV DWORD PTR SS:[EBP-4],0</font></td>
</tr>
<tr>
<td><font color="#808080">:_</font><font color="#808080"></p>
<p>                </font></td>
</tr>
</tbody>
</table>
<h5>3.1.2 OllyDbg</h5>
<p>It was about four years ago that I first saw this debugger by chance. For me, it was the best choice; I was not wealthy enough to purchase SoftICE, and at that time, SoftICE only had good functions for <a href="http://en.wikipedia.org/wiki/DOS" target="new">DOS</a>, <a href="http://en.wikipedia.org/wiki/Windows_98" target="new">Windows 98</a>, and <a href="http://en.wikipedia.org/wiki/Windows_2000" target="new">Windows 2000</a>. I found that this debugger supported all kinds of Windows versions. Therefore, I started to learn it very fast, and now it is my favorite debugger for the Windows OS. It is a debugger that can be used to trace all kinds of portable executable files except a <a href="http://en.wikipedia.org/wiki/Common_Language_Infrastructure" target="new">Common Language Infrastructure (CLI)</a> file format in user mode level, by using the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debugging_reference.asp" target="new">Windows debugging API</a>. <strong>Oleh Yuschuk</strong>, the author, is one of worthiest software developers I have seen in my life. He is a Ukrainian who now lives in Germany. I should mention here that his debugger is the best choice for hacker and cracker parties around the world! It is freeware! You can try it from the <a href="http://www.ollydbg.de/" target="new">OllyDbg Homepage</a>.</p>
<p>    <a name="more"><font color="#000000">&nbsp;</font>
<p><strong>Figure 2:</strong> OllyDbg CPU Window</p>
<p><img height="452" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=SCREENSHOT_JPG&amp;ds=20060302" width="500" alt="" /><br />    (</p>
<h5>3.1.3 Which parts are important in a debugger interface?</h5>
<p>I have introduced two debuggers without talking about how you can employ them, and also which parts you should pay attention to. Regarding using debuggers, I refer you to their instructions in help documents. However, I want to explain briefly the important parts of a debugger; of course, I am talking about low-level debuggers, or in other words, machine-language debuggers of the x86 CPU families.</p>
<p>All of low-level debuggers consist of the following subdivisions:</p>
<ol>
<li>Registers viewer.<br />
<table cellspacing="2" cellpadding="2" border="2">
<tbody>
<tr>
<td align="center"><font color="#808080">EAX</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">ECX</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">EDX</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">EBX</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">ESP</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">EBP</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">ESI</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">EDI</font></td>
</tr>
<tr>
<td align="center"><font color="#808080">EIP</font></td>
</tr>
<tr>
<td>
<p align="center"><font color="#808080">o</font><font color="#808080"> d t s z a p c</font></p>
</td>
</tr>
</tbody>
</table>
</li>
<li>Disassembler or Code viewer.<br />
<table cellspacing="2" cellpadding="2" border="2">
<tbody>
<tr>
<td>
<pre>010119E0 PUSH EBP010119E1 MOV EBP,ESP010119E3 PUSH -1010119E5 PUSH 01001570010119EA PUSH 01011D60010119EF MOV EAX,DWORD PTR FS:[0]010119F5 PUSH EAX010119F6 MOV DWORD PTR FS:[0],ESP010119FD ADD ESP,-6801011A00 PUSH EBX01011A01 PUSH ESI01011A02 PUSH EDI01011A03 MOV DWORD PTR SS:[EBP-18],ESP01011A06 MOV DWORD PTR SS:[EBP-4],0</pre>
</td>
</tr>
</tbody>
</table>
</li>
<li>Memory watcher.<br />
<table cellspacing="0" cellpadding="0" width="560" border="1">
<tbody>
<tr>
<td><font color="#808080">0023:01013000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;. 0023:01013010 01 00 00 00 20 00 00 00-0A 00 00 00 0A 00 00 00 &#8230;&#8230;&#8230;&#8230;&#8230;. 0023:01013020 20 00 00 00 00 00 00 00-53 63 69 43 61 6C 63 00 &#8230;&#8230;..SciCalc. 0023:01013030 00 00 00 00 00 00 00 00-62 61 63 6B 67 72 6F 75 &#8230;&#8230;..backgrou 0023:01013040 6E 64 00 00 00 00 00 00-2E 00 00 00 00 00 00 00 nd&#8230;&#8230;&#8230;&#8230;..</font></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
</li>
<li>Stack viewer.<br />
<table cellspacing="0" cellpadding="0" width="560" border="1">
<tbody>
<tr>
<td><font color="#808080">0010:0007FFC4 4F 6D 81 7C 38 07 91 7C-FF FF FF FF 00 90 FD 7F Om |8 b.| . 0010:0007FFD4 ED A6 54 80 C8 FF 07 00-E8 B4 F5 81 FF FF FF FF T . 0010:0007FFE4 F3 99 83 7C 58 6D 81 7C-00 00 00 00 00 00 00 00 Xm |&#8230;&#8230;.. 0010:0007FFF4 00 00 00 00 E0 19 01 01-00 00 00 00 00 00 00 00 &#8230;. &#8230;.</font></td>
</tr>
</tbody>
</table>
</li>
<li>Command line, command buttons, or shortcut keys to follow the debugging process.<br />
<table cellspacing="0" cellpadding="0" width="560" border="1">
<tbody>
<tr>
<td align="center">Command</td>
<td align="center">SoftICE</td>
<td align="center">OllyDbg</td>
</tr>
<tr>
<td align="center">Run</td>
<td align="center">F5</td>
<td align="center">F9</td>
</tr>
<tr>
<td align="center">Step Into</td>
<td align="center">F11</td>
<td align="center">F7</td>
</tr>
<tr>
<td align="center">Step Over</td>
<td align="center">F10</td>
<td align="center">F8</td>
</tr>
<tr>
<td align="center">Set Break Point</td>
<td align="center">F8</td>
<td align="center">F2</td>
</tr>
</tbody>
</table>
</li>
</ol>
<p>You can compare Figures 1 and 2 to distinguish the difference between SoftICE and OllyDbg. When you want to trace a PE file, you should mostly consider these five subdivisions. Furthermore, every debugger comprises of some other useful parts; you should discover them by yourself.</p>
<h4>3.2 Disassembler</h4>
<p>You can consider OllyDbg and SoftICE to be excellent disassemblers, but I also want to introduce another disassembler tool that is famous in the reverse engineering world.</p>
<h5>3.2.1 Proview disassembler</h5>
<p><a href="http://community.reverse-engineering.net/viewforum.php?f=50&amp;sid=a77c210bc1030dd395452bb7e1f67439" target="new">Proview</a> or <a href="http://pvdasm.reverse-engineering.net/" target="new" class="broken_link">PVDasm</a> is an admirable disassembler by the <a href="http://community.reverse-engineering.net/" target="new" class="broken_link">Reverse-Engineering-Community</a>; it is still under development and bug fixing. You can find its disassmbler source engine and employ it to create your own disassembler.</p>
<h5>3.2.2 W32Dasm</h5>
<p><a href="http://www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasemblers/WDASM.shtml" target="new">W32DASM</a> can disassemble both 16- and 32-bit executable file formats. In addition to its disassembling ability, you can employ it to analyze import, export, and resource data directories data.</p>
<h5>3.2.3 IDA Pro</h5>
<p>All reverse-engineering experts know that <a href="http://www.datarescue.com/idabase/idaproc.htm" target="new" class="broken_link">IDA Pro</a> can be used to investigate, not only x86 instructions, but that of various kinds of CPU types like AVR, PIC, and so forth. It can illustrate the assembly source of a portable executable file by using colored graphics and tables, and is very useful for any newbie in this area. Furthermore, it has the capability to trace an executable file inside the user mode level in the same way as OllyDbg.</p>
<h4>3.3 Some Useful Tools</h4>
<p>A good PE tools developer is conversant with the tools that save his time, so I recommend that you select some appropriate instruments to investigate the base information under a portable executable file.</p>
<h5>3.3.1 LordPE</h5>
<p><a href="http://www.softpedia.com/get/Programming/File-Editors/LordPE.shtml" target="new" class="broken_link">LordPE</a> by <a href="http://scifi.pages.at/yoda9k/aboutme.htm" target="new">y0da</a> is still the first choice to retrieve PE file information with the possibility to modify them.</p>
<p><img height="206" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=LORDPE_GIF&amp;ds=20060302" width="441" alt="" /></p>
<h5>3.3.2 PEiD</h5>
<p><a href="http://peid.has.it/" target="new">PE iDentifier</a> is valuable to identify the type of compilers, packers, and cryptors of PE files. As of now, it can detect more than 500 different signature types of PE files.</p>
<p><img height="166" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=PEID_GIF&amp;ds=20060302" width="296" alt="" /></p>
<h5>3.3.3 Resource Hacker</h5>
<p><a href="http://www.angusj.com/resourcehacker/" target="new">Resource Hacker </a>can be employed to modify resource directory information; icon, menu, version info, string table, and so on.</p>
<p><img height="141" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=RESOURCEHACKER_GIF&amp;ds=20060302" width="191" alt="" /></p>
<h5>3.3.4 WinHex</h5>
<p><a href="http://www.winhex.com/winhex/index-m.html" target="new">WinHex</a>, it is clear what you can do with this tool.</p>
<p><img height="230" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=WINHEX_GIF&amp;ds=20060302" width="329" alt="" /></p>
<h5>3.3.5 CFF Explorer</h5>
<p>Eventually, <a href="http://www.pmode.net/CFF.php" target="new" class="broken_link">CFF Explorer </a>by <a href="http://www.pmode.net/USERS/116/UserInfo.xml" target="new" class="broken_link">Ntoskrnl </a>is what you want to have as a PE Utility tool in your arsenal; it supports PE32/64, PE rebuild included <a href="http://en.wikipedia.org/wiki/Common_Language_Infrastructure" target="new">Common Language Infrastructure (CLI)</a> file. In other words, the <a href="http://en.wikipedia.org/wiki/Microsoft_.NET" target="new">.NET file</a>, a resource modifier, and much more facilities which can not be found in others. Just try to discover every unimaginable option by hand.</p>
<p><img height="217" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=CFFEXPLORER_GIF&amp;ds=20060302" width="301" alt="" /></p>
<h3>4 Add a New Section and Change the OEP</h3>
<p>You are ready to do the first step of making your project. I have provided a library to add a new section and rebuild the portable executable file. Before starting, I wnat you to get familiar with the headers of a PE file, by using <a href="http://www.ollydbg.de/" target="new">OllyDbg</a>. You should first open a PE file; that pops up a menu, <strong>View-&gt;Executable file</strong>. Again, you get a popup menu: <strong>Special-&gt;PE header</strong>. You will observe a scene similar to Figure 3. Now, come to the Main Menu <strong>View-&gt;Memory</strong>, and try to distinguish the sections inside the <strong>Memory map</strong> window.</p>
<h4>Figure 3</h4>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td><font color="#808080">
<pre>00000000000000020000000400000006000000080000000A0000000C0000000E00000010000000120000001400000016000000180000001A0000001C0000001D0000001E0000001F000000200000002100000022000000230000002400000025000000260000002700000028000000290000002A0000002B0000002C0000002D0000002E0000002F000000300000003100000032000000330000003400000035000000360000003700000038000000390000003A0000003B0000003C</pre>
<p>                </font></td>
<td>
<pre> 4D 5A 9000 0300 0000 0400 0000 FFFF 0000 B800 0000 0000 0000 4000 0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0000000</pre>
</td>
<td>
<pre> ASCII <font color="#008000">&quot;MZ&quot;</font> DW 0090 DW 0003 DW 0000 DW 0004 DW 0000 DW FFFF DW 0000 DW 00B8 DW 0000 DW 0000 DW 0000 DW 0040 DW 0000 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DB 00 DD <font color="#ff0000">000000F0</font></pre>
</td>
<td>
<pre> DOS EXE Signature DOS_PartPag = 90 (144.) DOS_PageCnt = 3 DOS_ReloCnt = 0 DOS_HdrSize = 4 DOS_MinMem = 0 DOS_MaxMem = FFFF (65535.) DOS_ReloSS = 0 DOS_ExeSP = B8 DOS_ChkSum = 0 DOS_ExeIP = 0 DOS_ReloCS = 0 DOS_TablOff = 40 DOS_Overlay = 0 Offset to PE signature</pre>
</td>
</tr>
</tbody>
</table>
<p>    <a name="more"><font color="#000000">&nbsp;</font>
<p>I want to explain how you can plainly change the Offset of Entry Point (OEP) in your sample file, <em>CALC.EXE</em> of Windows XP. First, by using a PE Tool, and also using your PE Viewer, you find OEP, <tt>0x00012475</tt>, and Image Base, <tt>0x01000000</tt>. This value of OEP is the Relative Virtual Address, so the Image Base value is used to convert it to the Virtual Address.</p>
<table cellspacing="0" cellpadding="0" width="450" border="1">
<tbody>
<tr>
<td>
<p><strong>Virtual_Address = Image_Base + Relative_Virtual_Address</strong></p>
</td>
</tr>
</tbody>
</table>
<pre>DWORD OEP_RVA = image_nt_headers-&gt;   OptionalHeader.AddressOfEntryPoint ;<span class="codeComment">// OEP_RVA = 0x00012475</span>DWORD OEP_VA = image_nt_headers-&gt;   OptionalHeader.ImageBase + OEP_RVA ;<span class="codeComment">// OEP_VA = 0x01000000 + 0x00012475 = 0x01012475</span></pre>
<h4>PE Maker: Step 1</h4>
<p>Download pemaker1.zip and test1.zip from the files at the end of this article.</p>
<p><tt>DynLoader()</tt>, in <em>loader.cpp</em>, is reserved for the data of the new section&mdash;in other words, the <strong>Loader</strong>.</p>
<h4>DynLoader Step 1</h4>
<pre><span class="codeKeyword">__stdcall</span> <span class="codeKeyword">void</span> DynLoader(){_asm{<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_MAGIC)<span class="codeComment">//----------------------------------</span>    MOV EAX,01012475h <span class="codeComment">// &lt;&lt; Original OEP</span>    JMP EAX<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_END_MAGIC)<span class="codeComment">//----------------------------------</span>}}</pre>
<p>Unfortunately, this source can only be applied for the sample test file. You should complete it by saving the value of the original OEP in the new section, and use it to reach the real OEP. I have accomplished it in Step 2 (Section 5).</p>
<h4>4.1 Retrieve and Rebuild PE file</h4>
<p>I have made a simple class library to recover PE information and to use it in a new PE file.</p>
<h4>CPELibrary Class Step 1</h4>
<pre><span class="codeComment">//----------------------------------------------------------------</span><span class="codeKeyword">class</span> CPELibrary{<span class="codeKeyword">private</span>:    <span class="codeComment">//-----------------------------------------</span>    PCHAR                   pMem;    DWORD                   dwFileSize;    <span class="codeComment">//-----------------------------------------</span><span class="codeKeyword">protected</span>:    <span class="codeComment">//-----------------------------------------</span>    PIMAGE_DOS_HEADER       image_dos_header;    PCHAR                   pDosStub;    DWORD                   dwDosStubSize, dwDosStubOffset;    PIMAGE_NT_HEADERS       image_nt_headers;    PIMAGE_SECTION_HEADER   image_section_header[MAX_SECTION_NUM];    PCHAR                   image_section[MAX_SECTION_NUM];    <span class="codeComment">//-----------------------------------------</span><span class="codeKeyword">protected</span>:    <span class="codeComment">//-----------------------------------------</span>    DWORD PEAlign(DWORD dwTarNum,DWORD dwAlignTo);    <span class="codeKeyword">void</span> AlignmentSections();    <span class="codeComment">//-----------------------------------------</span>    DWORD Offset2RVA(DWORD dwRO);    DWORD RVA2Offset(DWORD dwRVA);    <span class="codeComment">//-----------------------------------------</span>    PIMAGE_SECTION_HEADER ImageRVA2Section(DWORD dwRVA);    PIMAGE_SECTION_HEADER ImageOffset2Section(DWORD dwRO);    <span class="codeComment">//-----------------------------------------</span>    DWORD ImageOffset2SectionNum(DWORD dwRVA);    PIMAGE_SECTION_HEADER AddNewSection(<span class="codeKeyword">char</span>* szName,DWORD dwSize);    <span class="codeComment">//-----------------------------------------</span><span class="codeKeyword">public</span>:    <span class="codeComment">//-----------------------------------------</span>    CPELibrary();    ~CPELibrary();    <span class="codeComment">//-----------------------------------------</span>    <span class="codeKeyword">void</span> OpenFile(<span class="codeKeyword">char</span>* FileName);    <span class="codeKeyword">void</span> SaveFile(<span class="codeKeyword">char</span>* FileName);    <span class="codeComment">//-----------------------------------------</span>};</pre>
<p>In Table 1, the usage of <tt>image_dos_header</tt>, <tt>pDosStub</tt>, <tt>image_nt_headers</tt>, <tt>image_section_header</tt> [<tt>MAX_SECTION_NUM</tt>], and <tt>image_section</tt>[<tt>MAX_SECTION_NUM</tt>] is clear. You use <tt>OpenFile()</tt> and <tt>SaveFile()</tt> to retrieve and rebuild a PE file. Furthermore, <tt>AddNewSection()</tt> is employed to create the new section, the important step.</p>
<p>    </a><br />
<h4>4.2 Create data for the new section</h4>
<p><a name="more"><font color="#000000"> </font></a><a href="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=LINKTIP1_GIF&amp;ds=20060302" target="_blank">Full Size Image</a>)
<p>You can comprehend the difference between incremental link and no-incremental link by looking at the following picture:</p>
<p>    <img height="130" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=INCREMENTAL_LINK_GIF&amp;ds=20060302" width="415" alt="" />
<p>To acquire the virtual address of <tt>DynLoader()</tt>, you obtain the virtual address of <tt>JMP pemaker.DynLoader</tt> in the incremental link, but by no-incremental link, the real virtual address is gained by the following code:</p>
<pre>DWORD dwVA= (DWORD) DynLoader;</pre>
<p>This setting is more critical in the incremental link when you try to find the beginning and ending of the <strong>Loader</strong>, <tt>DynLoader()</tt>, by <tt>CPECryptor::ReturnToBytePtr()</tt>:</p>
<pre><span class="codeKeyword">void</span>* CPECryptor::ReturnToBytePtr(<span class="codeKeyword">void</span>* FuncName, DWORD findstr){    <span class="codeKeyword">void</span>* tmpd;    __asm   {        mov eax, FuncName        jmp dfhjg:    inc eaxdf:     mov ebx, [eax]        cmp ebx, findstr        jnz hjg        mov tmpd, eax    }    <span class="codeKeyword">return</span> tmpd;}</pre>
</p>
<p>In <em>pecrypt.cpp</em>, I have represented another class, <tt>CPECryptor</tt>, to comprise the data of the new section. Nevertheless, the data of the new section is created by <tt>DynLoader()</tt> in <em>loader.cpp</em>, DynLoader Step 1. You use the <tt>CPECryptor</tt> class to enter this data in to the new section, and also some other stuff.</p>
<h4>CPECryptor Class Step 1</h4>
<pre><span class="codeComment">//----------------------------------------------------------------</span><span class="codeKeyword">class</span> CPECryptor: <span class="codeKeyword">public</span> CPELibrary{<span class="codeKeyword">private</span>:    <span class="codeComment">//----------------------------------------</span>    PCHAR pNewSection;    <span class="codeComment">//----------------------------------------</span>    DWORD GetFunctionVA(<span class="codeKeyword">void</span>* FuncName);    <span class="codeKeyword">void</span>* ReturnToBytePtr(<span class="codeKeyword">void</span>* FuncName, DWORD findstr);    <span class="codeComment">//----------------------------------------</span><span class="codeKeyword">protected</span>:    <span class="codeComment">//----------------------------------------</span><span class="codeKeyword">public</span>:    <span class="codeComment">//----------------------------------------</span>    <span class="codeKeyword">void</span> CryptFile(<span class="codeKeyword">int</span>(__cdecl *callback) (<span class="codeKeyword">unsigned</span> <span class="codeKeyword">int</span>,                                           <span class="codeKeyword">unsigned</span> <span class="codeKeyword">int</span>));    <span class="codeComment">//----------------------------------------</span>};<span class="codeComment">//----------------------------------------------------------------</span></pre>
<h4>4.3 Some notes regarding creating a new PE file</h4>
<ul>
<li>Align the <tt>VirtualAddress</tt> and the <tt>VirtualSize</tt> of each section by <tt>SectionAlignment</tt>:
<pre>image_section_header[i]-&gt;VirtualAddress=    PEAlign(image_section_header[i]-&gt;VirtualAddress,    image_nt_headers-&gt;OptionalHeader.SectionAlignment);image_section_header[i]-&gt;Misc.VirtualSize=    PEAlign(image_section_header[i]-&gt;Misc.VirtualSize,    image_nt_headers-&gt;OptionalHeader.SectionAlignment);</pre>
</li>
<li>Align the <tt>PointerToRawData</tt> and the <tt>SizeOfRawData</tt> of each section by <tt>FileAlignment</tt>:
<pre>image_section_header[i]-&gt;PointerToRawData =    PEAlign(image_section_header[i]-&gt;PointerToRawData,            image_nt_headers-&gt;OptionalHeader.FileAlignment);image_section_header[i]-&gt;SizeOfRawData =    PEAlign(image_section_header[i]-&gt;SizeOfRawData,            image_nt_headers-&gt;OptionalHeader.FileAlignment);</pre>
</li>
<li>Correct the <tt>SizeofImage</tt> by the virtual size and the virtual address of the last section:
<pre>image_nt_headers-&gt;OptionalHeader.SizeOfImage =   image_section_header[LastSection]-&gt;VirtualAddress +   image_section_header[LastSection]-&gt;Misc.VirtualSize;</pre>
</li>
<li>Set the Bound Import Directory header to zero because this directory is not very important to execute a PE file:
<pre>image_nt_headers-&gt;   OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].  VirtualAddress = 0;image_nt_headers-&gt;   OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_                                IMPORT].Size = 0;</pre>
</li>
</ul>
<h4>4.4 Some notes regarding linking this VC Project</h4>
<ul>
<li>Set <em>Linker-&gt;General-&gt;Enable Incremental Linking</em> to <strong>No (/INCREMENTAL:NO)</strong>.</p>
<p>        <img height="125" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=LINKTIP1_GIF&amp;ds=20060302" width="500" alt="" /><br />        (</li>
</ul>
<h3>5 Store Important Data and Reach the Original OEP</h3>
<p>Right now, we save the Original OEP and also the Image Base in order to reach to the virtual address of OEP. I have reserved a free space at the end of <tt>DynLoader()</tt> to store them, DynLoader Step 2.</p>
<h4>PE Maker &#8211; Step 2</h4>
<p>Download the pemaker2.zip source files from the end of the article.</p>
<h4>DynLoader Step 2</h4>
<pre><span class="codeKeyword">__stdcall</span> <span class="codeKeyword">void</span> DynLoader(){_asm{<span class="codeComment">//------------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_MAGIC)<span class="codeComment">//------------------------------------</span>Main_0:    PUSHAD    <span class="codeComment">// get base ebp</span>    CALL Main_1Main_1:    POP EBP    SUB EBP,OFFSET Main_1    MOV EAX,DWORD PTR [EBP+_RO_dwImageBase]    ADD EAX,DWORD PTR [EBP+_RO_dwOrgEntryPoint]    PUSH EAX    RETN <span class="codeComment">// &gt;&gt; JMP to Original OEP</span><span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_DATA1)<span class="codeComment">//----------------------------------<font color="#ff0000"></font><span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_END_MAGIC)<span class="codeComment">//----------------------------------</span>}}</span>_RO_dwImageBase:                DWORD_TYPE(0xCCCCCCCC)_RO_dwOrgEntryPoint:            DWORD_TYPE(0xCCCCCCCC)</pre>
<p>The new function, <tt>CPECryptor::CopyData1()</tt>, will implement the copy of the Image Base value and the Offset of Entry Point value into 8 bytes of free space in the loader.</p>
<h4>5.1 Restore the first register&#8217;s context</h4>
<p>It is important to recover the Original Context of the thread. You have not yet done it in the DynLoader Step 2 source code. You can modify the source of <tt>DynLoader()</tt> to repossess the first Context.</p>
<pre><span class="codeKeyword">__stdcall</span> <span class="codeKeyword">void</span> DynLoader(){_asm{<span class="codeComment">//------------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_MAGIC)<span class="codeComment">//------------------------------------</span>Main_0:    <font color="#ff0000">PUSHAD<span class="codeComment">// Save the registers context in stack</span>    CALL Main_1Main_1:    POP EBP<span class="codeComment">// Get Base EBP</span>    SUB EBP,OFFSET Main_1    MOV EAX,DWORD PTR [EBP+_RO_dwImageBase]    ADD EAX,DWORD PTR [EBP+_RO_dwOrgEntryPoint]    MOV DWORD PTR [ESP+1Ch],EAX <span class="codeComment">// pStack.Eax &lt;- EAX</span>    <font color="#ff0000">POPAD <span class="codeComment">// Restore the first registers context from stack</span>    PUSH EAX    XOR  EAX, EAX    RETN <span class="codeComment">// &gt;&gt; JMP to Original OEP</span><span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_DATA1)<span class="codeComment">//----------------------------------</span>_RO_dwImageBase:                DWORD_TYPE(0xCCCCCCCC)_RO_dwOrgEntryPoint:            DWORD_TYPE(0xCCCCCCCC)<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_END_MAGIC)<span class="codeComment">//----------------------------------</span>}}</font></font></pre>
<h4>5.2 Restore the original stack</h4>
<p>You also can recover the original stack by setting the value of the beginning stack + <tt>0x34</tt> to the Original OEP, but it is not very important. Nevertheless, in the following code, I have accomplished the loader code by a simple trick to reach the OEP in addition to redecorating the stack. You can observe the implementation by tracing using <a href="http://www.ollydbg.de/" target="new">OllyDbg</a> or SoftICE.</p>
<pre><span class="codeKeyword">__stdcall</span> <span class="codeKeyword">void</span> DynLoader(){_asm{<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_MAGIC)<span class="codeComment">//----------------------------------</span>Main_0:    PUSHAD    <span class="codeComment">// Save the registers context in stack</span>    CALL Main_1Main_1:    POP EBP    SUB EBP,OFFSET Main_1    MOV EAX,DWORD PTR [EBP+_RO_dwImageBase]    ADD EAX,DWORD PTR [EBP+_RO_dwOrgEntryPoint]    MOV DWORD PTR [ESP+54h],EAX    <span class="codeComment">// pStack.Eip &lt;- EAX</span>    POPAD    <span class="codeComment">// Restore the first registers context from stack</span>    CALL _OEP_Jump    DWORD_TYPE(0xCCCCCCCC)_OEP_Jump:    PUSH EBP    MOV EBP,ESP    MOV EAX,DWORD PTR [ESP+3Ch]    <span class="codeComment">// EAX &lt;- pStack.Eip</span>    MOV DWORD PTR [ESP+4h],EAX     <span class="codeComment">// _OEP_Jump RETURN pointer &lt;- EAX</span>    XOR EAX,EAX    LEAVE    RETN<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_DATA1)<span class="codeComment">//----------------------------------</span>_RO_dwImageBase:                DWORD_TYPE(0xCCCCCCCC)_RO_dwOrgEntryPoint:            DWORD_TYPE(0xCCCCCCCC)<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_END_MAGIC)<span class="codeComment">//----------------------------------</span>}}</pre>
<h4>5.3 Approach OEP by structured exception handling</h4>
<p><a name="more"><font color="#000000"> </font></a><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/key_s-z_4.asp" target="new"><tt>try-except</tt> statement</a> in C++ clarifies the operation of <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/about_structured_exception_handling.asp" target="new">structured exception handling</a>. Besides the assembly code of this code, it elucidates the structured exception handler installation, the raise of an exception, and the exception handler function.</p>
<p>An exception is generated when a program falls into a fault code execution and an error happens, so in such a special condition, the program immediately jumps to a function called the exception handler from exception handler list of the Thread Information Block.</p>
<p>The next example of a </p>
<pre><span class="codeKeyword">#include</span> &quot;stdafx.h&quot;<span class="codeKeyword">#include</span> &quot;windows.h&quot;<span class="codeKeyword">void</span> RAISE_AN_EXCEPTION(){_asm{    INT 3    INT 3    INT 3    INT 3}}<span class="codeKeyword">int</span> _tmain(<span class="codeKeyword">int</span> argc, _TCHAR* argv[]){    <span class="codeKeyword">__try</span>    {        <span class="codeKeyword">__try</span>{            printf(&quot;1: Raise an Exception\n&quot;);            RAISE_AN_EXCEPTION();        }        <span class="codeKeyword">__finally</span>        {            printf(&quot;2: In Finally\n&quot;);        }    }    <span class="codeKeyword">__except</span>( printf(&quot;3: In Filter\n&quot;), EXCEPTION_EXECUTE_HANDLER )    {        printf(&quot;4: In Exception Handler\n&quot;);    }    <span class="codeKeyword">return</span> 0;}</pre>
<pre><font color="#000000"><strong>; main()</strong></font><font color="#808080">00401000: PUSH EBP00401001: MOV EBP,ESP00401003: PUSH -100401005: PUSH 00407160<font color="#000000"><strong>; <span class="codeKeyword">__try</span> {</strong></font><font color="#008000">; the structured exception handler (SEH) installation </font><font color="#0000ff">0040100A: PUSH _except_handler30040100F: MOV EAX,DWORD PTR FS:[0]00401015: PUSH EAX00401016: MOV DWORD PTR FS:[0],ESP</font>0040101D: SUB ESP,800401020: PUSH EBX00401021: PUSH ESI00401022: PUSH EDI00401023: MOV DWORD PTR SS:[EBP-18],ESP<font color="#000000"><strong>;     <span class="codeKeyword">__try</span> {</strong></font>00401026: XOR ESI,ESI00401028: MOV DWORD PTR SS:[EBP-4],ESI0040102B: MOV DWORD PTR SS:[EBP-4],100401032: PUSH OFFSET <font color="#a52a2a">&quot;1: Raise an Exception&quot;</font>00401037: CALL printf0040103C: ADD ESP,4<font color="#008000">; the raise a exception, INT 3 exception</font>; RAISE_AN_EXCEPTION()<font color="#0000ff">0040103F: INT300401040: INT300401041: INT300401042: INT3</font><font color="#000000"><strong>;     } <span class="codeKeyword">__finally</span> {</strong></font>00401043: MOV DWORD PTR SS:[EBP-4],ESI00401046: CALL 0040104D0040104B: JMP 004010800040104D: PUSH OFFSET <font color="#a52a2a">&quot;2: In Finally&quot;</font>00401052: CALL printf00401057: ADD ESP,40040105A: RETN<font color="#000000"><strong>;     }</strong></font><font color="#000000"><strong>; }</strong></font><font color="#000000"><strong>; <span class="codeKeyword">__except</span>( </strong></font>0040105B: JMP 004010800040105D: PUSH OFFSET <font color="#a52a2a">&quot;3: In Filter&quot;</font>00401062: CALL printf00401067: ADD ESP,40040106A: MOV EAX,1 ; EXCEPTION_EXECUTE_HANDLER = 10040106F: RETN<font color="#000000"><strong>;     , EXCEPTION_EXECUTE_HANDLER )</strong></font><font color="#000000"><strong>; {</strong></font><font color="#008000">; the exception handler funtion</font><font color="#0000ff">00401070: MOV ESP,DWORD PTR SS:[EBP-18]00401073: PUSH OFFSET <font color="#a52a2a">&quot;4: In Exception Handler&quot;</font>00401078: CALL printf0040107D: ADD ESP,4</font><font color="#000000"><strong>; }</strong></font>00401080: MOV DWORD PTR SS:[EBP-4],-10040108C: XOR EAX,EAX<font color="#008000">; restore previous SEH</font><font color="#0000ff">0040108E: MOV ECX,DWORD PTR SS:[EBP-10]00401091: MOV DWORD PTR FS:[0],ECX</font>00401098: POP EDI00401099: POP ESI0040109A: POP EBX0040109B: MOV ESP,EBP0040109D: POP EBP0040109E: RETN</font></pre>
<p>Make a Win32 console project, and link and run the preceding C++ code, to perceive the result:</p>
<p>
<table cellspacing="0" cellpadding="0" width="400" border="1">
<tbody bgcolor="#000000" color="gray">
<tr>
<td><font color="#ffffff"><strong>1: Raise an Exception<br />                3: In Filter<br />                2: In Finally<br />                4: In Exception Handler<br />                _</p>
<p>                </strong></font></td>
</tr>
</tbody>
</table>
<p>This program runs the exception expression, <tt>printf(&quot;3: In Filter\n&quot;);</tt>, when an exception happens&mdash;in this example, the <tt>INT 3</tt> exception. You can employ other kinds of exception too. In <a href="http://www.ollydbg.de/" target="new">OllyDbg</a>, <strong>Debugging options-&gt;Exceptions</strong>, you can see a short list of different types of exceptions.</p>
<p><img height="200" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=OLLYDBG_EXCEPTIONS_GIF&amp;ds=20060302" width="280" alt="" /></p>
<h5>5.3.1 Implement Exception Handler</h5>
<p>You want to construct a structured exception handler to reach OEP. Now, I think you have distinguished the SEH installation, the exception raise, and the exception expression filter, by foregoing the assembly code. To establish your exception handler approach, you need to comprise the following codes:</p>
<ul>
<li><strong>SEH installation</strong>:
<pre><font color="#808080">LEA EAX,[EBP+_except_handler1_OEP_Jump]PUSH EAXPUSH DWORD PTR FS:[0]MOV DWORD PTR FS:[0],ESP</font></pre>
</li>
<li><strong>An Exception Raise</strong>:
<pre><font color="#808080">INT 3</font></pre>
</li>
<li><strong>Exception handler expression filter</strong>:
<pre><font color="#808080">_except_handler1_OEP_Jump:   PUSH EBP   MOV EBP,ESP   ...   <span class="codeComment">// EXCEPTION_CONTINUE_SEARCH = 0</span>   MOV EAX, EXCEPTION_CONTINUE_SEARCH   LEAVE   RETN</font></pre>
</li>
</ul>
<p>So, you yearn to make the ensuing C++ code in assembly language to inaugurate your engine to approach the Offset of the Entry Point by SEH.</p>
<pre><span class="codeKeyword">__try</span>    <span class="codeComment">// SEH installation</span>{    __asm    {        INT 3    <span class="codeComment">// An Exception Raise</span>    }}<span class="codeKeyword">__except</span>( ..., EXCEPTION_CONTINUE_SEARCH ){}<span class="codeComment">// Exception handler expression filter</span></pre>
<p>In assembly code&#8230;</p>
<pre><font color="#808080">    <font color="#008000">; ----------------------------------------------------    ; the structured exception handler (SEH) installation    <font color="#000000"><strong>; <span class="codeKeyword">__try</span> {</strong></font></font>    LEA EAX,[EBP+_except_handler1_OEP_Jump]    PUSH EAX    PUSH DWORD PTR FS:[0]    MOV DWORD PTR FS:[0],ESP    <font color="#008000">; ----------------------------------------------------    ; the raise a INT 3 exception</font>    INT 3    INT 3    INT 3    INT 3    <font color="#000000"><strong>; }    ; <span class="codeKeyword">__except</span>( ... </strong></font>    <font color="#008000">; ----------------------------------------------------    ; exception handler expression filter</font>_except_handler1_OEP_Jump:    PUSH EBP    MOV EBP,ESP    ...    MOV EAX, EXCEPTION_CONTINUE_SEARCH ; EXCEPTION_CONTINUE_SEARCH = 0    LEAVE    RETN    <font color="#000000"><strong>; , EXCEPTION_CONTINUE_SEARCH ) { }</strong></font></font></pre>
<p>The exception value, <tt>__except(..., Value)</tt>, determines how the exception is handled. It can have three values: 1, 0, -1. To understand them, refer to the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/key_s-z_4.asp" target="new"><tt>try-except</tt> statement</a> description in the MSDN library. You set it to <tt>EXCEPTION_CONTINUE_SEARCH (0)</tt>, not to run the exception handler function; therefore, by this value, the exception is not recognized. It is simply ignored, and the thread continues its code execution.</p>
<h4>How the SEH installation is implemented</h4>
<p>As you perceived from the illustrated code, the SEH installation is done by the FS segment register. Microsoft Windows 32 bit uses the FS segment register as a pointer to the data block of the main thread. The first <font color="#0000ff">0x1C</font> bytes comprise the information of the Thread Information Block (TIB). Therefore, <tt>FS:[00h]</tt> refers to <tt>ExceptionList</tt> of the main thread, Table 3. In your code, you have pushed the pointer to <tt>_except_handler1_OEP_Jump</tt> in the stack and changed the value of <tt>ExceptionList</tt>, <tt>FS:[00h]</tt>, to the beginning of the stack, <tt>ESP</tt>.</p>
<h4>Thread Information Block (TIB)</h4>
<pre><span class="codeKeyword">typedef</span> <span class="codeKeyword">struct</span> _NT_TIB32 {   DWORD ExceptionList;   DWORD StackBase;   DWORD StackLimit;   DWORD SubSystemTib;   <span class="codeKeyword">union</span> {      DWORD FiberData;      DWORD Version;   };   DWORD ArbitraryUserPointer;   DWORD Self;} NT_TIB32, *PNT_TIB32;</pre>
<h4>Table 3: FS segment register and Thread Information Block</h4>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[00h]</font></td>
<td align="center">ExceptionList</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[04h]</font></td>
<td align="center">StackBase</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[08h]</font></td>
<td align="center">StackLimit</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[0Ch]</font></td>
<td align="center">SubSystemTib</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[10h]</font></td>
<td align="center">FiberData / Version</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[14h]</font></td>
<td align="center">ArbitraryUserPointer</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">DWORD PTR FS:[18h]</font></td>
<td align="center">Self</td>
</tr>
</tbody>
</table>
<h5>5.3.2 Attain OEP by adjusting the Thread Context</h5>
<p>In this part, you effectuate your performance by accomplishing the OEP approach. You change the Context of the thread and ignore every simple exception handling, and let the thread continue the execution, but in the original OEP!</p>
<p>    <a name="more"><font color="#000000">&nbsp;</font>
<p>When an exception happens, the context of the processor during the time of the exception is saved in the stack. Through </p>
<pre>MOV EAX, ContextRecordMOV EDI, dwOEP                   ; EAX &lt;- dwOEPMOV DWORD PTR DS:[EAX+0B8h], EDI ; pContext.Eip &lt;- EAX</pre>
<h4>Win32 Thread Context structure</h4>
<pre><span class="codeKeyword">#define</span> MAXIMUM_SUPPORTED_EXTENSION     512<span class="codeKeyword">typedef</span> <span class="codeKeyword">struct</span> _CONTEXT {    <span class="codeComment">//-----------------------------------------</span>    DWORD ContextFlags;    <span class="codeComment">//-----------------------------------------</span>    DWORD   Dr0;    DWORD   Dr1;    DWORD   Dr2;    DWORD   Dr3;    DWORD   Dr6;    DWORD   Dr7;    <span class="codeComment">//-----------------------------------------</span>    FLOATING_SAVE_AREA FloatSave;    <span class="codeComment">//-----------------------------------------</span>    DWORD   SegGs;    DWORD   SegFs;    DWORD   SegEs;    DWORD   SegDs;    <span class="codeComment">//-----------------------------------------</span>    DWORD   Edi;    DWORD   Esi;    DWORD   Ebx;    DWORD   Edx;    DWORD   Ecx;    DWORD   Eax;    <span class="codeComment">//-----------------------------------------</span>    DWORD   Ebp;    DWORD   Eip;    DWORD   SegCs;    DWORD   EFlags;    DWORD   Esp;    DWORD   SegSs;    <span class="codeComment">//-----------------------------------------</span>    BYTE    ExtendedRegisters[MAXIMUM_SUPPORTED_EXTENSION];    <span class="codeComment">//----------------------------------------</span>} CONTEXT,*LPCONTEXT;</pre>
<h4>Table 4: CONTEXT</h4>
<table cellspacing="0" cellpadding="0" width="200" border="1">
<tbody>
<tr>
<td align="center" height="35">Context Flags</td>
<td align="center" height="35"><font color="#0000ff">0&#215;00000000</font></td>
<td align="center" colspan="2" height="35"><tt>ContextFlags</tt></td>
</tr>
<tr>
<td align="center" rowspan="6">
<p>Context Debug Registers</p>
</td>
<td align="center"><font color="#0000ff">0&#215;00000004</font></td>
<td align="center" colspan="2"><tt>Dr0</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000008</font></td>
<td align="center" colspan="2"><tt>Dr1</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x0000000C</font></td>
<td align="center" colspan="2"><tt>Dr2</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000010</font></td>
<td align="center" colspan="2"><tt>Dr3</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000014</font></td>
<td align="center" colspan="2"><tt>Dr6</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000018</font></td>
<td align="center" colspan="2"><tt>Dr7</tt></td>
</tr>
<tr>
<td align="center" rowspan="9">
<p>Context Floating Point</p>
</td>
<td align="center"><font color="#0000ff">0x0000001C</font></td>
<td align="center" rowspan="9"><tt>FloatSave</tt></td>
<td align="center"><tt>StatusWord</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000020</font></td>
<td align="center"><tt>StatusWord</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000024</font></td>
<td align="center"><tt>TagWord</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000028</font></td>
<td align="center"><tt>ErrorOffset</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x0000002C</font></td>
<td align="center"><tt>ErrorSelector</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000030</font></td>
<td align="center"><tt>DataOffset</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000034</font></td>
<td align="center"><tt>DataSelector</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000038<br />                &#8230;<br />                0&#215;00000087</font></td>
<td align="center"><tt>RegisterArea</tt> [<font color="#0000ff">0x50</font>]</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000088</font></td>
<td align="center"><tt>Cr0NpxState</tt></td>
</tr>
<tr>
<td align="center" rowspan="4">Context Segments</td>
<td align="center"><font color="#0000ff">0x0000008C</font></td>
<td align="center" colspan="2"><tt>SegGs</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000090</font></td>
<td align="center" colspan="2"><tt>SegFs</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000094</font></td>
<td align="center" colspan="2"><tt>SegEs</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0&#215;00000098</font></td>
<td align="center" colspan="2"><tt>SegDs</tt></td>
</tr>
<tr>
<td align="center" rowspan="6">Context Integer</td>
<td align="center"><font color="#0000ff">0x0000009C</font></td>
<td align="center" colspan="2"><tt>Edi</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000A0</font></td>
<td align="center" colspan="2"><tt>Esi</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000A4</font></td>
<td align="center" colspan="2"><tt>Ebx</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000A8</font></td>
<td align="center" colspan="2"><tt>Edx</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000AC</font></td>
<td align="center" colspan="2"><tt>Ecx</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000B0</font></td>
<td align="center" colspan="2"><tt>Eax</tt></td>
</tr>
<tr>
<td align="center" rowspan="6">Context Control</td>
<td align="center"><font color="#0000ff">0x000000B4</font></td>
<td align="center" colspan="2"><tt>Ebp</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000B8</font></td>
<td align="center" colspan="2"><tt>Eip</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000BC</font></td>
<td align="center" colspan="2"><tt>SegCs</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000C0</font></td>
<td align="center" colspan="2"><tt>EFlags</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000C4</font></td>
<td align="center" colspan="2"><tt>Esp</tt></td>
</tr>
<tr>
<td align="center"><font color="#0000ff">0x000000C8</font></td>
<td align="center" colspan="2"><tt>SegSs</tt></td>
</tr>
<tr>
<td align="center">Context Extended Registers</td>
<td align="center">
<p align="center"><font color="#0000ff">0x000000CC<br />                &#8230;<br />                0x000002CB</font></p>
</td>
<td align="center" colspan="2"><tt>ExtendedRegisters</tt>[<font color="#0000ff">0x200</font>]</td>
</tr>
</tbody>
</table>
<p>By the following code, you have accomplished the main purpose of coming to OEP by the structured exception handler:</p>
<pre><span class="codeKeyword">__stdcall</span> <span class="codeKeyword">void</span> DynLoader(){_asm{<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_MAGIC)<span class="codeComment">//----------------------------------</span>Main_0:    PUSHAD  <span class="codeComment">// Save the registers context in stack</span>    CALL Main_1Main_1:    POP EBP    SUB EBP,OFFSET Main_1 <span class="codeComment">// Get Base EBP</span>    MOV EAX,DWORD PTR [EBP+_RO_dwImageBase]    ADD EAX,DWORD PTR [EBP+_RO_dwOrgEntryPoint]    MOV DWORD PTR [ESP+10h],EAX    <span class="codeComment">// pStack.Ebx &lt;- EAX</span>    LEA EAX,[EBP+_except_handler1_OEP_Jump]    MOV DWORD PTR [ESP+1Ch],EAX    <span class="codeComment">// pStack.Eax &lt;- EAX</span>    POPAD  <span class="codeComment">// Restore the first registers context from stack</span>    <span class="codeComment">//----------------------------------------------------</span>    <span class="codeComment">// the structured exception handler (SEH) installation</span>    PUSH EAX    XOR  EAX, EAX    PUSH DWORD PTR FS:[0]       <span class="codeComment">// NT_TIB32.ExceptionList</span>    MOV DWORD PTR FS:[0],ESP    <span class="codeComment">// NT_TIB32.ExceptionList &lt;-ESP</span>    <span class="codeComment">//----------------------------------------------------</span>    <span class="codeComment">// the raise a INT 3 exception</span>    DWORD_TYPE(0xCCCCCCCC)    <span class="codeComment">//--------------------------------------------------------</span><span class="codeComment">// -------- exception handler expression filter ----------</span>_except_handler1_OEP_Jump:    PUSH EBP    MOV EBP,ESP    <span class="codeComment">//------------------------------</span>    MOV EAX,DWORD PTR SS:[EBP+010h]   <span class="codeComment">// PCONTEXT: pContext &lt;- EAX</span>    <span class="codeComment">//==============================</span>    PUSH EDI    <span class="codeComment">// restore original SEH</span>    MOV EDI,DWORD PTR DS:[EAX+0C4h]    <span class="codeComment">// pContext.Esp</span>    PUSH DWORD PTR DS:[EDI]    POP DWORD PTR FS:[0]    ADD DWORD PTR DS:[EAX+0C4h],8    <span class="codeComment">// pContext.Esp</span>    <span class="codeComment">//------------------------------</span>    <span class="codeComment">// set the Eip to the OEP</span>    MOV EDI,DWORD PTR DS:[EAX+0A4h] <span class="codeComment">// EAX &lt;- pContext.Ebx</span>    MOV DWORD PTR DS:[EAX+0B8h],EDI <span class="codeComment">// pContext.Eip &lt;- EAX</span>    <span class="codeComment">//------------------------------</span>    POP EDI    <span class="codeComment">//==============================</span>    MOV EAX, EXCEPTION_CONTINUE_SEARCH    LEAVE    RETN<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_START_DATA1)<span class="codeComment">//----------------------------------</span>_RO_dwImageBase:                DWORD_TYPE(0xCCCCCCCC)_RO_dwOrgEntryPoint:            DWORD_TYPE(0xCCCCCCCC)<span class="codeComment">//----------------------------------</span>    DWORD_TYPE(DYN_LOADER_END_MAGIC)<span class="codeComment">//----------------------------------</span>}}</pre>
<h3>6 Build an Import Table and Reconstruct the Original Import Table</h3>
<p>There are two ways to use the Windows <a href="http://en.wikipedia.org/wiki/Microsoft_Dynamic_Link_Library" target="new">dynamic link library (DLL)</a> in Windows application programming:</p>
<ul>
<li><strong>Using Windows libraries by additional dependencies</strong>:&nbsp;<br />        <a name="more"><font color="#000000"> </font>
<p><font color="#000000"><img height="145" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=DEPENDENCIES_GIF&amp;ds=20060302" width="500" alt="" /><br />        </font>(</p>
<p>        </a><a href="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=DEPENDENCIES_GIF&amp;ds=20060302" target="_blank">Full Size Image</a>)</li>
<li><strong>Using Windows dynamic link libraries in run-time</strong>:
<pre><span class="codeComment">// DLL function signature</span><span class="codeKeyword">typedef</span> HGLOBAL (*importFunction_GlobalAlloc)(UINT, SIZE_T);...importFunction_GlobalAlloc __GlobalAlloc;<span class="codeComment">// Load DLL file</span>HINSTANCE hinstLib = LoadLibrary(&quot;Kernel32.dll&quot;);<span class="codeKeyword">if</span> (hinstLib == <span class="codeKeyword">NULL</span>){   <span class="codeComment">// Error - unable to load DLL</span>}<span class="codeComment">// Get function pointer</span>__GlobalAlloc =   (importFunction_GlobalAlloc)GetProcAddress(hinstLib,                                              &quot;GlobalAlloc&quot;);<span class="codeKeyword">if</span> (addNumbers == <span class="codeKeyword">NULL</span>){    <span class="codeComment">// Error - unable to find DLL function</span>}FreeLibrary(hinstLib);</pre>
</li>
</ul>
<p>When you make a Windows application project, the linker includes at least <em>kernel32.dll</em> in the base dependencies of your project. Without <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" target="new"><tt>LoadLibrary()</tt></a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp" target="new"><tt>GetProcAddress()</tt></a> of <em>Kernel32.dll</em>, you cannot load a DLL at run time. The dependencies information is stored in the import table section. By using <a href="http://www.microsoft.com/resources/documentation/Windows/XP/all/reskit/en-us/Default.asp?url=/resources/documentation/Windows/XP/all/reskit/en-us/prmb_tol_kewf.asp" target="new">Dependency Walker</a>, it is not so difficult to observe the DLL module and the functions that are imported into a PE file.</p>
<p><img height="352" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=DEPENDENCY_WALKER_GIF&amp;ds=20060302" width="480" alt="" /></p>
<p>You attempt to establish your custom import table to conduct your project. Furthermore, you have to fix up the original import table at the end to run the real code of the program.</p>
<h4>PE Maker: Step 3</h4>
<p>Download the pemaker3.zip source files from the end of the article.</p>
<h4>6.1 Construct the Client Import Table</h4>
<p>I strongly advise that you to read Section 6.4 of the <a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx" target="new">Microsoft Portable Executable and the Common Object File Format Specification</a> document. This section contains the principal information to comprehend the import table performance. The import table data is accessible by a second data directory of the optional header from PE headers, so you can access it by using the following code:</p>
<pre>DWORD dwVirtualAddress = image_nt_headers-&gt;   OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].      VirtualAddress;DWORD dwSize = image_nt_headers-&gt;   OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].      Size;</pre>
<p>The <tt>VirtualAddress</tt> refers to structures by <tt>IMAGE_IMPORT_DESCRIPTOR</tt>. This structure contains the pointer to the imported DLL name and the relative virtual address of the first thunk.</p>
<pre><span class="codeKeyword">typedef</span> <span class="codeKeyword">struct</span> _IMAGE_IMPORT_DESCRIPTOR {    <span class="codeKeyword">union</span> {        DWORD   Characteristics;        DWORD   OriginalFirstThunk;    };    DWORD   TimeDateStamp;    DWORD   ForwarderChain;    DWORD   <font color="#ff0000">Name</font>;         <span class="codeComment">// the imported DLL name</span>    DWORD   <font color="#ff0000">FirstThunk</font>;   <span class="codeComment">// the relative virtual address of the</span>                          <span class="codeComment">// first thunk</span>} IMAGE_IMPORT_DESCRIPTOR, *PIMAGE_IMPORT_DESCRIPTOR;</pre>
<p>When a program is running, the Windows Task Manager sets the thunks by the virtual address of the function. The virtual address is found by the name of the function. At first, the thunks hold the relative virtual address of the function name, as shown in Table 5; during execution, they are fixed up by the virtual address of the functions (see Table 6).</p>
<h4>Table 5: The Import Table in a file image</h4>
<p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td rowspan="8"><tt>IMAGE_IMPORT_<br />                DESCRIPTOR[0]</tt></td>
<td><tt>OriginalFirstThunk</tt></td>
<td colspan="2" rowspan="3">&nbsp;</td>
<td colspan="2" rowspan="4">&nbsp;</td>
</tr>
<tr>
<td><tt>TimeDateStamp</tt></td>
</tr>
<tr>
<td><tt>ForwarderChain</tt></td>
</tr>
<tr>
<td><tt>Name_RVA</tt></td>
<td>&#8212;&#8212;&gt;</td>
<td><font color="#a52a2a">&quot;kernel32.dll&quot;<font color="#0000ff">,0</font></font></td>
</tr>
<tr>
<td><tt>FirstThunk_RVA</tt></td>
<td>&#8212;&#8212;&gt;</td>
<td><tt>proc_1_name_RVA</tt></td>
<td>&#8212;&#8212;&gt;</td>
<td><font color="#0000ff">0,0,</font><font color="#a52a2a">&quot;LoadLibraryA&quot;</font><font color="#0000ff">,0</font></td>
</tr>
<tr>
<td colspan="2" rowspan="3">&nbsp;</td>
<td><tt>proc_2_name_RVA</tt></td>
<td>&#8212;&#8212;&gt;</td>
<td><font color="#0000ff">0,0,</font><font color="#a52a2a">&quot;GetProcAddress&quot;</font><font color="#0000ff">,0</font></td>
</tr>
<tr>
<td><tt>proc_3_name_RVA</tt></td>
<td>&#8212;&#8212;&gt;</td>
<td><font color="#0000ff">0,0,</font><font color="#a52a2a">&quot;GetModuleHandleA&quot;</font><font color="#0000ff">,0</font></td>
</tr>
<tr>
<td>&#8230;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><tt>IMAGE_IMPORT_<br />                DESCRIPTOR[1]</tt></td>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td><tt>...</tt></td>
<td colspan="5">&nbsp;</td>
</tr>
<tr>
<td><tt>IMAGE_IMPORT_<br />                DESCRIPTOR[n]</tt></td>
<td colspan="5">&nbsp;</td>
</tr>
</tbody>
</table>
<h4>Table 6: The Import Table in virtual memory</h4>
<p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td rowspan="8"><tt>IMAGE_IMPORT_DESCRIPTOR[0]</tt></td>
<td><tt>OriginalFirstThunk</tt></td>
<td colspan="2" rowspan="3">&nbsp;</td>
</tr>
<tr>
<td><tt>TimeDateStamp</tt></td>
</tr>
<tr>
<td><tt>ForwarderChain</tt></td>
</tr>
<tr>
<td><tt>Name_RVA</tt></td>
<td><tt>------&gt;</tt></td>
<td><font color="#a52a2a">&quot;kernel32.dll&quot;<font color="#0000ff">,0</font></font></td>
</tr>
<tr>
<td><tt>FirstThunk_RVA</tt></td>
<td><tt>------&gt;</tt></td>
<td><tt>proc_1_VA</tt></td>
</tr>
<tr>
<td colspan="2" rowspan="3">&nbsp;</td>
<td><tt>proc_2_VA</tt></td>
</tr>
<tr>
<td><tt>proc_3_VA</tt></td>
</tr>
<tr>
<td><tt>...</tt></td>
</tr>
<tr>
<td><tt>IMAGE_IMPORT_DESCRIPTOR[1]</tt></td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td><tt>...</tt></td>
<td colspan="3">&nbsp;</td>
</tr>
<tr>
<td><tt>IMAGE_IMPORT_DESCRIPTOR[n]</tt></td>
<td colspan="3">&nbsp;</td>
</tr>
</tbody>
</table>
<p>You want to make a simple import table to import <tt>LoadLibrary()</tt>, and <tt>GetProcAddress()</tt> from <em>Kernel32.dll</em>. You need these two essential API functions to cover other API functions in run-time. The following assembly code shows how easily you can reach your solution:</p>
<pre><font color="#808080">0101F000: <font color="#0000ff">00000000</font> ; OriginalFirstThunk0101F004: <font color="#0000ff">00000000</font> ; TimeDateStamp0101F008: <font color="#0000ff">00000000</font> ; ForwarderChain0101F00C: <font color="#0000ff">0001F034</font> ; Name;       ImageBase + 0001F034                                 -&gt; 0101F034 -&gt; &quot;Kernel32.dll&quot;,00101F010: <font color="#0000ff">0001F028</font> ; FirstThunk; ImageBase + 0001F028 -&gt; 0101F0280101F014: <font color="#0000ff">00000000</font>0101F018: <font color="#0000ff">00000000</font>0101F01C: <font color="#0000ff">00000000</font>0101F020: <font color="#0000ff">00000000</font>0101F024: <font color="#0000ff">00000000</font>0101F028: <font color="#0000ff">0001F041</font> ; ImageBase + 0001F041 -&gt; 0101F041                     -&gt; 0,0,&quot;LoadLibraryA&quot;,00101F02C: <font color="#0000ff">0001F050</font> ; ImageBase + 0001F050 -&gt; 0101F050                     -&gt; 0,0,&quot;GetProcAddress&quot;,00101F030: <font color="#0000ff">00000000</font>0101F034: <font color="#a52a2a"><span class="codeComment">'K' 'e' 'r' 'n' 'e' 'l' '3' '2' '.' 'd' 'l' 'l' </span>0001F041: <font color="#0000ff">00 00</font> <font color="#a52a2a"><span class="codeComment">'L' 'o' 'a' 'd' 'L' 'i' 'b' 'r' 'a' 'r' 'y' 'A'</span>0001F050: <font color="#0000ff">00 00</font> <font color="#a52a2a"><span class="codeComment">'G' 'e' 't' 'P' 'r' 'o' 'c' 'A' 'd' 'd' 'r' 'e' 's'</span>          <span class="codeComment">'s'</span></font> <font color="#0000ff">00</font></font> <font color="#0000ff">00</font></font><font color="#0000ff">00</font></font></pre>
<p>After running&#8230;</p>
<pre><font color="#808080">0101F000: <font color="#0000ff">00000000</font> ; OriginalFirstThunk0101F004: <font color="#0000ff">00000000</font> ; TimeDateStamp0101F008: <font color="#0000ff">00000000</font> ; ForwarderChain0101F00C: <font color="#0000ff">0001F034</font> ; Name;       ImageBase + 0001F034                                 -&gt; 0101F034 -&gt; &quot;Kernel32.dll&quot;,00101F010: <font color="#0000ff">0001F028</font> ; FirstThunk; ImageBase + 0001F028 -&gt; 0101F0280101F014: <font color="#0000ff">00000000</font>0101F018: <font color="#0000ff">00000000</font>0101F01C: <font color="#0000ff">00000000</font>0101F020: <font color="#0000ff">00000000</font>0101F024: <font color="#0000ff">00000000</font>0101F028: <font color="#ff0000">7C801D77</font> ; -&gt; Kernel32.LoadLibrary()0101F02C: <font color="#ff0000">7C80AC28</font> ; -&gt; Kernel32.GetProcAddress()0101F030: <font color="#0000ff">00000000</font>0101F034: <font color="#a52a2a"><span class="codeComment">'K' 'e' 'r' 'n' 'e' 'l' '3' '2' '.' 'd' 'l' 'l' </span>0001F041: <font color="#0000ff">00 00</font> <font color="#a52a2a"><span class="codeComment">'L' 'o' 'a' 'd' 'L' 'i' 'b' 'r' 'a' 'r' 'y' 'A'</span>0001F050: <font color="#0000ff">00 00</font> <font color="#a52a2a"><span class="codeComment">'G' 'e' 't' 'P' 'r' 'o' 'c' 'A' 'd' 'd' 'r' 'e' 's'</span>          <span class="codeComment">'s'</span></font> <font color="#0000ff">00</font></font> <font color="#0000ff">00</font></font><font color="#0000ff">00</font></font></pre>
<p>I have prepared a class library to make every import table by using a client string table. The <tt>CITMaker</tt> class library in <em>itmaker.h</em>; it will build an import table by <tt>sz_IT_EXE_strings</tt> and also the relative virtual address of the import table.</p>
<pre><span class="codeKeyword">static</span> <span class="codeKeyword">const</span> <span class="codeKeyword">char</span> *sz_IT_EXE_strings[]={    &quot;Kernel32.dll&quot;,    &quot;LoadLibraryA&quot;,    &quot;GetProcAddress&quot;,    0,,    0,};</pre>
<p>You subsequently employ this class library to establish an import table to support DLLs and OCXs, so this is a general library to present all possible import tables easily. The next step is clarified in the following code.</p>
<pre>CITMaker *<font color="#ff0000">ImportTableMaker</font> = <span class="codeKeyword">new</span> CITMaker( IMPORT_TABLE_EXE );...pimage_section_header=AddNewSection( &quot;.xxx&quot;, dwNewSectionSize );<span class="codeComment">// build import table by the current virtual address</span><font color="#ff0000">ImportTableMaker</font>-&gt;<font color="#008000">Build</font>( <font color="#0000ff">pimage_section_header-&gt;VirtualAddress</font> );memcpy( pNewSection, <font color="#ff0000">ImportTableMaker</font>-&gt;<font color="#008000">pMem</font>,<font color="#ff0000">ImportTableMaker</font>-&gt;<font color="#008000">dwSize</font> );...memcpy( image_section[image_nt_headers-&gt;FileHeader.NumberOfSections-1],        pNewSection,        dwNewSectionSize );...image_nt_headers-&gt;OptionalHeader.  DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress  = <font color="#0000ff">pimage_section_header-&gt;VirtualAddress</font>;image_nt_headers-&gt;OptionalHeader.  DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size  = <font color="#ff0000">ImportTableMaker</font>-&gt;<font color="#008000">dwSize</font>;...<span class="codeKeyword">delete</span> <font color="#ff0000">ImportTableMaker</font>;</pre>
<p>The import table is copied at the beginning of the new section, and the relevant data directory is adjusted to the relative virtual address of the new section and the size of the new import table.</p>
<h4>6.2 Using other API functions at run time</h4>
<p>At this time, you can load other DLLs and find the process address of other functions by using <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" target="new"><tt>LoadLibrary()</tt></a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp" target="new"><tt>GetProcAddress()</tt></a>:</p>
<pre><font color="#808080">lea edi, <font color="#ff0000">@</font><font color="#a52a2a">&quot;Kernel32.dll&quot;</font><span class="codeComment">//-------------------</span><font color="#0000ff">push edimov eax,offset _p_LoadLibrarycall [ebp+eax] <span class="codeComment">//LoadLibrary(lpLibFileName);</span><span class="codeComment">//-------------------</span>mov esi,eax    <span class="codeComment">// esi -&gt; hModule</span>lea edi, <font color="#ff0000">@</font><font color="#a52a2a">&quot;GetModuleHandleA&quot;</font><span class="codeComment">//-------------------</span><font color="#0000ff">push edipush esimov eax,offset _p_GetProcAddresscall [ebp+eax] <span class="codeComment">//GetModuleHandle=GetProcAddress(hModule, lpProcName);</span><span class="codeComment">//--------------------</span></font></font></font></pre>
<p>    <a name="more"><font color="#000000">&nbsp;</font></a><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" target="new"><tt>LoadLibrary()</tt></a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp" target="new"><tt>GetProcAddress()</tt></a> aid you in your effort to reach your intention.</p>
<p>I want to have a complete imported function table similar in performance done in a real EXE file. If you look inside a PE file, you will discover that an API call is done by an indirection jump through the virtual address of the API function:</p>
<h4>JMP DWORD PTR [XXXXXXXX]</h4>
<pre><font color="#808080">...0101F028: <font color="#ff0000">7C801D77</font>      ; Virtual Address of kernel32.LoadLibrary()...0101F120: JMP DWORD PTR [<font color="#ff0000">0101F028</font>]...0101F230: CALL <font color="#ff0000">0101F120</font> ;  JMP to kernel32.LoadLibrary...</font></pre>
<p>It makes it easy to expand the other part of your project by this performance, so you construct two data tables: the first for API virtual addresses, and the second for the <tt>JMP [XXXXXXXX]</tt>.</p>
<pre><span class="codeKeyword">#define</span> __jmp_api               byte_type(0xFF) byte_type(0x25)__asm{...<span class="codeComment">//----------------------------------------------------------------</span>_p_GetModuleHandle:             dword_type(0xCCCCCCCC)_p_VirtualProtect:              dword_type(0xCCCCCCCC)_p_GetModuleFileName:           dword_type(0xCCCCCCCC)_p_CreateFile:                  dword_type(0xCCCCCCCC)_p_GlobalAlloc:                 dword_type(0xCCCCCCCC)<span class="codeComment">//----------------------------------------------------------------</span>_jmp_GetModuleHandle:           __jmp_api   dword_type(0xCCCCCCCC)_jmp_VirtualProtect:            __jmp_api   dword_type(0xCCCCCCCC)_jmp_GetModuleFileName:         __jmp_api   dword_type(0xCCCCCCCC)_jmp_CreateFile:                __jmp_api   dword_type(0xCCCCCCCC)_jmp_GlobalAlloc:               __jmp_api   dword_type(0xCCCCCCCC)<span class="codeComment">//----------------------------------------------------------------</span>...}</pre>
<p>In the succeeding code, you have concluded your ambition to install a custom internal import table! (You cannot call it import table.)</p>
<pre><font color="#808080">    ...    lea edi,[ebp+_p_szKernel32]    lea ebx,[ebp+_p_GetModuleHandle]    lea ecx,[ebp+_jmp_GetModuleHandle]    add ecx,02h_api_get_lib_address_loop:        push ecx        <font color="#0000ff">push edi        mov eax,offset _p_LoadLibrary        call [ebp+eax]    <span class="codeComment">//LoadLibrary(lpLibFileName);</span>        pop ecx        mov esi,eax       <span class="codeComment">// esi -&gt; hModule</span>        push edi        call __strlen        add esp,04h        add edi,eax_api_get_proc_address_loop:            push ecx            <font color="#0000ff">push edi            push esi            mov eax,offset _p_GetProcAddress            <span class="codeComment">//GetModuleHandle=GetProcAddress(hModule, lpProcName);</span>            call [ebp+eax]            pop ecx</font>            <font color="#008000">mov [ebx],eax            mov [ecx],ebx    <span class="codeComment">// JMP DWORD PTR [XXXXXXXX]</span>            add ebx,04h            add ecx,06h            push edi            call __strlen            add esp,04h            add edi,eax            mov al,<span class="codeKeyword">byte</span> ptr [edi]        test al,al        jnz _api_get_proc_address_loop        inc edi        mov al,<span class="codeKeyword">byte</span> ptr [edi]    test al,al    jnz _api_get_lib_address_loop    ...</font></font></font></pre>
<h4>6.3 Fix up the Original Import Table</h4>
<p>To run the program again, you should fix up the thunks of the actual import table; otherwise, you have a corrupted target PE file. Your code must correct all of the thunks the same as Table 5 to Table 6. Once more, </p>
<pre><font color="#808080">    ...    mov ebx,[ebp+<font color="#ff0000">_p_dwImportVirtualAddress</font>]    test ebx,ebx    jz _it_fixup_end    mov esi,[ebp+<font color="#ff0000">_p_dwImageBase</font>]    add ebx,esi             <span class="codeComment">// dwImageBase + dwImportVirtualAddress</span>_it_fixup_get_lib_address_loop:        mov eax,[ebx+00Ch]  <span class="codeComment">// image_import_descriptor.Name</span>        test eax,eax        jz _it_fixup_end        mov ecx,[ebx+010h]  <span class="codeComment">// image_import_descriptor.FirstThunk</span>        add ecx,esi        mov [ebp+<font color="#ff0000">_p_dwThunk</font>],ecx    <span class="codeComment">// dwThunk</span>        mov ecx,[ebx]       <span class="codeComment">// image_import_descriptor.Characteristics</span>        test ecx,ecx        jnz _it_fixup_table            mov ecx,[ebx+010h]_it_fixup_table:        add ecx,esi        mov [ebp+<font color="#ff0000">_p_dwHintName</font>],ecx    <span class="codeComment">// dwHintName</span>        add eax,esi  <span class="codeComment">// image_import_descriptor.Name + dwImageBase = ModuleName</span>        <font color="#0000ff">push eax     <span class="codeComment">// lpLibFileName</span>        mov eax,offset _p_LoadLibrary        call [ebp+eax]               <span class="codeComment">// LoadLibrary(lpLibFileName);</span>        test eax,eax        jz _it_fixup_end        mov edi,eax_it_fixup_get_proc_address_loop:            mov ecx,[ebp+<font color="#ff0000">_p_dwHintName</font>]    <span class="codeComment">// dwHintName</span>            mov edx,[ecx]            <span class="codeComment">// image_thunk_data.Ordinal</span>            test edx,edx            jz _it_fixup_next_module            test edx,080000000h      <span class="codeComment">// .IF( import by ordinal )</span>            jz _it_fixup_by_name                and edx,07FFFFFFFh    <span class="codeComment">// get ordinal</span>                jmp _it_fixup_get_addr_it_fixup_by_name:            add edx,esi  <span class="codeComment">// image_thunk_data.Ordinal</span>                         <span class="codeComment">// + dwImageBase = OrdinalName</span>            inc edx            inc edx                  <span class="codeComment">// OrdinalName.Name</span>_it_fixup_get_addr:            <font color="#0000ff">push edx <span class="codeComment">//lpProcName</span>            push edi                 <span class="codeComment">// hModule</span>            mov eax,offset _p_GetProcAddress            call [ebp+eax]    <span class="codeComment">// GetProcAddress(hModule, lpProcName);</span>            <font color="#008000">mov ecx,[ebp+<font color="#ff0000">_p_dwThunk</font>]    <span class="codeComment">// dwThunk</span>            mov [ecx],eax  <span class="codeComment">// correction the thunk</span>            <span class="codeComment">// dwThunk =&gt; next dwThunk</span>            add dword ptr [ebp+<font color="#ff0000">_p_dwThunk</font>], <font color="#0000ff">004h</font>            <span class="codeComment">// dwHintName =&gt; next dwHintName</span>            add dword ptr [ebp+<font color="#ff0000">_p_dwHintName</font>],<font color="#0000ff">004h</font>        jmp _it_fixup_get_proc_address_loop_it_fixup_next_module:        add ebx,014h      <span class="codeComment">// sizeof(IMAGE_IMPORT_DESCRIPTOR)</span>    jmp _it_fixup_get_lib_address_loop_it_fixup_end:    ...</font></font></font></font></pre>
<pre>
<h3>7 Support DLL and OCX</h3>

Now, you intend to include the <a href="http://en.wikipedia.org/wiki/Microsoft_Dynamic_Link_Library" target="new">dynamic link library (DLL)</a> and <a href="http://en.wikipedia.org/wiki/OCX" target="new">OLE-ActiveX Control</a> in your PE builder project. Supporting them is very easy if you pay attention to the two-time arrival into the Offset of Entry Point, the relocation table implementation, and the client import table.
<h4>PE Maker: Step 4</h4>

&nbsp;

<a name="more"><font color="#000000"> </font></a><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" target="new"><tt>LoadLibrary()</tt></a>, or an OCX is registered by using <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/loadlibrary.asp" target="new"><tt>LoadLibrary()</tt></a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getprocaddress.asp" target="new"><tt>GetProcAddress()</tt></a> through calling <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/4442206b-b2ad-47d7-8add-18002c44c5a2.asp" target="new"><tt>DllRegisterServer()</tt></a>, the first of the OEP arrival is done.

&nbsp;
<pre>hinstDLL = LoadLibrary( &quot;test1.dll&quot; );hinstOCX = LoadLibrary( &quot;test1.ocx&quot; );_DllRegisterServer = GetProcAddress( hinstOCX,                                     &quot;DllRegisterServer&quot; );_DllRegisterServer();    <span class="codeComment">// ocx register</span></pre>
</p>
<p>Download the pemaker4.zip source files from the end of the article.</p>
<h4>7.1 Twice OEP approach</h4>
<p>The Offset of Entry Point of a DLL file or an OCX file is touched by the main program atleast twice:</p>
<ul>
<li><strong>Constructor</strong>: When a DLL is loaded by </li>
<li><strong>Destructor</strong>: When the main program frees the library usage by <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/freelibrary.asp" target="new"><tt>FreeLibrary()</tt></a>, the second OEP arrival happens.
<p>&nbsp;</p>
<pre>FreeLibrary( hinstDLL );FreeLibrary( hinstOCX );</pre>
</li>
</ul>
<p>To perform this, I have employed a trick that causes in the second time again, the instruction pointer (EIP) traveling towards the original OEP by the structured exception handler.</p>
<pre><font color="#808080"><font color="#000000">_main_0:    pushad    <span class="codeComment">// save the registers context in stack</span>    call _main_1_main_1:    pop ebp    sub ebp,offset _main_1    <span class="codeComment">// get base ebp</span>    <span class="codeComment">//---------------- support dll, ocx  -----------------</span>_support_dll_0:</font>    jmp _support_dll_1        <span class="codeComment">// <font color="#ff0000">nop; nop;    // &lt;&lt; trick</font></span>                              <span class="codeComment">// in the second time OEP</span>    <font color="#000000">jmp _support_dll_2</font>_support_dll_1:    <span class="codeComment">//----------------------------------------------------</span>    ...    <span class="codeComment">//---------------- support dll, ocx  1 ---------------</span>    mov edi,[ebp+_p_dwImageBase]    add edi,[edi+03Ch]            <span class="codeComment">// edi -&gt; IMAGE_NT_HEADERS</span>    mov ax,word ptr [edi+016h]    <span class="codeComment">// edi -&gt; image_nt_headers-&gt;</span>                                  <span class="codeComment">// FileHeader.Characteristics</span>    test ax,<font color="#008000">IMAGE_FILE_DLL</font>    jz _support_dll_2        mov ax, <font color="#ff0000">9090h <span class="codeComment">// &lt;&lt; trick</span>        mov word ptr [ebp+_support_dll_0],ax</font></font><font color="#000000">_support_dll_2:    <span class="codeComment">//----------------------------------------------------</span>    ...    into OEP by SEH ...</font></pre>
<p>I hope you caught the trick in the preceding code, but this is not all of it. You have a problem in <tt>ImageBase</tt>, when the library has been loaded in different image bases by the main program. You should write some code to find the real image base and store it to use forward.</p>
<pre><font color="#808080">    mov eax,<font color="#008000">[esp+24h]</font>    <span class="codeComment">// the real imagebase</span>    mov ebx,<font color="#008000">[esp+30h]</font>    <span class="codeComment">// oep</span>    cmp eax,ebx    ja _no_dll_pe_file_0        cmp word ptr [eax],IMAGE_DOS_SIGNATURE        jne _no_dll_pe_file_0            mov [ebp+_p_dwImageBase],eax_no_dll_pe_file_0:</font></pre>
<p>This code finds the real image base by investigating the stack information. By using the real image base and the formal image base, you should correct all memory calls inside the image program!! Don't be afraid; it will be done simply by the relocating the table information.</p>
<h4>7.2 Implement relocation table</h4>
<p>To understand the relocation table better, you can take a look at Section 6.6 of the <a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx" target="new">Microsoft Portable Executable and Common Object File Format Specification</a> document. The relocation table contains many packages to relocate the information related to the virtual address inside the virtual memory image. Each package is comprised of an 8-byte header to exhibit the base virtual address and the number of data, demonstrated by the <tt>IMAGE_BASE_RELOCATION</tt> data structure.</p>
<pre><span class="codeKeyword">typedef</span> <span class="codeKeyword">struct</span> _IMAGE_BASE_RELOCATION {   DWORD   VirtualAddress;   DWORD   SizeOfBlock;} IMAGE_BASE_RELOCATION, *PIMAGE_BASE_RELOCATION;</pre>
<h4>Table 7 - The Relocation Table</h4>
<p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td align="center" rowspan="7">Block[1]</td>
<td align="center" colspan="4">VirtualAddress</td>
</tr>
<tr>
<td align="center" colspan="4">SizeOfBlock</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">...</td>
<td align="center">...</td>
<td align="center">...</td>
<td align="center">...</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">00</td>
<td align="center">00</td>
</tr>
<tr>
<td align="center" rowspan="7">Block[2]</td>
<td align="center" colspan="4">VirtualAddress</td>
</tr>
<tr>
<td align="center" colspan="4">SizeOfBlock</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">...</td>
<td align="center">...</td>
<td align="center">...</td>
<td align="center">...</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">00</td>
<td align="center">00</td>
</tr>
<tr>
<td align="center">...</td>
<td align="center" colspan="4">
<p>&nbsp;</p>
<p>... </p>
<p>&nbsp;</p>
</td>
</tr>
<tr>
<td align="center" rowspan="7">Block[n]</td>
<td align="center" colspan="4">VirtualAddress</td>
</tr>
<tr>
<td align="center" colspan="4">SizeOfBlock</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">type:4</td>
<td align="center">offset:12</td>
</tr>
<tr>
<td align="center">...</td>
<td align="center">...</td>
<td align="center">...</td>
<td align="center">...</td>
</tr>
<tr>
<td align="center">type:4</td>
<td align="center">offset:12</td>
<td align="center">00</td>
<td align="center">00</td>
</tr>
</tbody>
</table>
<p>Table 7 illustrates the main idea of the relocation table. Furthermore, you can upload a DLL or an OCX file in <a href="http://www.ollydbg.de/" target="new">OllyDbg</a> to observe the relocation table, the <em>&quot;.reloc&quot;</em> section through <em>Memory map window</em>. By the way, you find the position of the relocation table by using the following code in your project:</p>
<pre>DWORD dwVirtualAddress = image_nt_headers-&gt;  OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].  VirtualAddress;DWORD dwSize = image_nt_headers-&gt;  OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;</pre>
<p>By OllyDbg, you have the same as the following for the <em>&quot;.reloc&quot;</em> section, by using the Long Hex viewer mode. In this example, the base virtual address is <strong>0x1000</strong> and the size of the block is <strong>0x184</strong>.</p>
<pre>008E1000 : 00001000  00000184  30163000  30403028008E1010 : 30683054  308C3080  30AC309C  30D830CC008E1020 : 30E030DC  30E830E4  30F030EC  310030F4008E1030 : 3120310D  315F3150  31A431A0  31C031A8008E1040 : 31D031CC  31F431EC  31FC31F8  32043200008E1050 : 320C3208  32143210  324C322C  32583254008E1060 : 3260325C  32683264  3270326C  32B03274</pre>
<p>It relocates the data in the subsequent virtual addresses:</p>
<pre>0x1000 + 0x0000 = 0x10000x1000 + 0x0016 = 0x10160x1000 + 0x0028 = 0x10280x1000 + 0x0040 = 0x10400x1000 + 0x0054 = 0x1054...</pre>
<p>Each package performs the relocation by using consecutive 4 bytes form its internal information. The first byte refers to the type of relocation and the next three bytes are the offset that must be used with the base virtual address and the image base to correct the image information.</p>
<p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td align="center" width="30">type</td>
<td align="center" colspan="3">offset</td>
</tr>
<tr>
<td align="center"><font color="#0000ff">03</font></td>
<td align="center"><font color="#0000ff">00</font></td>
<td align="center"><font color="#0000ff">00</font></td>
<td align="center"><font color="#0000ff">00</font></td>
</tr>
</tbody>
</table>
<h4>What is the type?</h4>
<p>The type can be one of the following values:</p>
<ul>
<li><tt>IMAGE_REL_BASED_ABSOLUTE (0)</tt>: No effect </li>
<li><tt>IMAGE_REL_BASED_HIGH (1)</tt>: Relocate by the high 16 bytes of the base virtual address and the offset </li>
<li><tt>IMAGE_REL_BASED_LOW (2)</tt>: Relocate by the low 16 bytes of the base virtual address and the offset </li>
<li><tt>IMAGE_REL_BASED_HIGHLOW (3)</tt>: Relocate by the base virtual address and the offset </li>
</ul>
<h4>What is done in the relocation?</h4>
<p>By relocation, some values inside the virtual memory are corrected according to the current image base by the <em>&quot;.reloc&quot;</em> section packages.</p>
<p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td align="center"><strong>delta_ImageBase = current_ImageBase - image_nt_headers-&gt;OptionalHeader.ImageBase</strong></td>
</tr>
</tbody>
</table>
<pre>mem[ current_ImageBase + 0x1000 ] =   mem[ current_ImageBase + 0x1000 ] + delta_ImageBase ;mem[ current_ImageBase + 0x1016 ] =   mem[ current_ImageBase + 0x1016 ] + delta_ImageBase ;mem[ current_ImageBase + 0x1028 ] =   mem[ current_ImageBase + 0x1028 ] + delta_ImageBase ;mem[ current_ImageBase + 0x1040 ] =   mem[ current_ImageBase + 0x1040 ] + delta_ImageBase ;mem[ current_ImageBase + 0x1054 ] =  mem[ current_ImageBase + 0x1054 ] + delta_ImageBase ;...</pre>
<p>I have employed the following code from Morphine packer to implement the relocation.</p>
<pre><font color="#808080">    ..._reloc_fixup:    mov eax,[ebp+_p_dwImageBase]    mov edx,eax    mov ebx,eax    add ebx,[ebx+3Ch]    <span class="codeComment">// edi -&gt; IMAGE_NT_HEADERS</span>    <span class="codeComment">// edx -&gt;image_nt_headers-&gt;OptionalHeader.ImageBase</span>    mov ebx,[ebx+034h]    <font color="#ff0000">sub edx,ebx <span class="codeComment">// edx -&gt; reloc_correction    // delta_ImageBase</span>    je _reloc_fixup_end    mov ebx,[ebp+_p_dwRelocationVirtualAddress]    test ebx,ebx    jz _reloc_fixup_end    add ebx,eax_reloc_fixup_block:    mov eax,[ebx+004h]          <span class="codeComment">//ImageBaseRelocation.SizeOfBlock</span>    test eax,eax    jz _reloc_fixup_end    lea ecx,[eax-008h]    shr ecx,001h    lea edi,[ebx+008h]_reloc_fixup_do_entry:        movzx eax,word ptr [edi]<span class="codeComment">//Entry</span>        push edx        mov edx,eax        shr eax,00Ch            <span class="codeComment">//Type = Entry &gt;&gt; 12</span>        mov esi,[ebp+_p_dwImageBase]<span class="codeComment">//ImageBase</span>        and dx,00FFFh        add esi,[ebx]        add esi,edx        pop edx_reloc_fixup_HIGH:              <span class="codeComment">// IMAGE_REL_BASED_HIGH</span>        dec eax        jnz _reloc_fixup_LOW            mov eax,edx            shr eax,010h        <span class="codeComment">//HIWORD(Delta)</span>            jmp _reloc_fixup_LOW_fixup_reloc_fixup_LOW:               <span class="codeComment">// IMAGE_REL_BASED_LOW</span>            dec eax        jnz _reloc_fixup_HIGHLOW        movzx eax,dx            <span class="codeComment">//LOWORD(Delta)</span>_reloc_fixup_LOW_fixup:            <font color="#ff0000">add word ptr [esi],ax<span class="codeComment">// mem[x] = mem[x] + delta_ImageBase</span>        jmp _reloc_fixup_next_entry_reloc_fixup_HIGHLOW:           <span class="codeComment">// IMAGE_REL_BASED_HIGHLOW</span>            dec eax        jnz _reloc_fixup_next_entry        <font color="#ff0000">add [esi],edx           <span class="codeComment">// mem[x] = mem[x] + delta_ImageBase</span>_reloc_fixup_next_entry:        inc edi        inc edi                 <span class="codeComment">//Entry++</span>        loop _reloc_fixup_do_entry_reloc_fixup_next_base:    add ebx,[ebx+004h]    jmp _reloc_fixup_block_reloc_fixup_end:    ...</font></font></font></font></pre>
<h4>7.3 Build a special import table</h4>
<p>To support the <a href="http://en.wikipedia.org/wiki/OCX" target="new">OLE-ActiveX Control</a> registration, you should present an appropriate import table to your target OCX and DLL file. Therefore, I have established an import table by the following string:</p>
<pre><span class="codeKeyword">const</span> <span class="codeKeyword">char</span> *sz_IT_OCX_strings[]={   &quot;Kernel32.dll&quot;,   &quot;LoadLibraryA&quot;,   &quot;GetProcAddress&quot;,   &quot;GetModuleHandleA&quot;,   0,   &quot;User32.dll&quot;,   &quot;GetKeyboardType&quot;,   &quot;WindowFromPoint&quot;,   0,   &quot;AdvApi32.dll&quot;,   &quot;RegQueryValueExA&quot;,   &quot;RegSetValueExA&quot;,   &quot;StartServiceA&quot;,   0,   &quot;Oleaut32.dll&quot;,   &quot;SysFreeString&quot;,   &quot;CreateErrorInfo&quot;,   &quot;SafeArrayPtrOfIndex&quot;,   0,   &quot;Gdi32.dll&quot;,   &quot;UnrealizeObject&quot;,   0,   &quot;Ole32.dll&quot;,   &quot;CreateStreamOnHGlobal&quot;,   &quot;IsEqualGUID&quot;,   0,   &quot;ComCtl32.dll&quot;,   &quot;ImageList_SetIconSize&quot;,   0,   0,};</pre>
<p>Without these API functions, the library can not be loaded, and moreover the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/4442206b-b2ad-47d7-8add-18002c44c5a2.asp" target="new"><tt>DllregisterServer()</tt></a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/b71137a7-284e-4521-a3b2-9dad9c9d3c54.asp" target="new"><tt>DllUregisterServer()</tt></a> will not operate. In <tt>CPECryptor::CryptFile</tt>, I have distinguished between EXE files and DLL files in the initialization of the new import table object during creation:</p>
<pre><span class="codeKeyword">if</span>(( image_nt_headers-&gt;FileHeader.Characteristics             &amp; IMAGE_FILE_DLL ) == IMAGE_FILE_DLL ){    ImportTableMaker = <span class="codeKeyword">new</span> CITMaker( IMPORT_TABLE_OCX );}<span class="codeKeyword">else</span>{    ImportTableMaker = <span class="codeKeyword">new</span> CITMaker( IMPORT_TABLE_EXE );}</pre>
<p>&nbsp;</p>
<h3>8 Preserve the Thread Local Storage</h3>
<p>By using Thread Local Storage (TLS), a program is able to execute a multithreaded process, This performance mostly is used by <a href="http://www.borland.com/" target="new">Borland</a> linkers: <a href="http://www.borland.com/us/products/delphi/index.html" target="new">Delphi</a> and <a href="http://www.borland.com/us/products/cbuilder/index.html" target="new">C++ Builder</a>. When you pack a PE file, you should take care to keep the TLS clean; otherwise, your packer will not support Borland Delphi and C++ Builder linked EXE files. To comprehend TLS, I refer you to Section 6.7 of the <a href="http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx" target="new">Microsoft Portable Executable and Common Object File Format Specification</a> document. You can observe the TLS structure by <tt>IMAGE_TLS_DIRECTORY32</tt> in <em>winnt.h</em>.</p>
<pre><span class="codeKeyword">typedef</span> <span class="codeKeyword">struct</span> _IMAGE_TLS_DIRECTORY32 {   DWORD   StartAddressOfRawData;   DWORD   EndAddressOfRawData;   DWORD   AddressOfIndex;   DWORD   AddressOfCallBacks;   DWORD   SizeOfZeroFill;   DWORD   Characteristics;} IMAGE_TLS_DIRECTORY32, * PIMAGE_TLS_DIRECTORY32;</pre>
<p>    <a name="more"><font color="#000000"> </font></a><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/dialogboxreference/dialogboxfunctions/messagebox.asp" target="new"><tt>MessageBox()</tt></a> from <em>user32.dll</em>.</p>
<p>To keep the TLS directory safe, I have copied it in a special place inside the loader:</p>
<pre><font color="#808080">..._tls_dwStartAddressOfRawData:   dword_type(0xCCCCCCCC)_tls_dwEndAddressOfRawData:     dword_type(0xCCCCCCCC)_tls_dwAddressOfIndex:          dword_type(0xCCCCCCCC)_tls_dwAddressOfCallBacks:      dword_type(0xCCCCCCCC)_tls_dwSizeOfZeroFill:          dword_type(0xCCCCCCCC)_tls_dwCharacteristics:         dword_type(0xCCCCCCCC)...</font></pre>
<p>It is necessary to correct the TLS directory entry in the Optional Header:</p>
<pre><span class="codeKeyword">if</span>(image_nt_headers-&gt;   OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].   VirtualAddress!=0){   memcpy(&amp;pDataTable-&gt;image_tls_directory,          image_tls_directory,          <span class="codeKeyword">sizeof</span>(IMAGE_TLS_DIRECTORY32));   dwOffset=DWORD(pData1)-DWORD(pNewSection);   dwOffset+=<span class="codeKeyword">sizeof</span>(t_DATA_1)-<span class="codeKeyword">sizeof</span>(IMAGE_TLS_DIRECTORY32);   image_nt_headers-&gt;      OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].      VirtualAddress=dwVirtualAddress + dwOffset;}</pre>
<h3>9 Inject Your Code</h3>
<p>You are ready to place your code inside the new section. Your code is a &quot;Hello World!&quot; message by </p>
<pre><font color="#808080">...push MB_OK | MB_ICONINFORMATIONlea eax,[ebp+_p_szCaption]push eaxlea eax,[ebp+_p_szText]push eaxpush <span class="codeKeyword">NULL</span>call _jmp_MessageBox<span class="codeComment">// MessageBox(NULL, szText, szCaption, MB_OK | MB_ICONINFORMATION) ;</span>...</font></pre>
<h4>PE Maker: Step 5</h4>
<p>Download the pemaker5.zip source files from the end of the article.</p>
<p><img height="119" src="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=HELLOWORLD_GIF&amp;ds=20060302" width="146" alt="" /></p>
<h3>10 Conclusion</h3>
<p>By reading this article, you have perceived how easily you can inject code to a portable executable file. You can complete the code by using the source of other packers, create a packer in the same way as <a href="http://yodap.sourceforge.net/" target="new">Yoda's Protector</a>, and make your packer undetectable by mixing up with <a href="http://www.hxdef.org/download.php" target="new" class="broken_link">Morphine</a> source code. I hope that you have enjoyed this brief discussion of one part of the reverse engineering field. See you again in the next discussion!</p>
<p>&nbsp;</p>
</pre>
<p>    </a><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/exception_pointers_str.asp" target="new"><tt>EXCEPTION_POINTERS</tt></a>, you have access to the pointer of <tt>ContextRecord</tt>. The <tt>ContextRecord</tt> has the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/context_str.asp" target="new"><tt>CONTEXT</tt></a> data structure, as seen in Table 4. This is the thread context during the exception time. When you ignore the exception by <tt>EXCEPTION_CONTINUE_SEARCH (0)</tt>, the instruction pointer, as well as the context, will be set to <tt>ContextRecord</tt> to return to the previous condition. Therefore, if you change the <tt>Eip</tt> of the Win32 Thread Context to the Original Offset of Entry Point, it will come clearly into OEP.</a><a href="http://www.codeguru.com/dbfiles/get_image.php?id=11393&amp;lbl=SCREENSHOT_JPG&amp;ds=20060302" target="_blank">Full Size Image</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/330.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toolband (Toolbar for IE) sample using WTL</title>
		<link>http://www.donevii.com/post/326.html</link>
		<comments>http://www.donevii.com/post/326.html#comments</comments>
		<pubDate>Mon, 21 May 2007 02:12:58 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[toolbar]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[wtl]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=326</guid>
		<description><![CDATA[转至: http://www.codeproject.com Download source files &#8211; 40 Kb Download demo project &#8211; 40 Kb Credits and Acknowlegements This module is based on the ATL DeskBand Wizard article that was created by Erik Thompson. My thanks goes to him for pr... ]]></description>
			<content:encoded><![CDATA[<p>转至: <a href="http://www.codeproject.com">http://www.codeproject.com</a></p>
<ul class="download">
<li><a href="http://www.codeproject.com/wtl/toolband/toolband_src.zip" class="broken_link">Download source files &#8211; 40 Kb</a> </li>
<li><a href="http://www.codeproject.com/wtl/toolband/toolband_demo.zip" class="broken_link">Download demo project &#8211; 40 Kb</a> </li>
</ul>
<p><!-- Article image -->
<p><img height="298" alt="Sample Image - toolband10.gif" width="595" src="http://www.codeproject.com/wtl/toolband/toolband10.gif" /></p>
<p><!-- Add the rest of your HTML here --><br />
<h2>Credits and Acknowlegements</h2>
<p nd="1">This module is based on the <a href="http://www.codeproject.com/atl/rbdeskband.asp">ATL DeskBand Wizard article</a> that was created by <a href="mailto:erikt@radbytes.com">Erik Thompson</a>. My thanks goes to him for producing a great wizard that saves you the nity grity of creating DeskBands.</p>
<p align="left" nd="2">Please note that this project does not use MFC, for all those MFC die hards that want to use MFC in their Tool Bands I suggest you down load the KKBar sample from microsofts MSDN site.</p>
<p align="left"><a href="http://support.microsoft.com/support/kb/articles/Q246/2/34.ASP" class="broken_link">The KBBar Sample can be downloaded from here</a></p>
<h2 align="left">Creating the ToolBand Module</h2>
<p align="left" nd="3">You will need to install the <a href="http://www.codeproject.com/atl/rbdeskband.asp">ATL DeskBand Wizard</a> in order to create ToolBands. Please follow the instruction in this article.</p>
<p align="left" nd="4">The best way to kick start a Tool band project is to use the ATL COM App Wizard. The COM Object needs to be in-proc therefore choose &#8216;DLL&#8217; as the <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">Server</a> Type. The rest of the options can be kept in there default state. (Note this project does not use MFC, as the project is based on ATL and <a href="http://www.donevii.com/post/tag/wtl" class="st_tag internal_tag" rel="tag" title="Posts tagged with wtl">WTL</a>)</p>
<p align="left" nd="5">Once you have a ATL COM Project, You can use the <a href="http://www.codeproject.com/atl/rbdeskband.asp">ATL DeskBand Wizard</a> to create the Initial Tool Band.</p>
<h2 align="left">Adding Support for WTL</h2>
<p align="left" nd="6">You will require the WTL Libraries, these can be downloaded from the microsoft site</p>
<p align="left" nd="7">See <a href="http://www.codeproject.com/wtl/wtlinst.asp">Installation of WTL</a></p>
<p align="left" nd="8">The following header files needs to be added to stdafx.h file</p>
<pre nd="9">atlapp.hatlwin.hatlctrls.hatlmisc.h</pre>
<h2 align="left">Create the <a href="http://www.donevii.com/post/tag/toolbar" class="st_tag internal_tag" rel="tag" title="Posts tagged with toolbar">ToolBar</a></h2>
<p nd="10">You can create your <a class="iAs" style="FONT-WEIGHT: normal; FONT-SIZE: 100%; PADDING-BOTTOM: 1px; COLOR: darkgreen; BORDER-BOTTOM: darkgreen 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" target="_blank" itxtdid="3922941" href="http://www.codeproject.com/wtl/toolband.asp#"><font color="#006400">toolbar</font></a> in the usual way via the resource editor, Once you have your toolbar you need to create the toolbar dynamically. The <code nd="11">CBandToolBarCtrl</code> is inherited from CToolBarCtrl and is used to create the toolbar dynamically.</p>
<pre nd="12">DWORD dStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |                CCS_NODIVIDER | CCS_NORESIZE | CCS_NOPARENTALIGN |                TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;    HWND hWnd = m_wndToolBar.CreateSimpleToolBarCtrl(hWndChild, IDR_TOOLBAR_TEST,                                                  FALSE, dStyle); </pre>
<p align="left" nd="13">This is done in the RegisterAndCreateWindow function that is called from SetSite method.</p>
<h2 align="left">Message Reflection</h2>
<p align="left" nd="14">The best way to handle the messages of the toolbar is to let the control handle its own messages via &quot;Message Reflection&quot;. The best way to reflect the messages is to create an invisible control that acts as the parent for the toolbar. The invisible control will reflect the toolbar messages back to itself. See <code nd="15">CBandToolBarReflectorCtrl</code> <a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> for the invisible control that will be used.</p>
<p align="left" nd="16">The following code shows the parent child relationship that is used to achieve Message Reflection.</p>
<pre id="pre2" style="MARGIN-TOP: 0px" nd="18">BOOL CToolBandObj::RegisterAndCreateWindow(){    RECT rectClientParent;    ::GetClientRect(m_hWndParent, &amp;rectClientParent);    <span class="cpp-comment">// We need to create an Invisible Child Window using the Parent Window, </span>    <span class="cpp-comment">// this will also be used to reflect Command</span>    <span class="cpp-comment">// messages from the rebar</span>    HWND hWndChild = m_wndInvisibleChildWnd.Create(m_hWndParent,                                                    rectClientParent,                                                    NULL, WS_CHILD);        <span class="cpp-comment">// Now we can create the Tool Bar, using the Invisible Child</span>    DWORD dStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |                    CCS_NODIVIDER | CCS_NORESIZE | CCS_NOPARENTALIGN |                    TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;        HWND hWnd = m_wndToolBar.CreateSimpleToolBarCtrl(hWndChild,

                                                     IDR_TOOLBAR_TEST,                                                      FALSE, dStyle);     m_wndToolBar.SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);              m_wndToolBar.m_ctlBandEdit.m_pBand = <span class="cpp-keyword">this</span>;    <span class="cpp-keyword">return</span> ::IsWindow(m_wndToolBar.m_hWnd);}</pre>
<p align="left" nd="19">The following Macros are used to identify the reflected messages from the ordinary messages. e.g WM_COMMAND reflected comes back as OCM_COMMAND.</p>
<pre nd="21"><span class="cpp-preprocessor" nd="20">#define OCM_COMMAND_CODE_HANDLER(code, func) \</span><span class="cpp-keyword">if</span>(uMsg == OCM_COMMAND &amp;&amp; code == HIWORD(wParam)) \{ \    bHandled = TRUE; \    lResult = func(HIWORD(wParam), LOWORD(wParam), (HWND)lParam, bHandled); \    <span class="cpp-keyword">if</span>(bHandled) \    <span class="cpp-keyword">return</span> TRUE; \}<span class="cpp-preprocessor" nd="22">#define OCM_COMMAND_ID_HANDLER(id, func) \</span><span class="cpp-keyword">if</span>(uMsg == OCM_COMMAND &amp;&amp; id == LOWORD(wParam)) \{ \    bHandled = TRUE; \    lResult = func(HIWORD(wParam), LOWORD(wParam), (HWND)lParam, bHandled); \    <span class="cpp-keyword">if</span>(bHandled) \    <span class="cpp-keyword">return</span> TRUE; \}<span class="cpp-preprocessor" nd="23">#define OCM_NOTIFY_CODE_HANDLER(cd, func) \</span><span class="cpp-keyword">if</span>(uMsg == OCM_NOTIFY &amp;&amp; cd == ((LPNMHDR)lParam)-&gt;code) \{ \    bHandled = TRUE; \    lResult = func((<span class="cpp-keyword">int</span>)wParam, (LPNMHDR)lParam, bHandled); \    <span class="cpp-keyword">if</span>(bHandled) \    <span class="cpp-keyword">return</span> TRUE; \}</pre>
<h2 align="left">Browser Navigation</h2>
<p align="left" nd="24">In order to Navigate on the <a class="iAs" style="FONT-WEIGHT: normal; FONT-SIZE: 100%; PADDING-BOTTOM: 1px; COLOR: darkgreen; BORDER-BOTTOM: darkgreen 0.07em solid; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" target="_blank" itxtdid="3924683" href="http://www.codeproject.com/wtl/toolband.asp#"><font color="#006400">browser</font></a> you need to instantiate the IWebBrowser2 COM Object. This is usually done on the SetSite Method e.g.</p>
<pre nd="25">IServiceProviderPtr pServiceProvider = pUnkSite;<span class="cpp-keyword">if</span> (_Module.m_pWebBrowser)    _Module.m_pWebBrowser = NULL; <span class="cpp-keyword">if</span>(FAILED(pServiceProvider-&gt;QueryService(SID_SWebBrowserApp, IID_IWebBrowser,                                             (<span class="cpp-keyword">void</span>**)&amp;_Module.m_pWebBrowser)))<span class="cpp-keyword">return</span> E_FAIL;</pre>
<p align="left" nd="26">Once you have the COM Object Instantiated, you can move to your URL using the navigate method</p>
<pre nd="27">	_variant_t varURL = _bstr_t(<span class="cpp-string" nd="28"><a href="http://www.codeproject.com">www.codeproject.com</a></span>); _variant_t varEmpty;_Module.m_pWebBrowser-&gt;Navigate2(&amp;varURL, &amp;varEmpty, &amp;varEmpty,                                     &amp;varEmpty, &amp;varEmpty);</pre>
<h2>Drag and Drop Edit and ComboBox Control</h2>
<p nd="29">The CBandEditCtrl class is inherited from a WTL CEdit control that has drag and drop facility. i.e. you can drag text from the browser straight to the CEdit Control.</p>
<p nd="30">The CBandComboBoxCtrl class is inherited from a WTL CComboBox control that has drag and drop facility. i.e. you can drag text from the browser straight to the CComboBox Control.</p>
<p><img height="162" alt="Drag and Drop URL - toolband2.gif" width="630" src="http://www.codeproject.com/wtl/toolband/toolband2.gif" /></p>
<p>The Edit control in this sample module allows you to drag a URL from Explorer or any other Dragable enabled container. Once you have dropped the URL the toolband will go to that site. </p>
<h2>Configurable Toolbar Button Styles</h2>
<p nd="32">The CBandToolBarCtrl class allows you to have the follwoing button styles on the toolbar</p>
<ul>
<li nd="33">Image and Text on the right </li>
<li nd="34">Image and Text on the bottom </li>
<li nd="35">Image only</li>
</ul>
<p><img height="80" alt="Text on Right - toolband3.gif" width="560" src="http://www.codeproject.com/wtl/toolband/toolband3.gif" /></p>
<p><img height="89" alt="No Text - toolband4.gif" width="397" src="http://www.codeproject.com/wtl/toolband/toolband4.gif" /></p>
<p><img height="112" alt="Text Under - toolband5.gif" width="551" src="http://www.codeproject.com/wtl/toolband/toolband5.gif" /></p>
<h2>Pop-up Menu Tracking</h2>
<p nd="36">A pop up menu is used to get to the configuration options</p>
<p><img height="200" alt="Popup Menu Tracking - toolband6.gif" width="570" src="http://www.codeproject.com/wtl/toolband/toolband6.gif" /></p>
<h2>ToolTips</h2>
<p nd="37">This has been taken from MSDN to explain why you need to handle the tooltips on the toolbar your self.</p>
<p><em>Tool tips are automatically displayed for buttons and other controls contained in a parent window derived from CFrameWnd. This is because CFrameWnd has a default handler for the TTN_GETDISPINFO notification, which handles TTN_NEEDTEXT notifications from tool tip controls associated with controls.</em></p>
<p><em>However, this default handler is not called when the TTN_NEEDTEXT notification is sent from a tool tip control associated with a control in a window that is not a CFrameWnd, such as a control on a dialog box or a form view. Therefore, it is necessary for you to provide a handler function for the TTN_NEEDTEXT notification message in order to display tool tips for child controls.</em> </p>
<p nd="38">This can be easily done in WTL by using the following Message Handler in the overriden ToolBar Control</p>
<pre nd="39">NOTIFY_CODE_HANDLER(TTN_NEEDTEXT, OnToolbarNeedText)</pre>
<p nd="40">The following is the code that loads the tool tips from the resources and sets the tool tip text. </p>
<pre nd="41">LRESULT CBandToolBarCtrl::OnToolbarNeedText(<span class="cpp-keyword">int</span> <span class="cpp-comment">/*idCtrl*/</span>, LPNMHDR pnmh, BOOL&amp;                                             bHandled)	{    CString sToolTip;        <span class="cpp-comment">//-- make sure this 1is not a seperator</span>    <span class="cpp-keyword">if</span> (idCtrl != <span class="cpp-literal">0</span>)     {        <span class="cpp-keyword">if</span> (!sToolTip.LoadString(idCtrl))        {            bHandled = FALSE;            <span class="cpp-keyword">return</span> <span class="cpp-literal">0;</span>        }    }    LPNMTTDISPINFO pttdi = <span class="cpp-keyword">reinterpret_cast</span>&lt;LPNMTTDISPINFO&gt;	    (pnmh);pttdi-&gt;lpszText = MAKEINTRESOURCE(idCtrl);    pttdi-&gt;hinst = _Module.GetResourceInstance();    pttdi-&gt;uFlags = TTF_DI_SETITEM;    <span class="cpp-comment">//-- message processed</span>    <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}</pre>
<h2>Update the Status Bar</h2>
<p nd="42">You can update the status bar using the browser method put_StatusText, this method can be used typically in the WM_MENUSELECT event</p>
<p nd="43">The following is the code that loads up the menu text from the resources and displays it on the browser status bar.</p>
<div class="precollapse" id="premain8" style="WIDTH: 100%"><img id="preimg8" style="CURSOR: hand" height="9" alt="" width="9" preid="8" src="http://www.codeproject.com/images/minus.gif" /><span id="precollapse8" style="MARGIN-BOTTOM: 0px; CURSOR: hand" nd="44" preid="8"> Collapse</span></div>
<pre id="pre8" style="MARGIN-TOP: 0px" nd="45">LRESULT CBandToolBarCtrl::OnMenuSelect(UINT <span class="cpp-comment">/*uMsg*/</span>,                                        WPARAM wParam, LPARAM lParam,                                        BOOL&amp; bHandled) {    WORD nID = LOWORD(wParam);    WORD wFlags = HIWORD(wParam);        <span class="cpp-comment">//-- make sure this is not a seperator</span>    CString sStatusBarDesc;    <span class="cpp-keyword">if</span> ( !(wFlags &amp; MF_POPUP) )    {        <span class="cpp-keyword">if</span> (nID != <span class="cpp-literal">0</span>)         {            <span class="cpp-keyword">if</span> (!sStatusBarDesc.LoadString(nID))            {	        bHandled = FALSE;                <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;            }            <span class="cpp-keyword">int</span> nPos = sStatusBarDesc.Find(_T(<span class="cpp-string" nd="46">&quot;\n&quot;</span>));            <span class="cpp-keyword">if</span> (nPos != -<span class="cpp-literal">1</span>)            {                sStatusBarDesc =                 sStatusBarDesc.Left(nPos+<span class="cpp-literal">1</span>);                _Module.m_pWebBrowser-&gt;                put_StatusText(_bstr_t(sStatusBarDesc));                <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;            }        }    }    <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}</pre>
<h2>How to add a Chevron to your toolband</h2>
<p nd="47">In order to add a chevron to your toolband you need to add the DBIMF_USECHEVRON flags to your GetBandInfo method in the DBIM_MODEFLAGS mask.</p>
<pre nd="48"> ... <span class="cpp-keyword">if</span>(pdbi-&gt;dwMask == DBIM_MODEFLAGS)     {         <span class="cpp-comment">//AddChevron</span>        pdbi-&gt;dwModeFlags = DBIMF_NORMAL | DBIMF_VARIABLEHEIGHT |                                   DBIMF_USECHEVRON | DBIMF_BREAK;    }...</pre>
<p nd="49">This basically add the ability to show the chevron, if you want to see it appear on your toolband, then you must make sure the pdbi-&gt;ptMinSize.x value is less then your pdbi-&gt;ptActual.x value (Again this values can be set in the GetBandInfo method.</p>
<p nd="50">In order to handle the events of the button in the chevron menu, you must subclass the rebar control which is hosting your toolbar. The following steps have been used to sublass the rebar control</p>
<p nd="51">1. Find the rebar control &#8211; This can be achieved by getting the browsers window handle and searching for all its child <a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">windows</a>, until you find the rebar control.</p>
<p nd="52">2. Once you have found the rebar control you can simply subclass it using an ATL CContainedWindow.</p>
<pre nd="53">    BOOL CBandToolBarCtrl::SetBandRebar()    {         HWND hWnd(NULL);        _Module.m_pWebBrowser-&gt;get_HWND((<span class="cpp-keyword">long</span>*)&amp;hWnd);         <span class="cpp-keyword">if</span> (hWnd == NULL) 	    <span class="cpp-keyword">return</span> FALSE;	m_ctlRebar.m_hWnd = FindRebar(hWnd);	<span class="cpp-keyword">if</span> (m_ctlRebar.m_hWnd == NULL) 	    <span class="cpp-keyword">return</span> FALSE;	m_RebarContainer.SubclassWindow(m_ctlRebar);	<span class="cpp-keyword">return</span> TRUE;    }</pre>
<p nd="54">Once you have subclass the window, the events should reach the toolbar class.</p>
<h2>
<h2>Append to the Browser Context Menu</h2>
</h2>
<p><img height="451" alt="Sample Image - toolband11.gif" width="601" src="http://www.codeproject.com/wtl/toolband/toolband11.gif" /></p>
<p nd="55">I tried to duplicate the google and codeproject toolband right mouse browser context menu searches without any luck, until I looked at the binary resources, I found that you can extend the internet explorer menu by adding the option in the registry. This can easily be achieved by adding an entry in your .rgs file.</p>
<pre nd="56">HKCU{    NoRemove Software    {        NoRemove Microsoft        {            NoRemove 'Internet Explorer'            {                NoRemove MenuExt                {                    ForceRemove '&amp;Sample Toolband Serach' = s'res:<span class="cpp-comment">//%MODULE%/MENUSEARCH.HTM'</span>                    {                        val Contexts = b '<span class="cpp-literal">10</span>'                    }                }            }        }    }}</pre>
<p nd="57">Note the value of the registry item (a <a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">html</a> script that exist in the resources of the module).</p>
<p nd="58">see MENUSERACH.HTM under HTML in the module resource to see what the script is doing</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/326.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

