|
| 1 | +/* |
| 2 | + * GoodbyeDPI — Passive DPI blocker and Active DPI circumvention utility. |
| 3 | + */ |
| 4 | + |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <signal.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <string.h> |
| 10 | +#include <winsock2.h> |
| 11 | +#include "windivert.h" |
| 12 | + |
| 13 | +#define die() do { printf("Something went wrong!\n" \ |
| 14 | + "Make sure you're running this program with administrator privileges\n"); \ |
| 15 | + sleep(10); exit(1); } while (0) |
| 16 | + |
| 17 | +#define MAX_FILTERS 4 |
| 18 | +#define MAX_PACKET_SIZE 1516 |
| 19 | +#define IPV4_HDR_LEN 20 |
| 20 | +#define TCP_HDR_LEN 20 |
| 21 | +#define IPV4_TOTALLEN_OFFSET 2 |
| 22 | +#define TCP_WINDOWSIZE_OFFSET 14 |
| 23 | + |
| 24 | +static HANDLE filters[MAX_FILTERS]; |
| 25 | +static int filter_num = 0; |
| 26 | +static const char *http_redirect_10 = "HTTP/1.0 30"; |
| 27 | +static const char *http_redirect_11 = "HTTP/1.1 30"; |
| 28 | +static const char *http_host_find = "\r\nHost: "; |
| 29 | +static const char *http_host_replace = "\r\nhoSt: "; |
| 30 | +/*static const char *blocklist[] = { |
| 31 | + "warning.rt.ru", |
| 32 | +};*/ |
| 33 | + |
| 34 | +static char* dumb_memmem(char* haystack, int hlen, char* needle, int nlen) { |
| 35 | + // naive implementation |
| 36 | + if (nlen > hlen) return 0; |
| 37 | + int i; |
| 38 | + for (i=0; i<hlen-nlen+1; i++) { |
| 39 | + if (memcmp(haystack+i,needle,nlen)==0) { |
| 40 | + return haystack+i; |
| 41 | + } |
| 42 | + } |
| 43 | + return NULL; |
| 44 | +} |
| 45 | + |
| 46 | +static HANDLE init(char *filter, UINT64 flags) { |
| 47 | + filter = WinDivertOpen(filter, WINDIVERT_LAYER_NETWORK, 0, flags); |
| 48 | + if (filter != INVALID_HANDLE_VALUE) |
| 49 | + return filter; |
| 50 | + return NULL; |
| 51 | +} |
| 52 | + |
| 53 | +static int deinit(HANDLE handle) { |
| 54 | + if (handle) { |
| 55 | + WinDivertClose(handle); |
| 56 | + return 1; |
| 57 | + } |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static void deinit_all() { |
| 62 | + for (int i = 0; i < filter_num; i++) { |
| 63 | + deinit(filters[i]); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +static void sigint_handler(int sig) { |
| 68 | + deinit_all(); |
| 69 | + exit(0); |
| 70 | +} |
| 71 | + |
| 72 | +static int find_passivedpi_redirect(char *pktdata) { |
| 73 | + if (memcmp(pktdata, http_redirect_11, strlen(http_redirect_11)) == 0 |
| 74 | + || memcmp(pktdata, http_redirect_10, strlen(http_redirect_10)) == 0) { |
| 75 | + return 1; |
| 76 | + } |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +/* Finds Host header with \r\n before it */ |
| 81 | +static PVOID find_host_header(char *pktdata, int pktlen) { |
| 82 | + return dumb_memmem(pktdata, pktlen, |
| 83 | + (char*)http_host_find, strlen(http_host_find)); |
| 84 | +} |
| 85 | + |
| 86 | +static void change_window_size(char *pkt) { |
| 87 | + *(pkt + IPV4_HDR_LEN + TCP_WINDOWSIZE_OFFSET) = 0x00; |
| 88 | + *(pkt + IPV4_HDR_LEN + TCP_WINDOWSIZE_OFFSET + 1) = 0x02; |
| 89 | +} |
| 90 | + |
| 91 | +int main(int argc, char *argv[]) { |
| 92 | + int i, should_reinject = 0; |
| 93 | + HANDLE w_filter = NULL; |
| 94 | + WINDIVERT_ADDRESS addr; |
| 95 | + char packet[MAX_PACKET_SIZE]; |
| 96 | + PVOID packet_data; |
| 97 | + UINT packetLen; |
| 98 | + UINT packet_dataLen; |
| 99 | + PWINDIVERT_IPHDR ppIpHdr; |
| 100 | + PWINDIVERT_TCPHDR ppTcpHdr; |
| 101 | + |
| 102 | + int do_passivedpi, do_fragment, do_host, do_host_removespace; |
| 103 | + int temp; |
| 104 | + char *data_addr, *data_addr_rn, *host_addr = NULL; |
| 105 | + int host_len, fromhost_uptoend_len; |
| 106 | + |
| 107 | + printf("GoodbyeDPI: Passive DPI blocker and Active DPI circumvention utility\n\n"); |
| 108 | + |
| 109 | + if (argc == 2) { |
| 110 | + temp = atoi(argv[1]); |
| 111 | + do_passivedpi = !!(temp & 1); |
| 112 | + do_fragment = !!(temp & 2); |
| 113 | + do_host = !!(temp & 4); |
| 114 | + do_host_removespace = !!(temp & 8); |
| 115 | + |
| 116 | + printf("Block passive: %d, Fragment: %d, hoSt: %d, Host no space: %d\n", |
| 117 | + do_passivedpi, do_fragment, do_host, do_host_removespace); |
| 118 | + } |
| 119 | + else { |
| 120 | + printf("goodbyedpi.exe [1: block passive DPI, 2: fragment outbound, " |
| 121 | + "4: replace Host with hoSt, 8: remove space between host header and value]\n"); |
| 122 | + printf("Default: 15 (all enabled)\n"); |
| 123 | + |
| 124 | + do_passivedpi = 1; |
| 125 | + do_fragment = 1; |
| 126 | + do_host = 1; |
| 127 | + do_host_removespace = 1; |
| 128 | + } |
| 129 | + |
| 130 | + printf("Opening filter\n"); |
| 131 | + filter_num = 0; |
| 132 | + |
| 133 | + if (do_passivedpi) { |
| 134 | + /* Filter for inbound RST packets with ID = 0 or 1 */ |
| 135 | + filters[filter_num] = init("inbound and (ip.Id == 0x0001 or ip.Id == 0x0000) and " |
| 136 | + "(tcp.SrcPort == 443 or tcp.SrcPort == 80) and tcp.Rst", |
| 137 | + WINDIVERT_FLAG_DROP); |
| 138 | + filter_num++; |
| 139 | + } |
| 140 | + |
| 141 | + /* |
| 142 | + * Filter for inbound HTTP redirection packets and |
| 143 | + * active DPI circumvention |
| 144 | + */ |
| 145 | + filters[filter_num] = init("(inbound and (ip.Id == 0x0001 or ip.Id == 0x0000) and tcp.SrcPort == 80 and tcp.Ack) " |
| 146 | + "or (inbound and (tcp.SrcPort == 80 or tcp.SrcPort == 443) and tcp.Ack and tcp.Syn) " |
| 147 | + "or (outbound and (tcp.DstPort == 80 or tcp.DstPort == 443) and tcp.Ack)", |
| 148 | + 0); |
| 149 | + |
| 150 | + w_filter = filters[filter_num]; |
| 151 | + filter_num++; |
| 152 | + |
| 153 | + for (i = 0; i < filter_num; i++) { |
| 154 | + if (filters[i] == NULL) |
| 155 | + die(); |
| 156 | + } |
| 157 | + |
| 158 | + printf("Filter activated!\n"); |
| 159 | + signal(SIGINT, sigint_handler); |
| 160 | + |
| 161 | + while (1) { |
| 162 | + if (WinDivertRecv(w_filter, packet, sizeof(packet), &addr, &packetLen)) { |
| 163 | + //printf("Got %s packet, len=%d!\n", addr.Direction ? "inbound" : "outbound", |
| 164 | + // packetLen); |
| 165 | + should_reinject = 1; |
| 166 | + |
| 167 | + if (WinDivertHelperParsePacket(packet, packetLen, &ppIpHdr, |
| 168 | + NULL, NULL, NULL, &ppTcpHdr, NULL, &packet_data, &packet_dataLen)) { |
| 169 | + //printf("Got parsed packet, len=%d!\n", packet_dataLen); |
| 170 | + /* Got a packet WITH DATA */ |
| 171 | + |
| 172 | + /* Handle INBOUND packet with data and find HTTP REDIRECT in there */ |
| 173 | + if (addr.Direction == WINDIVERT_DIRECTION_INBOUND && packet_dataLen > 16) { |
| 174 | + /* If INBOUND packet with DATA (tcp.Ack) */ |
| 175 | + |
| 176 | + /* Drop packets from blocklist */ |
| 177 | + /* for (i = 0; i < sizeof(blocklist) / sizeof(*blocklist); i++) { |
| 178 | + if (dumb_memmem(packet_data, packet_dataLen, (char*)blocklist[i], |
| 179 | + strlen(blocklist[i])) != NULL) { |
| 180 | + printf("Dropping packet!\n"); |
| 181 | + dropped = 1; |
| 182 | + break; |
| 183 | + } |
| 184 | + } */ |
| 185 | + |
| 186 | + /* Drop packets from filter with HTTP 30x Redirect */ |
| 187 | + if (do_passivedpi && find_passivedpi_redirect(packet_data)) { |
| 188 | + //printf("Dropping HTTP Redirect packet!\n"); |
| 189 | + should_reinject = 0; |
| 190 | + } |
| 191 | + } |
| 192 | + /* Handle OUTBOUND packet, search for Host header */ |
| 193 | + else if (addr.Direction == WINDIVERT_DIRECTION_OUTBOUND && |
| 194 | + packet_dataLen > 16 && ppTcpHdr->DstPort == htons(80)) { |
| 195 | + if (do_host || do_host_removespace) { |
| 196 | + data_addr = find_host_header(packet_data, packet_dataLen); |
| 197 | + } |
| 198 | + |
| 199 | + if (do_host && data_addr) { |
| 200 | + /* Replace "Host: " with "hoSt: " */ |
| 201 | + memcpy(data_addr, http_host_replace, strlen(http_host_replace)); |
| 202 | + //printf("Replaced Host header!\n"); |
| 203 | + } |
| 204 | + |
| 205 | + if (do_host_removespace && data_addr) { |
| 206 | + host_addr = data_addr + strlen(http_host_find); |
| 207 | + |
| 208 | + data_addr_rn = dumb_memmem(host_addr, |
| 209 | + packet_dataLen - ((PVOID)host_addr - packet_data), |
| 210 | + "\r\n", 2); |
| 211 | + if (data_addr_rn) { |
| 212 | + host_len = data_addr_rn - host_addr; |
| 213 | + fromhost_uptoend_len = packet_dataLen - ((PVOID)host_addr - packet_data); |
| 214 | + if (host_len <= 64) { |
| 215 | + /* Move memory left by 1 byte and reduce packet size for 1 byte */ |
| 216 | + memmove(host_addr - 1, host_addr, fromhost_uptoend_len); |
| 217 | + /* Reduce "Total Length" in IP header by 1 byte */ |
| 218 | + *(uint16_t*)(packet + IPV4_TOTALLEN_OFFSET) = ntohs( |
| 219 | + htons(*(uint16_t*)(packet + IPV4_TOTALLEN_OFFSET)) - 1); |
| 220 | + /* Reduce packetLen by 1 byte */ |
| 221 | + packetLen--; |
| 222 | + //printf("Replaced Host header!\n"); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + if (do_host || do_host_removespace) { |
| 227 | + WinDivertHelperCalcChecksums(packet, packetLen, 0); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + /* Else if we got TCP packet without data */ |
| 232 | + else if (WinDivertHelperParsePacket(packet, packetLen, &ppIpHdr, |
| 233 | + NULL, NULL, NULL, &ppTcpHdr, NULL, NULL, NULL)) { |
| 234 | + /* If we got SYN+ACK packet */ |
| 235 | + if (addr.Direction == WINDIVERT_DIRECTION_INBOUND && |
| 236 | + ppTcpHdr->Syn == 1) { |
| 237 | + if (do_fragment) { |
| 238 | + //printf("Changing Window Size!\n"); |
| 239 | + change_window_size(packet); |
| 240 | + WinDivertHelperCalcChecksums(packet, packetLen, 0); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + if (should_reinject) { |
| 246 | + //printf("Re-injecting!\n"); |
| 247 | + WinDivertSend(w_filter, packet, packetLen, &addr, NULL); |
| 248 | + } |
| 249 | + } |
| 250 | + else { |
| 251 | + // error, ignore |
| 252 | + printf("Error receiving packet!\n"); |
| 253 | + break; |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments