-
Notifications
You must be signed in to change notification settings - Fork 13
/
hh6c.nasm
221 lines (205 loc) · 6.98 KB
/
hh6c.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
;
; hh6c.nasm: small (668 bytes) and ultraportable Win32 PE .exe
; Compile: nasm -O0 -f bin -o hh6c.exe hh6c.nasm
;
; It works on Windows NT 3.1--Windows 10, tested on Windows NT 3.1, Windows
; 95, Windows XP and Wine 5.0.
;
; This file is based on hh6b.nasm, and the .text section in the file was
; truncated to make it 512 bytes smaller.
;
; 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
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.
AFTER_LAST_SECTION_ALIGNMENT equ 1 ; Set it to 512 to get an 1024-byte .exe.
BSS_SIZE EQU 1 ; !! Can it be 0?
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
Characteristics: dw 0x030f
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-SECTION_TEXT) ; 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 ((SECTION_TEXT_end-SECTION_TEXT+4095)&~4095)+((BSS_SIZE+4095)&~4095)
SizeOfHeaders: dd HEADERS_end_aligned
CheckSum: dd 0xd43c ; !! Change to 0 to avoid checking.
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-SECTION_TEXT)
.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 0
.Size: dd 0
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-SECTION_TEXT)
.Size: dd IMPORT_ADDRESS_TABLE_end-IMPORT_ADDRESS_TABLE
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-SECTION_TEXT
VADDR_TEXT equ 0x1000
.VirtualAddress: dd VADDR_TEXT
.SizeOfRawData: dd SECTION_TEXT_end_aligned-SECTION_TEXT
.PointerToRawData: dd SECTION_TEXT
.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:
times ($$-$)&511 db 0
HEADERS_end_aligned:
SECTION_TEXT:
aa $$+0x0200
_start:
push strict byte -11 ; STD_OUTPUT_HANDLE.
call [__imp__GetStdHandle@4+(IMAGE_BASE+VADDR_TEXT-SECTION_TEXT)]
;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_TEXT-SECTION_TEXT) ; Argument 2: message.
push eax ; if it was saved, strict dword [esp+20] ; Argument 1: Stdout handle.
call [__imp__WriteFile@20+(IMAGE_BASE+VADDR_TEXT-SECTION_TEXT)]
push byte 0 ; EXIT_SUCCESS == 0.
call [__imp__ExitProcess@4+(IMAGE_BASE+VADDR_TEXT-SECTION_TEXT)]
;add esp, 8 ; Too late, we've already exited.
;ret ; Too late, we've already exited.
;SECTION_DATA:
message:
db 'Hello, World!', 13, 10
message_end:
IMAGE_IMPORT_DESCRIPTORS:
IMAGE_IMPORT_DESCRIPTOR_0:
.OriginalFirstThunk: dd IMPORT_ADDRESS_TABLE+(VADDR_TEXT-SECTION_TEXT)
.TimeDateStamp: dd 0
.ForwarderChain: dd 0
.Name: dd NAME_KERNEL32_DLL+(VADDR_TEXT-SECTION_TEXT)
.FirstThunk: dd IMPORT_ADDRESS_TABLE+(VADDR_TEXT-SECTION_TEXT)
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.
IMAGE_IMPORT_DESCRIPTORS_end:
IMPORT_ADDRESS_TABLE: ; Import address table. Modified by the PE loader before jumping to _entry.
__imp__GetStdHandle@4: dd NAME_GetStdHandle+(VADDR_TEXT-SECTION_TEXT)
__imp__WriteFile@20: dd NAME_WriteFile+(VADDR_TEXT-SECTION_TEXT)
__imp__ExitProcess@4 dd NAME_ExitProcess+(VADDR_TEXT-SECTION_TEXT)
dd 0 ; Marks end-of-list.
IMPORT_ADDRESS_TABLE_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
dd 0 ; Why is this needed? A dw is not enough.
times ($$-$)&15 db 0
SECTION_TEXT_end:
times (($$-$) %% AFTER_LAST_SECTION_ALIGNMENT+AFTER_LAST_SECTION_ALIGNMENT)%AFTER_LAST_SECTION_ALIGNMENT db 0
SECTION_TEXT_end_aligned:
;aa $$+0x0400