Writing Nimless
Writing Nimless
Writing Nimless
Note: Default compiles with threads on and orc memory management (results in more IAT imports)
0x02 - basic nim program
main__hello95world_u2 has:
- Overflow checks
- Pseudo vPointer table
- Assertions
- Allocs/Deallocs
- etc.
0x03 - basic nim program (without runtime)
Limitations:
- Drastically different way of writing in Nim
Informations:
- Nim Compiler: Nim Compiler Version 2.0.2
- MingW Compiler: gcc.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.1.0
- (This should work with other Nim and gcc versions, might need slight modification to
code/nim.cfg)
0x03 - basic nim program (without runtime)
We use a nim.cfg that is modified config from Bitmancer repo to facilitate stripping away the
Nim and C runtime.
GetModuleHandle Replacement
Goto functionality
Use function
Winim is doing heavy lifting for us on typecasting; if not in winim, operator has to define.
0x04 - self-deleting program
Almost APT level encryption, just use imagination. (aka, floss finds this).
(see slides 0x9)
0x06 - self-injecting loader with direct syscalls
getPayloadFromUrlA uses the WinAPI to
download a file, it is not very interesting,
We’ve done everything inside of it.
Target is a freshly installed and updated Windows 10 machine with Defender enabled.
0x08 - demo
0x09 - improving stackStringA/W macro
stackString macros are cool. Let’s improve it by including a single byte xor key at compile time.
We create a compileTime
function that generates us a
single byte xor key.
We define our singleByteXor operation on the array that is generated (I,T are
generics to handle different lengths and CHAR/WCHAR). This can be defined with
the noinline pragma if we don’t want the proc inlined.
We use rotr (rotateRightBits) for our index value. This is matched in our complexXor proc.
0x10 - improving stackStringA/W macro pt 2
We want to xor everything except
the last byte, as done in previous
examples.
We can choose to inline or not.
The codegenDecl pragma will attempt to always_inline. Which is successful for our case.
0x11 - improving winim interoperability
We’ve seen winstrConverterCString
before, but what is it?
It is now gone
0x12 - nim.cfg
One thing that was glossed over was the use of a config file. The config bootstraps stripping
away the Nim and C Runtime (NRT & CRT). Let’s discuss What’s needed and what everything is
doing. It’s already heavily documented from zimawhit3’s Bitmancer.
If needing to specify which optimization level (-O0, -O1..), make sure to use
View the cache/<projectname>/main.json file to see how the program is being compiled/linked.
Changing gcc versions allow for different results, gcc version 13.2.0 has access to -Oz, which results
in slightly smaller shellcode.
0x12 - damn, what else?
Play with your compiler
Enabling advanced
instruction set can make it
more difficult to RE, at the
cost of possibly inflating the
binary
0xffffffff - the talk ends
Previous Lecture:
● https://github.com/us-cyber-tea
m/nim_for_hackers2
Contact:
● @m4ul3r_0x00 (twitter)