-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtimeshifter.c
257 lines (195 loc) · 6.68 KB
/
timeshifter.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// https://www.anfractuosity.com/projects/timeshifter/
#include <stdint.h>
#include <time.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <arpa/inet.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <time.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/timex.h>
#include <math.h>
#define NF_IP_PRE_ROUTING 0
#define NF_IP_LOCAL_IN 1
#define NF_IP_FORWARD 2
#define NF_IP_LOCAL_OUT 3
#define NF_IP_POST_ROUTING 4
#define NF_IP_NUMHOOKS 5
#define ERR_INIT -1
int first = 1;
struct timeval start, elapsed;
int startp = -1;
char buff[1];
int zerothreshold = -1;
long long onesleep = -1;
unsigned char getbit(unsigned char *bits, unsigned long n) {
return (bits[n / 8] & (unsigned char)pow(2, n % 8)) >> n % 8;
}
void setbit(unsigned char *bits, unsigned long n, unsigned char val) {
bits[n / 8] =
(bits[n / 8] & ~(unsigned char)pow(2, n % 8)) | ((unsigned char)
pow(2,
n % 8) * val);
}
static int difference_micro(struct timeval *before, struct timeval *after) {
return (signed long long)after->tv_sec * 1000000ll +
(signed long long)after->tv_usec -
(signed long long)before->tv_sec * 1000000ll -
(signed long long)before->tv_usec;
}
static uint32_t nfqueue_packet_get_id(struct nfq_data *packet) {
uint32_t id = -1;
struct nfqnl_msg_packet_hdr *packetHeader;
if ((packetHeader = nfq_get_msg_packet_hdr(packet)) != NULL)
id = ntohl(packetHeader->packet_id);
return id;
}
static uint32_t nfqueue_packet_get_hook(struct nfq_data *packet) {
uint32_t hook = -1;
struct nfqnl_msg_packet_hdr *packetHeader;
if ((packetHeader = nfq_get_msg_packet_hdr(packet)) != NULL)
hook = packetHeader->hook;
return hook;
}
static int manage_packet(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
struct nfq_data *nfa, void *data2) {
uint32_t hook = nfqueue_packet_get_hook(nfa);
uint32_t id = nfqueue_packet_get_id(nfa);
switch (hook) {
case NF_IP_LOCAL_IN:
if (first) {
printf("Reciever...\n");
gettimeofday(&start, 0x0);
first = 0;
startp = 0;
} else {
gettimeofday(&elapsed, 0x0);
long ms =
difference_micro(&start, &elapsed) / 1000;
if (startp == 0)
printf("BYTE: ");
if (ms <= zerothreshold) {
printf("0");
setbit(buff, startp, 0);
} else {
printf("1");
setbit(buff, startp, 1);
}
fflush(stdout);
startp++;
if (startp % 8 == 0) {
printf(" (%c) \n", buff[0]);
startp = 0;
}
start.tv_sec = elapsed.tv_sec;
start.tv_usec = elapsed.tv_usec;
}
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
case NF_IP_FORWARD:
puts("capturing packet from FORWARD iptables hook");
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
case NF_IP_LOCAL_OUT:{
if (startp == -1) {
printf("Transmitter...\n");
int b = fgetc(stdin);
if (b == EOF)
b = 0;
buff[0] = b;
startp = 0;
return nfq_set_verdict(qh, id,
NF_ACCEPT, 0,
NULL);
}
unsigned char bit = getbit(buff, startp);
if (startp == 0) {
printf("BYTE: ");
}
if (bit == 0) {
printf("0");
} else if (bit == 1) {
printf("1");
int seconds = onesleep / 1000;
long long milliseconds =
(((double)onesleep / (double)1000) -
(double)seconds) * 1000 * 1000000;
nanosleep((struct timespec[]) { {
seconds, milliseconds}},
NULL);
}
fflush(stdout);
startp++;
if (startp % 8 == 0) {
printf(" (%c)\n", buff[0]);
int b = fgetc(stdin);
if (b == EOF)
b = 0;
buff[0] = b;
startp = 0;
}
return nfq_set_verdict(qh, id, NF_ACCEPT, 0,
NULL);
}
default:
puts("error: capturing packet from an iptables hook we shouldn't");
return nfq_set_verdict(qh, id, NF_ACCEPT, 0, NULL);
}
}
int main(int argc, char **argv) {
struct nfq_handle *handle;
struct nfq_q_handle *queue;
struct nfnl_handle *netlink_handle;
uint32_t nfqueue_fd;
if (argc != 4) {
printf("Argument: queue_number zerothreshold onesleep\n");
printf("Times are to be represented in milliseconds.\n");
return -1;
}
int16_t queuenum = atoi(argv[1]);
zerothreshold = atoi(argv[2]);
onesleep = atoi(argv[3]);
handle = nfq_open();
if (!handle) {
perror("Error: during nfq_open()");
return ERR_INIT;
}
if (nfq_unbind_pf(handle, AF_INET) < 0) {
perror("Error: during nfq_unbind_pf()");
return ERR_INIT;
}
if (nfq_bind_pf(handle, AF_INET) < 0) {
perror("Error: during nfq_bind_pf()");
return ERR_INIT;
}
queue = nfq_create_queue(handle, queuenum, &manage_packet, NULL);
if (!queue) {
perror("Error: during nfq_create_queue()");
return ERR_INIT;
}
if (nfq_set_mode(queue, NFQNL_COPY_PACKET, 0xffff) < 0) {
perror("Error: can't set packet_copy mode");
return ERR_INIT;
}
netlink_handle = nfq_nfnlh(handle);
nfqueue_fd = nfnl_fd(netlink_handle);
char buf[4096] __attribute__ ((aligned));
int received;
while (1) {
if ((received = recv(nfqueue_fd, buf, sizeof(buf), 0)) >= 0) {
nfq_handle_packet(handle, buf, received);
}
}
if (!queue)
nfq_destroy_queue(queue);
if (!handle)
nfq_close(handle);
return 0;
}