Last active
August 29, 2015 14:03
-
-
Save 7shi/bd16b8996b9a707c2cd5 to your computer and use it in GitHub Desktop.
EXE入門で作ったプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <string.h> | |
void align(FILE *f, int size) { | |
int pos = ftell(f); | |
int aligned = (pos + size - 1) / size * size; | |
for (; pos < aligned; ++pos) { | |
fwrite("\0", 1, 1, f); | |
} | |
} | |
IMAGE_DOS_HEADER dosh; | |
IMAGE_NT_HEADERS32 nth; | |
IMAGE_SECTION_HEADER sects[1]; | |
int main(void) { | |
strncpy((char *)&dosh.e_magic, "MZ", 2); | |
dosh.e_cblp = 0x90; | |
dosh.e_cp = 3; | |
dosh.e_cparhdr = 4; | |
dosh.e_maxalloc = 0xffff; | |
dosh.e_sp = 0xb8; | |
dosh.e_lfarlc = 0x40; | |
dosh.e_lfanew = 0x80; | |
strncpy((char *)&nth.Signature, "PE", 4); | |
nth.FileHeader.Machine = 0x014c; | |
nth.FileHeader.NumberOfSections = 1; | |
nth.FileHeader.TimeDateStamp = 0x4da65f9b; | |
nth.FileHeader.SizeOfOptionalHeader = sizeof(nth.OptionalHeader); | |
nth.FileHeader.Characteristics = 0x0102; | |
nth.OptionalHeader.Magic = 0x010b; | |
nth.OptionalHeader.MajorLinkerVersion = 0x0a; | |
nth.OptionalHeader.SizeOfCode = 0x0200; | |
nth.OptionalHeader.AddressOfEntryPoint = 0x1000; | |
nth.OptionalHeader.BaseOfCode = 0x1000; | |
nth.OptionalHeader.BaseOfData = 0x2000; | |
nth.OptionalHeader.ImageBase = 0x400000; | |
nth.OptionalHeader.SectionAlignment = 0x1000; | |
nth.OptionalHeader.FileAlignment = 0x0200; | |
nth.OptionalHeader.MajorOperatingSystemVersion = 5; | |
nth.OptionalHeader.MinorOperatingSystemVersion = 1; | |
nth.OptionalHeader.MajorSubsystemVersion = 5; | |
nth.OptionalHeader.MinorSubsystemVersion = 1; | |
nth.OptionalHeader.SizeOfImage = 0x2000; | |
nth.OptionalHeader.SizeOfHeaders = 0x0200; | |
nth.OptionalHeader.Subsystem = 2; | |
nth.OptionalHeader.SizeOfStackReserve = 0x100000; | |
nth.OptionalHeader.SizeOfStackCommit = 0x001000; | |
nth.OptionalHeader.SizeOfHeapReserve = 0x100000; | |
nth.OptionalHeader.SizeOfHeapCommit = 0x001000; | |
nth.OptionalHeader.NumberOfRvaAndSizes = 16; | |
strncpy((char *)sects[0].Name, ".text", 8); | |
sects[0].Misc.VirtualSize = 1; | |
sects[0].VirtualAddress = 0x1000; | |
sects[0].SizeOfRawData = 0x0200; | |
sects[0].PointerToRawData = 0x0200; | |
sects[0].Characteristics = 0x60000020; | |
FILE *f = fopen("test.exe", "wb"); | |
fwrite(&dosh, sizeof(dosh), 1, f); | |
BYTE dos_stub[] = { | |
0x0e, // push cs | |
0x1f, // pop ds | |
0xba, 0x0e, 0x00, // mov dx, 0x000e | |
0xb4, 0x09, // mov ah, 0x09 | |
0xcd, 0x21, // int 0x21 | |
0xb8, 0x01, 0x4c, // mov ax, 0x4c01 | |
0xcd, 0x21 // int 0x21 | |
}; | |
fwrite(dos_stub, sizeof(dos_stub), 1, f); | |
const char *dos_msg = "This program cannot be run in DOS mode.\r\r\n$"; | |
fwrite(dos_msg, strlen(dos_msg), 1, f); | |
align(f, dosh.e_lfanew); | |
fwrite(&nth, sizeof(nth), 1, f); | |
fwrite(sects, sizeof(sects), 1, f); | |
align(f, nth.OptionalHeader.FileAlignment); | |
BYTE text[] = { 0xc3 }; | |
fwrite(text, sizeof(text), 1, f); | |
align(f, nth.OptionalHeader.FileAlignment); | |
fclose(f); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment