-
Notifications
You must be signed in to change notification settings - Fork 13
/
hh6r.nasm
253 lines (234 loc) · 8.02 KB
/
hh6r.nasm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
;
; hh6r.nasm: small and ultraportable Win32 console PE .exe with relocations
; by pts@fazekas.hu on Mon Aug 3 20:03:00 CEST 2020
;
; Compile: nasm -O0 -f bin -o hh6r.exe hh6r.nasm
;
; It works on Windows NT 3.1--Windows 10, tested on Windows NT 3.1, Windows
; 95, Windows XP, Windows 7, Windows 10, Wine 5.0 and ReactOS 0.4.14.
;
; This file is based on hh6r.nasm, and relocations were added.
;
; Asserts that we are at offset %1 from the beginning of the input file
%macro aa 1
times $-(%1) times 0 nop
times (%1)-$ times 0 nop
%endmacro
bits 32
cpu 386
SECTION_HEADER:
VADDR_HEADER equ 0
FILE_HEADER:
IMAGE_DOS_HEADER:
aa $$+0x0000
; https://github.com/pts/pts-nasm-fullprog/blob/master/pe_stub1.nasm
.mz_signature: dw 'MZ'
.image_size_lo: dw IMAGE_NT_HEADERS
.image_size_hi: dw 1
dw 0, 1, 0x0fff, -1, 1, -1, 0, 8, 0
aa $$+24
.stub_start:
push ss
pop ds
mov ah, 9 ; WRITE_DOLLAR_STDOUT.
db 0xba ; 16-bit mov dx, ...
dw 6 ; .stub_msg, based on .stub_data.
int 0x21
db 0xb8 ; 16-bit mov ax, ...
dw 0x4c01 ; EXIT(1).
int 0x21
.stub_msg: db 'Not a DOS program.', 13, 10, '$'
times 60-($-$$) db 0
dd IMAGE_NT_HEADERS
aa $$+0x40
IMAGE_BASE equ 0x00400000 ; Variable.
;IMAGE_BASE equ 0x10000000 ; Also works on Windows NT 3.1.
BSS_SIZE EQU 0
;HEADER_end_aligned EQU 0x400
HEADER_end_aligned EQU 0x200
IMAGE_NT_HEADERS:
db 'PE', 0, 0
IMAGE_FILE_HEADER:
Machine: dw 0x14c ; IMAGE_FILE_MACHINE_I386
NumberOfSections: dw (IMAGE_SECTION_HEADER_end-IMAGE_SECTION_HEADER)/40
TimeDateStamp: dd 0
PointerToSymbolTable: dd 0
NumberOfSymbols: dd 0
SizeOfOptionalHeader: dw IMAGE_OPTIONAL_HEADER32_end-IMAGE_OPTIONAL_HEADER32
IMAGE_FILE_RELOCS_STRIPPED equ 1
Characteristics: dw 0x030e ; Doesn't have IMAGE_FILE_RELOCS_STRIPPED.
IMAGE_OPTIONAL_HEADER32:
Magic: dw 0x10b ; IMAGE_NT_OPTIONAL_HDR32_MAGIC
MajorLinkerVersion: db 6
MinorLinkerVersion: db 0
SizeOfCode: dd 0x00000000
SizeOfInitializedData: dd 0x00000000
SizeOfUninitializedData: dd 0x00000000
AddressOfEntryPoint: dd _start+(VADDR_TEXT-HEADER_end_aligned) ; Also called starting address.
BaseOfCode: dd VADDR_TEXT
BaseOfData: dd VADDR_TEXT
ImageBase: dd IMAGE_BASE
SectionAlignment: dd 0x1000 ; Single allowed value for Windows XP.
FileAlignment: dd 0x200 ; Minimum value for Windows NT 3.1.
MajorOperatingSystemVersion: dw 4
MinorOperatingSystemVersion: dw 0
MajorImageVersion: dw 0
MinorImageVersion: dw 0
MajorSubsystemVersion: dw 3 ; Windows NT 3.1.
MinorSubsystemVersion: dw 10 ; Windows NT 3.1.
Win32VersionValue: dd 0
SizeOfImage: dd VADDR_TEXT+SECTION_TEXT_end-HEADER_end_aligned+EXTRA_BSS_SIZE+BSS_SIZE
SizeOfHeaders: dd HEADERS_end
CheckSum: dd 0
Subsystem: dw 3 ; IMAGE_SUBSYSTEM_WINDOWS_CUI; gcc -mconsole
DllCharacteristics: dw 0
SizeOfStackReserve: dd 0x00100000
SizeOfStackCommit: dd 0x00001000
SizeOfHeapReserve: dd 0x100000 ; Why not 0?
SizeOfHeapCommit: dd 0x1000 ; Why not 0?
LoaderFlags: dd 0
NumberOfRvaAndSizes: dd (IMAGE_DATA_DIRECTORY_end-IMAGE_DATA_DIRECTORY)/8
IMAGE_DATA_DIRECTORY:
IMAGE_DIRECTORY_ENTRY_EXPORT:
.VirtualAddress: dd 0x00000000
.Size: dd 0x00000000
IMAGE_DIRECTORY_ENTRY_IMPORT:
.VirtualAddress: dd IMAGE_IMPORT_DESCRIPTORS+(VADDR_TEXT-HEADER_end_aligned)
.Size: dd IMAGE_IMPORT_DESCRIPTORS_end-IMAGE_IMPORT_DESCRIPTORS
IMAGE_DIRECTORY_ENTRY_RESOURCE:
.VirtualAddress: dd 0x00000000
.Size: dd 0x00000000
IMAGE_DIRECTORY_ENTRY_EXCEPTION:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_SECURITY:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_BASERELOC:
.VirtualAddress: dd RELOC_BLOCKS+(VADDR_HEADER-SECTION_HEADER)
.Size: dd RELOC_BLOCKS_end-RELOC_BLOCKS
IMAGE_DIRECTORY_ENTRY_DEBUG:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_ARCHITECTURE:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_GLOBALPTR:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_TLS:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT:
.VirtualAddress: dd 0
.Size: dd 0
IMAGE_DIRECTORY_ENTRY_IAT: ; Import address table.
.VirtualAddress: dd IMPORT_ADDRESS_TABLE+(VADDR_TEXT-HEADER_end_aligned)
.Size: dd IMPORT_ADDRESS_TABLE_end-IMPORT_ADDRESS_TABLE
; These entries are not needed.
;IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT:
;.VirtualAddress: dd 0
;.Size: dd 0
;IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR: ; Nonzero for .NET .exe.
;.VirtualAddress: dd 0
;.Size: dd 0
;IMAGE_DIRECTORY_ENTRY_RESERVED:
;.VirtualAddress: dd 0
;.Size: dd 0
IMAGE_DATA_DIRECTORY_end:
IMAGE_OPTIONAL_HEADER32_end:
IMAGE_SECTION_HEADER:
IMAGE_SECTION_HEADER__0:
.Name: db '.text'
times ($$-$)&7 db 0
.VirtualSize: dd SECTION_TEXT_end-HEADER_end_aligned+EXTRA_BSS_SIZE+BSS_SIZE
VADDR_TEXT equ 0x1000
.VirtualAddress: dd VADDR_TEXT
.SizeOfRawData: dd SECTION_TEXT_end-HEADER_end_aligned
.PointerToRawData: dd HEADER_end_aligned
.PointerToRelocations: dd 0
.PointerToLineNumbers: dd 0
.NumberOfRelocations: dw 0
.NumberOfLineNumbers: dw 0
IMAGE_SCN_CNT_CODE equ 0x20
IMAGE_SCN_MEM_EXECUTE equ 0x20000000
IMAGE_SCN_MEM_READ equ 0x40000000
IMAGE_SCN_CNT_INITIALIZED_DATA equ 0x40
IMAGE_SCN_MEM_WRITE equ 0x80000000
.Characteristics: dd IMAGE_SCN_CNT_CODE|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_WRITE
IMAGE_SECTION_HEADER_end:
NAME_KERNEL32_DLL: db 'kernel32.dll', 0
; The `0, 0, ' is the .Hint.
NAME_GetStdHandle: db 0, 0, 'GetStdHandle', 0
NAME_WriteFile: db 0, 0, 'WriteFile', 0
NAME_ExitProcess: db 0, 0, 'ExitProcess', 0
message:
db 'Hello, World!', 13, 10
message_end:
; Without these relocation blocks, Win32s wouldn't be able to run the .exe.
; They are ignored by Wine, Windows NT, Windows 95 and Windows XP+.
RELOC_BLOCKS:
RELOC_BLOCK__0:
dd VADDR_TEXT
dd RELOC_BLOCK__0_end-RELOC_BLOCK__0
.relocs:
BASERELOC_HIGHLOW equ 0x3000
dw reloc0-SECTION_TEXT+BASERELOC_HIGHLOW
dw reloc1-SECTION_TEXT+BASERELOC_HIGHLOW
dw reloc2-SECTION_TEXT+BASERELOC_HIGHLOW
dw reloc3-SECTION_TEXT+BASERELOC_HIGHLOW
%if ($-.relocs)&2 ; !! Is this padding really needed? Which Windows version breaks without it?
dw 0 ; Padding to even number of relocs.
%endif
RELOC_BLOCK__0_end:
RELOC_BLOCKS_end:
HEADERS_end:
%if $-FILE_HEADER<HEADER_end_aligned
times HEADER_end_aligned-($-FILE_HEADER) db 'R'
%endif
SECTION_TEXT:
_start:
push strict byte -11 ; STD_OUTPUT_HANDLE.
call [__imp__GetStdHandle@4+(IMAGE_BASE+VADDR_TEXT-HEADER_end_aligned)]
reloc0 equ $-4
;push eax ; Save stdout handle.
push eax ; Value is arbitrary, we allocate an output variable on the stack.
mov ebx, esp
push strict byte 0 ; Argument 5: lpOverlapped = 0.
push ebx ; Argument 4: Address of the output variable.
push strict byte message_end-message ; Argument 3: message size.
push strict dword message+(IMAGE_BASE+VADDR_HEADER) ; Argument 2: message.
reloc1 equ $-4
push eax ; if it was saved, strict dword [esp+20] ; Argument 1: Stdout handle.
call [__imp__WriteFile@20+(IMAGE_BASE+VADDR_TEXT-HEADER_end_aligned)]
reloc2 equ $-4
push byte 0 ; EXIT_SUCCESS == 0.
call [__imp__ExitProcess@4+(IMAGE_BASE+VADDR_TEXT-HEADER_end_aligned)]
reloc3 equ $-4
;add esp, 8 ; Too late, we've already exited.
;ret ; Too late, we've already exited.
;SECTION_DATA:
; Because of the modification, this must start after SECTION_TEXT.
IMPORT_ADDRESS_TABLE: ; Import address table. Modified by the PE loader before jumping to _entry.
__imp__GetStdHandle@4: dd NAME_GetStdHandle+(VADDR_HEADER)
__imp__WriteFile@20: dd NAME_WriteFile+(VADDR_HEADER)
__imp__ExitProcess@4 dd NAME_ExitProcess+(VADDR_HEADER)
dd 0 ; Marks end-of-list.
IMPORT_ADDRESS_TABLE_end:
; Windows 95 requires this to be part of a section; Windows NT 3.1 and
; Windows XP work if this is in the header.
IMAGE_IMPORT_DESCRIPTORS:
IMAGE_IMPORT_DESCRIPTOR_0:
.OriginalFirstThunk: dd IMPORT_ADDRESS_TABLE+(VADDR_TEXT-HEADER_end_aligned)
.TimeDateStamp: dd 0
.ForwarderChain: dd 0
.Name: dd NAME_KERNEL32_DLL+(VADDR_HEADER) ; !!!
.FirstThunk: dd IMPORT_ADDRESS_TABLE+(VADDR_TEXT-HEADER_end_aligned)
IMAGE_IMPORT_DESCRIPTOR_1: ; Last Import directory table, marks end-of-list.
;dd 0, 0, 0, 0, 0 ; Same fields as above, filled with 0s.
EXTRA_BSS_SIZE equ 4*5 ; For the end-of-list bytes above.
IMAGE_IMPORT_DESCRIPTORS_end equ $+EXTRA_BSS_SIZE
SECTION_TEXT_end: