Skip to content

Commit 5bb0e65

Browse files
committed
Initial commit
1 parent b7317b5 commit 5bb0e65

File tree

6 files changed

+336
-0
lines changed

6 files changed

+336
-0
lines changed

Makefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
TARGET = goodbyedpi.exe
2+
LIBS = -L ../binary -lWinDivert -lws2_32
3+
CC = x86_64-w64-mingw32-gcc
4+
CCWINDRES = x86_64-w64-mingw32-windres
5+
CFLAGS = -Wall -I ../../include -L ../binary -O2
6+
7+
.PHONY: default all clean
8+
9+
default: manifest $(TARGET)
10+
all: default
11+
12+
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) goodbyedpi-rc.o
13+
HEADERS = $(wildcard *.h)
14+
15+
%.o: %.c $(HEADERS)
16+
$(CC) $(CFLAGS) -c $< -o $@
17+
18+
manifest:
19+
$(CCWINDRES) goodbyedpi-rc.rc goodbyedpi-rc.o
20+
21+
.PRECIOUS: $(TARGET) $(OBJECTS)
22+
23+
$(TARGET): $(OBJECTS)
24+
$(CC) $(OBJECTS) -Wall $(LIBS) -s -o $@
25+
26+
clean:
27+
-rm -f *.o
28+
-rm -f $(TARGET)

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
GoodbyeDPI — Passive Deep Packet Inspection blocker and Active DPI circumvention utility
2+
=========================
3+
4+
This software designed to bypass Deep Packet Inspection systems found in many Internet Service Providers which block access to certain websites.
5+
6+
It handles DPI connected using optical splitter or port mirroring (**Passive DPI**) which do not block any data but just replying faster then requested destination, and **Active DPI** connected in sequence.
7+
8+
**Windows 7, 8, 8.1 and 10** with administrator privileges required.
9+
10+
# How to use
11+
12+
Download [latest version from Releases page](https://github.com/ValdikSS/GoodbyeDPI/releases) and run.
13+
14+
# How does it work
15+
16+
### Passive DPI
17+
18+
Most Passive DPI send HTTP 301 Redirect if you try to access blocked website over HTTP and TCP Reset in case of HTTPS, faster then destination website. Packets sent by DPI have always have IP Identification field equal to `0x0000` or `0x0001`, as seen with Russian providers. These packets are blocked by GoodbyeDPI.
19+
20+
### Active DPI
21+
22+
Active DPI is more tricky to fool. Currently the software uses 3 methods to circumvent Active DPI:
23+
24+
* TCP-level fragmentation for first data packet
25+
* Replacing `Host` header with `hoSt`
26+
* Removing space between header name and value in `Host` header
27+
28+
These methods do not break any website as are fully compatible with TCP and HTTP standards, yet it's sufficient to prevent DPI data classification and to circumvent censorship.
29+
30+
# Similar projects
31+
32+
[zapret](https://github.com/bol-van/zapret) by @bol-van (for Linux).
33+
34+
# Kudos
35+
36+
Thanks @basil00 for [WinDivert](https://github.com/basil00/Divert). That's the main part of this program.
37+
38+
Thanks for every [BlockCheck](https://github.com/ValdikSS/blockcheck) contributor. It would be impossible to understand DPI behaviour without this utility.

goodbyedpi-rc.rc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1 24 "goodbyedpi.exe.manifest"
2+
id ICON "icon.ico"

goodbyedpi.c

+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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+
}

goodbyedpi.exe.manifest

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
3+
<assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="GoodbyeDPI" type="win32"/>
4+
<description>Divert</description>
5+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
6+
<security>
7+
<requestedPrivileges>
8+
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
9+
</requestedPrivileges>
10+
</security>
11+
</trustInfo>
12+
</assembly>

icon.ico

11.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)