This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathnotsodeep.c
188 lines (145 loc) · 3.78 KB
/
notsodeep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#define UNUSED(x) (void)(x)
static inline bool tcp_synack_segment(struct tcphdr *tcphdr ) {
if (tcphdr->urg == 0 &&
tcphdr->ack == 1 &&
tcphdr->psh == 0 &&
tcphdr->rst == 0 &&
tcphdr->syn == 1 &&
tcphdr->fin == 0) {
return 1;
}
return 0;
}
void tcp_checksum(struct iphdr *pIph, unsigned short *ipPayload) {
register unsigned long sum = 0;
unsigned short tcpLen = ntohs(pIph->tot_len) - (pIph->ihl<<2);
struct tcphdr *tcphdrp = (struct tcphdr*)(ipPayload);
sum += (pIph->saddr>>16)&0xFFFF;
sum += (pIph->saddr)&0xFFFF;
sum += (pIph->daddr>>16)&0xFFFF;
sum += (pIph->daddr)&0xFFFF;
sum += htons(IPPROTO_TCP);
sum += htons(tcpLen);
tcphdrp->check = 0;
while (tcpLen > 1) {
sum += * ipPayload++;
tcpLen -= 2;
}
if(tcpLen > 0) {
sum += ((*ipPayload)&htons(0xFF00));
}
while (sum>>16) {
sum = (sum & 0xffff) + (sum >> 16);
}
sum = ~sum;
tcphdrp->check = (unsigned short)sum;
}
int rewrite_win_size( unsigned char *packet ) {
static uint16_t new_window = 40;
struct iphdr *iphdr = (struct iphdr *) packet;
struct tcphdr *tcphdr = (struct tcphdr *) (packet + (iphdr->ihl<<2));
tcphdr->window = htons(new_window);
tcp_checksum(iphdr, (unsigned short*)tcphdr);
return 0;
}
int rewrite_host_header( unsigned char *packet, int len) {
unsigned char *nhost = NULL;
char host[4] = "HoSt";
struct iphdr *iphdr = (struct iphdr *) packet;
struct tcphdr *tcphdr = (struct tcphdr *) (packet + (iphdr->ihl<<2));
while (len>=8)
{
if (!memcmp(packet,"\r\nHost: ",8)) {
nhost= packet;
break;
}
packet++;
len--;
}
if (nhost) {
memcpy(nhost+2,host,4);
tcp_checksum(iphdr, (unsigned short*)tcphdr);
return 1;
}
return 0;
}
int callback( struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa,
void *data ) {
UNUSED(nfmsg);
UNUSED(data);
struct iphdr *iphdr = NULL;
struct tcphdr *tcphdr = NULL;
struct nfqnl_msg_packet_hdr *ph = NULL;
unsigned char *packet= NULL;
int id = 0;
ph = nfq_get_msg_packet_hdr(nfa);
if (ph) {
id = ntohl(ph->packet_id);
} else {
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
if (nfq_get_payload(nfa, &packet) == -1) {
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
iphdr = (struct iphdr *) packet;
if ((iphdr->ihl < 5) || (iphdr->ihl > 15)) {
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
tcphdr = (struct tcphdr *) (packet + (iphdr->ihl<<2));
if (tcp_synack_segment(tcphdr)) {
if (rewrite_win_size(packet) != 0) {
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
return nfq_set_verdict(qh, id, NF_ACCEPT, ntohs(iphdr->tot_len), packet);
} else {
if (rewrite_host_header(packet, ntohs(iphdr->tot_len))) {
return nfq_set_verdict(qh, id, NF_ACCEPT, ntohs(iphdr->tot_len), packet);
}
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
}
int init_libnfq( struct nfq_handle **h, struct nfq_q_handle **qh ) {
*h = nfq_open();
if (!(*h)) {
return 1;
}
if (nfq_unbind_pf(*h, AF_INET) < 0) {
return 1;
}
if (nfq_bind_pf(*h, AF_INET) < 0) {
return 1;
}
*qh = nfq_create_queue(*h, 200, &callback, NULL);
if (!(*qh)) {
return 1;
}
if (nfq_set_mode(*qh, NFQNL_COPY_PACKET, 0xffff) < 0) {
return 1;
}
return 0;
}
int main() {
int fd = 0;
int rv = 0;
char buf[4096] __attribute__ ((aligned));
struct nfq_handle *h;
struct nfq_q_handle *qh;
if (init_libnfq(&h, &qh) != 0) {
return 1;
}
fd = nfq_fd(h);
while ((rv = recv(fd, buf, sizeof(buf), 0)) && rv >= 0) {
nfq_handle_packet(h, buf, rv);
}
nfq_destroy_queue(qh);
nfq_close(h);
return 0;
}