Why write your own packer when there are so many existing ones to choose from? Well, aside from making your executables smaller, packing is a good way to quickly and easily obfuscate your work. Existing well-know packers either have an explicit 'unpack' function, or there are readily available procdump scripts for generating an unpacked version.
市面上已经有这么多的加壳工具可供选择为什么还要自己写呢?答案是,除了让你的软件更加小,加壳还有一个好处就是快而简单的使自己的软件得到保护。现有的知名的加壳工具基本上都含有“脱壳”功能,或者可以得到加壳脚本而写出一个脱壳的版本。
Since this document has quickly exploded in length I'm going to break it up into separate installments. In this installment I will cover the qualitative aspects of producing a packer. I'll discuss what you're getting into and how the packer is structured in general. I'll briefly discuss some pitfalls, and I'll give some links to technical information you will need to be familiar with before going into the next installments.
因为这个文档比较大,我将其分为几个环节。在这个部分里我会描述关于制作加壳工具的质量问题。我会讨论关于如何开始以及一个壳的基本结构。主要涉及一些问题,而且我会提供一些关于技术信息的链接,在你进入第二环节前使你对写壳有个更深入的了解。
In the next two installments I'll go into details of how to implement the components of the packer and how I usually go about producing them.
在接下来的2个环节里我会详细讲述如何完善你的壳和平常如何加在你的程序里。
What You're Getting Into
如何开始
It's not really hard, per se, but it is rather tedious code. Lots of pointer manipulation and translation to keep track of. Aside from that, if you can write code to add and subtract integers and do file IO, you've got all the skill needed! As mentioned, it is tedious code so you will probably do well to not attempt this coding on a hangover; trust me, I know.
说实话其实不难,只是无聊的写代码。很多的指针操作和转换需要明白。把那个放一边,假如你会写代码,会加减整数,还有会操作文件IO,我想你已经掌握了所有的需要的技术!像先前提到的,这个只是无聊的代码编写,你可以做的很好,相信我,我知道。
FYI, the last packer I produced was fairly full-functioned (exes and dlls, several compression algorithms with debug capability and advanced support such as TLS (critical for Delphi apps)) and it weighed in at about 3700 lines for the packer tool and about 1000 lines for the decompression stub it embeds in the target. That's somewhere around 70 printed pages of code. So, not a huge app, but not a tiny one either. The first one I produced took about 1.5 weeks to produce including research and bug fixing. Subsequent ones took far less since I had already done the hard part, which is figuring out how. Hopefully this document will save you that time as well!
FYI,我做的最新的一个功能相当完备的加壳机(EXEs 和 DLLs, 几种压缩算法,具有调试功能,高级技术的使用入TLS(对于Delphi的程序可能有点错误)),该程序差不多有3700行代码用于加壳工具,还有1000行用于嵌入目标程序的解压块。打印了70页纸。也就是说,不是很大的程序,但也不是一个小的。我第一个开发的差不多花了一个半星期(包括开发和调试)。接下来的几个花的时间就少了,因为我已经将困难的部分做好了。希望这个文档同样可以给你省下时间。
You do not have to use assembler for the most part. If you can part with supporting some esoteric features, you won't have to use it at all. All of that is relevant for the decompression stub only anyway. The packer can be in Logo or Object-Oriented COBOL if you like.
你大部分时间不需要使用汇编工具。假如你不开发一些深奥的功能,你根本没必要用。所有的都只和嵌入的解压程序段有关,假如你喜欢,你可以使用LOGO或者Object-Oriented COBOL。
OK, enough of the blahblahblah, on to technical stuff....
好,废话不多说了,开始我们的技术资料。
Big Picture
总览
Simple. Executable is analyzed, transformed, and an extra piece of code is attached which gets invoked instead of the original program. This piece is called a 'stub' and decompresses the image to its original location. Then it jumps to that original location. But you know this already.
简单,程序经过分析,转换,然后将一段代码插入程序代替原程序首先被调用,这个被称为‘stub’,解压源程序到原地址,然后它再跳到原地址。这个你已经知道。
Sounds simple, but there are pitfalls that await you. Some of these include:
听起来简单,但是还有一些问题在等待你,部分如下:
* Support for simplified Thread Local Storage, which is key in supporting Delphi applications
支持单一的线程局部存储器(Thread Local Storage),支持Delphi程序的关键。
* Support for code relocation fixups in dlls if you care about packing dlls. Recall ActiveX controls are dlls too, as are other common things you might be interested in packing
支持DLLs加壳,要对代码的偏移修正。同样ActiveX也是DLLs, 假如你感兴趣的话还有其他一般性的东西你需要考虑。
* Support for some stuff that must be available even in the compressed form. This includes some of your resources and export names in dlls
支持压缩形式的数据,包括一些资源和DLLs里的输出名。
* Dealing with bound imports
处理被限制的输入。
* Support for stripping out directory entries that will confuse the Windows loader since the decompression won't have happened and they will point to nothing useful, like the IAT and debug info
支持剥除可能使Windows载入器出错的目录项,使解压呈现可以执行,防止代码指向无用的数据,比如图象注释带还有调试信息。
* Support for doing relocation fixups manually on your decompression stub since it will certainly be in a different memory location than where the linker thought it would be when it was compiled
支持对解压程序段手工进行偏移矫正,因为被编译后的文件在连接过程中可能内存地址会有出入。
* Dealing with differences in interpretation of the PE spec between different vendor's linkers. Borland linkers interpret aspects of the spec differently from Microsoft's so you need to be ready for that.
对不同的连接器必须有不同的PE格式解释。Borland连接器和Microsoft的对于格式的解释是不一样的,你必须有准备。
* Working around bugs in Microsoft code. There is an infamous one relating to OLE and the resource section. Many packers do not accommodate this and this is important for ActiveX support.
处理Microsoft代码里的bug。一个很有名的是关于对象链接和嵌入以及资源段。很多加壳工具不能很好的处理这个,而且这个对于支持ActiveX是非常重要的。
First Step
第一步
OK, enough of the horror stories. The first step is to get painfully familiar with the file format of executables. This is called the 'Portable Executable' format, or PE for short. I will discuss it briefly here. You will need more detail in reality. Rather than attempting to duplicate that, here are some references you will find helpful:
讲的恐怖了些。第一步很痛苦,就是要熟悉可执行文件格式(PE,Portable Executable)。我会简单讨论一下,但你需要更多具体的细节。为了避免直接抄袭,这里有一些参考资料可能对你有帮助:
a good and readable discussion, but not totally accurate when it comes to the import section.
Dead wrong in implying that these sections always exist -- they easily can not exist. Still, a good read.
不错而且易理解的讨论,但在涉及输入段(import section)的时候不是很确切。有个致命错误就是认为这些段是必需存在的--事实上他们可以不存在。但还是不错的文章。
OK, after you've gotten familiar with those, we can start to write some code. I'm going to save that for the next installments (probably two). They will detail:
在你了解了以上内容后,我们可以开始写一些代码了。接下来的部分(可能有两个)我们会具体讲解:
* Making the Unpacker Stub
制作脱壳程序段
The stub has several responsibilities aside from the obvious decompression. It also has to perform duties normally done by the Windows loader.
这个程序段除了解压功能,还要充当Windows载入器的功能。
* Making the Packer Application
制作加壳程序
The packer application does all the hard work. This makes since when you realize the stub is supposed to do as little as possible to have a minimum impact on runtime.
加壳程序的制作是最困难的部分。因为你必须考虑到加壳以后对原程序在运行时间上的影响要最小。
I'll try to keep code examples to a minimum but there may be some reference to structure members when describing what's going on and maybe a snippet or two where code is clearer than human language. Most of the important structures can be found in WINNT.H for those who wish to read ahead
我会试着保留一些最精简的代码,但是当描述程序的执行过程时可能涉及到结构成员(structure member)而且有些地方是非常好理解的。大多数重要的的结构可以在WINNT.H里找到,你可以去看一下。
Continuo...
继续。。
This series is about creating exe packers for Windows 32-bit files.
这个系列是关于对Windows 32位程序进行加壳的程序的制作。
Last installment I mentioned some of the big-picture aspects of creating an exe packer. In this installment I am going to talk about a particular part of the packer, the decompression stub. This is the simpler part. In the next installment(s) I'll talk about the packer application itself. Again, this isn't going to be source for a packer, but I might do a straightforward one and publish it as an addendum to this series if folks are interested in having some working source as a starting point.
上一部分我提到了关于制作加壳程序主要方面。这一部分我将讨论加壳程序的一个具体部分:解压程序段。这个是比较简单的部分。在下面几部分里面我会讨论加壳程序主体的制作。同样在附录部分我会直接给你一个程序假如你想将这个作为你自己程序的加壳器,或者做一些改进。
Intro
介绍
The decompression stub has several responsibilities:
解压程序段必须具备以下功能:
* Find the packed data
定位已加壳的数据
* Restore data contents
恢复数据内容
* Perform relocation fixups
对偏移的修正
* Resolve all imports since the Windows loader couldn't do it
决定所有输入,因为Windows载入器无法使用。
* Perform thread local storage duties since the Windows loader couldn't do it
充当线程局部存储器(thread local storage)的功能,因为Windows载入器无法使用。
* Boink over to the original program
要指向原程序
* You may also have to handle being reentered if you are packing a dll
假如给DLLs加壳还需要重新加入句柄。
Oh, and it also has to run. So lets start with that...
而且它需要运行,所以让我们从这里开始吧。。
A Stub That Runs
可以运行的程序段
It's useful to remember that your decompression stub is actually a parasite onto a program that was never expecting for it to be there. As such, you should try to minimize your impact on the runtime environment in your packer. I had mentioned before that you could make a packer in Logo or Object-Oriented COBOL, and that really was only partially true. You can make the packer application that way fer sure -- and you might even be able to make the unpacker that way sometimes -- but you will really be much happier with C/C++/ASM for the stub part. I personally like C++. Anyway, it will be smaller. If you don't care about the size, still using stuff like Delphi or VB for the stub would be problematic because it hoists in subtle stuff like TLS and runtimes, and they don't have primitives needed to thunk over to the original program. Plus it can hose COM stuff that the original app isn't expecting. So let's assume the unpacker will be in the lower-level languages I spoke of and take solace that this is pretty straightforward code, and that the packer still can be in whatever.
你必须牢记你的解压程序对于原程序来说就像寄生虫一样,它原本不该在那里,所以,你要将它对程序运行时间的影响降至最低。我曾提到过你可以用Logo或者面向对象的 COBOL,那只是相对正确的。你当然可以那样做你的加壳程序--而且你甚至可以在同样条件下做出解壳程序--但是,假如你用C/C++/ASM来编写你的程序段会更令人高兴。我个人喜欢C++,它做的会比较小。假如你不介意程序大小,仍旧使用Delphi 或 VB可能会有一些问题,因为它在敏感信息(像线程局部存储器thread local storage)和运行时间处理上不是很好,而且无法跳至原程序。加上它可能会使原程序丢失一些COM信息。因此用我提到的低级语言来编写解壳程序,这样就可以得到非常简洁的代码,而且无论怎样加壳程序都是可用的。
Since the stub is a parasite, and since it will have to be located in a spot at the original application's convenience, we will have to be relocating it dynamically in the packer application. To help with this we will make the stub with relocation records. These are usually used for dlls when they can't be loaded at their preferred address. We will make use of them when binding the stub to the original application.
因为你的程序段就像寄生虫,而且它的插入会给原程序带来不便。我们必须在加壳程序里动态对其进行重新定位。因此我们会使该程序段包含偏移信息。这个原本是用于DLLs的,当一个DLL不能被载入适当的地址。我们会使用这一技术来将程序段绑定到原程序。
If you're an avid ASM coder, many things are more straightforward since you can take care to produce position-independent code. This won't necessarily free you from all relocation concerns, however. The decompression library of choice may well not be position independent. Also, and references to global data will need to be fixed up.
假如你想用汇编,很多事情看来可能更直接,因为你要侧重编写自由定位的代码。你仍旧需要关心所有的偏移,解压信息库的选择最好不是自由定位的,还有公用数据的地址也需要修正。
You can pretty much use whatever compressor library you want, so long as you build it in a way that doesn't spew out stuff via printf or other UI functions. There are plenty free compressors out there. You might want to start with something like zlib. It won't give you the best compression, but I know it works in this scenario. Also, another is UCL. This compresses better and is much smaller code-wise. It is GPL, however, and maybe you care about the licensing implications.
你可以选择自己喜欢的压缩库,获取他们的输出以供使用。现在有很多免费的压缩器。你或许打算采用zlib压缩库。这个压缩比并非最高,但是对我们来说比较适用(译注:估计是免费,使用也方面什么的)。另外一个叫做UCL的,这个压缩比更好,并且代码更加优化。但是这个是GPL,存在版权问题。
Check the docs to the compressor you want for configuration options and related stuff. For example, BZip2 requires BZ_NO_STDIO to be defined to have no printf stuff.
检查一下你所采用的压缩库的文档。例如,BZip2需要BZ_NO_STDIO来定义成不显示输出结果。
Configure the build to be compatible with the stub and compression library. For me, I disable RTTI and make sure I am linking the static runtime library, multithreaded. I optimize for size. The output should produce a static library, of course, rather than a dll, since the goal is to add no dependencies beyond the apps original ones.
配置好相应压缩库的初始资源。例如我这里,取消了RTTT并且连接上静态运行库,多线程,优化大小。这样便可以得到一个静态库,请注意不是一个DLL,因为我们不希望在在引入表里面显示(译:这句话有点深。。。)
Setting Up Projects -- and now for something completely different
设定工程 -- 以及一些明显不同的地方
OK, I am going to take a brief break from code and technological stuff and talk about project configuration. Normally I wouldn't since that's a personal choice, however this time I will because things I talk about later will be dependent upon some of the configuration assumptions. In real life you don't have to do it this way, but let's temporarily pretend we are and at the end of this series you'll know how you might like to do it different.
现在,我们不谈代码和技术,说一下工程配置。因为每个人都可能有自己的配置选择,而且我们下面的工作将会和这些配置选项有关。一般你或许不这么做,但是我们不妨尝试这么做一下,你就会明白这么做的好处。
Big picture is that there will be two projects, producing two distinct executables -- the packer stub and the packer application. Their configuration will be significantly different.
大的方向是2个工程,处理2种不同的可执行文件 --- 局部加壳和全部加壳。这2种配置有很大的区别。
We are going to do a bit of ledgerdemain with the stub project which will be explained later, but for now, configure a boiler plate project for your stub thusly:
现在说一下suub部分的配置:
* Produce a DLL
建立DLL
* Use static multithreaded runtime libraries
采用静态多线程运行库
* Disable RTTI and exception support
禁用RTTT和异常支持
If there are options for the boilerplate code it generates, make it simple, so that there is just DllMain being implemented. We're going to throw all that away anyway. Go ahead and build it as a sanity check, which should go through fine.
如果采用的样本代码有选项,那就尽量让他简单点,这样仅配置DLLMain就可以了。我们打算把这些多余的东西抛到一边,做一个比较完善的检测,就可以继续往下。
We're making the packer stub a DLL not because it will ultimately be a DLL -- it won't. We're doing this because we want the relocation records. You _can_ create it as an exe project and cause it to have relocation records (linker option /FIXED:no), but I find the Microsoft's linker will crash randomly in that configuration. Stick with the DLL config and you'll be OK.
之所以把局部加壳做成一个DLL,并非因为它只能是DLL,而是因为我们需要局部纪录。你也可以把它做成一个exe工程,并且让他有局部纪录(linker选项中/FIXSD: no),但是我发现M$的linker有时候会因此崩溃。处理好DLL配置,然后继续。
Next, change the config thusly (this is for Microsoft's tools, you'll have to look up the equivalents for Borland's or gcc):
下一步,配置编译选项(这里说的是M$工具,你可以自己查一下Borland和gcc相关部分):
Linker options:
Likner 选项:
add any library paths your compressor lib will be needing
/nodefaultlib don't use default libs
/map(filename) DO generate a mapfile
remove /debug don't generate debug info
change /incremental:yes to /incremental:no disable incremental linking
添加压缩库的路径需要/nodefaultlib ,请不要采用默认库
/map(filename) 创建映象文件
移除 /debug 将不会创建debug信息
把/incremental 改变为 no, 将禁用增量连接
Compiler options:
编译选项:
add any defines or header paths your compressor lib will be needing
/FAcs generate listing files with source, assembly, and machine code
/Fa(filename) specify where these listings go
remove /GZ compiler-generated stack checks
remove any debug options, it won't help us where we're going
加入压缩库需要的定义和头文件路径
/FAcs 指定列出的代码文件,汇编和机器指令
/Fa(filename) 指定列表指向的文件
禁止 /GZ 编译选项 -- 指定堆栈检查
禁止任何Debug选项,因为这些对我们没有什么帮助
these options are probably available as checkboxes, so you won't have to manually add them.
这些选项一般都可以在GUI界面看到,因此也不必手工添加。
The gist is that we are not going to have normal debug capabilites so we turn off that stuff. Instead, we will be relying on the listing of the compiler-generated assembly to see code and the linker-generated mapfile to see actual addresses. All this is interesting stuff in any project really, but it is all we have for debugging in this one.
由于不需要一般的调试功能,因此我们关闭他们。但是我们需要借助编译器察看汇编代码,和用于察看代码地址的连接选项。在一般的项目看来这些都不比要,但对我们来说这些比较重要。
If you build now you will should get a linker error complaining about an unresolved external symbol DllMainCRTStartup@12. This is good! If you don't get that then the default libs are coming in. The symbol is possible different for Borland stuff. Other errors probably mean something else needs to be fixed; this is the only one you should get for Microsoft's compiler.
如果你建立创建项目,你或许会碰到类似错误提示:linker error complaining about an unresolved external symbol DllMainCRTStartup@12。这是正常的!如果你没有引入这个文件,系统会引入默认的库文件。在Borland编译环境下,调试符号或许有所不同。其他的错误或许是因为别的文件没有配置好。但M$编译器只会出现上面说到的错误。
Runtime dependencies
运行辅助
You cannot assume what runtime dependencies the original app has. Thus, you cannot make calls to funky dlls (vbrunX.dll, etc). You have no idea if they are there. You will do well to statically link your runtime library. You will do much (much) better, however, to not link any runtime libraries at all! ASM coders will take delight in this fact already, because they are hard-core, but this need not dissuade the C/C++ coders who are accustomed to malloc() strcmp() new std::vector<> or such. All this is doable. You will just have to provide your own implementation of these functions. Fortunately, this is pretty easy since you can call native functions exported by Kernel32.dll. /That/ dll is certainly present, and certainly one that is already used by the original app you are packing so feel free to use it when you like.
很难想象到被加壳程序运行时需要设计的其他文件。因此,你不能处理一些相关Dll文件(例如vbrunX.dll)。如果存在这些东西,我们也无能为力。你所作的是尽量处理好静态连接库。你或许会做的更好,而不需要连接运行库!事实上汇编程序员更乐意处理这些东西,因为他们受到的限制比较少,但并不是说要打击那些使用malloc() strcmp() new std:vector<>
等等的C/C++程序员。因为所有这些都可以完成工作,你只需要提供正确的函数就可以了。幸运的是,有了Kernel32.dll提供的函数,这些工作变得比较简单。由于被加壳软件肯定引用这个DLL,因此只要你愿意,可以处理这个dll。
Making a Trivial C Runtime to Link Instead of the Proper One
写一个小的 C 运行库进行连接而不是采用合适的(译注:这句话有点深。。。)
Replacing the C Runtime might sound scary but remember we only want to implement what is necessary; this will turn out to be a small set of things. The linker will help you figure out what these are. Recall that we turned off default library searching with the /nodefault switch (or equivalent for your linker, that's for Microsoft's). If you configured as I suggested above, we've got a linker error already: DllMainCRTStartup@12 We'll fix that one first.
替换C运行库或许听起来有点恐怖,但是由于我们仅仅处理需要的部分,因此工作量其实比较小。连接器可以提供一些相关帮助。请注意我们关闭了用于搜索默认库的 /nodefault 选项(对于M$编译器有其它相关选项)。由于按照我上面讲解的进行配置,那么就很正常会出现编译错误:
DllMainCRTStartup@12 We'll fix that one first.
Discard your boiler-plate DllMain. Replace it with:
扔掉那写破烂DllMain,用下面的进行替换:
BOOL WINAPI _DllMainCRTStartup ( HANDLE, DWORD, LPVOID )
{
//(program will go here)
return TRUE;
}
This should resolve the linker error and will be our entry point. The place our program will ultimately go is indicated by the comment. Ultimately we'll never hit the 'return TRUE' statement; it's just there to make the compiler happy, and the function signature is what it is to make the linker happy.
这段代码可以解决掉那个连接错误,同时也是程序入口。注释的地方就是程序的入口。这里返回的TRUE不需要再次改动,因为他仅仅被用来满足编译要求,并且前面的函数标记也是为了满足连接要求。
If you want to be more arty, you can do the following:
如果你想看起来更气派一些,不妨采用下面的:
#pragma comment ( linker, "/entry:\"StubEntryPoint\"" )
void declspec ( naked ) StubEntryPoint() {
//(program will go here)
}
which is syntactically clearer.
这样看起来更明了一些。
This is cosmetic so don't feel bad if you find the equivalent pragmas for your compiler/linker. Also, this perverts what the compiler normally thinks about and I have seen it crash randomly. I have found when the compiler gets in a crashing mood, that putting in:
这些并非是一成不变的,所以如果看到其他类似参数也是很正常的。并且这和一般编译器编译情况不太一样,所以经常会碰到崩溃的时候。我发现当编译器崩溃的时候,一般都会出现以下情况:
asm nop
in a couple places seems to get it back on track. Ain't that a laugh?! Whatever...
在2个看起来需要返回的地方。xxxxxx(译注:理解不了。。。。。,好像是淫笑之类)
As code is added, you should periodically build. The linker will add more and more complaints like above and we will have to implement the underlying methods the compiler is emitting references to. Here's a tip: when you installed your dev tools, you may have had the option to install the source to the C Runtime. It will be helpful in some cases since you can cut and paste some parts. In particular, a function:
随着代码的增加,你需要定期进行检测。连接器将会出现一些类似于上面的错误并且我们或许不得不严格采用编译器所要求的方法。这里有一条经验:当安装开发工具的时候,你或许注意到要求安装C运行库代码的选项。有时候的确或许会对其中一部分进行操作。一般来说,一个函数:
extern "C" declspec ( naked ) void _chkstk(void)
is sometimes emitted by the compiler quietly (if you have a large array on the stack, like for a buffer). Just cut-and-paste that one; it's funky.
有时候会被编译器进行扩展(如果你在堆栈中定义了一个大的数据,例如缓冲区)。只需要剪切/粘贴那段,这是很有趣的。(译注:这句也有点深。。。。。)
FYI, I typically have to implement:
一般,我会使用以下操作:
memcpy
memset
memcmp
malloc
free
realloc
calloc
operator new ( unsigned int nSize )
operator delete ( void* pv )
To get you going on what it means to do this sort of roll-your-own-C-runtime, please see the following article. It's good and will save me from repeating the infomation here. There's sample implementation as well.
其它常用函数可以参考下面的文档,我这里不再嗷述。
Reduce EXE and DLL Size with LIBCTINY.LIB
http://msdn.microsoft.com/msdnmag/i...od/default.aspx
OK, we're now setup to do the work!
好了,现在我们开始正式工作!
Unpacking Stub Responsibilities
解压部分的工作
I mentioned way back that the stub has the following duties:
我重新说以下这部分需要做的工作:
* Find the packed data
* Restore data contents
* Perform relocation fixups, if needed
* Resolve all imports since the Windows loader couldn't do it
* Perform thread local storage duties since the Windows loader couldn't do it
* Boink over to the original program
* You may also have to handle being reentered if you are packing a dll
找到压缩数据
重新存储数据内容
如果需要的话,重定位他们
解决Windows不能处理的输入表
解决WIndows不能处理的线程局部存储
Boink over to the original program
如果对DLL文件加壳,还需要处理相关句柄
It's important that the stub restore the original data to it's exact original location. This is because we don't know about what references are in the original code to things like global data structures and functions pointers in things like vtables.
将原始数据存储到附加部分是很重要的。因为我们不知道原始代码中针对全局结构和函数指针的接口。
Recall that the format of the PE file (links to good discussions were provided in the previous installment) is organized into sections, which have a location and size. This information is stored in the section headers, which describe where the sections go in memory (relative to the load address).
PE文件格式(上面的安装部分已经讨论过)由区段组成,这些区段都有地址和大小。这些信息都被保存在文件头。
To do this properly, we will be needing to know our load address. If we are a stub for an exe we can simply do a GetModuleHandle(NULL) and the returned module handle is the base load address. This won't work for a dll however. The module handle for the dll is on the stack. We can write some code to get it, or we can choose not to do the 'arty entry point' and it is referenceble as a parameter (do not attempt to reference those parameters if it is the stub for an exe unless you are fond of crashes).
为了确保正确,我们需要知道加载地址。如果是针对exe文件,那么GetModuleHandle(NULL)就可以返回加载地址。但是对于DLL文件却不可以这样做。DLL文件模块的句柄存放在堆栈中。我们可以写代码获取他,或者可以选择不处理这些“气派的入口”(译:有点深。。。,就是说不处理)并且让他作为参数入口(如果是exe文件,不要试图处理这些参数,否则会导致崩溃)。
My preferred technique, however, is to get the packer application to help me out. That way the same stub works for exes and dlls and in the same way. It involves a global variable, and there are going to be several of those, so let me discuss that first.
然而我比较倾向于让壳程序帮助我处理这个事情。如此一来,对于exes和dlls,头文件都是一样的。这涉及到一个全局变量,并且会有很多此类变量,所以我们先来讨论这个。
Packer Parameter Globals
壳的全局变量
There are going to be parameters that are computed by the packing application and that will be embedded in the stub so it can do it's work at runtime. These require a bit of special handling because the packer application needs to find these items at pack time. You could hard-code in the addresses into the packer. You would get these addresses from the mapfile generated by the linker. This is a bit tacky because you will have to double check it each time you alter the stub, which will be quite frequently while developing. Instead, I prefer to do a bit of legerdemain with structures, sections, and segments. This only needs to be done for the variables published to the packer. Regular globals you might want to have can be done as usual without concern.
壳程序会计算出很多参数并且放置在文件头中以供运行时候使用。这需要一些特殊的句柄,因为加壳程序在加壳时需要查找相应资源。你需要往壳相应地址中写入硬编码。这些地址可以用连接器提供的文件映象中获得。这种事情有点麻烦,因为每次改变该部分就需要检查2次,而且开发过程中这些事情都是很频繁的。如果不这样,可以在结构,区段和寄存器中是用一些小技巧,仅仅需要对壳中出现的变量进行处理就可以了。这样那些你需要的变量就会被自动处理。
First, simple stuff. I make one structure with all the important globals. Then one global instance of that structure. Thus there is only one object the packer has to locate at pack time. Let's call that structure:
首先,说一下基本素材。我写了一个关于重要变量的结构。然后定义了该全局变量的事例。这样,壳在加壳时候仅仅需要处理这个结构就可以了。这个结构如下:
//in a header, say GlobalExternVars.h
struct GlobalExternVars
{
//stuff goes here
};
Now we will do some kookiness in our main .cpp file:
然后我们在我们的 main.cpp 文件中作一些大的处理: