zapret/tpws/tamper.c

386 lines
12 KiB
C
Raw Normal View History

2021-03-04 11:30:38 +00:00
#define _GNU_SOURCE
#include "tamper.h"
#include "params.h"
#include "hostlist.h"
#include "protocol.h"
2023-10-12 09:35:06 +00:00
#include "helpers.h"
2021-03-04 11:30:38 +00:00
#include <string.h>
#include <stdio.h>
2023-10-12 09:35:06 +00:00
// segment buffer has at least 5 extra bytes to extend data block
void tamper_out(t_ctrack *ctrack, uint8_t *segment,size_t segment_buffer_size,size_t *size, size_t *split_pos, uint8_t *split_flags)
2021-03-04 11:30:38 +00:00
{
2023-10-12 09:35:06 +00:00
uint8_t *p, *pp, *pHost = NULL;
2021-03-04 11:30:38 +00:00
size_t method_len = 0, pos;
const char *method;
bool bBypass = false, bHaveHost = false, bHostExcluded = false;
2023-10-26 12:12:32 +00:00
char *pc, Host[256];
2021-03-04 11:30:38 +00:00
2024-08-23 18:15:27 +00:00
DBGPRINT("tamper_out\n");
2023-10-26 12:12:32 +00:00
2021-03-04 11:30:38 +00:00
*split_pos=0;
*split_flags=0;
2021-03-04 11:30:38 +00:00
if ((method = HttpMethod(segment,*size)))
2021-03-04 11:30:38 +00:00
{
method_len = strlen(method)-2;
2024-08-23 18:15:27 +00:00
VPRINT("Data block looks like http request start : %s\n", method);
2023-10-26 12:12:32 +00:00
if (!ctrack->l7proto) ctrack->l7proto=HTTP;
2021-03-04 11:30:38 +00:00
// 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) && HttpFindHost(&pHost,segment,*size))
2021-03-04 11:30:38 +00:00
{
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';
2023-10-26 12:12:32 +00:00
bHaveHost = true;
2024-08-23 18:15:27 +00:00
VPRINT("Requested Host is : %s\n", Host);
2023-10-12 09:35:06 +00:00
for(pc = Host; *pc; pc++) *pc=tolower(*pc);
bBypass = !HostlistCheck(Host, &bHostExcluded);
2021-03-04 11:30:38 +00:00
}
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
2024-08-23 18:15:27 +00:00
VPRINT("Found double EOL at pos %td. Stop replacing.\n", pp - segment);
2021-03-04 11:30:38 +00:00
break;
}
pp = p;
}
pHost = NULL; // invalidate
}
if (params.methodeol && (*size+1+!params.unixeol)<=segment_buffer_size)
{
2024-08-23 18:15:27 +00:00
VPRINT("Adding EOL before method\n");
2021-03-04 11:30:38 +00:00
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<segment_buffer_size)
{
// we only work with data blocks looking as HTTP query, so method is at the beginning
2024-08-23 18:15:27 +00:00
VPRINT("Adding extra space after method\n");
2021-03-04 11:30:38 +00:00
p = segment + method_len + 1;
pos = method_len + 1;
memmove(p + 1, p, *size - pos);
*p = ' '; // insert extra space
(*size)++; // block will grow by 1 byte
if (pHost) pHost++; // Host: position will move by 1 byte
}
if ((params.hostdot || params.hosttab) && *size<segment_buffer_size && HttpFindHost(&pHost,segment,*size))
2021-03-04 11:30:38 +00:00
{
p = pHost + 5;
while (p < (segment + *size) && *p != '\r' && *p != '\n') p++;
if (p < (segment + *size))
{
pos = p - segment;
2024-08-23 18:15:27 +00:00
VPRINT("Adding %s to host name at pos %zu\n", params.hostdot ? "dot" : "tab", pos);
2021-03-04 11:30:38 +00:00
memmove(p + 1, p, *size - pos);
*p = params.hostdot ? '.' : '\t'; // insert dot or tab
(*size)++; // block will grow by 1 byte
}
}
if (params.domcase && HttpFindHost(&pHost,segment,*size))
2021-03-04 11:30:38 +00:00
{
p = pHost + 5;
pos = p - segment;
2024-08-23 18:15:27 +00:00
VPRINT("Mixing domain case at pos %zu\n",pos);
2021-03-04 11:30:38 +00:00
for (; p < (segment + *size) && *p != '\r' && *p != '\n'; p++)
*p = (((size_t)p) & 1) ? tolower(*p) : toupper(*p);
}
if (params.hostnospace && HttpFindHost(&pHost,segment,*size) && (pHost+5)<(segment+*size) && pHost[5] == ' ')
2021-03-04 11:30:38 +00:00
{
p = pHost + 6;
pos = p - segment;
2024-08-23 18:15:27 +00:00
VPRINT("Removing space before host name at pos %zu\n", pos);
2021-03-04 11:30:38 +00:00
memmove(p - 1, p, *size - pos);
(*size)--; // block will shrink by 1 byte
}
if (params.hostcase && HttpFindHost(&pHost,segment,*size))
2021-03-04 11:30:38 +00:00
{
2024-08-23 18:15:27 +00:00
VPRINT("Changing 'Host:' => '%c%c%c%c:' at pos %td\n", params.hostspell[0], params.hostspell[1], params.hostspell[2], params.hostspell[3], pHost - segment);
2021-03-04 11:30:38 +00:00
memcpy(pHost, params.hostspell, 4);
}
if (params.hostpad && HttpFindHost(&pHost,segment,*size))
2021-03-04 11:30:38 +00:00
{
// add : XXXXX: <padding?[\r\n|\n]
char s[8];
size_t hsize = params.unixeol ? 8 : 9;
size_t hostpad = params.hostpad<hsize ? hsize : params.hostpad;
if ((hsize+*size)>segment_buffer_size)
2024-08-23 18:15:27 +00:00
VPRINT("could not add host padding : buffer too small\n");
2021-03-04 11:30:38 +00:00
else
{
if ((hostpad+*size)>segment_buffer_size)
{
hostpad=segment_buffer_size-*size;
2024-08-23 18:15:27 +00:00
VPRINT("host padding reduced to %zu bytes : buffer too small\n", hostpad);
2021-03-04 11:30:38 +00:00
}
else
2024-08-23 18:15:27 +00:00
VPRINT("host padding with %zu bytes\n", hostpad);
2021-03-04 11:30:38 +00:00
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)<hsize) padsize+=hostpad-padsize-hsize;
snprintf(s,sizeof(s),"%c%04x: ", 'a'+rand()%('z'-'a'+1), rand() & 0xFFFF);
memcpy(p,s,7);
p+=7;
memset(p,'a'+rand()%('z'-'a'+1),padsize);
p+=padsize;
if (params.unixeol)
*p++='\n';
else
{
*p++='\r';
*p++='\n';
}
hostpad-=hsize+padsize;
}
pHost = NULL; // invalidate
}
}
*split_pos = HttpPos(params.split_http_req, params.split_pos, segment, *size);
if (params.disorder_http) *split_flags |= SPLIT_FLAG_DISORDER;
if (params.oob_http) *split_flags |= SPLIT_FLAG_OOB;
2021-03-04 11:30:38 +00:00
}
else
{
2024-08-23 18:15:27 +00:00
VPRINT("Not acting on this request\n");
2021-03-04 11:30:38 +00:00
}
}
2023-12-19 10:40:28 +00:00
else if (IsTLSClientHello(segment,*size,false))
2021-03-04 11:30:38 +00:00
{
size_t tpos=0,spos=0;
2023-10-26 12:12:32 +00:00
if (!ctrack->l7proto) ctrack->l7proto=TLS;
2021-03-04 11:30:38 +00:00
2024-08-23 18:15:27 +00:00
VPRINT("packet contains TLS ClientHello\n");
2023-10-12 09:35:06 +00:00
// we need host only if hostlist is present
2023-12-19 10:40:28 +00:00
if ((params.hostlist || params.hostlist_exclude) && TLSHelloExtractHost((uint8_t*)segment,*size,Host,sizeof(Host),false))
2023-10-12 09:35:06 +00:00
{
2024-08-23 18:15:27 +00:00
VPRINT("hostname: %s\n",Host);
2023-10-26 12:12:32 +00:00
bHaveHost = true;
bBypass = !HostlistCheck(Host, &bHostExcluded);
2021-03-04 11:30:38 +00:00
}
2023-10-26 12:12:32 +00:00
if (bBypass)
2023-10-12 09:35:06 +00:00
{
2024-08-23 18:15:27 +00:00
VPRINT("Not acting on this request\n");
2023-10-12 09:35:06 +00:00
}
2023-10-26 12:12:32 +00:00
else
2023-10-12 09:35:06 +00:00
{
spos = TLSPos(params.split_tls, params.split_pos, segment, *size, 0);
if ((5+*size)<=segment_buffer_size)
2023-10-12 09:35:06 +00:00
{
tpos = TLSPos(params.tlsrec, params.tlsrec_pos+5, segment, *size, 0);
if (tpos>5)
2023-10-26 12:12:32 +00:00
{
// construct 2 TLS records from one
uint16_t l = pntoh16(segment+3); // length
if (l>=2)
{
// length is checked in IsTLSClientHello and cannot exceed buffer size
if ((tpos-5)>=l) tpos=5+1;
2024-08-23 18:15:27 +00:00
VPRINT("making 2 TLS records at pos %zu\n",tpos);
memmove(segment+tpos+5,segment+tpos,*size-tpos);
segment[tpos] = segment[0];
segment[tpos+1] = segment[1];
segment[tpos+2] = segment[2];
phton16(segment+tpos+3,l-(tpos-5));
phton16(segment+3,tpos-5);
*size += 5;
// split pos present and it is not before tlsrec split. increase split pos by tlsrec header size (5 bytes)
if (spos && spos>=tpos) spos+=5;
}
2023-10-26 12:12:32 +00:00
}
2023-10-12 09:35:06 +00:00
}
if (spos && spos < *size)
{
2024-08-23 18:15:27 +00:00
VPRINT("split pos %zu\n",spos);
*split_pos = spos;
}
if (params.disorder_tls) *split_flags |= SPLIT_FLAG_DISORDER;
if (params.oob_tls) *split_flags |= SPLIT_FLAG_OOB;
2023-10-26 12:12:32 +00:00
}
2021-03-04 11:30:38 +00:00
}
2023-10-26 12:12:32 +00:00
else if (params.split_any_protocol && params.split_pos < *size)
2023-10-12 09:35:06 +00:00
*split_pos = params.split_pos;
2023-10-26 12:12:32 +00:00
2024-04-02 15:14:03 +00:00
if (bHaveHost && bBypass && !bHostExcluded && *params.hostlist_auto_filename)
2023-10-26 12:12:32 +00:00
{
2024-08-23 18:15:27 +00:00
DBGPRINT("tamper_out put hostname : %s\n", Host);
2024-04-02 15:14:03 +00:00
if (ctrack->hostname) free(ctrack->hostname);
2023-10-26 12:12:32 +00:00
ctrack->hostname=strdup(Host);
}
if (params.disorder) *split_flags |= SPLIT_FLAG_DISORDER;
if (params.oob) *split_flags |= SPLIT_FLAG_OOB;
2023-10-26 12:12:32 +00:00
}
static void auto_hostlist_reset_fail_counter(const char *hostname)
{
if (hostname)
{
hostfail_pool *fail_counter;
fail_counter = HostFailPoolFind(params.hostlist_auto_fail_counters, hostname);
if (fail_counter)
{
HostFailPoolDel(&params.hostlist_auto_fail_counters, fail_counter);
2024-08-23 18:15:27 +00:00
VPRINT("auto hostlist : %s : fail counter reset. website is working.\n", hostname);
HOSTLIST_DEBUGLOG_APPEND("%s : fail counter reset. website is working.", hostname);
}
}
}
2023-10-26 12:12:32 +00:00
static void auto_hostlist_failed(const char *hostname)
{
hostfail_pool *fail_counter;
fail_counter = HostFailPoolFind(params.hostlist_auto_fail_counters, hostname);
if (!fail_counter)
{
fail_counter = HostFailPoolAdd(&params.hostlist_auto_fail_counters, hostname, params.hostlist_auto_fail_time);
if (!fail_counter)
{
2024-08-23 18:15:27 +00:00
DLOG_ERR("HostFailPoolAdd: out of memory\n");
2023-10-26 12:12:32 +00:00
return;
}
}
fail_counter->counter++;
2024-08-23 18:15:27 +00:00
VPRINT("auto hostlist : %s : fail counter %d/%d\n", hostname, fail_counter->counter, params.hostlist_auto_fail_threshold);
2023-11-09 09:08:09 +00:00
HOSTLIST_DEBUGLOG_APPEND("%s : fail counter %d/%d", hostname, fail_counter->counter, params.hostlist_auto_fail_threshold);
2023-10-26 12:12:32 +00:00
if (fail_counter->counter >= params.hostlist_auto_fail_threshold)
{
2024-08-23 18:15:27 +00:00
VPRINT("auto hostlist : fail threshold reached. adding %s to auto hostlist\n", hostname);
2023-10-26 12:12:32 +00:00
HostFailPoolDel(&params.hostlist_auto_fail_counters, fail_counter);
2024-08-23 18:15:27 +00:00
VPRINT("auto hostlist : rechecking %s to avoid duplicates\n", hostname);
bool bExcluded=false;
if (!HostlistCheck(hostname, &bExcluded) && !bExcluded)
2023-10-26 12:12:32 +00:00
{
2024-08-23 18:15:27 +00:00
VPRINT("auto hostlist : adding %s\n", hostname);
2023-11-09 09:08:09 +00:00
HOSTLIST_DEBUGLOG_APPEND("%s : adding", hostname);
if (!StrPoolAddStr(&params.hostlist, hostname))
{
2024-08-23 18:15:27 +00:00
DLOG_ERR("StrPoolAddStr out of memory\n");
return;
}
if (!append_to_list_file(params.hostlist_auto_filename, hostname))
{
2024-08-23 18:15:27 +00:00
DLOG_PERROR("write to auto hostlist:");
return;
}
params.hostlist_auto_mod_time = file_mod_time(params.hostlist_auto_filename);
2023-10-26 12:12:32 +00:00
}
else
2023-11-09 09:08:09 +00:00
{
2024-08-23 18:15:27 +00:00
VPRINT("auto hostlist : NOT adding %s\n", hostname);
2023-11-09 09:08:09 +00:00
HOSTLIST_DEBUGLOG_APPEND("%s : NOT adding, duplicate detected", hostname);
}
2023-10-26 12:12:32 +00:00
}
}
void tamper_in(t_ctrack *ctrack, uint8_t *segment,size_t segment_buffer_size,size_t *size)
{
bool bFail=false;
2024-08-23 18:15:27 +00:00
DBGPRINT("tamper_in hostname=%s\n", ctrack->hostname);
2023-10-26 12:12:32 +00:00
2024-04-03 19:21:17 +00:00
if (*params.hostlist_auto_filename)
2023-10-26 12:12:32 +00:00
{
2024-04-03 19:21:17 +00:00
HostFailPoolPurgeRateLimited(&params.hostlist_auto_fail_counters);
if (ctrack->l7proto==HTTP && ctrack->hostname)
2023-10-26 12:12:32 +00:00
{
2024-04-03 19:21:17 +00:00
if (IsHttpReply(segment,*size))
2023-11-09 09:08:09 +00:00
{
2024-08-23 18:15:27 +00:00
VPRINT("incoming HTTP reply detected for hostname %s\n", ctrack->hostname);
2024-04-03 19:21:17 +00:00
bFail = HttpReplyLooksLikeDPIRedirect(segment, *size, ctrack->hostname);
if (bFail)
{
2024-08-23 18:15:27 +00:00
VPRINT("redirect to another domain detected. possibly DPI redirect.\n");
2024-04-03 19:21:17 +00:00
HOSTLIST_DEBUGLOG_APPEND("%s : redirect to another domain", ctrack->hostname);
}
else
2024-08-23 18:15:27 +00:00
VPRINT("local or in-domain redirect detected. it's not a DPI redirect.\n");
2023-11-09 09:08:09 +00:00
}
2023-10-26 12:12:32 +00:00
else
2024-04-03 19:21:17 +00:00
{
// received not http reply. do not monitor this connection anymore
2024-08-23 18:15:27 +00:00
VPRINT("incoming unknown HTTP data detected for hostname %s\n", ctrack->hostname);
2024-04-03 19:21:17 +00:00
}
if (bFail) auto_hostlist_failed(ctrack->hostname);
}
if (!bFail) auto_hostlist_reset_fail_counter(ctrack->hostname);
2024-04-03 19:21:17 +00:00
2023-10-26 12:12:32 +00:00
}
ctrack->bTamperInCutoff = true;
}
void rst_in(t_ctrack *ctrack)
{
2024-08-23 18:15:27 +00:00
DBGPRINT("rst_in hostname=%s\n", ctrack->hostname);
2023-10-26 12:12:32 +00:00
2024-04-02 15:14:03 +00:00
if (!*params.hostlist_auto_filename) return;
2023-10-26 12:12:32 +00:00
HostFailPoolPurgeRateLimited(&params.hostlist_auto_fail_counters);
if (!ctrack->bTamperInCutoff && ctrack->hostname)
{
2024-08-23 18:15:27 +00:00
VPRINT("incoming RST detected for hostname %s\n", ctrack->hostname);
2023-11-09 09:08:09 +00:00
HOSTLIST_DEBUGLOG_APPEND("%s : incoming RST", ctrack->hostname);
2023-10-26 12:12:32 +00:00
auto_hostlist_failed(ctrack->hostname);
}
}
void hup_out(t_ctrack *ctrack)
{
2024-08-23 18:15:27 +00:00
DBGPRINT("hup_out hostname=%s\n", ctrack->hostname);
2023-10-26 12:12:32 +00:00
2024-04-02 15:14:03 +00:00
if (!*params.hostlist_auto_filename) return;
2023-10-26 12:12:32 +00:00
HostFailPoolPurgeRateLimited(&params.hostlist_auto_fail_counters);
if (!ctrack->bTamperInCutoff && ctrack->hostname)
{
// local leg dropped connection after first request. probably due to timeout.
2024-08-23 18:15:27 +00:00
VPRINT("local leg closed connection after first request (timeout ?). hostname: %s\n", ctrack->hostname);
2023-11-09 09:08:09 +00:00
HOSTLIST_DEBUGLOG_APPEND("%s : client closed connection without server reply", ctrack->hostname);
2023-10-26 12:12:32 +00:00
auto_hostlist_failed(ctrack->hostname);
}
2021-03-04 11:30:38 +00:00
}