commit 4ca01f911b4f003f81b0b1cd201761ef2efc31eb Author: Zack Weinberg zackw@cmu.edu Date: Mon Apr 30 16:21:57 2012 -0700
Header file hygiene in steg/. --- src/steg/b64cookies.cc | 7 +- src/steg/cookies.cc | 5 +- src/steg/cookies.h | 18 ++-- src/steg/http.cc | 17 ++-- src/steg/jsSteg.cc | 265 +++++++++++++++++++++++++----------------------- src/steg/jsSteg.h | 68 +++++-------- src/steg/payloads.cc | 3 + src/steg/payloads.h | 48 +++------ src/steg/swfSteg.h | 3 +- 9 files changed, 205 insertions(+), 229 deletions(-)
diff --git a/src/steg/b64cookies.cc b/src/steg/b64cookies.cc index b75bedc..2efa76a 100644 --- a/src/steg/b64cookies.cc +++ b/src/steg/b64cookies.cc @@ -2,12 +2,9 @@ * See LICENSE for other credits and copying information */
+#include "util.h" #include "b64cookies.h"
-#include <stdio.h> -#include <stdlib.h> -#include <string.h> - int unwrap_b64_cookie(char* inbuf, char* outbuf, int buflen) { int i,j; j = 0; @@ -71,7 +68,7 @@ int gen_b64_cookie_field(char* outbuf, char* data, int datalen) { int cnt = gen_one_b64cookie(outbuf, onecookielen, data + consumed, datalen - consumed);
if (cnt < 0) { - fprintf(stderr, "error: couldn't create cookie %d\n", cnt); + log_warn("couldn't create cookie: %d\n", cnt); return cnt; }
diff --git a/src/steg/cookies.cc b/src/steg/cookies.cc index 885c8cb..a945397 100644 --- a/src/steg/cookies.cc +++ b/src/steg/cookies.cc @@ -2,6 +2,7 @@ * See LICENSE for other credits and copying information */
+#include "util.h" #include "cookies.h"
int unwrap_cookie(unsigned char* inbuf, unsigned char* outbuf, int buflen) { @@ -118,7 +119,7 @@ int gen_cookie_field(unsigned char* outbuf, int total_cookie_len, unsigned char*
if (total_cookie_len < 4) { - fprintf(stderr, "error: cookie length too small\n"); + log_warn("cookie length too small (have %d, need 4)", total_cookie_len); return -1; }
@@ -128,7 +129,7 @@ int gen_cookie_field(unsigned char* outbuf, int total_cookie_len, unsigned char* int cnt = gen_one_cookie(outbuf, cookielen, data + consumed, datalen - consumed);
if (cnt < 0) { - fprintf(stderr, "error: couldn't create cookie %d\n", cnt); + log_warn("couldn't create cookie: %d", cnt); return cnt; }
diff --git a/src/steg/cookies.h b/src/steg/cookies.h index a7ed856..3955a54 100644 --- a/src/steg/cookies.h +++ b/src/steg/cookies.h @@ -5,16 +5,14 @@ #ifndef _COOKIES_H #define _COOKIES_H
-#include <stdio.h> -#include <strings.h> -#include <stdlib.h> -#include <string.h> - int unwrap_cookie(unsigned char* inbuf, unsigned char* outbuf, int buflen); -int gen_cookie_field(unsigned char* outbuf, int total_cookie_len, unsigned char* data, int datalen); -int gen_one_cookie(unsigned char* outbuf, int cookielen, unsigned char* data, int datalen); -int gen_one_cookie2(unsigned char* outbuf, int cookielen, unsigned char* data, int datalen); -int gen_cookie_field2(unsigned char* outbuf, int total_cookie_len, unsigned char* data, int datalen); - +int gen_cookie_field(unsigned char* outbuf, int total_cookie_len, + unsigned char* data, int datalen); +int gen_one_cookie(unsigned char* outbuf, int cookielen, + unsigned char* data, int datalen); +int gen_one_cookie2(unsigned char* outbuf, int cookielen, + unsigned char* data, int datalen); +int gen_cookie_field2(unsigned char* outbuf, int total_cookie_len, + unsigned char* data, int datalen);
#endif diff --git a/src/steg/http.cc b/src/steg/http.cc index 7267e6f..c413cdc 100644 --- a/src/steg/http.cc +++ b/src/steg/http.cc @@ -17,7 +17,6 @@ #include "b64cookies.h"
#include <event2/buffer.h> -#include <stdio.h>
#define MIN_COOKIE_SIZE 24 #define MAX_COOKIE_SIZE 1024 @@ -219,6 +218,7 @@ lookup_peer_name_from_ip(const char* p_ip, char* p_name) { struct addrinfo* ailist; struct addrinfo* aip; struct addrinfo hint; + int res; char buf[128];
hint.ai_flags = AI_CANONNAME; @@ -234,15 +234,16 @@ lookup_peer_name_from_ip(const char* p_ip, char* p_name) { buf[strchr(buf, ':') - buf] = 0;
- if (getaddrinfo(buf, NULL, &hint, &ailist)) { - fprintf(stderr, "error: getaddrinfo() %s\n", p_ip); - exit(1); + if ((res = getaddrinfo(buf, NULL, &hint, &ailist))) { + log_warn("getaddrinfo(%s) failed: %s", p_ip, gai_strerror(res)); + return 0; }
for (aip = ailist; aip != NULL; aip = aip->ai_next) { char buf[512]; - if (getnameinfo(aip->ai_addr, sizeof(struct sockaddr), buf, 512, NULL, 0, 0) == 0) { - sprintf(p_name, "%s", buf); + if (getnameinfo(aip->ai_addr, sizeof(struct sockaddr), + buf, 512, NULL, 0, 0) == 0) { + strcpy(p_name, buf); return 1; } } @@ -437,7 +438,7 @@ int gen_uri_field(char* uri, unsigned int uri_sz, char* data, int datalen) {
if (so_far > uri_sz - 6) { - fprintf(stderr, "too small\n"); + log_warn("too small\n"); return 0; } } @@ -529,8 +530,6 @@ http_client_uri_transmit (http_steg_t *s, }
- // fprintf(stderr, "outbuf = %s\n", outbuf); - if (evbuffer_add(dest, outbuf, datalen) || // add uri field evbuffer_add(dest, "HTTP/1.1\r\nHost: ", 19) || evbuffer_add(dest, s->peer_dnsname, strlen(s->peer_dnsname)) || diff --git a/src/steg/jsSteg.cc b/src/steg/jsSteg.cc index 1d71918..6ff0b89 100644 --- a/src/steg/jsSteg.cc +++ b/src/steg/jsSteg.cc @@ -2,10 +2,23 @@ * See LICENSE for other credits and copying information */
+#include "util.h" #include "payloads.h" #include "jsSteg.h" #include "cookies.h" #include "compression.h" +#include "connections.h" + +#include <ctype.h> + +#include <event2/buffer.h> + +// error codes +#define INVALID_BUF_SIZE -1 +#define INVALID_DATA_CHAR -2 + +// controlling content gzipping for jsSteg +#define JS_GZIP_RESP 1
void buf_dump(unsigned char* buf, int len, FILE *out);
@@ -43,7 +56,7 @@ int isxString(char *str) { * this function returns 1; otherwise, it returns 0 * * Assumptions: - * msg is null terminated + * msg is null terminated * */ int isGzipContent (char *msg) { @@ -63,7 +76,7 @@ int isGzipContent (char *msg) { gzipFlag = 1; break; } - + if (!strncmp(end, "\r\n\r\n", 4)){ break; } @@ -86,7 +99,7 @@ int isGzipContent (char *msg) { * otherwise: return 0 * * Assumptions: - * msg is null terminated + * msg is null terminated * */ int findContentType (char *msg) { @@ -102,16 +115,16 @@ int findContentType (char *msg) { }
if (!strncmp(ptr, "Content-Type:", 13)) { - - if (!strncmp(ptr+14, "text/javascript", 15) || - !strncmp(ptr+14, "application/javascript", 22) || + + if (!strncmp(ptr+14, "text/javascript", 15) || + !strncmp(ptr+14, "application/javascript", 22) || !strncmp(ptr+14, "application/x-javascript", 24)) { return HTTP_CONTENT_JAVASCRIPT; } if (!strncmp(ptr+14, "text/html", 9)) { return HTTP_CONTENT_HTML; } - if (!strncmp(ptr+14, "application/pdf", 15) || + if (!strncmp(ptr+14, "application/pdf", 15) || !strncmp(ptr+14, "application/x-pdf", 17)) { return HTTP_CONTENT_PDF; } @@ -119,7 +132,7 @@ int findContentType (char *msg) { return HTTP_CONTENT_SWF; } } - + if (!strncmp(end, "\r\n\r\n", 4)){ break; } @@ -141,7 +154,7 @@ int findContentType (char *msg) { * function returns the number of characters in data successfully * embedded in jData, or returns one of the error codes * - * approach: + * approach: * replaces characters in jTemplate that are hexadecimal (i.e., {0-9,a-f,A-F}) * with those in data, and leave the non-hex char in place * @@ -170,14 +183,14 @@ int findContentType (char *msg) { * */ int encode(char *data, char *jTemplate, char *jData, - unsigned int dlen, unsigned int jtlen, unsigned int jdlen ) + unsigned int dlen, unsigned int jtlen, unsigned int jdlen ) { unsigned int encCnt = 0; /* num of data encoded in jData */ char *dp, *jtp, *jdp; /* current pointers for data, jTemplate, and jData */ - + unsigned int j;
- /* + /* * insanity checks */ if (jdlen < jtlen) { return INVALID_BUF_SIZE; } @@ -196,9 +209,9 @@ int encode(char *data, char *jTemplate, char *jData, *jdp = *dp; dp++; encCnt++; - if (encCnt == dlen) { - jtp++; jdp++; - break; + if (encCnt == dlen) { + jtp++; jdp++; + break; } } else { *jdp = *jtp; @@ -229,12 +242,12 @@ int encode(char *data, char *jTemplate, char *jData, * the JS to JS_DELIMITER_REPLACEMENT, before all the data is encoded. * * Output: - * fin - signal the caller whether all data has been encoded and + * fin - signal the caller whether all data has been encoded and * a JS_DELIMITER has been added */ int encode2(char *data, char *jTemplate, char *jData, - unsigned int dlen, unsigned int jtlen, - unsigned int jdlen, int *fin) + unsigned int dlen, unsigned int jtlen, + unsigned int jdlen, int *fin) { unsigned int encCnt = 0; /* num of data encoded in jData */ char *dp, *jtp, *jdp; /* current pointers for data, jTemplate, and jData */ @@ -329,8 +342,8 @@ int encode2(char *data, char *jTemplate, char *jData,
int encodeHTTPBody(char *data, char *jTemplate, char *jData, - unsigned int dlen, unsigned int jtlen, - unsigned int jdlen, int mode) + unsigned int dlen, unsigned int jtlen, + unsigned int jdlen, int mode) { char *dp, *jtp, *jdp; // current pointers for data, jTemplate, and jData unsigned int encCnt = 0; // num of data encoded in jData @@ -340,8 +353,8 @@ int encodeHTTPBody(char *data, char *jTemplate, char *jData, int scriptLen; int fin; unsigned int dlen2 = dlen; - dp = data; - jtp = jTemplate; + dp = data; + jtp = jTemplate; jdp = jData;
@@ -357,16 +370,16 @@ int encodeHTTPBody(char *data, char *jTemplate, char *jData, #endif return encCnt;
- } + }
else if (mode == CONTENT_HTML_JAVASCRIPT) { while (encCnt < dlen2) { jsStart = strstr(jtp, startScriptTypeJS); - if (jsStart == NULL) { + if (jsStart == NULL) { #ifdef DEBUG - printf("lack of usable JS; can't find startScriptType\n"); + printf("lack of usable JS; can't find startScriptType\n"); #endif - return encCnt; + return encCnt; } skip = strlen(startScriptTypeJS)+jsStart-jtp; #ifdef DEBUG2 @@ -375,11 +388,11 @@ int encodeHTTPBody(char *data, char *jTemplate, char *jData, memcpy(jdp, jtp, skip); jtp = jtp+skip; jdp = jdp+skip; jsEnd = strstr(jtp, endScriptTypeJS); - if (jsEnd == NULL) { + if (jsEnd == NULL) { #ifdef DEBUG - printf("lack of usable JS; can't find endScriptType\n"); + printf("lack of usable JS; can't find endScriptType\n"); #endif - return encCnt; + return encCnt; }
// the JS for encoding data is between jsStart and jsEnd @@ -388,7 +401,7 @@ int encodeHTTPBody(char *data, char *jTemplate, char *jData, n = encode2(dp, jtp, jdp, dlen, scriptLen, jdlen, &fin); // update encCnt, dp, and dlen based on n if (n > 0) { - encCnt = encCnt+n; dp = dp+n; dlen = dlen-n; + encCnt = encCnt+n; dp = dp+n; dlen = dlen-n; } // update jtp, jdp, jdlen skip = jsEnd-jtp; @@ -405,9 +418,9 @@ int encodeHTTPBody(char *data, char *jTemplate, char *jData, // added by encode() if (fin == 0 && dlen == 0) { if (skip > 0) { - *jtp = JS_DELIMITER; - jtp = jtp+1; jdp = jdp+1; - skip--; + *jtp = JS_DELIMITER; + jtp = jtp+1; jdp = jdp+1; + skip--; } } memcpy(jdp, jtp, skip); @@ -450,13 +463,13 @@ int encodeHTTPBody(char *data, char *jTemplate, char *jData, * jData = "01p_or2=M3th.r4n5om()*6789ABCDEF0000000; dfp_tile = 1;" * jdlen = 54 * dlen = 16 - * dataBufSize = 1000 + * dataBufSize = 1000 * decode() returns 16 * dataBuf= "0123456789ABCDEF" * */ int decode (char *jData, char *dataBuf, unsigned int jdlen, - unsigned int dlen, unsigned int dataBufSize ) + unsigned int dlen, unsigned int dataBufSize ) { unsigned int decCnt = 0; /* num of data decoded */ char *dp, *jdp; /* current pointers for dataBuf and jData */ @@ -468,10 +481,10 @@ int decode (char *jData, char *dataBuf, unsigned int jdlen, for (j=0; j<jdlen; j++) { if ( isxdigit(*jdp) ) { if (decCnt < dlen) { - decCnt++; - *dp++ = *jdp++; + decCnt++; + *dp++ = *jdp++; } else { - break; + break; } } else { jdp++; @@ -487,16 +500,16 @@ int decode (char *jData, char *dataBuf, unsigned int jdlen, * stops when JS_DELIMITER is encountered. */ int decode2 (char *jData, char *dataBuf, unsigned int jdlen, - unsigned int dataBufSize, int *fin ) + unsigned int dataBufSize, int *fin ) { unsigned int decCnt = 0; /* num of data decoded */ char *dp, *jdp; /* current pointers for dataBuf and jData */ int i,j; int cjdlen = jdlen; - + *fin = 0; dp = dataBuf; jdp = jData; - + i = offset2Hex(jdp, cjdlen, 0); while (i != -1) { // return if JS_DELIMITER exists between jdp and jdp+i @@ -515,9 +528,9 @@ int decode2 (char *jData, char *dataBuf, unsigned int jdlen, jdp = jdp+1; cjdlen--; dp = dp+1; dataBufSize--; decCnt++; - + // find the next hex char - i = offset2Hex(jdp, cjdlen, 1); + i = offset2Hex(jdp, cjdlen, 1); }
// look for JS_DELIMITER between jdp to jData+jdlen @@ -526,7 +539,7 @@ int decode2 (char *jData, char *dataBuf, unsigned int jdlen, *fin = 1; break; } - jdp = jdp+1; + jdp = jdp+1; }
return decCnt; @@ -534,7 +547,7 @@ int decode2 (char *jData, char *dataBuf, unsigned int jdlen,
int decodeHTTPBody (char *jData, char *dataBuf, unsigned int jdlen, - unsigned int dataBufSize, int *fin, int mode ) + unsigned int dataBufSize, int *fin, int mode ) { char *jsStart, *jsEnd; char *dp, *jdp; // current pointers for data and jData @@ -542,31 +555,31 @@ int decodeHTTPBody (char *jData, char *dataBuf, unsigned int jdlen, int decCnt = 0; int n; int dlen = dataBufSize; - dp = dataBuf; jdp = jData; + dp = dataBuf; jdp = jData;
if (mode == CONTENT_JAVASCRIPT) { decCnt = decode2(jData, dataBuf, jdlen, dataBufSize, fin); if (*fin == 0) { log_warn("Unable to find JS_DELIMITER"); } - } + } else if (mode == CONTENT_HTML_JAVASCRIPT) { *fin = 0; while (*fin == 0) { jsStart = strstr(jdp, startScriptTypeJS); if (jsStart == NULL) { #ifdef DEBUG - printf("Can't find startScriptType for decoding data inside script type JS\n"); + printf("Can't find startScriptType for decoding data inside script type JS\n"); #endif - return decCnt; + return decCnt; } jdp = jsStart+strlen(startScriptTypeJS); jsEnd = strstr(jdp, endScriptTypeJS); - if (jsEnd == NULL) { + if (jsEnd == NULL) { #ifdef DEBUG - printf("Can't find endScriptType for decoding data inside script type JS\n"); + printf("Can't find endScriptType for decoding data inside script type JS\n"); #endif - return decCnt; + return decCnt; }
// the JS for decoding data is between jsStart and jsEnd @@ -592,18 +605,18 @@ int decodeHTTPBody (char *jData, char *dataBuf, unsigned int jdlen, void printerr(int errno) { if (errno == INVALID_BUF_SIZE) { printf ("Error: Output buffer too small\n"); - } + } else if (errno == INVALID_DATA_CHAR) { printf ("Error: Non-hex char in data\n"); - } + } else { printf ("Unknown error: %i\n", errno); } }
-int testEncode(char *data, char *js, char *outBuf, unsigned int dlen, unsigned int jslen, - unsigned int outBufLen, int testNum) { +int testEncode(char *data, char *js, char *outBuf, unsigned int dlen, unsigned int jslen, + unsigned int outBufLen, int testNum) { int r;
printf ("***** Start of testEncode (%i) *****\n", testNum); @@ -625,8 +638,8 @@ int testEncode(char *data, char *js, char *outBuf, unsigned int dlen, unsigned i return r; }
-int testDecode(char *inBuf, char *outBuf, unsigned int inBufSize, unsigned int dlen, - unsigned int outBufSize, int testNum) { +int testDecode(char *inBuf, char *outBuf, unsigned int inBufSize, unsigned int dlen, + unsigned int outBufSize, int testNum) {
int r;
@@ -650,9 +663,9 @@ int testDecode(char *inBuf, char *outBuf, unsigned int inBufSize, unsigned int d }
-int testEncode2(char *data, char *js, char *outBuf, - unsigned int dlen, unsigned int jslen, unsigned int outBufLen, - int mode, int testNum) { +int testEncode2(char *data, char *js, char *outBuf, + unsigned int dlen, unsigned int jslen, unsigned int outBufLen, + int mode, int testNum) { int r; // int fin;
@@ -667,14 +680,14 @@ int testEncode2(char *data, char *js, char *outBuf,
if (r < 0) { printerr(r); - } + } else { printf ("\nOutput:\n"); printf ("%i char of data embedded in outBuf\n", r); // printf ("fin = %d\n", fin); outBuf[jslen] = '\0'; printf ("outBuf = %s\n", outBuf); - + if ((unsigned int) r < dlen) { printf ("Incomplete data encoding\n"); } @@ -686,9 +699,9 @@ int testEncode2(char *data, char *js, char *outBuf,
-int testDecode2(char *inBuf, char *outBuf, - unsigned int inBufSize, unsigned int outBufSize, - int mode, int testNum) { +int testDecode2(char *inBuf, char *outBuf, + unsigned int inBufSize, unsigned int outBufSize, + int mode, int testNum) { int r; int fin;
@@ -764,7 +777,7 @@ http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn, (int) sbuflen, (int) mjs); return -1; } - + // Convert data in 'source' to hexadecimal and write it to data cnt = 0; for (i = 0; i < nv; i++) { @@ -826,8 +839,8 @@ http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn, // work in progress if (gzipMode == 1) { // conservative estimate: - // sizeof outbuf2 = cLen + 10-byte for gzip header + 8-byte for crc - outbuf2 = (char *)xmalloc(cLen+18); + // sizeof outbuf2 = cLen + 10-byte for gzip header + 8-byte for crc + outbuf2 = (char *)xmalloc(cLen+18);
outbuf2len = compress((const uint8_t *)outbuf, cLen, (uint8_t *)outbuf2, cLen+18, c_format_gzip); @@ -844,7 +857,7 @@ http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn, outbuf2len = cLen; }
- // outbuf2 points to the HTTP payload (of length outbuf2len) to be sent + // outbuf2 points to the HTTP payload (of length outbuf2len) to be sent
if (mode == CONTENT_JAVASCRIPT) { // JavaScript in HTTP body newHdrLen = gen_response_header((char*) "application/x-javascript", gzipMode, @@ -863,7 +876,7 @@ http_server_JS_transmit (payloads& pl, struct evbuffer *source, conn_t *conn, return -1; }
- // newHdr points to the HTTP header (of length newHdrLen) to be sent + // newHdr points to the HTTP header (of length newHdrLen) to be sent
if (evbuffer_add(dest, newHdr, newHdrLen)) { log_warn("SERVER ERROR: evbuffer_add() fails for newHdr"); @@ -903,39 +916,39 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str
unsigned char *field, *fieldStart, *fieldEnd, *fieldValStart; char *httpBody; - + int decCnt, fin, i, j, k, gzipMode=0, httpBodyLen, buf2len, contentType = 0; ev_ssize_t r; struct evbuffer * scratch; char c; - - + + s2 = evbuffer_search(source, "\r\n\r\n", sizeof ("\r\n\r\n") -1 , NULL); if (s2.pos == -1) { log_debug("CLIENT Did not find end of HTTP header %d", (int) evbuffer_get_length(source)); // evbuffer_dump(source, stderr); return RECV_INCOMPLETE; } - + log_debug("CLIENT received response header with len %d", (int)s2.pos); - + response_len = 0; - hdrLen = s2.pos + strlen("\r\n\r\n"); + hdrLen = s2.pos + strlen("\r\n\r\n"); response_len += hdrLen; - + // get content length, e.g., Content-Length: 22417 field = evbuffer_pullup(source, s2.pos); if (field == NULL) { log_debug("CLIENT unable to pullup the complete HTTP header"); return RECV_BAD; } - + fieldStart = (unsigned char*) strstr((char*) field, "Content-Length: "); if (fieldStart == NULL) { log_debug("CLIENT unable to find Content-Length in the header"); return RECV_BAD; } - + fieldEnd = (unsigned char*) strstr((char *)fieldStart, "\r\n"); if (fieldEnd == NULL) { log_debug("CLIENT unable to find end of line for Content-Length"); @@ -949,15 +962,15 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str } memcpy(buf, fieldValStart, fieldEnd-fieldValStart); buf[fieldEnd-fieldValStart] = 0; - + content_len = atoi(buf); log_debug("CLIENT received Content-Length = %d\n", content_len); - + response_len += content_len;
if (response_len > (int) evbuffer_get_length(source)) return RECV_INCOMPLETE; - + // read the entire HTTP resp if (response_len < HTTP_MSG_BUF_SIZE) { r = evbuffer_copyout(source, respMsg, response_len); @@ -975,12 +988,12 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str log_debug("CLIENT: HTTP response too large to handle"); return RECV_BAD; } - + log_debug("CLIENT received HTTP response with length %d\n", response_len); // buf_dump((unsigned char*)respMsg, response_len, stderr); // log_debug("HTTP response header:"); // buf_dump((unsigned char*)respMsg, hdrLen+80, stderr); - + contentType = findContentType (respMsg); if (contentType != HTTP_CONTENT_JAVASCRIPT && contentType != HTTP_CONTENT_HTML) { log_warn("ERROR: Invalid content type (%d)", contentType); @@ -997,16 +1010,14 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str (uint8_t *)buf2, HTTP_MSG_BUF_SIZE); if (buf2len <= 0) { log_warn("gzInflate for httpBody fails"); - fprintf(stderr, "gzInflate for httpBody fails"); - exit(-1); return RECV_BAD; } buf2[buf2len] = 0; httpBody = buf2; httpBodyLen = buf2len; } - - if (contentType == HTTP_CONTENT_JAVASCRIPT) { + + if (contentType == HTTP_CONTENT_JAVASCRIPT) { decCnt = decodeHTTPBody(httpBody, data, httpBodyLen, HTTP_MSG_BUF_SIZE, &fin, CONTENT_JAVASCRIPT); } else { @@ -1014,59 +1025,56 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str &fin, CONTENT_HTML_JAVASCRIPT); } data[decCnt] = 0; - + log_debug("After decodeHTTPBody; decCnt: %d\n", decCnt);
// decCnt is an odd number or data is not a hex string if (decCnt % 2) { - fprintf(stderr, "CLIENT ERROR: An odd number of hex characters received\n"); - // buf_dump((unsigned char*)data, decCnt, stderr); + log_debug("CLIENT ERROR: An odd number of hex characters received\n"); return RECV_BAD; } - + if (! isxString(data)) { - log_warn("CLIENT ERROR: Data received not hex"); - // buf_dump((unsigned char*)data, decCnt, stderr); + log_debug("CLIENT ERROR: Data received not hex"); return RECV_BAD; } - + // log_debug("Hex data received:"); // buf_dump ((unsigned char*)data, decCnt, stderr); - + // get a scratch buffer scratch = evbuffer_new(); if (!scratch) return RECV_BAD; - + if (evbuffer_expand(scratch, decCnt/2)) { log_warn("CLIENT ERROR: Evbuffer expand failed \n"); evbuffer_free(scratch); return RECV_BAD; } - + // convert hex data back to binary for (i=0, j=0; i< decCnt; i=i+2, ++j) { sscanf(&data[i], "%2x", (unsigned int*) &k); c = (char)k; evbuffer_add(scratch, &c, 1); } - + // log_debug("CLIENT Done converting hex data to binary:\n"); // evbuffer_dump(scratch, stderr); - - - // fprintf(stderr, "CLIENT RECEIVED payload of size %d\n", (int) evbuffer_get_length(scratch)); + + // add the scratch buffer (which contains the data) to dest - + if (evbuffer_add_buffer(dest, scratch)) { evbuffer_free(scratch); log_warn("CLIENT ERROR: Failed to transfer buffer"); return RECV_BAD; } log_debug("Added scratch (buffer) to dest\n"); - + evbuffer_free(scratch); - - + + if (response_len <= (int) evbuffer_get_length(source)) { if (evbuffer_drain(source, response_len) == -1) { log_warn("CLIENT ERROR: Failed to drain source"); @@ -1077,10 +1085,10 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str log_warn("response_len > buffer size... can't drain"); exit(-1); } - - + + log_debug("Drained source for %d char\n", response_len); - + // downcast_steg(s)->have_received = 1; conn->expect_close(); return RECV_GOOD; @@ -1090,25 +1098,25 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str /***** int main() { - int jDataSize = 1000; + int jDataSize = 1000; char jData[jDataSize]; - int outDataBufSize = 1000; + int outDataBufSize = 1000; char outDataBuf[outDataBufSize];
int r; // test case 1: data embedded in javascript r = testEncode2(data1, js1, jData, strlen(data1), strlen(js1), jDataSize, - CONTENT_JAVASCRIPT, 1); + CONTENT_JAVASCRIPT, 1); if (r > 0) { testDecode2(jData, outDataBuf, strlen(js1), outDataBufSize, CONTENT_JAVASCRIPT, 1); } - + // test case 4: data embedded in one script type javascript r = testEncode2(data1, js4, jData, strlen(data1), strlen(js4), jDataSize, - CONTENT_HTML_JAVASCRIPT, 4); + CONTENT_HTML_JAVASCRIPT, 4); if (r > 0) { testDecode2(jData, outDataBuf, strlen(js4), outDataBufSize, CONTENT_HTML_JAVASCRIPT, 4); }
// test case 5: data embedded in one script type javascript r = testEncode2(data1, js5, jData, strlen(data1), strlen(js5), jDataSize, - CONTENT_HTML_JAVASCRIPT, 5); + CONTENT_HTML_JAVASCRIPT, 5); if (r > 0) { testDecode2(jData, outDataBuf, strlen(js5), outDataBufSize, CONTENT_HTML_JAVASCRIPT, 5); }
@@ -1119,43 +1127,42 @@ http_handle_client_JS_receive(steg_t *, conn_t *conn, struct evbuffer *dest, str /***** int main() { - int jDataSize = 1000; + int jDataSize = 1000; char jData[jDataSize]; - int jDataSmallSize = 5; + int jDataSmallSize = 5; char jDataSmall[jDataSmallSize];
- int outDataBufSize = 1000; + int outDataBufSize = 1000; char outDataBuf[outDataBufSize]; - int outDataSmallSize = 5; + int outDataSmallSize = 5; char outDataSmall[outDataSmallSize];
int r;
// test case 1: data embedded in javascript - r = testEncode(data1, js1, jData, strlen(data1), strlen(js1), jDataSize, 1); + r = testEncode(data1, js1, jData, strlen(data1), strlen(js1), jDataSize, 1); if (r > 0) { testDecode(jData, outDataBuf, strlen(js1), r, outDataBufSize, 1); }
// test case 2: data embedded in javascript - r = testEncode(data1, js2, jData, strlen(data1), strlen(js2), jDataSize, 2); + r = testEncode(data1, js2, jData, strlen(data1), strlen(js2), jDataSize, 2); if (r > 0) { testDecode(jData, outDataBuf, strlen(js2), r, outDataBufSize, 2); }
// test case 3: data partially embedded in javascript; num of hex char in js < data len - r = testEncode(data1, js3, jData, strlen(data1), strlen(js3), jDataSize, 3); + r = testEncode(data1, js3, jData, strlen(data1), strlen(js3), jDataSize, 3); if (r > 0) { testDecode(jData, outDataBuf, strlen(js3), r, outDataBufSize, 3); }
// test case 4: data embedded in javascript; larger data - r = testEncode(data2, js1, jData, strlen(data2), strlen(js1), jDataSize, 4); + r = testEncode(data2, js1, jData, strlen(data2), strlen(js1), jDataSize, 4); if (r > 0) { testDecode(jData, outDataBuf, strlen(js1), r, outDataBufSize, 4); }
// test case 5 (for encode): err for non-hex data - testEncode(nonhexstr, js1, jData, strlen(nonhexstr), strlen(js1), jDataSize, 5); - + testEncode(nonhexstr, js1, jData, strlen(nonhexstr), strlen(js1), jDataSize, 5); + // test case 6 (for encode): err for small output buf - testEncode(data1, js1, jDataSmall, strlen(data1), strlen(js1), jDataSmallSize, 6); + testEncode(data1, js1, jDataSmall, strlen(data1), strlen(js1), jDataSmallSize, 6);
// test case 7 (for decode): err for small output buf - r = testEncode(data1, js1, jData, strlen(data1), strlen(js1), jDataSize, 7); + r = testEncode(data1, js1, jData, strlen(data1), strlen(js1), jDataSize, 7); if (r > 0) { testDecode(jData, outDataSmall, strlen(js1), r, outDataSmallSize, 7); } } *****/ - diff --git a/src/steg/jsSteg.h b/src/steg/jsSteg.h index 40ec611..e8232d5 100644 --- a/src/steg/jsSteg.h +++ b/src/steg/jsSteg.h @@ -5,74 +5,58 @@ #ifndef _JSSTEG_H #define _JSSTEG_H
- -#include <stdio.h> -#include <string.h> -#include <ctype.h> -#include "util.h" -#include "connections.h" -#include "steg.h" -#include <event2/buffer.h> - -// error codes -#define INVALID_BUF_SIZE -1 -#define INVALID_DATA_CHAR -2 - -// 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); +int encodeHTTPBody(char *data, char *jTemplate, char *jData, unsigned int dlen, + unsigned int jtlen, unsigned int jdlen, int mode);
int isxString(char *str);
-int isGzipContent (char *msg); +int isGzipContent (char *msg);
int findContentType (char *msg);
int decodeHTTPBody (char *jData, char *dataBuf, unsigned int jdlen, - unsigned int dataBufSize, int *fin, int mode); + unsigned int dataBufSize, int *fin, int mode);
int encode(char *data, char *jTemplate, char *jData, - unsigned int dlen, unsigned int jtlen, unsigned int jdlen ); + unsigned int dlen, unsigned int jtlen, unsigned int jdlen );
int encode2(char *data, char *jTemplate, char *jData, - unsigned int dlen, unsigned int jtlen, - unsigned int jdlen, int *fin); + unsigned int dlen, unsigned int jtlen, + unsigned int jdlen, int *fin);
int decode (char *jData, char *dataBuf, unsigned int jdlen, - unsigned int dlen, unsigned int dataBufSize ); + unsigned int dlen, unsigned int dataBufSize );
int decode2 (char *jData, char *dataBuf, unsigned int jdlen, - unsigned int dataBufSize, int *fin ); + unsigned int dataBufSize, int *fin );
void printerr(int errno);
-int testEncode(char *data, char *js, char *outBuf, unsigned int dlen, unsigned int jslen, - unsigned int outBufLen, int testNum); - -int testDecode(char *inBuf, char *outBuf, unsigned int inBufSize, unsigned int dlen, - unsigned int outBufSize, int testNum); +int testEncode(char *data, char *js, char *outBuf, + unsigned int dlen, unsigned int jslen, + unsigned int outBufLen, int testNum);
-int testEncode2(char *data, char *js, char *outBuf, - unsigned int dlen, unsigned int jslen, unsigned int outBufLen, - int mode, int testNum); +int testDecode(char *inBuf, char *outBuf, unsigned int inBufSize, + unsigned int dlen, + unsigned int outBufSize, int testNum);
-int testDecode2(char *inBuf, char *outBuf, - unsigned int inBufSize, unsigned int outBufSize, - int mode, int testNum); +int testEncode2(char *data, char *js, char *outBuf, + unsigned int dlen, unsigned int jslen, unsigned int outBufLen, + int mode, int testNum);
+int testDecode2(char *inBuf, char *outBuf, + unsigned int inBufSize, unsigned int outBufSize, + int mode, int testNum);
-int -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); - +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);
#endif - - diff --git a/src/steg/payloads.cc b/src/steg/payloads.cc index d24711f..7116e36 100644 --- a/src/steg/payloads.cc +++ b/src/steg/payloads.cc @@ -6,6 +6,9 @@ #include "payloads.h" #include "swfSteg.h"
+#include <ctype.h> +#include <time.h> + /* * fixContentLen corrects the Content-Length for an HTTP msg that * has been ungzipped, and removes the "Content-Encoding: gzip" diff --git a/src/steg/payloads.h b/src/steg/payloads.h index cb809ba..b19c5be 100644 --- a/src/steg/payloads.h +++ b/src/steg/payloads.h @@ -5,15 +5,6 @@ #ifndef _PAYLOADS_H #define _PAYLOADS_H
-#include <stdio.h> -#include <stdlib.h> -#include <time.h> -#include <string.h> -#include <sys/types.h> -#include <arpa/inet.h> -#include <ctype.h> - - /* three files: server_data, client data, protocol data */ @@ -22,8 +13,6 @@ #define RECV_INCOMPLETE 0 #define RECV_BAD -1
- - #define CONN_DATA_REQUEST 1 /* payload packet sent by client */ #define CONN_DATA_REPLY 2 /* payload packet sent by server */
@@ -33,7 +22,7 @@ #define MAX_RESP_HDR_SIZE 512
// max number of payloads that have enough capacity from which -// we choose the best fit +// we choose the best fit #define MAX_CANDIDATE_PAYLOADS 10
// jsSteg-specific defines @@ -52,6 +41,8 @@
#define HTML_MIN_AVAIL_SIZE 1026
+#define HTTP_MSG_BUF_SIZE 100000 + #define PDF_DELIMITER_SIZE 2 #define PDF_MIN_AVAIL_SIZE 10240 // PDF_MIN_AVAIL_SIZE should reflect the min number of data bytes @@ -73,7 +64,6 @@ #define CONTENT_JAVASCRIPT 1 #define CONTENT_HTML_JAVASCRIPT 2
- // payloads for specific content type // // MAX_CONTENT_TYPE specifies the maximum number of supported content types @@ -92,8 +82,6 @@
#define MAX_CONTENT_TYPE 11
- - typedef int SID; typedef short PacketType; typedef short StateFlag; @@ -106,23 +94,20 @@ typedef short StateFlag;
/* struct for reading in the payload_gen dump file */ -typedef struct { +struct pentry_header { PacketType ptype; int length; ushort port; /* network format */ -}pentry_header; - - - +};
-typedef struct service_state { +struct service_state { SID id; PacketType data_type; SID next_state; // double* probabilities; StateFlag flg; int dir; -}state; +};
struct payloads { int initTypePayload[MAX_CONTENT_TYPE]; @@ -139,12 +124,10 @@ struct payloads { int payload_count; };
- -#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); +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); @@ -152,8 +135,10 @@ 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 (payloads& pl, int contentType, char** buf, int* size, int* cap); -int get_payload (payloads& pl, 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); @@ -168,14 +153,15 @@ unsigned int capacityJS3 (char* buf, int len, int mode); unsigned int get_max_JS_capacity(void); unsigned int get_max_HTML_capacity(void);
-char * strInBinary (const char *pattern, unsigned int patternLen, const char *blob, unsigned int blobLen); - +char * strInBinary (const char *pattern, unsigned int patternLen, + const char *blob, unsigned int blobLen);
unsigned int capacityPDF (char* buf, int len); unsigned int get_max_PDF_capacity(void); int find_content_length (char *hdr, int hlen); int find_uri_type(char* buf, int size);
-int gen_response_header(char* content_type, int gzip, int length, char* buf, int buflen); +int gen_response_header(char* content_type, int gzip, int length, + char* buf, int buflen);
#endif diff --git a/src/steg/swfSteg.h b/src/steg/swfSteg.h index 938b05a..e1f3ff7 100644 --- a/src/steg/swfSteg.h +++ b/src/steg/swfSteg.h @@ -20,6 +20,7 @@ int http_server_SWF_transmit(payloads& pl, struct evbuffer *source, conn_t *conn);
int -http_handle_client_SWF_receive(steg_t *s, conn_t *conn, struct evbuffer *dest, struct evbuffer* source); +http_handle_client_SWF_receive(steg_t *s, conn_t *conn, struct evbuffer *dest, + struct evbuffer* source);
#endif
tor-commits@lists.torproject.org