1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
/*
cgi.c
Author: Liraz Siri <liraz@bigfoot.com>
Copyright (c) 1998 Liraz Siri <liraz@bigfoot.com>, Ariel, Israel
All rights reserved
Created: Sat Sep 12 01:19:21 IST 1998
Updated: Sat Sep 26 05:16:13 GMT 1998
Code for all the CGI hook functions over in cgihooks.h.
*/
/*
* Revision <Sat Sep 26 05:19:39 GMT 1998> : liraz :
* - Modified cgi_makerequest to detect the presence or non-presence
* of a requested CGI.
* - Created the generic evaluator routines using the uname_ident
* - call, including cgi_exec_uname, and cgi_evaluate_potential.
* - revised optimized, sliced and diced most of the CGI code.
* - Added most of the CGI hook routines. (based on nessus templates)
* - Fixed strseps (again).
*/
#include "BASS.h"
#include "uname.h"
#include "network.h"
#include "cgi.h"
#include "log.h"
char *strseps(char **stringp);
/* general cgi handler. We return -1 on failure and set errno = EBADRQC
if the cgi script requested is not present. tconnect/twrite/network_gets
will set thier own errno on failure and these can be handled at a higher
level, as cgi_makerequest does not tamper with thier failure codes in any
way. note that we allocate memory for response only if *response is
handed out with the value of NULL. From the server we get a response
of maximum max_response_size bytes, and max_response_lines lines. */
int cgi_makerequest(char *request, char **response, int max_response_size,
int max_response_lines, struct sockaddr_in *addr,
int timer)
{
char cgi_request[CGI_REQUEST_LEN], *cp;
int wwwfd, n, received, received_lines;
struct sockaddr_in raddr;
cgi_request[CGI_REQUEST_LEN - 1] = '\0';
memcpy(&raddr, addr, sizeof(struct sockaddr_in));
raddr.sin_port = htons(WEBPORT);
errno = 0;
if((wwwfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ||
tconnect(wwwfd, (struct sockaddr *)&raddr, sizeof( struct sockaddr ),
timer) < 0 ) return -1;
snprintf(cgi_request, CGI_REQUEST_LEN - 1, CGI_TEMPLATE, request);
if(network_twrite(wwwfd, cgi_request, strlen(cgi_request), timer) < 0)
return -1;
max_response_lines += CGI_HEADER_LEN;
if( *response == NULL ) *response = malloc(max_response_size);
for(received = 0, received_lines = 0; received < max_response_size &&
received_lines < max_response_lines;
received += n, received_lines++)
if( (n = network_gets(wwwfd, (*response + received),
max_response_size - received - 1, 10)) < 0)
{
if( errno == ECONNRESET ) break;
return -1;
}
close(wwwfd);
/* Cgi not installed */
if(!strstr(*response, CGI_HTTP_HEADER_10) &&
!strstr(*response, CGI_HTTP_HEADER_11)) {
errno = EBADRQC;
return -1;
}
/* Skip the HTTP header. I'm not sure this is really a convention but
on least on apache we receive double newlines after the header. */
if((cp = strstr(*response, "\n\n"))) {
cp += 2;
received -= (cp - *response);
memmove(*response, cp, received + 1);
}
return received;
}
/* Parse a buffer by newlines. Note that this WILL modify the original
string. So its integrity is void once parsing it to newlines with
strseps */
char *strseps(char **stringp)
{
int i, j;
const char delim[] = "\n\r";
if( !(*stringp)[0] || !*stringp )
return NULL;
for(i = 0; (*stringp)[i]; i++)
for(j = 0; delim[j]; j++)
if((*stringp)[i] == delim[j]) goto found_match;
*stringp += i;
return (*stringp - i);
found_match:
(*stringp)[i] = '\0';
*stringp += i + 1;
return (*stringp - i - 1);
}
/* cgi_exec_uname expects the request to handle the execution of uname,
and will parse the server's reply checking for OS identification
substrings you would expect to receive when executing uname.
We return 1 if execution of uname was detected, 0 if execution
was NOT detected, and -1 on any real error. Note that we put the output
from uname in os_version, but don't deal with it's value otherwise.
we take care of logging ourselves, and thus the need for cgi_alias */
int cgi_exec_uname(char *cgi_alias, char *request, struct sockaddr_in *addr,
char *host, int timer)
{
char *cgi_response, *stringp, *token;
cgi_response = NULL;
if(cgi_makerequest(request, &cgi_response, CGI_MAX_RESPONSE_SIZE,
CGI_DEFAULT_MAX_LINES, addr, timer) < 0) return -1;
stringp = cgi_response;
while((token = strseps(&stringp)))
if(uname_ident(token) != -1) {
token[20] = '\0';
log("%s - [%s] vulnerable version detected (%s).", cgi_alias,
network_getname(host), token);
return 1;
}
log("%s - [%s] immune/dummy cgi version.", cgi_alias, host);
return 0;
}
/* This routine will attempt to assert wether or not a CGI is present
on a remote web server. It takes care of it's OWN non-error logging,
and should be called by hooks that don't/can't get a execution output
(Ie. output from a program like uname/cat etc.), as a milder
alternative to cgi_exec_uname */
int cgi_evaluate_potential(char *cgi_alias, char *request,
struct sockaddr_in *addr, char *host, int timer)
{
char *cgi_response = NULL;
if(cgi_makerequest(request, &cgi_response, CGI_MAX_RESPONSE_SIZE,
CGI_DEFAULT_MAX_LINES, addr, timer) < 0) return -1;
log("%s - [%s] potentially (commonly) vulnerable cgi detected.",
cgi_alias, network_getname(host));
free(cgi_response);
return 0;
}
int cgi_wwwcount_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
char *cgi_response, *stringp, *token;
cgi_response = NULL;
if(cgi_makerequest(WWWCOUNT_REQUEST, &cgi_response, CGI_MAX_RESPONSE_SIZE,
WWWCOUNT_LINES, addr, timer) < 0) return -1;
stringp = cgi_response;
if(!(token = strseps(&stringp)) ||
strncasecmp(token, "GIF", 3) != 0)
return (errno = -1);
if(!(token = strseps(&stringp)) ||
strncasecmp(token, WWWCOUNT_REQUEST, strlen(WWWCOUNT_REQUEST)) != 0) {
log("wwwcount - [%s] no version returned, probably immune.", host);
return 0;
}
token += strlen(WWWCOUNT_REQUEST) + 1;
if( atof(token) < atof(WWWCOUNT_IMMUNE_VERSION) ) {
log("wwwcount - [%s] probably vulnerable version (%s)",
network_getname(host), token);
} else log("wwwcount - [%s] immune version (%s)", host, token);
return 0;
}
int cgi_phf_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
char *cgi_response, *stringp, *token;
cgi_response = NULL;
if(cgi_makerequest(PHF_REQUEST, &cgi_response, CGI_MAX_RESPONSE_SIZE,
PHF_LINES, addr, timer) < 0) return -1;
stringp = cgi_response;
while((token = strseps(&stringp)))
if( !strncasecmp(token, PHF_MARK, strlen(PHF_MARK)) ) break;
if( !token ) return (errno = -1);
if(token[strlen (token) - 1] == '\\') {
log("phf - [%s] definately patched/immune version", host);
return 0;
}
while((token = strseps(&stringp)))
if(uname_ident(token) != -1) {
token[20] = '\0';
log("phf - [%s] vulnerable version (%s)", network_getname(host),
token);
return 0;
}
log("phf - [%s] possible dummy detected. immune.", host);
return 0;
}
int cgi_campas_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_exec_uname("campas", CAMPAS_REQUEST, addr, host, timer);
}
int cgi_faxsurvey_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_exec_uname("faxsurvey", FAXSURVEY_REQUEST, addr, host, timer);
}
int cgi_webdist_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_exec_uname("webdist", WEBDIST_REQUEST, addr, host, timer);
}
int cgi_ews_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_evaluate_potential("ews (Excite search)", EWS_REQUEST, addr,
host, timer);
}
int cgi_glimpse_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_evaluate_potential("glimpse", GLIMPSE_REQUEST, addr, host, timer);
}
int cgi_handler_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_exec_uname("handler", HANDLER_REQUEST, addr, host, timer);
}
int cgi_info2www_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_evaluate_potential("info2www", INFO2WWW_REQUEST, addr, host, timer);
}
int cgi_webgais_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_evaluate_potential("webgais", WEBGAIS_REQUEST, addr, host, timer);
}
int cgi_websendmail_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
return cgi_evaluate_potential("websendmail", WEBSENDMAIL_REQUEST, addr, host,
timer);
}
int cgi_php_handlehost(struct sockaddr_in *addr, char *host, int timer)
{
int wwwfd, received, n;
char *cgi_response = NULL;
char *buff, *crap;
struct sockaddr_in raddr;
if(cgi_makerequest(PHP_REQUEST, &cgi_response, CGI_MAX_RESPONSE_SIZE,
CGI_DEFAULT_MAX_LINES, addr, timer) < 0) return -1;
free(cgi_response);
memcpy(&raddr, addr, sizeof(raddr));
raddr.sin_port = htons(WEBPORT);
/*-- php is present on remote web server --*/
if((wwwfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ||
tconnect(wwwfd, (struct sockaddr *)&raddr, sizeof(struct sockaddr),
timer) < 0) return -1;
/*-- This is rediculous. Why would there be a problem allocating rougly
3k??? BTW, we have to send the request ourselves because cgi_makerequest
is limited to a 256 byte long request length --*/
if(!(buff = (char *)alloca(PHP_REQUEST_LEN)) ||
!(crap = (char *)alloca(PHP_OVERFLOW_LEN + strlen(PHP_REQUEST) + 1)))
{
errno = ENOMEM;
return -1;
}
strcpy(crap, PHP_REQUEST);
memset((crap + strlen(PHP_REQUEST)), 'A', PHP_OVERFLOW_LEN);
crap[PHP_OVERFLOW_LEN + strlen(PHP_REQUEST)] = 0;
snprintf(buff, PHP_REQUEST_LEN - 1, CGI_TEMPLATE, crap);
network_twrite(wwwfd, buff, strlen(buff), timer);
for(received = 0; received < PHP_REQUEST_LEN - 1; received += n)
if((n = network_gets(wwwfd, (buff + received),
PHP_REQUEST_LEN - received - 1, timer)) < 0)
break;
if(!strstr(buff, CGI_HTTP_HEADER_10) &&
!strstr(buff, CGI_HTTP_HEADER_11)) {
log("php - [%s] detected vulnerable (bufferoverflowed) cgi.",
network_getname(host));
return 0;
}
log("php - [%s] probably immune/dummy cgi version.", host);
return 0;
}
|