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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
|
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/fuzz.c,v $
*
* Purpose : Fuzz-related functions for Privoxy.
*
* Copyright : Written by and Copyright (C) 2014-16 by
* Fabian Keil <fk@fabiankeil.de>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*********************************************************************/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "project.h"
#include "filters.h"
#include "loaders.h"
#include "parsers.h"
#include "miscutil.h"
#include "errlog.h"
#include "actions.h"
#include "cgi.h"
#include "loadcfg.h"
#include "urlmatch.h"
#include "filters.h"
#include "jbsockets.h"
#include "gateway.h"
#include "jcc.h"
#include "list.h"
#ifdef FUZZ
static int fuzz_action(struct client_state *csp, char *fuzz_input_file);
static int fuzz_client_header(struct client_state *csp, char *fuzz_input_file);
static int fuzz_deflate(struct client_state *csp, char *fuzz_input_file);
static int fuzz_filter(struct client_state *csp, char *fuzz_input_file);
static int fuzz_gif(struct client_state *csp, char *fuzz_input_file);
static int fuzz_gzip(struct client_state *csp, char *fuzz_input_file);
static int fuzz_socks(struct client_state *csp, char *fuzz_input_file);
static int fuzz_pcrs_substitute(struct client_state *csp, char *fuzz_input_file);
static int fuzz_server_header(struct client_state *csp, char *fuzz_input_file);
struct fuzz_mode
{
const char *name;
const char *expected_input;
const int stdin_support;
int (* const handler)(struct client_state *csp, char *input_file);
};
static const struct fuzz_mode fuzz_modes[] = {
{ "action", "Text to parse as action file.", 0, fuzz_action },
{ "client-request", "Client request to parse. Currently incomplete", 1, fuzz_client_request },
{ "client-header", "Client header to parse.", 1, fuzz_client_header },
{ "chunked-transfer-encoding", "Chunk-encoded data to dechunk.", 1, fuzz_chunked_transfer_encoding },
{ "deflate", "deflate-compressed data to decompress.", 1, fuzz_deflate },
{ "filter", "Text to parse as filter file.", 0, fuzz_filter },
{ "gif", "gif to deanimate.", 1, fuzz_gif },
{ "gzip", "gzip-compressed data to decompress.", 1, fuzz_gzip },
{ "pcrs-substitute", "A pcrs-substitute to compile. Not a whole pcrs job! Example: Bla $1 bla \x43 $3 blah.", 1, fuzz_pcrs_substitute },
{ "server-header", "Server header to parse.", 1, fuzz_server_header },
{ "server-response", "Server response to parse.", 1, fuzz_server_response },
{ "socks", "A socks server response. Only reads from stdin!", 1, fuzz_socks },
};
/*********************************************************************
*
* Function : load_fuzz_input_from_stdin
*
* Description : Loads stdin into a buffer.
*
* Parameters :
* 1 : csp = Used to store the data.
*
* Returns : JB_ERR_OK in case of success,
*
*********************************************************************/
static jb_err load_fuzz_input_from_stdin(struct client_state *csp)
{
static char buf[BUFFER_SIZE];
int ret;
while (0 < (ret = read_socket(0, buf, sizeof(buf))))
{
log_error(LOG_LEVEL_INFO,
"Got %d bytes from stdin: %E. They look like this: %N",
ret, ret, buf);
if (add_to_iob(csp->iob, csp->config->buffer_limit, buf, ret))
{
log_error(LOG_LEVEL_FATAL, "Failed to buffer them.");
}
}
log_error(LOG_LEVEL_INFO, "Read %d bytes from stdin",
csp->iob->eod -csp->iob->cur);
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : load_fuzz_input_from_file
*
* Description : Loads file content into a buffer.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : filename = Name of the file to be loaded.
*
* Returns : JB_ERR_OK in case of success,
*
*********************************************************************/
static jb_err load_fuzz_input_from_file(struct client_state *csp, const char *filename)
{
FILE *fp;
size_t length;
long ret;
fp = fopen(filename, "rb");
if (NULL == fp)
{
log_error(LOG_LEVEL_FATAL, "Failed to open %s: %E", filename);
}
/* Get file length */
if (fseek(fp, 0, SEEK_END))
{
log_error(LOG_LEVEL_FATAL,
"Unexpected error while fseek()ing to the end of %s: %E",
filename);
}
ret = ftell(fp);
if (-1 == ret)
{
log_error(LOG_LEVEL_FATAL,
"Unexpected ftell() error while loading %s: %E",
filename);
}
length = (size_t)ret;
/* Go back to the beginning. */
if (fseek(fp, 0, SEEK_SET))
{
log_error(LOG_LEVEL_FATAL,
"Unexpected error while fseek()ing to the beginning of %s: %E",
filename);
}
csp->iob->size = length + 1;
csp->iob->buf = malloc_or_die(csp->iob->size);
csp->iob->cur = csp->iob->buf;
csp->iob->eod = csp->iob->buf + length;
if (1 != fread(csp->iob->cur, length, 1, fp))
{
/*
* May theoretically happen if the file size changes between
* fseek() and fread() because it's edited in-place. Privoxy
* and common text editors don't do that, thus we just fail.
*/
log_error(LOG_LEVEL_FATAL,
"Couldn't completely read file %s.", filename);
}
*csp->iob->eod = '\0';
fclose(fp);
return JB_ERR_OK;
}
/*********************************************************************
*
* Function : load_fuzz_input
*
* Description : Loads a file into a buffer. XXX: Reverse argument order
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : filename = Name of the file to be loaded.
*
* Returns : JB_ERR_OK in case of success,
*
*********************************************************************/
jb_err load_fuzz_input(struct client_state *csp, const char *filename)
{
if (strcmp(filename, "-") == 0)
{
return load_fuzz_input_from_stdin(csp);
}
return load_fuzz_input_from_file(csp, filename);
}
/*********************************************************************
*
* Function : remove_forbidden_bytes
*
* Description : Sanitizes fuzzed data to decrease the likelihood of
* premature parse abortions.
*
* Parameters :
* 1 : csp = Used to store the data.
*
* Returns : N/A
*
*********************************************************************/
static void remove_forbidden_bytes(struct client_state *csp)
{
char *p = csp->iob->cur;
char first_valid_byte = ' ';
while (p < csp->iob->eod)
{
if (*p != '\0')
{
first_valid_byte = *p;
break;
}
p++;
}
p = csp->iob->cur;
while (p < csp->iob->eod)
{
if (*p == '\0')
{
*p = first_valid_byte;
}
p++;
}
}
/*********************************************************************
*
* Function : fuzz_action
*
* Description : Treat the fuzzed input as action file.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
int fuzz_action(struct client_state *csp, char *fuzz_input_file)
{
csp->config->actions_file[0] = fuzz_input_file;
return(load_action_files(csp));
}
/*********************************************************************
*
* Function : fuzz_client_header
*
* Description : Treat the fuzzed input as a client header.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
int fuzz_client_header(struct client_state *csp, char *fuzz_input_file)
{
char *header;
header = get_header(csp->iob);
if (NULL == header)
{
return 1;
}
if (JB_ERR_OK != enlist(csp->headers, header))
{
return 1;
}
/*
* Silence an insightful client_host_adder() warning
* about ignored weirdness.
*/
csp->flags |= CSP_FLAG_HOST_HEADER_IS_SET;
/* Adding headers doesn't depend on the fuzzed input */
csp->flags |= CSP_FLAG_CLIENT_CONNECTION_HEADER_SET;
/* +hide-if-modified-since{+60} */
csp->action->flags |= ACTION_HIDE_IF_MODIFIED_SINCE;
csp->action->string[ACTION_STRING_IF_MODIFIED_SINCE] = "+60";
/* XXX: Enable more actions. */
return(JB_ERR_OK == sed(csp, FILTER_CLIENT_HEADERS));
}
/*********************************************************************
*
* Function : fuzz_filter
*
* Description : Treat the fuzzed input as filter file.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
int fuzz_filter(struct client_state *csp, char *fuzz_input_file)
{
csp->config->re_filterfile[0] = fuzz_input_file;
return (load_one_re_filterfile(csp, 0));
}
/*********************************************************************
*
* Function : fuzz_deflate
*
* Description : Treat the fuzzed input as data to deflate.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
static int fuzz_deflate(struct client_state *csp, char *fuzz_input_file)
{
csp->content_type = CT_DEFLATE;
return(JB_ERR_OK == decompress_iob(csp));
}
/*********************************************************************
*
* Function : fuzz_gif
*
* Description : Treat the fuzzed input as a gif to deanimate.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
static int fuzz_gif(struct client_state *csp, char *fuzz_input_file)
{
char *deanimated_gif;
if (6 < csp->iob->size)
{
/* Why yes of course, officer, this is a gif. */
memcpy(csp->iob->cur, "GIF87a", 6);
}
/* Using the last image requires parsing of all images */
csp->action->string[ACTION_STRING_DEANIMATE] = "last";
deanimated_gif = gif_deanimate_response(csp);
if (NULL != deanimated_gif)
{
free(deanimated_gif);
return 0;
}
return 1;
}
/*********************************************************************
*
* Function : fuzz_gzip
*
* Description : Treat the fuzzed input as data to unzip
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
static int fuzz_gzip(struct client_state *csp, char *fuzz_input_file)
{
csp->content_type = CT_GZIP;
return(JB_ERR_OK == decompress_iob(csp));
}
/*********************************************************************
*
* Function : fuzz_socks
*
* Description : Treat the fuzzed input as a socks response.
* XXX: This is pretty useless as parsing socks response
* is trivial.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
static int fuzz_socks(struct client_state *csp, char *fuzz_input_file)
{
return(JB_ERR_OK == socks_fuzz(csp));
}
/*********************************************************************
*
* Function : fuzz_pcrs_substitute
*
* Description : Treat the fuzzed input as a pcrs substitute.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
static int fuzz_pcrs_substitute(struct client_state *csp, char *fuzz_input_file)
{
static pcrs_substitute *result;
int err;
remove_forbidden_bytes(csp);
result = pcrs_compile_fuzzed_replacement(csp->iob->cur, &err);
if (NULL == result)
{
log_error(LOG_LEVEL_ERROR,
"Failed to compile pcrs replacement. Error: %s", pcrs_strerror(err));
return 1;
}
log_error(LOG_LEVEL_INFO, "%s", pcrs_strerror(err));
free(result->text);
freez(result);
return 0;
}
/*********************************************************************
*
* Function : fuzz_server_header
*
* Description : Treat the fuzzed input as a server header.
*
* Parameters :
* 1 : csp = Used to store the data.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Result of fuzzed function
*
*********************************************************************/
int fuzz_server_header(struct client_state *csp, char *fuzz_input_file)
{
char *header;
header = get_header(csp->iob);
if (NULL == header)
{
return 1;
}
if (JB_ERR_OK != enlist(csp->headers, header))
{
return 1;
}
/* Adding headers doesn't depend on the fuzzed input */
csp->flags |= CSP_FLAG_CLIENT_HEADER_PARSING_DONE;
csp->flags |= CSP_FLAG_SERVER_CONNECTION_HEADER_SET;
/* +overwrite-last-modified{randomize} */
csp->action->flags |= ACTION_OVERWRITE_LAST_MODIFIED;
csp->action->string[ACTION_STRING_LAST_MODIFIED] = "randomize";
/* +limit-cookie-lifetime{60} */
csp->action->flags |= ACTION_LIMIT_COOKIE_LIFETIME;
csp->action->string[ACTION_STRING_LIMIT_COOKIE_LIFETIME] = "60";
/* XXX: Enable more actions. */
return(JB_ERR_OK == sed(csp, FILTER_SERVER_HEADERS));
}
/*********************************************************************
*
* Function : process_fuzzed_input
*
* Description : Process the fuzzed input in a specified file treating
* it like the input type specified.
*
* Parameters :
* 1 : fuzz_input_type = Type of input.
* 2 : fuzz_input_file = File to read the input from.
*
* Returns : Return value of the fuzzed function
*
*********************************************************************/
int process_fuzzed_input(char *fuzz_input_type, char *fuzz_input_file)
{
static struct client_state csp_stack_storage;
static struct configuration_spec config_stack_storage;
struct client_state *csp;
int i;
csp = &csp_stack_storage;
csp->config = &config_stack_storage;
csp->config->buffer_limit = 4096 * 1024;
csp->config->receive_buffer_size = 4096;
/* In --stfu mode, these will be ignored ... */
set_debug_level(LOG_LEVEL_ACTIONS|LOG_LEVEL_CONNECT|LOG_LEVEL_DEANIMATE|LOG_LEVEL_INFO|LOG_LEVEL_ERROR|LOG_LEVEL_RE_FILTER|LOG_LEVEL_HEADER|LOG_LEVEL_WRITING|LOG_LEVEL_RECEIVED);
csp->flags |= CSP_FLAG_FUZZED_INPUT;
csp->config->feature_flags |= RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS;
#ifdef FEATURE_CLIENT_TAGS
csp->config->trust_x_forwarded_for = 1;
#endif
for (i = 0; i < SZ(fuzz_modes); i++)
{
if (strcmp(fuzz_modes[i].name, fuzz_input_type) == 0)
{
if (fuzz_modes[i].stdin_support &&
(strcmp(fuzz_input_type, "client-request") != 0) &&
(strcmp(fuzz_input_type, "server-response") != 0) &&
(strcmp(fuzz_input_type, "socks") != 0))
{
load_fuzz_input(csp, fuzz_input_file);
}
return (fuzz_modes[i].handler(csp, fuzz_input_file));
}
}
log_error(LOG_LEVEL_FATAL,
"Unrecognized fuzz type %s for input file %s. You may need --help.",
fuzz_input_type, fuzz_input_file);
/* Not reached. */
return 1;
}
/*********************************************************************
*
* Function : show_fuzz_usage
*
* Description : Shows the --fuzz usage. D'oh.
*
* Parameters : Pointer to argv[0] for identifying ourselves
*
* Returns : void
*
*********************************************************************/
void show_fuzz_usage(const char *name)
{
int i;
printf("%s%s --fuzz fuzz-mode ./path/to/fuzzed/input [--stfu]\n\n",
" ", name);
printf("Supported fuzz modes and the expected input:\n");
for (i = 0; i < SZ(fuzz_modes); i++)
{
printf(" %s: %s\n", fuzz_modes[i].name, fuzz_modes[i].expected_input);
}
printf("\n");
printf("The following fuzz modes read data from stdin if the 'file' is '-'\n");
for (i = 0; i < SZ(fuzz_modes); i++)
{
if (fuzz_modes[i].stdin_support)
{
printf(" %s\n", fuzz_modes[i].name);
}
}
printf("\n");
}
#endif
|