| 1 |
<section> <date> February 2003 </date> |
| 2 |
<h2> SFX-Make </h2> combining an EXE with a ZIP archive |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
<section> |
| 7 |
<h3> How To </h3> |
| 8 |
<P> |
| 9 |
In this section we walk you through the steps of combining an EXE |
| 10 |
with a ZIP archive. The basic scheme goes like this: the final |
| 11 |
file will have an EXE starting at offset null, followed by the |
| 12 |
data entries of a ZIP archive. The <em>last</em> part of the ZIP |
| 13 |
archive is the ZIP central-directory which ends at the end of the file. |
| 14 |
</P> |
| 15 |
<P> |
| 16 |
The basic problem lies in the fact that the zip central-directory |
| 17 |
entries reference their data section with an offset from the |
| 18 |
start-of-file so that you can not just append a zip archive after |
| 19 |
an exe stub. The trick goes like adding the EXE as the first data |
| 20 |
part of the ZIP archive - so that the offsets for each entry will |
| 21 |
be correct when we are finished with it. |
| 22 |
</P> |
| 23 |
<P> |
| 24 |
Again, one can not just use a zip tool to put the EXE as the first |
| 25 |
part - since each data part is preceded with an infoblock of a |
| 26 |
few bytes. The data of the first data part will therefore not |
| 27 |
start at offset zero. We solve this problem with moving the data |
| 28 |
a few bytes later - so that the final file will not start with a |
| 29 |
"PK" magic (from the zip info header) but with an "MZ" or "ELF" |
| 30 |
magic (from the exe info header). |
| 31 |
</P> |
| 32 |
|
| 33 |
</section><section> |
| 34 |
<h3> Step 1: Creating The Zip Combination </h3> |
| 35 |
<P> |
| 36 |
Choose your exe file (example.exe) and wrap that file into a |
| 37 |
zip container - ensure that the zip tool does <em>not</em> |
| 38 |
use any compression algorithm on the data. This is usually |
| 39 |
done with saying "zero compression level" as an option to |
| 40 |
the zip tool. Also note that <em>no other</em> file is |
| 41 |
wrapped as some zip tools reorder the entries from the |
| 42 |
order on the command line to alphabetic order. Here is an |
| 43 |
example with infozip's `zip` (e.g. on linux): |
| 44 |
<pre> zip -0 -j example.zip example.exe </pre> |
| 45 |
</P> |
| 46 |
<P> |
| 47 |
There is no zip tool that would reorder the data entries in |
| 48 |
an existing zip archive. This mode is used here - the real |
| 49 |
compressed data entries can now be added to the existing |
| 50 |
zip archive that currently just wraps the exe part. With |
| 51 |
specifying maximum compression ("-9" = compression level 9) |
| 52 |
and throwing away any subdirectory part ("-j" = junk path) |
| 53 |
it might look like |
| 54 |
<pre> zip -9 -j example.zip data/* </pre> |
| 55 |
</P> |
| 56 |
<P> |
| 57 |
Now we need to move the exe part by a few bytes to the |
| 58 |
real start of the file. This can be done as easily as |
| 59 |
writing the exe file again on to the start of the file. |
| 60 |
However, one can not just use a shell-direction or |
| 61 |
copy-operation since that would truncate (!!) the zip |
| 62 |
file to the length of the exe part. The overwrite-operation |
| 63 |
must be done without truncation. For maximum OS independence |
| 64 |
the zziplib ships with a little tool in "test/zzipsetstub.c" |
| 65 |
that you can reuse for this task: |
| 66 |
<pre> zzipsetstub example.zip example.exe </pre> |
| 67 |
</P> |
| 68 |
<P> |
| 69 |
This is it - the `unzip` tool can still access all data |
| 70 |
entries but the first EXE - the first EXE will be listed |
| 71 |
in the central-directory of the ZIP archive but one can |
| 72 |
not extract the data since the "PK" magic at offset null |
| 73 |
has been overwritten with the EXE magic. The data of all |
| 74 |
the other entries can still be extracted with a normal |
| 75 |
`unzip` tool - or any tool from the zziplib be used for |
| 76 |
the same task. |
| 77 |
</P> |
| 78 |
|
| 79 |
</section><section> |
| 80 |
<h3> Step 2: Accessing The Data From The Program </h3> |
| 81 |
|
| 82 |
<P> |
| 83 |
There is an example in test/zzipself.c that show how to do |
| 84 |
it. The OS will provide each program with its own name in |
| 85 |
argv[0] of the main() routine. This program file (!!) is |
| 86 |
also the zip archive that carries the compressed data |
| 87 |
entries along. Therefore, we can just issue a zzip_opendir |
| 88 |
on argv[0] to access the zip central-directory. |
| 89 |
</P> |
| 90 |
<P> |
| 91 |
Likewise one can open a file within it by just prepending |
| 92 |
the string argv[0] to the filename stem, i.e. you could |
| 93 |
do like |
| 94 |
<pre> ZZIP_FILE* f = zzip_fopen ("example.exe/start.gif", "rbi")</pre> |
| 95 |
</P><P> |
| 96 |
however you are advised to use the _ext_io cousin to be |
| 97 |
platform independet - different Operating Systems use |
| 98 |
different file extensions for executables, it's not always |
| 99 |
an ".exe". |
| 100 |
</P> |
| 101 |
<P> |
| 102 |
Once the file is opened, the data can be zzip_fread or |
| 103 |
passed through an SDL_rwops structure into the inner |
| 104 |
parts of your program. |
| 105 |
</P> |
| 106 |
|
| 107 |
</section><section> |
| 108 |
<h3> Step 3: Using Obfuscation Along </h3> |
| 109 |
|
| 110 |
<P> |
| 111 |
The next level uses obfuscatation on the data part of the |
| 112 |
application. That way there is no visible data to be seen |
| 113 |
from outside, it looks like it had been compiled right into |
| 114 |
the C source part. One can furthermore confuse a possible |
| 115 |
attacker with staticlinking the zziplib into the executable |
| 116 |
(this is possible in a limited set of conditions). |
| 117 |
</P> |
| 118 |
<P> |
| 119 |
The first pass is again in creating the zip - here we must |
| 120 |
ensure that only the ZIP archive part is obfuscated but |
| 121 |
the EXE part must be plain data so that the operationg |
| 122 |
system can read and relocate it into main memory. Using |
| 123 |
xor-obfuscation this is easy - applying xor twice will |
| 124 |
yield the original data. The steps look like this now: |
| 125 |
<pre> |
| 126 |
zzipxorcopy example.exe example.xor |
| 127 |
zip -0 application.zip example.xor |
| 128 |
zip -9 application.zip data/* |
| 129 |
zzipsetstub application.zip example.xor |
| 130 |
zzipxorcopy application.zip application.exe |
| 131 |
</pre> |
| 132 |
</P> |
| 133 |
<P> |
| 134 |
In the second step the open-routine in your application |
| 135 |
needs to be modified - there are quite some examples in |
| 136 |
the zziplib that show you how to add an xor-read routine |
| 137 |
and passing it in the "io"-part of an zzip_open_ext_io |
| 138 |
routine (see zzipxorcat.c). |
| 139 |
<pre> |
| 140 |
static int xor_value = 0x55; |
| 141 |
|
| 142 |
static zzip_ssize_t xor_read (int f, void* p, zzip_size_t l) |
| 143 |
{ |
| 144 |
zzip_ssize_t r = read(f, p, l); |
| 145 |
zzip_ssize_t x; char* q; for (x=0, q=p; x < r; x++) q[x] ^= xor_value; |
| 146 |
return r; |
| 147 |
} |
| 148 |
|
| 149 |
static struct zzip_plugin_io xor_handlers; |
| 150 |
static zzip_strings_t xor_fileext[] = { ".exe", ".EXE", "", 0 }; |
| 151 |
|
| 152 |
main(...) |
| 153 |
{ |
| 154 |
zzip_init_io (&xor_handlers, 0); xor_handlers.read = &xor_read; |
| 155 |
|
| 156 |
ZZIP_FILE* fp = zzip_open_ext_io (filename, |
| 157 |
O_RDONLY|O_BINARY, ZZIP_CASELESS|ZZIP_ONLYZIP, |
| 158 |
xor_fileext, &xor_handlers); |
| 159 |
.... |
| 160 |
</pre> |
| 161 |
</P> |
| 162 |
<P> |
| 163 |
You may want to pick your own xor-value instead of the default 0x55, |
| 164 |
the zziplib-shipped tool `zzipxorcopy` does know an option to just |
| 165 |
set the xor-value with which to obfuscate the data. |
| 166 |
</P> |
| 167 |
</section></section> |