#define _GNU_SOURCE #include "tamper.h" #include "params.h" #include "hostlist.h" #include "protocol.h" #include #include // pHost points to "Host: ..." bool find_host(char **pHost,char *buf,size_t bs) { if (!*pHost) { *pHost = memmem(buf, bs, "\nHost:", 6); if (*pHost) { (*pHost)++; VPRINT("Found Host: at pos %td",*pHost - buf) } } return !!*pHost; } static const char *http_methods[] = { "GET /","POST /","HEAD /","OPTIONS /","PUT /","DELETE /","CONNECT /","TRACE /",NULL }; void modify_tcp_segment(char *segment,size_t segment_buffer_size,size_t *size,size_t *split_pos) { char *p, *pp, *pHost = NULL; size_t method_len = 0, pos; const char **method; bool bIsHttp = false, bBypass = false; char bRemovedHostSpace = 0; char Host[128]; *split_pos=0; for (method = http_methods; *method; method++) { method_len = strlen(*method); if (method_len <= *size && !memcmp(segment, *method, method_len)) { bIsHttp = true; method_len -= 2; // "GET /" => "GET" break; } } if (bIsHttp) { VPRINT("Data block looks like http request start : %s", *method) // cpu saving : we search host only if and when required. we do not research host every time we need its position if ((params.hostlist || params.hostlist_exclude) && find_host(&pHost,segment,*size)) { p = pHost + 5; while (p < (segment + *size) && (*p == ' ' || *p == '\t')) p++; pp = p; while (pp < (segment + *size) && (pp - p) < (sizeof(Host) - 1) && *pp != '\r' && *pp != '\n') pp++; memcpy(Host, p, pp - p); Host[pp - p] = '\0'; VPRINT("Requested Host is : %s", Host) for(p = Host; *p; p++) *p=tolower(*p); bBypass = !HostlistCheck(params.hostlist, params.hostlist_exclude, Host); } if (!bBypass) { if (params.unixeol) { p = pp = segment; while ((p = memmem(p, segment + *size - p, "\r\n", 2))) { *p = '\n'; p++; memmove(p, p + 1, segment + *size - p - 1); (*size)--; if (pp == (p - 1)) { // probably end of http headers VPRINT("Found double EOL at pos %td. Stop replacing.", pp - segment) break; } pp = p; } pHost = NULL; // invalidate } if (params.methodeol && (*size+1+!params.unixeol)<=segment_buffer_size) { VPRINT("Adding EOL before method") if (params.unixeol) { memmove(segment + 1, segment, *size); (*size)++;; segment[0] = '\n'; } else { memmove(segment + 2, segment, *size); *size += 2; segment[0] = '\r'; segment[1] = '\n'; } pHost = NULL; // invalidate } if (params.methodspace && *size '%c%c%c%c:' at pos %td", params.hostspell[0], params.hostspell[1], params.hostspell[2], params.hostspell[3], pHost - segment) memcpy(pHost, params.hostspell, 4); } if (params.hostpad && find_host(&pHost,segment,*size)) { // add : XXXXX: segment_buffer_size) VPRINT("could not add host padding : buffer too small") else { if ((hostpad+*size)>segment_buffer_size) { hostpad=segment_buffer_size-*size; VPRINT("host padding reduced to %zu bytes : buffer too small", hostpad) } else VPRINT("host padding with %zu bytes", hostpad) p = pHost; pos = p - segment; memmove(p + hostpad, p, *size - pos); (*size) += hostpad; while(hostpad) { #define MAX_HDR_SIZE 2048 size_t padsize = hostpad > hsize ? hostpad-hsize : 0; if (padsize>MAX_HDR_SIZE) padsize=MAX_HDR_SIZE; // if next header would be too small then add extra padding to the current one if ((hostpad-padsize-hsize)