commit b9452572f1dfdb6473f3f6308a99c5723a842238 Author: Zack Weinberg zackw@panix.com Date: Thu Mar 29 19:49:00 2012 -0700
Make payload data per-steg configuration. It *should* now be possible to run the automated tests on HTTP steg. --- src/steg/http.cc | 41 +++++---- src/steg/jsSteg.cc | 12 ++- src/steg/jsSteg.h | 3 +- src/steg/payloads.cc | 248 +++++++++++++++++++------------------------------- src/steg/payloads.h | 34 +++++-- src/steg/pdfSteg.cc | 9 +- src/steg/pdfSteg.h | 4 +- src/steg/swfSteg.cc | 15 +-- src/steg/swfSteg.h | 10 +-- 9 files changed, 166 insertions(+), 210 deletions(-)
diff --git a/src/steg/http.cc b/src/steg/http.cc index 1ee4254..77ea859 100644 --- a/src/steg/http.cc +++ b/src/steg/http.cc @@ -61,6 +61,7 @@ namespace { struct http_steg_config_t : steg_config_t { bool is_clientside : 1; + payloads pl;
STEG_CONFIG_DECLARE_METHODS(http); }; @@ -86,6 +87,17 @@ http_steg_config_t::http_steg_config_t(config_t *cfg) : steg_config_t(cfg), is_clientside(cfg->mode != LSN_SIMPLE_SERVER) { + + if (is_clientside) + load_payloads(this->pl, "traces/client.out"); + else { + load_payloads(this->pl, "traces/server.out"); + init_JS_payload_pool(this->pl, HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, JS_MIN_AVAIL_SIZE); + // init_JS_payload_pool(this, HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, JS_MIN_AVAIL_SIZE, HTTP_CONTENT_HTML); + init_HTML_payload_pool(this->pl, HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, HTML_MIN_AVAIL_SIZE); + init_PDF_payload_pool(this->pl, HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, PDF_MIN_AVAIL_SIZE); + init_SWF_payload_pool(this->pl, HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, 0); + } }
http_steg_config_t::~http_steg_config_t() @@ -160,17 +172,6 @@ http_steg_t::http_steg_t(http_steg_config_t *cf, conn_t *cn) have_transmitted(false), have_received(false) { memset(peer_dnsname, 0, sizeof peer_dnsname); - - if (config->is_clientside) - load_payloads("traces/client.out"); - else { - load_payloads("traces/server.out"); - init_JS_payload_pool(HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, JS_MIN_AVAIL_SIZE); - // init_JS_payload_pool(HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, JS_MIN_AVAIL_SIZE, HTTP_CONTENT_HTML); - init_HTML_payload_pool(HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, HTML_MIN_AVAIL_SIZE); - init_PDF_payload_pool(HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, PDF_MIN_AVAIL_SIZE); - init_SWF_payload_pool(HTTP_MSG_BUF_SIZE, TYPE_HTTP_RESPONSE, 0); - } }
http_steg_t::~http_steg_t() @@ -207,7 +208,7 @@ http_steg_t::transmit_room() return 1024;
case HTTP_CONTENT_JAVASCRIPT: - mjc = get_max_JS_capacity() / 2; + mjc = config->pl.max_JS_capacity / 2; if (mjc > 1024) { // it should be 1024 + ...., but seems like we need to be a little bit smaller (chopper bug?) int rval = 512 + rand()%(mjc - 1024); @@ -218,7 +219,7 @@ http_steg_t::transmit_room() exit(-1);
case HTTP_CONTENT_HTML: - mjc = get_max_HTML_capacity() / 2; + mjc = config->pl.max_HTML_capacity / 2; if (mjc > 1024) { // it should be 1024 + ...., but seems like we need to be a little bit smaller (chopper bug?) int rval = 512 + rand()%(mjc - 1024); @@ -323,7 +324,8 @@ http_client_cookie_transmit (http_steg_t *s, struct evbuffer *source,
// retry up to 10 times while (!payload_len) { - payload_len = find_client_payload(buf, bufsize, TYPE_HTTP_REQUEST); + payload_len = find_client_payload(s->config->pl, buf, bufsize, + TYPE_HTTP_REQUEST); if (cnt++ == 10) { goto err; } @@ -547,7 +549,8 @@ http_client_uri_transmit (http_steg_t *s,
// retry up to 10 times while (!len) { - len = find_client_payload(buf, sizeof(buf), TYPE_HTTP_REQUEST); + len = find_client_payload(s->config->pl, buf, sizeof(buf), + TYPE_HTTP_REQUEST); if (cnt++ == 10) return -1; }
@@ -617,19 +620,19 @@ http_steg_t::transmit(struct evbuffer *source) switch(type) {
case HTTP_CONTENT_SWF: - rval = http_server_SWF_transmit(this, source, conn); + rval = http_server_SWF_transmit(this->config->pl, source, conn); break;
case HTTP_CONTENT_JAVASCRIPT: - rval = http_server_JS_transmit(this, source, conn, HTTP_CONTENT_JAVASCRIPT); + rval = http_server_JS_transmit(this->config->pl, source, conn, HTTP_CONTENT_JAVASCRIPT); break;
case HTTP_CONTENT_HTML: - rval = http_server_JS_transmit(this, source, conn, HTTP_CONTENT_HTML); + rval = http_server_JS_transmit(this->config->pl, source, conn, HTTP_CONTENT_HTML); break;
case HTTP_CONTENT_PDF: - rval = http_server_PDF_transmit(this, source, conn); + rval = http_server_PDF_transmit(this->config->pl, source, conn); break; }
diff --git a/src/steg/jsSteg.cc b/src/steg/jsSteg.cc index 335b78e..60893ef 100644 --- a/src/steg/jsSteg.cc +++ b/src/steg/jsSteg.cc @@ -706,8 +706,10 @@ int testDecode2(char *inBuf, char *outBuf, }
-int -http_server_JS_transmit (steg_t*, struct evbuffer *source, conn_t *conn, unsigned int content_type) { +int +http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn, + unsigned int content_type) +{
struct evbuffer_iovec *iv; int nv; @@ -742,9 +744,9 @@ http_server_JS_transmit (steg_t*, struct evbuffer *source, conn_t *conn, unsigne }
if (content_type == HTTP_CONTENT_JAVASCRIPT) { - mjs = get_max_JS_capacity(); + mjs = pl.max_JS_capacity; } else if (content_type == HTTP_CONTENT_HTML) { - mjs = get_max_HTML_capacity(); + mjs = pl.max_HTML_capacity; }
if (mjs <= 0) { @@ -781,7 +783,7 @@ http_server_JS_transmit (steg_t*, struct evbuffer *source, conn_t *conn, unsigne
- if (get_payload(content_type, datalen, &jsTemplate, &jsLen) == 1) { + if (get_payload(pl, content_type, datalen, &jsTemplate, &jsLen) == 1) { log_debug("SERVER found the applicable HTTP response template with size %d", jsLen); } else { log_warn("SERVER couldn't find the applicable HTTP response template"); diff --git a/src/steg/jsSteg.h b/src/steg/jsSteg.h index 8a351cc..709ceb0 100644 --- a/src/steg/jsSteg.h +++ b/src/steg/jsSteg.h @@ -18,6 +18,7 @@ // controlling content gzipping for jsSteg #define JS_GZIP_RESP 1
+struct payloads;
int encodeHTTPBody(char *data, char *jTemplate, char *jData,unsigned int dlen, unsigned int jtlen, unsigned int jdlen, int mode); @@ -62,7 +63,7 @@ int testDecode2(char *inBuf, char *outBuf,
int -http_server_JS_transmit (steg_t* s, struct evbuffer *source, conn_t *conn, unsigned int content_type); +http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn, unsigned int content_type);
int http_handle_client_JS_receive(steg_t *s, conn_t *conn, struct evbuffer *dest, struct evbuffer* source); diff --git a/src/steg/payloads.cc b/src/steg/payloads.cc index c7e2638..d9546d2 100644 --- a/src/steg/payloads.cc +++ b/src/steg/payloads.cc @@ -2,38 +2,6 @@ #include "payloads.h" #include "swfSteg.h"
- -/* These variables below are write-once, hence they should be race-safe */ - -static int initTypePayload[MAX_CONTENT_TYPE]; -static int typePayloadCount[MAX_CONTENT_TYPE]; -static int typePayload[MAX_CONTENT_TYPE][MAX_PAYLOADS]; -static int typePayloadCap[MAX_CONTENT_TYPE][MAX_PAYLOADS]; - - -static unsigned int max_JS_capacity = 0; -static unsigned int max_HTML_capacity = 0; -static unsigned int max_PDF_capacity = 0; - -static pentry_header payload_hdrs[MAX_PAYLOADS]; -static char* payloads[MAX_PAYLOADS]; -static int payload_count = 0; - - -unsigned int get_max_JS_capacity() { - return max_JS_capacity; -} - -unsigned int get_max_HTML_capacity() { - return max_HTML_capacity; -} - -unsigned int get_max_PDF_capacity() { - return max_PDF_capacity; -} - - - /* * fixContentLen corrects the Content-Length for an HTTP msg that * has been ungzipped, and removes the "Content-Encoding: gzip" @@ -208,7 +176,8 @@ log_debug("new: hdrLen = %d, bodyLen = %d, payloadLen = %d", hdrLen, bodyLen, hd return -1; }
-void load_payloads(const char* fname) { +void load_payloads(payloads& pl, const char* fname) +{ FILE* f; char buf[HTTP_MSG_BUF_SIZE]; char buf2[HTTP_MSG_BUF_SIZE]; @@ -216,9 +185,6 @@ void load_payloads(const char* fname) { int pentryLen; int r;
- if (payload_count != 0) - return; - srand(time(NULL)); f = fopen(fname, "r"); if (f == NULL) { @@ -226,9 +192,10 @@ void load_payloads(const char* fname) { exit(1); }
- bzero(payload_hdrs, sizeof(payload_hdrs)); + bzero(pl.payload_hdrs, sizeof(pl.payload_hdrs)); + pl.payload_count = 0;
- while (payload_count < MAX_PAYLOADS) { + while (pl.payload_count < MAX_PAYLOADS) {
if (fread(&pentry, 1, sizeof(pentry_header), f) < sizeof(pentry_header)) { break; @@ -270,22 +237,21 @@ void load_payloads(const char* fname) { // }
if (r < 0) { - payloads[payload_count] = (char *)xmalloc(pentry.length + 1); - memcpy(payloads[payload_count], buf, pentry.length); + pl.payloads[pl.payload_count] = (char *)xmalloc(pentry.length + 1); + memcpy(pl.payloads[pl.payload_count], buf, pentry.length); } else { pentry.length = r; - payloads[payload_count] = (char *)xmalloc(pentry.length + 1); - memcpy(payloads[payload_count], buf2, pentry.length); + pl.payloads[pl.payload_count] = (char *)xmalloc(pentry.length + 1); + memcpy(pl.payloads[pl.payload_count], buf2, pentry.length); } - payload_hdrs[payload_count] = pentry; - payloads[payload_count][pentry.length] = 0; - payload_count++; + pl.payload_hdrs[pl.payload_count] = pentry; + pl.payloads[pl.payload_count][pentry.length] = 0; + pl.payload_count++; } // while
-#ifdef DEBUG - printf("loading payload count = %d\n", payload_count); -#endif - + + log_debug("loaded %d payloads from %s\n", pl.payload_count, fname); + fclose(f); }
@@ -561,18 +527,16 @@ find_uri_type(char* buf) {
-unsigned int find_client_payload(char* buf, int len, int type) { - int r = rand() % payload_count; +unsigned int find_client_payload(payloads& pl, char* buf, int len, int type) { + int r = rand() % pl.payload_count; int cnt = 0; char* inbuf;
-#ifdef DEBUG - fprintf(stderr, "TRYING payload %d \n", r); -#endif + log_debug("trying payload %d", r); while (1) { - pentry_header* p = &payload_hdrs[r]; + pentry_header* p = &pl.payload_hdrs[r]; if (p->ptype == type) { - inbuf = payloads[r]; + inbuf = pl.payloads[r]; if (find_uri_type(inbuf, p->length) != HTTP_CONTENT_SWF && find_uri_type(inbuf, p->length) != HTTP_CONTENT_HTML && find_uri_type(inbuf, p->length) != HTTP_CONTENT_JAVASCRIPT && @@ -588,12 +552,11 @@ unsigned int find_client_payload(char* buf, int len, int type) { break; } next: - r = (r+1) % payload_count; - + r = (r+1) % pl.payload_count;
// no matching payloads... - if (cnt++ == payload_count) { - fprintf(stderr, "NO MATCHING PAYLOADS... \n"); + if (cnt++ == pl.payload_count) { + log_warn("no matching payloads"); return 0; } } @@ -602,7 +565,6 @@ unsigned int find_client_payload(char* buf, int len, int type) {
// clean up the buffer... return parse_client_headers(inbuf, buf, len); - }
@@ -1132,10 +1094,9 @@ unsigned int capacityPDF (char* buf, int len) {
-int init_JS_payload_pool(int len, int type, int minCapacity) { - +int init_JS_payload_pool(payloads& pl, int len, int type, int minCapacity) { // stat for usable payload - int minPayloadSize = 0, maxPayloadSize = 0; + int minPayloadSize = 0, maxPayloadSize = 0; int sumPayloadSize = 0; int minPayloadCap = 0, maxPayloadCap = 0; int sumPayloadCap = 0; @@ -1149,40 +1110,35 @@ int init_JS_payload_pool(int len, int type, int minCapacity) { int cap; int mode;
- - - if (payload_count == 0) { + if (pl.payload_count == 0) { log_debug("payload_count == 0; forgot to run load_payloads()?\n"); return 0; } - - if (initTypePayload[contentType] != 0) return 1; // init is done already
- - for (r = 0; r < payload_count; r++) { - p = &payload_hdrs[r]; + for (r = 0; r < pl.payload_count; r++) { + p = &pl.payload_hdrs[r]; if (p->ptype != type || p->length > len) { continue; }
- msgbuf = payloads[r]; + msgbuf = pl.payloads[r];
mode = has_eligible_HTTP_content(msgbuf, p->length, HTTP_CONTENT_JAVASCRIPT); if (mode == CONTENT_JAVASCRIPT) { - + cap = capacityJS3(msgbuf, p->length, mode); - if (cap < JS_DELIMITER_SIZE) + if (cap < JS_DELIMITER_SIZE) continue;
cap = (cap - JS_DELIMITER_SIZE)/2;
if (cap > minCapacity) { - typePayloadCap[contentType][cnt] = cap; // (cap-JS_DELIMITER_SIZE)/2; + pl.typePayloadCap[contentType][cnt] = cap; // (cap-JS_DELIMITER_SIZE)/2; // because we use 2 hex char to encode every data byte, the available // capacity for encoding data is divided by 2 - typePayload[contentType][cnt] = r; + pl.typePayload[contentType][cnt] = r; cnt++; - + // update stat if (cnt == 1) { minPayloadSize = p->length; maxPayloadSize = p->length; @@ -1203,13 +1159,13 @@ int init_JS_payload_pool(int len, int type, int minCapacity) { }
- max_JS_capacity = maxPayloadCap; + pl.max_JS_capacity = maxPayloadCap;
- initTypePayload[contentType] = 1; - typePayloadCount[contentType] = cnt; + pl.initTypePayload[contentType] = 1; + pl.typePayloadCount[contentType] = cnt; log_debug("init_payload_pool: typePayloadCount for contentType %d = %d", - contentType, typePayloadCount[contentType]); + contentType, pl.typePayloadCount[contentType]); log_debug("minPayloadSize = %d", minPayloadSize); log_debug("maxPayloadSize = %d", maxPayloadSize); log_debug("avgPayloadSize = %f", (float)sumPayloadSize/(float)cnt); @@ -1220,7 +1176,7 @@ int init_JS_payload_pool(int len, int type, int minCapacity) { }
-int init_HTML_payload_pool(int len, int type, int minCapacity) { +int init_HTML_payload_pool(payloads& pl, int len, int type, int minCapacity) {
// stat for usable payload int minPayloadSize = 0, maxPayloadSize = 0; @@ -1239,21 +1195,18 @@ int init_HTML_payload_pool(int len, int type, int minCapacity) {
- if (payload_count == 0) { + if (pl.payload_count == 0) { log_debug("payload_count == 0; forgot to run load_payloads()?\n"); return 0; } - - if (initTypePayload[contentType] != 0) return 1; // init is done already
- - for (r = 0; r < payload_count; r++) { - p = &payload_hdrs[r]; + for (r = 0; r < pl.payload_count; r++) { + p = &pl.payload_hdrs[r]; if (p->ptype != type || p->length > len) { continue; }
- msgbuf = payloads[r]; + msgbuf = pl.payloads[r];
mode = has_eligible_HTTP_content(msgbuf, p->length, HTTP_CONTENT_HTML); if (mode == CONTENT_HTML_JAVASCRIPT) { @@ -1265,10 +1218,10 @@ int init_HTML_payload_pool(int len, int type, int minCapacity) { cap = (cap - JS_DELIMITER_SIZE)/2;
if (cap > minCapacity) { - typePayloadCap[contentType][cnt] = cap; // (cap-JS_DELIMITER_SIZE)/2; + pl.typePayloadCap[contentType][cnt] = cap; // (cap-JS_DELIMITER_SIZE)/2; // because we use 2 hex char to encode every data byte, the available // capacity for encoding data is divided by 2 - typePayload[contentType][cnt] = r; + pl.typePayload[contentType][cnt] = r; cnt++; // update stat @@ -1291,13 +1244,12 @@ int init_HTML_payload_pool(int len, int type, int minCapacity) { }
- max_HTML_capacity = maxPayloadCap; + pl.max_HTML_capacity = maxPayloadCap;
- initTypePayload[contentType] = 1; - typePayloadCount[contentType] = cnt; + pl.typePayloadCount[contentType] = cnt; log_debug("init_payload_pool: typePayloadCount for contentType %d = %d", - contentType, typePayloadCount[contentType]); + contentType, pl.typePayloadCount[contentType]); log_debug("minPayloadSize = %d", minPayloadSize); log_debug("maxPayloadSize = %d", maxPayloadSize); log_debug("avgPayloadSize = %f", (float)sumPayloadSize/(float)cnt); @@ -1307,14 +1259,9 @@ int init_HTML_payload_pool(int len, int type, int minCapacity) { return 1; }
- - - - - - - -int init_PDF_payload_pool(int len, int type, int minCapacity) { +int +init_PDF_payload_pool(payloads& pl, int len, int type, int minCapacity) +{
// stat for usable payload int minPayloadSize = 0, maxPayloadSize = 0; @@ -1331,21 +1278,18 @@ int init_PDF_payload_pool(int len, int type, int minCapacity) { unsigned int contentType = HTTP_CONTENT_PDF;
- if (payload_count == 0) { + if (pl.payload_count == 0) { fprintf(stderr, "payload_count == 0; forgot to run load_payloads()?\n"); return 0; } - - if (initTypePayload[contentType] != 0) return 1; // init is done already -
- for (r = 0; r < payload_count; r++) { - p = &payload_hdrs[r]; + for (r = 0; r < pl.payload_count; r++) { + p = &pl.payload_hdrs[r]; if (p->ptype != type || p->length > len) { continue; }
- msgbuf = payloads[r]; + msgbuf = pl.payloads[r];
mode = has_eligible_HTTP_content(msgbuf, p->length, HTTP_CONTENT_PDF); if (mode > 0) { @@ -1356,8 +1300,8 @@ int init_PDF_payload_pool(int len, int type, int minCapacity) { log_debug("got pdf (index %d) with capacity %d", r, cap); if (cap > minCapacity) { log_debug("pdf (index %d) greater than mincapacity %d", cnt, minCapacity); - typePayloadCap[contentType][cnt] = (cap-PDF_DELIMITER_SIZE)/2; - typePayload[contentType][cnt] = r; + pl.typePayloadCap[contentType][cnt] = (cap-PDF_DELIMITER_SIZE)/2; + pl.typePayload[contentType][cnt] = r; cnt++; // update stat @@ -1376,11 +1320,11 @@ int init_PDF_payload_pool(int len, int type, int minCapacity) { } }
- max_PDF_capacity = maxPayloadCap; - initTypePayload[contentType] = 1; - typePayloadCount[contentType] = cnt; + pl.max_PDF_capacity = maxPayloadCap; + pl.initTypePayload[contentType] = 1; + pl.typePayloadCount[contentType] = cnt; log_debug("init_payload_pool: typePayloadCount for contentType %d = %d", - contentType, typePayloadCount[contentType]); + contentType, pl.typePayloadCount[contentType]); log_debug("minPayloadSize = %d", minPayloadSize); log_debug("maxPayloadSize = %d", maxPayloadSize); log_debug("avgPayloadSize = %f", (float)sumPayloadSize/(float)cnt); @@ -1390,12 +1334,9 @@ int init_PDF_payload_pool(int len, int type, int minCapacity) { return 1; }
- - - - -int init_SWF_payload_pool(int len, int type, int /*unused */) { - +int +init_SWF_payload_pool(payloads& pl, int len, int type, int /*unused */) +{ // stat for usable payload int minPayloadSize = 0, maxPayloadSize = 0; int sumPayloadSize = 0; @@ -1408,26 +1349,23 @@ int init_SWF_payload_pool(int len, int type, int /*unused */) { unsigned int contentType = HTTP_CONTENT_SWF;
- if (payload_count == 0) { + if (pl.payload_count == 0) { fprintf(stderr, "payload_count == 0; forgot to run load_payloads()?\n"); return 0; } - - if (initTypePayload[contentType] != 0) return 1; // init is done already -
- for (r = 0; r < payload_count; r++) { - p = &payload_hdrs[r]; + for (r = 0; r < pl.payload_count; r++) { + p = &pl.payload_hdrs[r]; if (p->ptype != type || p->length > len) { continue; }
- msgbuf = payloads[r]; + msgbuf = pl.payloads[r]; // found a payload corr to the specified contentType
mode = has_eligible_HTTP_content(msgbuf, p->length, HTTP_CONTENT_SWF); if (mode > 0) { - typePayload[contentType][cnt] = r; + pl.typePayload[contentType][cnt] = r; cnt++; // update stat if (cnt == 1) { @@ -1444,10 +1382,10 @@ int init_SWF_payload_pool(int len, int type, int /*unused */) { } }
- initTypePayload[contentType] = 1; - typePayloadCount[contentType] = cnt; + pl.initTypePayload[contentType] = 1; + pl.typePayloadCount[contentType] = cnt; log_debug("init_payload_pool: typePayloadCount for contentType %d = %d", - contentType, typePayloadCount[contentType]); + contentType, pl.typePayloadCount[contentType]); log_debug("minPayloadSize = %d", minPayloadSize); log_debug("maxPayloadSize = %d", maxPayloadSize); log_debug("avgPayloadSize = %f", (float)sumPayloadSize/(float)cnt); @@ -1462,27 +1400,29 @@ int init_SWF_payload_pool(int len, int type, int /*unused */) {
-int get_next_payload (int contentType, char** buf, int* size, int* cap) { +int get_next_payload (payloads& pl, int contentType, char** buf, + int* size, int* cap) +{ int r;
log_debug("get_next_payload: contentType = %d, initTypePayload = %d, typePayloadCount = %d", - contentType, initTypePayload[contentType], typePayloadCount[contentType]); + contentType, pl.initTypePayload[contentType], pl.typePayloadCount[contentType]);
if (contentType <= 0 || contentType >= MAX_CONTENT_TYPE || - initTypePayload[contentType] == 0 || - typePayloadCount[contentType] == 0) + pl.initTypePayload[contentType] == 0 || + pl.typePayloadCount[contentType] == 0) return 0;
- r = rand() % typePayloadCount[contentType]; + r = rand() % pl.typePayloadCount[contentType]; // int r = 1; // log_debug("SERVER: *** always choose the same payload ***");
log_debug("SERVER: picked payload with index %d", r); - *buf = payloads[typePayload[contentType][r]]; - *size = payload_hdrs[typePayload[contentType][r]].length; - *cap = typePayloadCap[contentType][r]; + *buf = pl.payloads[pl.typePayload[contentType][r]]; + *size = pl.payload_hdrs[pl.typePayload[contentType][r]].length; + *cap = pl.typePayloadCap[contentType][r]; return 1; }
@@ -1493,20 +1433,20 @@ int get_next_payload (int contentType, char** buf, int* size, int* cap) {
-int get_payload (int contentType, int cap, char** buf, int* size) { +int get_payload (payloads& pl, int contentType, int cap, char** buf, int* size) { int r, i, cnt, found = 0, numCandidate = 0, first, best, current;
log_debug("get_payload: contentType = %d, initTypePayload = %d, typePayloadCount = %d", - contentType, initTypePayload[contentType], typePayloadCount[contentType]); + contentType, pl.initTypePayload[contentType], pl.typePayloadCount[contentType]);
if (contentType <= 0 || contentType >= MAX_CONTENT_TYPE || - initTypePayload[contentType] == 0 || - typePayloadCount[contentType] == 0) + pl.initTypePayload[contentType] == 0 || + pl.typePayloadCount[contentType] == 0) return 0;
- cnt = typePayloadCount[contentType]; + cnt = pl.typePayloadCount[contentType]; r = rand() % cnt; best = r; first = r; @@ -1518,12 +1458,12 @@ int get_payload (int contentType, int cap, char** buf, int* size) { i++; current = (r+i)%cnt;
- if (typePayloadCap[contentType][current] <= cap) + if (pl.typePayloadCap[contentType][current] <= cap) continue;
if (found) { - if (payload_hdrs[typePayload[contentType][best]].length > - payload_hdrs[typePayload[contentType][current]].length) + if (pl.payload_hdrs[pl.typePayload[contentType][best]].length > + pl.payload_hdrs[pl.typePayload[contentType][current]].length) best = current; } else { first = current; @@ -1535,11 +1475,11 @@ int get_payload (int contentType, int cap, char** buf, int* size) {
if (found) { log_debug("first payload size=%d, best payload size=%d, num candidate=%d\n", - payload_hdrs[typePayload[contentType][first]].length, - payload_hdrs[typePayload[contentType][best]].length, + pl.payload_hdrs[pl.typePayload[contentType][first]].length, + pl.payload_hdrs[pl.typePayload[contentType][best]].length, numCandidate); - *buf = payloads[typePayload[contentType][best]]; - *size = payload_hdrs[typePayload[contentType][best]].length; + *buf = pl.payloads[pl.typePayload[contentType][best]]; + *size = pl.payload_hdrs[pl.typePayload[contentType][best]].length; return 1; } else { return 0; diff --git a/src/steg/payloads.h b/src/steg/payloads.h index 34e7edc..05a7307 100644 --- a/src/steg/payloads.h +++ b/src/steg/payloads.h @@ -120,22 +120,36 @@ typedef struct service_state { int dir; }state;
+struct payloads { + int initTypePayload[MAX_CONTENT_TYPE]; + int typePayloadCount[MAX_CONTENT_TYPE]; + int typePayload[MAX_CONTENT_TYPE][MAX_PAYLOADS]; + int typePayloadCap[MAX_CONTENT_TYPE][MAX_PAYLOADS];
-#define HTTP_MSG_BUF_SIZE 100000 + unsigned int max_JS_capacity; + unsigned int max_HTML_capacity; + unsigned int max_PDF_capacity; + + pentry_header payload_hdrs[MAX_PAYLOADS]; + char* payloads[MAX_PAYLOADS]; + int payload_count; +};
-void load_payloads(const char* fname); -unsigned int find_client_payload(char* buf, int len, int type); -unsigned int find_server_payload(char** buf, int len, int type, int contentType);
-int init_JS_payload_pool(int len, int type, int minCapacity); -int init_SWF_payload_pool(int len, int type, int minCapacity); -int init_PDF_payload_pool(int len, int type,int minCapacity); -int init_HTML_payload_pool(int len, int type, int minCapacity); +#define HTTP_MSG_BUF_SIZE 100000 + +void load_payloads(payloads& pl, const char* fname); +unsigned int find_client_payload(payloads& pl, char* buf, int len, int type); +unsigned int find_server_payload(payloads& pl, char** buf, int len, int type, int contentType);
+int init_JS_payload_pool(payloads& pl, int len, int type, int minCapacity); +int init_SWF_payload_pool(payloads& pl, int len, int type, int minCapacity); +int init_PDF_payload_pool(payloads& pl, int len, int type,int minCapacity); +int init_HTML_payload_pool(payloads& pl, int len, int type, int minCapacity);
-int get_next_payload (int contentType, char** buf, int* size, int* cap);
-int get_payload (int contentType, int cap, char** buf, int* size); +int get_next_payload (payloads& pl, int contentType, char** buf, int* size, int* cap); +int get_payload (payloads& pl, int contentType, int cap, char** buf, int* size);
int has_eligible_HTTP_content (char* buf, int len, int type); int fixContentLen (char* payload, int payloadLen, char *buf, int bufLen); diff --git a/src/steg/pdfSteg.cc b/src/steg/pdfSteg.cc index 504c3d4..c67b0f8 100644 --- a/src/steg/pdfSteg.cc +++ b/src/steg/pdfSteg.cc @@ -296,7 +296,10 @@ pdfUnwrap (char *data, unsigned int dlen,
-int http_server_PDF_transmit (steg_t*, struct evbuffer *source, conn_t *conn) { +int +http_server_PDF_transmit (payloads& pl, struct evbuffer *source, + conn_t *conn) +{
struct evbuffer *dest = conn->outbound(); size_t sbuflen = evbuffer_get_length(source); @@ -341,7 +344,7 @@ int http_server_PDF_transmit (steg_t*, struct evbuffer *source, conn_t *conn) {
log_debug("SERVER sbuflen = %d; cnt = %d", (int)sbuflen, cnt);
- mpdf = get_max_PDF_capacity(); + mpdf = pl.max_PDF_capacity;
if (mpdf <= 0) { log_warn("SERVER ERROR: No pdfTemplate found\n"); @@ -354,7 +357,7 @@ int http_server_PDF_transmit (steg_t*, struct evbuffer *source, conn_t *conn) { return -1; }
- if (get_payload(HTTP_CONTENT_PDF, sbuflen, &pdfTemplate, &pdfTemplateSize) == 1) { + if (get_payload(pl, HTTP_CONTENT_PDF, sbuflen, &pdfTemplate, &pdfTemplateSize) == 1) { log_debug("SERVER found the next HTTP response template with size %d", pdfTemplateSize); } else { log_warn("SERVER couldn't find the next HTTP response template"); diff --git a/src/steg/pdfSteg.h b/src/steg/pdfSteg.h index 3c7a4bd..3d494e1 100644 --- a/src/steg/pdfSteg.h +++ b/src/steg/pdfSteg.h @@ -10,7 +10,7 @@ #include "steg.h" #include <event2/buffer.h>
- +struct payloads;
#define PDF_DELIMITER '?' #define PDF_DELIMITER2 '.' @@ -21,7 +21,7 @@ int pdfUnwrap (char *data, unsigned int dlen, char *outbuf, unsigned int outbufs int addDelimiter(char *inbuf, int inbuflen, char *outbuf, int outbuflen, const char delimiter1, const char delimiter2); int removeDelimiter(char *inbuf, int inbuflen, char *outbuf, int outbuflen, const char delimiter1, int* endFlag, int* escape);
-int http_server_PDF_transmit (steg_t* s, struct evbuffer *source, conn_t *conn); +int http_server_PDF_transmit (payloads& pl, struct evbuffer *source, conn_t *conn); int http_handle_client_PDF_receive(steg_t *s, conn_t *conn, struct evbuffer *dest, struct evbuffer* source);
diff --git a/src/steg/swfSteg.cc b/src/steg/swfSteg.cc index d7c35ec..cd371f3 100644 --- a/src/steg/swfSteg.cc +++ b/src/steg/swfSteg.cc @@ -20,7 +20,7 @@ static const char http_response_1[] =
unsigned int -swf_wrap(char* inbuf, int in_len, char* outbuf, int out_sz) { +swf_wrap(payloads& pl, char* inbuf, int in_len, char* outbuf, int out_sz) {
char* swf; int in_swf_len; @@ -38,7 +38,7 @@ swf_wrap(char* inbuf, int in_len, char* outbuf, int out_sz) {
- if (!get_payload(HTTP_CONTENT_SWF, -1, &resp, &resp_len)) { + if (!get_payload(pl, HTTP_CONTENT_SWF, -1, &resp, &resp_len)) { log_warn("swfsteg: no suitable payload found\n"); return -1; } @@ -118,12 +118,9 @@ swf_unwrap(char* inbuf, int in_len, char* outbuf, int out_sz) { return inf_len - SWF_SAVE_HEADER_LEN - SWF_SAVE_FOOTER_LEN; }
- - - - -int -http_server_SWF_transmit (steg_t*, struct evbuffer *source, conn_t *conn) { +int +http_server_SWF_transmit(payloads& pl, struct evbuffer *source, conn_t *conn) +{
struct evbuffer *dest = conn->outbound(); size_t sbuflen = evbuffer_get_length(source); @@ -141,7 +138,7 @@ http_server_SWF_transmit (steg_t*, struct evbuffer *source, conn_t *conn) { outbuf = (char *)xmalloc(4*sbuflen + SWF_SAVE_FOOTER_LEN + SWF_SAVE_HEADER_LEN + 512);
// fprintf(stderr, "server wrapping swf len %d\n", (int) sbuflen); - outlen = swf_wrap(inbuf, sbuflen, outbuf, 4*sbuflen + SWF_SAVE_FOOTER_LEN + SWF_SAVE_HEADER_LEN + 512); + outlen = swf_wrap(pl, inbuf, sbuflen, outbuf, 4*sbuflen + SWF_SAVE_FOOTER_LEN + SWF_SAVE_HEADER_LEN + 512);
if (outlen < 0) { log_warn("swf_wrap failed\n"); diff --git a/src/steg/swfSteg.h b/src/steg/swfSteg.h index 7be5207..712a3a3 100644 --- a/src/steg/swfSteg.h +++ b/src/steg/swfSteg.h @@ -14,24 +14,20 @@ #include <event2/buffer.h> #include <stdio.h>
- - - - - +struct payloads;
#define SWF_SAVE_HEADER_LEN 1500 #define SWF_SAVE_FOOTER_LEN 1500
unsigned int -swf_wrap(char* inbuf, int in_len, char* outbuf, int out_sz); +swf_wrap(payloads& pl, char* inbuf, int in_len, char* outbuf, int out_sz);
unsigned int swf_unwrap(char* inbuf, int in_len, char* outbuf, int out_sz);
int -http_server_SWF_transmit (steg_t* s, struct evbuffer *source, conn_t *conn); +http_server_SWF_transmit(payloads& pl, struct evbuffer *source, conn_t *conn);
int
tor-commits@lists.torproject.org