佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 259|回复: 0

制作了512字节大小的EXE

[复制链接]
发表于 9-1-2024 01:54 PM | 显示全部楼层 |阅读模式
这是我第一次突破1KB(1024字节)的限制,制作出512字节(对我来说是很小了)的x86 PE(Windows EXE).


已知现代最小EXE有:
  • 268字节(不过无法在Windows 10运行)
  • 282字节(可以在Windows 10运行,不过字符串不能自定义)
  • 345字节(可以在Windows 10运行,而且字符串可以自定义)


不过上述所有EXE(包括我的)都据报无法在Windows 11运行,因为FileAlignment和SectionAlignment无法设定到512以下的值。

我的512字节的EXE的代码可以用fasmg编译:
  1. format binary as "exe"

  2. macro align? pow2*,value:?
  3.         db  (-$) and (pow2-1)  dup value
  4. end macro

  5. include '80386.inc'
  6. use32

  7. IMAGE_BASE := 0x400000
  8. org IMAGE_BASE

  9. FILE_ALIGNMENT := 16
  10. SECTION_ALIGNMENT := 16

  11. Stub:
  12.         .Signature                      dw "MZ"
  13.         .BytesInLastSector              dw SIZE_OF_STUB mod 512
  14.         .NumberOfSectors                dw (SIZE_OF_STUB-1)/512 + 1
  15.         .NumberOfRelocations            dw 0
  16.         .NumberOfHeaderParagraphs       dw SIZE_OF_STUB_HEADER / 16
  17.                                         db 0x3C - ($-Stub) dup 0
  18.         .NewHeaderOffset                dd Header-IMAGE_BASE

  19. align 16

  20. SIZE_OF_STUB_HEADER := $ - Stub

  21.         ; The code of a DOS program would go here.

  22. SIZE_OF_STUB := $ - Stub

  23. align 8

  24. Header:
  25.         .Signature                      dw "PE",0
  26.         .Machine                        dw 0x14C ; IMAGE_FILE_MACHINE_I386
  27.         .NumberOfSections               dw NUMBER_OF_SECTIONS
  28.         .TimeDateStamp                  dd %t
  29.         .PointerToSymbolTable           dd 0
  30.         .NumberOfSymbols                dd 0
  31.         .SizeOfOptionalHeader           dw SectionTable - OptionalHeader
  32.         .Characteristics                dw 0x102 ; IMAGE_FILE_32BIT_MACHINE + IMAGE_FILE_EXECUTABLE_IMAGE

  33. OptionalHeader:
  34.         .Magic                          dw 0x10B
  35.         .MajorLinkerVersion             db 0
  36.         .MinorLinkerVersion             db 0
  37.         .SizeOfCode                     dd 0
  38.         .SizeOfInitializedData          dd 0
  39.         .SizeOfUninitializedData        dd 0
  40.         .AddressOfEntryPoint            dd EntryPoint-IMAGE_BASE
  41.         .BaseOfCode                     dd 0
  42.         .BaseOfData                     dd 0
  43.         .ImageBase                      dd IMAGE_BASE
  44.         .SectionAlignment               dd SECTION_ALIGNMENT
  45.         .FileAlignment                  dd FILE_ALIGNMENT
  46.         .MajorOperatingSystemVersion    dw 3
  47.         .MinorOperatingSystemVersion    dw 10
  48.         .MajorImageVersion              dw 0
  49.         .MinorImageVersion              dw 0
  50.         .MajorSubsystemVersion          dw 3
  51.         .MinorSubsystemVersion          dw 10
  52.         .Win32VersionValue              dd 0
  53.         .SizeOfImage                    dd SIZE_OF_IMAGE
  54.         .SizeOfHeaders                  dd SIZE_OF_HEADERS
  55.         .CheckSum                       dd 0
  56.         .Subsystem                      dw 2 ; IMAGE_SUBSYSTEM_WINDOWS_GUI
  57.         .DllCharacteristics             dw 0
  58.         .SizeOfStackReserve             dd 4096
  59.         .SizeOfStackCommit              dd 4096
  60.         .SizeOfHeapReserve              dd 65536
  61.         .SizeOfHeapCommit               dd 0
  62.         .LoaderFlags                    dd 0
  63.         .NumberOfRvaAndSizes            dd NUMBER_OF_RVA_AND_SIZES

  64. RvaAndSizes:
  65.         .Export.Rva                     dd 0
  66.         .Export.Size                    dd 0
  67.         .Import.Rva                     dd ImportTable-IMAGE_BASE
  68.         .Import.Size                    dd ImportTable.End-ImportTable
  69.         ;.Resource.Rva                   dd 0
  70.         ;.Resource.Size                  dd 0
  71.         ;.Exception.Rva                  dd 0
  72.         ;.Exception.Size                 dd 0
  73.         ;.Certificate.Rva                dd 0
  74.         ;.Certificate.Size               dd 0
  75.         ;.BaseRelocation.Rva             dd 0
  76.         ;.BaseRelocation.Size            dd 0
  77.         ;.Debug.Rva                      dd 0
  78.         ;.Debug.Size                     dd 0
  79.         ;.Architecture.Rva               dd 0
  80.         ;.Architecture.Size              dd 0
  81.         ;.GlobalPtr.Rva                  dd 0
  82.         ;.GlobalPtr.Size                 dd 0
  83.         ;.TLS.Rva                        dd 0
  84.         ;.TLS.Size                       dd 0
  85.         ;.LoadConfig.Rva                 dd 0
  86.         ;.LoadConfig.Size                dd 0
  87.         ;.BoundImport.Rva                dd 0
  88.         ;.BoundImport.Size               dd 0
  89.         ;.IAT.Rva                        dd 0
  90.         ;.IAT.Size                       dd 0
  91.         ;.DelayImport.Rva                dd 0
  92.         ;.DelayImport.Size               dd 0
  93.         ;.COMPlus.Rva                    dd 0
  94.         ;.COMPlus.Size                   dd 0
  95.         ;.Reserved.Rva                   dd 0
  96.         ;.Reserved.Size                  dd 0

  97. SectionTable:

  98.         .1.Name                         dq +'.text'
  99.         .1.VirtualSize                  dd Section.1.End - Section.1
  100.         .1.VirtualAddress               dd Section.1 - IMAGE_BASE
  101.         .1.SizeOfRawData                dd Section.1.SIZE_IN_FILE
  102.         .1.PointerToRawData             dd Section.1.OFFSET_IN_FILE
  103.         .1.PointerToRelocations         dd 0
  104.         .1.PointerToLineNumbers         dd 0
  105.         .1.NumberOfRelocations          dw 0
  106.         .1.NumberOfLineNumbers          dw 0
  107.         .1.Characteristics              dd 0x60000000 ; IMAGE_SCN_MEM_EXECUTE + IMAGE_SCN_MEM_READ

  108.         .2.Name                         dq +'.rdata'
  109.         .2.VirtualSize                  dd Section.2.End - Section.2
  110.         .2.VirtualAddress               dd Section.2 - IMAGE_BASE
  111.         .2.SizeOfRawData                dd Section.2.SIZE_IN_FILE
  112.         .2.PointerToRawData             dd Section.2.OFFSET_IN_FILE
  113.         .2.PointerToRelocations         dd 0
  114.         .2.PointerToLineNumbers         dd 0
  115.         .2.NumberOfRelocations          dw 0
  116.         .2.NumberOfLineNumbers          dw 0
  117.         .2.Characteristics              dd 0x40000000 ; IMAGE_SCN_MEM_READ

  118. SectionTable.End:

  119. NUMBER_OF_RVA_AND_SIZES := (SectionTable-RvaAndSizes)/8
  120. NUMBER_OF_SECTIONS := (SectionTable.End-SectionTable)/40
  121. SIZE_OF_HEADERS := Section.1.OFFSET_IN_FILE

  122. align SECTION_ALIGNMENT
  123. Section.1:

  124. section $%%
  125. align FILE_ALIGNMENT,0
  126. Section.1.OFFSET_IN_FILE:

  127. section Section.1

  128.         EntryPoint:

  129.                 push    0
  130.                 push    CaptionString
  131.                 push    MessageString
  132.                 push    0
  133.                 call    [MessageBoxA]

  134.                 push    0
  135.                 call    [ExitProcess]

  136. Section.1.End:

  137. align SECTION_ALIGNMENT
  138. Section.2:

  139. section $%%
  140. align FILE_ALIGNMENT,0
  141. Section.1.SIZE_IN_FILE := $ - Section.1.OFFSET_IN_FILE
  142. Section.2.OFFSET_IN_FILE:

  143. section Section.2

  144.         ImportTable:

  145.                 .1.ImportLookupTableRva         dd KernelLookupTable-IMAGE_BASE
  146.                 .1.TimeDateStamp                dd 0
  147.                 .1.ForwarderChain               dd 0
  148.                 .1.NameRva                      dd KernelDLLName-IMAGE_BASE
  149.                 .1.ImportAddressTableRva        dd KernelAddressTable-IMAGE_BASE

  150.                 .2.ImportLookupTableRva         dd UserLookupTable-IMAGE_BASE
  151.                 .2.TimeDateStamp                dd 0
  152.                 .2.ForwarderChain               dd 0
  153.                 .2.NameRva                      dd UserDLLName-IMAGE_BASE
  154.                 .2.ImportAddressTableRva        dd UserAddressTable-IMAGE_BASE

  155.                                                 dd 0,0,0,0,0

  156.                 KernelLookupTable:
  157.                                 dd ExitProcessLookup-IMAGE_BASE
  158.                                 dd 0
  159.                 KernelAddressTable:
  160.                 ExitProcess     dd ExitProcessLookup-IMAGE_BASE ; this is going to be replaced with the address of the function
  161.                                 dd 0

  162.                 UserLookupTable:
  163.                                 dd MessageBoxALookup-IMAGE_BASE
  164.                                 dd 0
  165.                 UserAddressTable:
  166.                 MessageBoxA     dd MessageBoxALookup-IMAGE_BASE ; this is going to be replaced with the address of the function
  167.                                 dd 0

  168.                                 align 2
  169.                 ExitProcessLookup:
  170.                         .Hint   dw 0
  171.                         .Name   db 'ExitProcess',0
  172.                                 align 2
  173.                 MessageBoxALookup:
  174.                         .Hint   dw 0
  175.                         .Name   db 'MessageBoxA',0

  176.                 KernelDLLName   db 'KERNEL32.DLL',0
  177.                 UserDLLName     db 'USER32.DLL',0

  178.         ImportTable.End:

  179.         CaptionString db "PE tutorial",0
  180.         MessageString db "I am alive and well!",0

  181. Section.2.End:

  182. align SECTION_ALIGNMENT
  183. SIZE_OF_IMAGE := $ - IMAGE_BASE

  184. section $%%
  185. align FILE_ALIGNMENT,0
  186. Section.2.SIZE_IN_FILE := $ - Section.2.OFFSET_IN_FILE   
复制代码


回复

使用道具 举报


ADVERTISEMENT

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 3-5-2024 02:41 AM , Processed in 0.052251 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表