GithubHelp home page GithubHelp logo

min-sized-rust-windows's Introduction

Minimum Binary Size Windows

CI

The smallest hello world I could get on win10 x64 in rust. This isn't something meant to be used in production, more of a challenge. I'm in no ways an expert and I have seen windows binaries get smaller on windows. [2] If you can go smaller let me know how you did it ๐Ÿ˜

Results

464b ๐Ÿ˜Ž

โฏ cargo +nightly install anonlink
โฏ anonlink
โฏ cargo +nightly run --release
Hello World!

โฏ cargo +nightly build --release && (Get-Item ".\target\release\min-sized-rust-windows.exe").Length
   Compiling min-sized-rust-windows v0.1.0 (**\min-sized-rust-windows)
    Finished release [optimized] target(s) in 1.33s
464

Strategies

I'm excluding basic strategies here such as enabling lto and setting opt-level = 'z'. [0]

  • no_std
  • no_main
  • Merge .rdata and .pdata sections into .text section linker flag. [1]
    • Using the LINK.exe /MERGE
      flag found at the bottom of main.rs.
    • Section definitions add more junk to the final output, and I believe they have a min-size. For this example we really don't care about readonly data (.rdata) or exception handlers (.pdata) so we "merge" these empty sections into the .text sections.
  • No imports.
    • To avoid having an extra .idata section (more bytes and cannot be merged into .text section using LINK.exe) we do the following.
    • Resolve stdout handle from PEB's process parameters (thanks ChrisSD). [3][4]
    • Invoke NtWriteFile/ZwWriteFile using syscall 0x80. [5][6]
      1. This is undocumented behaviour in windows, syscalls change over time. [5]
      2. I can't guarantee this will work on your edition of windows.. it's tested on my local machine (W10) and on GH actions (windows-2022 and windows-2019) server editions.
  • Custom LINK.exe stub.
    • A custom built stub created to remove Rich PE header. More information can be found here.
    • Credits to @Frago9876543210 for finding, and implementing this.
  • Drop debug info in pe header.
    • Add /EMITPOGOPHASEINFO /DEBUG:NONE flags.
    • Credits to @Frago9876543210 for finding, and implementing this.

Future

  • Using strategies shown in [2] we could post process the exe and merge headers to get closer to the 600-500b mark although we start straying away from the goal of this project.
  • Provided the call signature of ZwWriteFile I could use build.rs to make a script to dynamically resolve the syscall number from ntdll using something like iced-x86.
  • Go pure assembly (drop type definitions for PEB).

References

  1. https://github.com/johnthagen/min-sized-rust
  2. www.catch22.net/tuts/win32/reducing-executable-size#use-the-right-linker-settings
  3. https://github.com/pts/pts-tinype
  4. https://news.ycombinator.com/item?id=25266892 (Thank you anonunivgrad & ChrisSD!)
  5. https://processhacker.sourceforge.io/doc/struct___r_t_l___u_s_e_r___p_r_o_c_e_s_s___p_a_r_a_m_e_t_e_r_s.html
  6. https://j00ru.vexillium.org/syscalls/nt/64/
  7. https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/nf-ntifs-ntwritefile

Credits

  • @Frago9876543210 - Brought binary size from 760b -> 600b ๐Ÿ˜
  • @Frago9876543210 - Brought binary size from 600b -> 560b ๐Ÿ˜
  • @ironhaven - Brought binary size from 560b -> 536b ๐Ÿ˜
  • @StackOverflowExcept1on - Brought binary size from 536b -> 464b ๐Ÿ˜

min-sized-rust-windows's People

Contributors

dependabot[bot] avatar frago9876543210 avatar ironhaven avatar mcountryman avatar mkantor avatar stackoverflowexcept1on avatar zohnannor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

min-sized-rust-windows's Issues

invoke NtWriteFile for printing on console compability.

Invoking NtWriteFile for printing on console is working under Windows 8 and above only (via ConDrv.sys driver).
Below that version, it invokes CsrClientCallServer for printing on console.
Windows XP-Vista didn't have conhost.
Windows 7 have conhost spawned from csrss.
From Windows 8 and above, each console have own conhost and it has ConDrv.sys driver.

Reference : https://www.mandiant.com/resources/blog/monitoring-windows-console-activity-part-one

Post-compilation script

  • Add post-compilation rust script to merge headers
  • This is totally not the ship of Theseus, I swear

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.