flat assembler
Message board for the users of flat assembler.

Index > Windows > Tiny PE in win64

Goto page Previous  1, 2, 3, 4  Next
Author
Thread Post new topic Reply to topic
MatQuasar



Joined: 25 Oct 2023
Posts: 105
MatQuasar 19 Apr 2024, 14:22
jochenvnltn wrote:
What about a self replicating 64bit EXE ?


You can, either submit a pull request (late entry will be accepted) or use your tiny PE and wait for BGGP 2024 (begin in June).

I was the winner of PE category (@fliermate) with 1024 bytes.
But my late entry is a 528-byte 32-bit PE, as demonstrated in the second last post in page 1 of this thread. (Or you can click link: https://board.flatassembler.net/topic.php?p=237311#237311 )

With your 309-byte tiny PE, I think you can easily beat mine 528-byte PE (and 1KB PE).
Post 19 Apr 2024, 14:22
View user's profile Send private message Reply with quote
jochenvnltn



Joined: 15 Jul 2011
Posts: 96
jochenvnltn 19 Apr 2024, 17:02
MatQuasar wrote:
jochenvnltn wrote:
What about a self replicating 64bit EXE ?


You can, either submit a pull request (late entry will be accepted) or use your tiny PE and wait for BGGP 2024 (begin in June).

I was the winner of PE category (@fliermate) with 1024 bytes.
But my late entry is a 528-byte 32-bit PE, as demonstrated in the second last post in page 1 of this thread. (Or you can click link: https://board.flatassembler.net/topic.php?p=237311#237311 )

With your 309-byte tiny PE, I think you can easily beat mine 528-byte PE (and 1KB PE).


I now got a working x64 PE, with an import table that shows a MessageBox to

277bytes Smile
Post 19 Apr 2024, 17:02
View user's profile Send private message MSN Messenger Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4558
Location: vpcmpistri
bitRAKE 22 Apr 2024, 07:13
jochenvnltn wrote:
Ill delete my post sorry !
Nothing to be sorry about. If you say you did a thing that is enough for me - I don't need proof. Of course, if you did something novel we could all learn from then a code snippet would be most generous.

Here is a related article, perhaps of interest ...
https://secret.club/2023/06/05/spoof-pe-sections.html
(FYI, this technique still works in Win11.)

One of the beautiful things about such an advanced assembler, is that we can write code to verify what we know about the loader!
Code:
; verify some known rules of loader:

load e_lfanew:4 from $$+3Ch ; Offset to the NT header
assert e_lfanew < ($-$$-4) ; can't be larger than file
load PE_Sig:4 from $$+e_lfanew
assert PE_Sig = 'PE'    
... this way we can try complex overlapping windows and other tricks and assemble-time code verifies we haven't broken something simple. Put this verifier in another file to be included at the end, and it'll save a lot of time. Find ways to add all the discoveries you make.

_________________
¯\(°_o)/¯ AI may [not] have aided with the above reply.
Post 22 Apr 2024, 07:13
View user's profile Send private message Visit poster's website Reply with quote
jochenvnltn



Joined: 15 Jul 2011
Posts: 96
jochenvnltn 23 Apr 2024, 17:21
I found this : https://github.com/ayaka14732/TinyPE-on-Win10
Not sure if i want to just take what he did and put it into FASM code.
He got to this result by his own effort and i don't want to replicate his project..
A PE file that's 277 bytes and works with Win10 & 11 isn't that bad Smile
Post 23 Apr 2024, 17:21
View user's profile Send private message MSN Messenger Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 23 Jun 2024, 21:01
A virus researcher's guide to Tiny PE header, with comments for each header field!

http://pferrie.epizy.com/misc/tiny/pehdr.htm
Post 23 Jun 2024, 21:01
View user's profile Send private message Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 25 Jun 2024, 13:13
Any tiny PE example using GetProcAddress and then LoadLibraryA to call API, especially when need to call more than one API function and want to get rid of import table.

I see the example code uses this in the beginning:
Code:
mov eax, [fs:ecx+0x30]    


What is "fs"?
Post 25 Jun 2024, 13:13
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 826
Location: Russian Federation, Sochi
ProMiNick 25 Jun 2024, 13:59
(related to win32 subsys) In flat memory model all segments force address space to be mapped exactly at its origin, but fs segment maps memory from TEB structure located somewhere in the middle of address space. for any thread TEB is always located by random addresses from launch to launch. But fs in every thread allways points to TEB of that thread.
Post 25 Jun 2024, 13:59
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21023
Location: In your JS exploiting you and your system
revolution 25 Jun 2024, 14:42
MatQuasar2 wrote:
What is "fs"?
A segment register.

CS
DS
ES
FS
GS
SS

All those are segment registers.

Because of paging, segments are mostly useless nowadays, but Windows still uses FS to point to the thread local storage area.
Post 25 Jun 2024, 14:42
View user's profile Send private message Visit poster's website Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 25 Jun 2024, 14:47
Thanks ProMiNick and revolution, I learned something useful today from both of you.
Post 25 Jun 2024, 14:47
View user's profile Send private message Reply with quote
MatQuasar2



Joined: 10 Jun 2024
Posts: 26
MatQuasar2 25 Jun 2024, 15:44
MatQuasar2 wrote:
Any tiny PE example using GetProcAddress and then LoadLibraryA to call API, especially when need to call more than one API function and want to get rid of import table.



I found a great example: https://keyj.emphy.de/win32-pe/

TEB --> PEB --> base address of Kernel32.dll in memory --> ....
Post 25 Jun 2024, 15:44
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2075
Roman 26 Jun 2024, 11:27
Quote:

CS
DS
ES
FS
GS
SS


In Windows this registers unavailable.
6 registers i could using for my calculation.
Sad but my program crash if i do mov es,ax or mov gs,ax or mov ds,ax.
Post 26 Jun 2024, 11:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21023
Location: In your JS exploiting you and your system
revolution 26 Jun 2024, 11:36
Officially, in protected mode they aren't called registers, they are called selectors. And you can't arbitrarily select a different segment.

If you want to use them as registers, then you have to switch to (un)real mode.
Post 26 Jun 2024, 11:36
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 2075
Roman 26 Jun 2024, 15:25
Quote:
If you want to use them as registers, then you have to switch to (un)real mode.

How do this in Windows ?
This is option in fasm compiler setup ?
Post 26 Jun 2024, 15:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21023
Location: In your JS exploiting you and your system
revolution 26 Jun 2024, 15:30
You can't switch any current version of Windows to real mode. Real mode is the 1MB, 16-bit mode, with segments. It can be extended to unreal mode using some tricks, but there is no way you can get Windows to run there.

You can run DOS in real mode. Or write your own OS and use real mode there.
Post 26 Jun 2024, 15:30
View user's profile Send private message Visit poster's website Reply with quote
therektafire



Joined: 06 Dec 2023
Posts: 7
therektafire 06 Jul 2026, 21:42
Mikl___ wrote:
a working PE64 with import, size of exe-file is 282 bytes run on Windows 10
Code:
format binary as "exe"
include "win64a.inc"
struc dbs [data]
{
  common
  . db data
  .size = $ - .
}
 
IMAGE_DOS_SIGNATURE        equ 5A4Dh
IMAGE_NT_SIGNATURE        equ 00004550h
PROCESSOR_AMD_X8664        equ 8664h
IMAGE_SCN_CNT_CODE        equ 00000020h
IMAGE_SCN_MEM_WRITE        equ 80000000h
IMAGE_SCN_MEM_READ        equ 40000000h
IMAGE_SCN_CNT_INITIALIZED_DATA    equ 00000040h
IMAGE_SUBSYSTEM_WINDOWS_GUI    equ 2
IMAGE_NT_OPTIONAL_HDR64_MAGIC    equ 20Bh
IMAGE_FILE_RELOCS_STRIPPED    equ 1
IMAGE_FILE_EXECUTABLE_IMAGE    equ 2
IMAGE_BASE            equ 0x400000
align1                equ 4;0x10
IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE equ 8000h
use64
org 0
;--------DOS-stub-------------------------------
Signature       dw IMAGE_DOS_SIGNATURE,0
;-------PE--------------------------------------------------
ntHeader        dd IMAGE_NT_SIGNATURE;'PE'
;image_header----
Machine             dw PROCESSOR_AMD_X8664
Count_of_section    dw 1
TimeStump           dd 0
Symbol_table_offset dd 0
Symbol_table_count  dd 0
Size_of_optional_header dw section_table-optional_header
Characteristics     dw IMAGE_FILE_RELOCS_STRIPPED or \
IMAGE_FILE_EXECUTABLE_IMAGE
;-------
optional_header:
Magic_optional_header    dw IMAGE_NT_OPTIONAL_HDR64_MAGIC
Linker_version_major_and_minor dw 9
Size_of_code        dd Import_Table-begin
Size_of_init_data   dd 0x70
Size_of_uninit_data dd 0
entry_point         dd begin
base_of_code        dd ntHeader
;-----------------------------------------------------
image_base          dq IMAGE_BASE
section_alignment   dd align1
file_alignment      dd align1
OS_version_major_minor    dw 5,2
image_version_major_minor dd 0
subsystem_version_major_minor dw 5,2
Win32_version       dd 0
size_of_image       dd end_import
size_of_header      dd begin
checksum            dd 0
subsystem           dw IMAGE_SUBSYSTEM_WINDOWS_GUI
DLL_flag            dw IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE
Stack_allocation    dq 0x100000
Stack_commit        dq 0x1000
Heap_allocation     dq 0x100000
Heap_commit         dq 0x1000
loader_flag         dd 0
number_of_dirs      dd (section_table-export_RVA_size)/8
export_RVA_size     dq 0
import_RVA_size     dd _import,0x3C
;------------------------------------------------
section_table       dq ".text"
.virtual_size       dd 0x55
.virtual_address    dd begin
.Physical_size      dd end_import-begin
.Physical_offset    dd begin
.Relocations_and_Linenumbers dq 0
.Relocations_and_Linenumbers_count dd 0
.Attributes         dd IMAGE_SCN_MEM_WRITE or IMAGE_SCN_CNT_CODE or IMAGE_SCN_MEM_READ ;or IMAGE_SCN_CNT_INITIALIZED_DATA
;------------------------------------------------
begin:
    push rbp
    xor ecx,ecx
    mov edx,user32_dll+IMAGE_BASE
    lea r8d,[rdx+12]
    xor r9d,r9d
    call [MessageBox]
    pop rbp
    retn
;------------------------------------------------
Import_Table:
user32_table:
MessageBox  dq _MessageBox,0
_import:
dd 0,0,0,user32_dll,user32_table
dd 0
user32_dll db "user32",0,0
dw 0
_MessageBox        db 0,0,"MessageBoxA"
end_import:    


Hey so I'm coming back to this since I want to use this as a basis for a personal asm project I'm trying to get started with, and I'm wondering if there is any way to automatically calculate some of the hardcoded stuff like the virtual size and the import RVA size? Having to figure out exactly what values they need to be every time I want to make a code change would be annoying. And this is more so related to my complete and utter lack of windows PE knowledge but I'm also not sure how I would extend the import table to add multiple dlls since I also want to use specific functions from opengl32 and kernel32.
Post 06 Jul 2026, 21:42
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8549
Location: Kraków, Poland
Tomasz Grysztar 06 Jul 2026, 22:10
therektafire wrote:
Hey so I'm coming back to this since I want to use this as a basis for a personal asm project I'm trying to get started with, and I'm wondering if there is any way to automatically calculate some of the hardcoded stuff like the virtual size and the import RVA size? Having to figure out exactly what values they need to be every time I want to make a code change would be annoying. And this is more so related to my complete and utter lack of windows PE knowledge but I'm also not sure how I would extend the import table to add multiple dlls since I also want to use specific functions from opengl32 and kernel32.
I don't know the exact requirements of your project, but to get accustomed with manual construction of PE please consider my PE tutorial. The templates provided there are flexible, with all the values computed properly.
Post 06 Jul 2026, 22:10
View user's profile Send private message Visit poster's website Reply with quote
therektafire



Joined: 06 Dec 2023
Posts: 7
therektafire 07 Jul 2026, 21:02
Tomasz Grysztar wrote:
therektafire wrote:
Hey so I'm coming back to this since I want to use this as a basis for a personal asm project I'm trying to get started with, and I'm wondering if there is any way to automatically calculate some of the hardcoded stuff like the virtual size and the import RVA size? Having to figure out exactly what values they need to be every time I want to make a code change would be annoying. And this is more so related to my complete and utter lack of windows PE knowledge but I'm also not sure how I would extend the import table to add multiple dlls since I also want to use specific functions from opengl32 and kernel32.
I don't know the exact requirements of your project, but to get accustomed with manual construction of PE please consider my PE tutorial. The templates provided there are flexible, with all the values computed properly.

Thanks, I'm reading through it now and it does indeed seem pretty useful 👍 I already understand most of the actual assembly stuff, it's just the exe formatting part that's tripping me up. It's definitely not a requirement for me to manually create everything, I could just use the built in fasm x64 PE formatting, I just wanted to do it for the "I made it super small" brag Laughing
Post 07 Jul 2026, 21:02
View user's profile Send private message Visit poster's website Reply with quote
Calendos



Joined: 20 Jan 2021
Posts: 20
Location: France
Calendos 19 Jul 2026, 17:00
Hello everyone!
In one of the Win64 programs provided as an example in the FASM package (Template.asm), I see the instruction `sub rsp,8` at the entry point. Unless I'm mistaken, I don't see the point of aligning the code to a DQWORD at the beginning of the section when it seems to me that this alignment is implicit. Furthermore, subtracting 8 bytes from `RSP` doesn't seem to guarantee this alignment requirement. What do you think?
I've included the beginning of the ASM file below.
Thank you!
Code:
format PE64 GUI 5.0
entry start

include 'win64a.inc'

section '.text' code readable executable

  start:
        sub     rsp,8           ; Make stack dqword aligned

        invoke  GetModuleHandle,0
        mov     [wc.hInstance],rax
        invoke  LoadIcon,0,IDI_APPLICATION
        mov     [wc.hIcon],rax
        mov     [wc.hIconSm],rax
        invoke  LoadCursor,0,IDC_ARROW
        mov     [wc.hCursor],rax
        invoke  RegisterClassEx,wc
        test    rax,rax
        jz      error

        invoke  CreateWindowEx,0,_class,_title,WS_VISIBLE+WS_DLGFRAME+WS_SYSMENU,128,128,256,192,NULL,NULL,[wc.hInstance],NULL
        test    rax,rax
        jz      error   
        ….
    
Post 19 Jul 2026, 17:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21023
Location: In your JS exploiting you and your system
revolution 19 Jul 2026, 17:30
The stack alignment is a requirement for the FASTCALL convention to work.

It is guaranteed to make RSP aligned, because the loader will call the entry point with an unaligned RSP, as per the FASTCALL convention.

Without the stack alignment the program can crash when the OS call (using invoke) uses MOVAPD to copy the registers to the shadow stack.

So, yes, the sub rsp,8 is required, unless the desire is to have unreliable code that crashes.

BTW: I recommend to use push rbp instead of sub rsp,8. It is shorter and is more compatible with debuggers.
Post 19 Jul 2026, 17:30
View user's profile Send private message Visit poster's website Reply with quote
Calendos



Joined: 20 Jan 2021
Posts: 20
Location: France
Calendos 19 Jul 2026, 17:57
Thank you very much for your very quick and extremely detailed response. However, I need some time to think about what you said. I am struggling to understand why the 'text' section doesn't implicitly provide the required alignment. Am I making myself clear?
Post 19 Jul 2026, 17:57
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3, 4  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.