forge-socket.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Forge Socket Banner Grab Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include "logger.h"
  12. #include <event.h>
  13. #include <event2/bufferevent_ssl.h>
  14. #include <getopt.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <ulimit.h>
  20. #include "forge_socket.h"
  21. #define MAX_BANNER_LEN 1024
  22. #define BASE64_ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  23. struct config {
  24. int read_timeout; // how long to wait once connected for the banner (seconds)
  25. int current_running;
  26. int max_concurrent;
  27. struct event_base *base;
  28. struct bufferevent *stdin_bev;
  29. int stdin_closed;
  30. enum {FORMAT_HEX, FORMAT_BASE64, FORMAT_ASCII} format;
  31. char *send_str;
  32. long send_str_size;
  33. struct stats_st {
  34. int init_connected_hosts; // Number of hosts we have even tried to connect to
  35. int connected_hosts; // # hosts that picked up
  36. int conn_timed_out; // # hosts that timed out during connection
  37. int read_timed_out; // # hosts that connected, but sent no data (banner)
  38. int timed_out; // # hosts that timed out at all (conn_timed_out+read_timed_out)?
  39. int completed_hosts; // # hosts that presented a banner
  40. } stats;
  41. };
  42. struct state {
  43. struct config *conf;
  44. uint32_t src_ip;
  45. uint32_t dst_ip;
  46. uint16_t sport;
  47. uint16_t dport;
  48. uint32_t seq;
  49. uint32_t seq_ack;
  50. enum {CONNECTING, CONNECTED, RECEIVED} state;
  51. };
  52. void stdin_readcb(struct bufferevent *bev, void *arg);
  53. void print_status(evutil_socket_t fd, short events, void *arg)
  54. {
  55. struct event *ev;
  56. struct config *conf = arg;
  57. struct event_base *base = conf->base;
  58. struct timeval status_timeout = {1, 0};
  59. ev = evtimer_new(base, print_status, conf);
  60. evtimer_add(ev, &status_timeout);
  61. (void)fd; (void)events;
  62. log_info("forge-socket", "(%d/%d in use) - Totals: %d inited, %d connected, %d conn timeout, %d read timeout %d completed",
  63. conf->current_running, conf->max_concurrent,
  64. conf->stats.init_connected_hosts,
  65. conf->stats.connected_hosts, conf->stats.conn_timed_out,
  66. conf->stats.read_timed_out, conf->stats.completed_hosts);
  67. }
  68. void decrement_cur_running(struct state *st)
  69. {
  70. struct config *conf = st->conf;
  71. conf->current_running--;
  72. log_debug("forge-socket", "done, down to %d",
  73. conf->current_running);
  74. if (evbuffer_get_length(bufferevent_get_input(conf->stdin_bev)) > 0) {
  75. stdin_readcb(conf->stdin_bev, conf);
  76. }
  77. free(st);
  78. if (conf->stdin_closed && conf->current_running == 0) {
  79. // Done
  80. log_info("forge-socket", "done");
  81. print_status(0, 0, conf);
  82. exit(0);
  83. }
  84. }
  85. void event_cb(struct bufferevent *bev, short events, void *arg)
  86. {
  87. struct state *st = arg;
  88. struct config *conf = st->conf;
  89. struct in_addr addr;
  90. addr.s_addr = st->src_ip;
  91. if (events & BEV_EVENT_CONNECTED) {
  92. log_error("forge-socket", "%s connected - wat?", inet_ntoa(addr));
  93. } else {
  94. if (st->state == CONNECTED) {
  95. // Print out that we just didn't receive data
  96. printf("%s X\n", inet_ntoa(addr));
  97. fflush(stdout);
  98. conf->stats.read_timed_out++;
  99. } else {
  100. conf->stats.conn_timed_out++;
  101. }
  102. log_debug("forge-socket", "%s bailing..", inet_ntoa(addr));
  103. bufferevent_free(bev);
  104. conf->stats.timed_out++;
  105. decrement_cur_running(st);
  106. }
  107. }
  108. // Grab these bytes, and close the connection.
  109. // Even if we don't need to read any bytes,
  110. // we have to have this so that libevent thinks we have
  111. // a read event, so that it can timeout TCP connects
  112. // (as a read timeout)
  113. void read_cb(struct bufferevent *bev, void *arg)
  114. {
  115. struct evbuffer *in = bufferevent_get_input(bev);
  116. struct state *st = arg;
  117. size_t len = evbuffer_get_length(in);
  118. struct in_addr addr;
  119. addr.s_addr = st->src_ip;
  120. log_debug("forge-socket", "read_cb for %s", inet_ntoa(addr));
  121. if (len > MAX_BANNER_LEN) {
  122. len = MAX_BANNER_LEN;
  123. }
  124. if (len > 0) {
  125. // Grab the banner
  126. unsigned int i;
  127. unsigned char *buf = malloc(len+1);
  128. st->state = RECEIVED;
  129. if (!buf) {
  130. log_fatal("forge-socket", "cannot alloc %d byte buf", len+1);
  131. return;
  132. }
  133. evbuffer_remove(in, buf, len);
  134. printf("%s ", inet_ntoa(addr));
  135. if (st->conf->format == FORMAT_ASCII) {
  136. // Ascii
  137. buf[len] = '\0';
  138. printf("%s\n", buf);
  139. } else if (st->conf->format == FORMAT_HEX) {
  140. // Hex output
  141. for (i=0; i<len; i++) {
  142. printf("%02x", buf[i]);
  143. }
  144. printf("\n");
  145. } else if (st->conf->format == FORMAT_BASE64) {
  146. // Base64
  147. int i=0;
  148. char out[4] = {0,0,0,0};
  149. while (i < len) {
  150. uint32_t value = 0;
  151. value += (i < len) ? buf[i++] << 16 : 0;
  152. value += (i < len) ? buf[i++] << 8 : 0;
  153. value += (i < len) ? buf[i++] : 0;
  154. out[0] = BASE64_ALPHABET[(value >> 18) & 0x3F];
  155. out[1] = BASE64_ALPHABET[(value >> 12) & 0x3F];
  156. out[2] = BASE64_ALPHABET[(value >> 6) & 0x3F];
  157. out[3] = BASE64_ALPHABET[(value ) & 0x3F];
  158. if (i < len) {
  159. printf("%c%c%c%c", out[0], out[1], out[2], out[3]);
  160. }
  161. }
  162. if (len > 0) {
  163. switch (len % 3) {
  164. case 1:
  165. out[2] = '=';
  166. case 2:
  167. out[3] = '=';
  168. default:
  169. break;
  170. }
  171. printf("%c%c%c%c\n", out[0], out[1], out[2], out[3]);
  172. }
  173. }
  174. fflush(stdout);
  175. free(buf);
  176. st->conf->stats.completed_hosts++;
  177. }
  178. bufferevent_free(bev);
  179. decrement_cur_running(st);
  180. }
  181. int set_sock_state(int sock, struct tcp_state *st)
  182. {
  183. struct sockaddr_in sin;
  184. sin.sin_family = AF_INET;
  185. sin.sin_addr.s_addr = st->src_ip;
  186. sin.sin_port = st->sport;
  187. int value = 1;
  188. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)) < 0) {
  189. perror("setsockopt SO_REUSEADDR");
  190. return -1;
  191. }
  192. if (setsockopt(sock, SOL_IP, IP_TRANSPARENT, &value, sizeof(value)) < 0) {
  193. perror("setsockopt IP_TRANSPARENT");
  194. return -1;
  195. }
  196. if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  197. perror("bind");
  198. return -1;
  199. }
  200. if (setsockopt(sock, IPPROTO_TCP, TCP_STATE, st, sizeof(struct tcp_state)) < 0) {
  201. perror("setsockopt TCP_STATE");
  202. return -1;
  203. }
  204. return 0;
  205. }
  206. void grab_banner(struct state *st)
  207. {
  208. struct sockaddr_in addr;
  209. struct bufferevent *bev;
  210. struct timeval read_to = {st->conf->read_timeout, 0};
  211. struct tcp_state tcp_st;
  212. int sock = socket(AF_INET, SOCK_FORGE, 0);
  213. addr.sin_addr.s_addr = st->src_ip;
  214. if (sock < 0) {
  215. perror("SOCK_FORGE socket");
  216. log_fatal("forge_socket", "(did you insmod forge_socket.ko?)");
  217. return;
  218. }
  219. memset(&tcp_st, 0, sizeof(tcp_st));
  220. // These need to be in network order for forge socket"
  221. tcp_st.src_ip = st->dst_ip;
  222. tcp_st.dst_ip = st->src_ip;
  223. tcp_st.sport = htons(st->dport);
  224. tcp_st.dport = htons(st->sport);
  225. // This should be in ???
  226. tcp_st.seq = st->seq_ack;
  227. tcp_st.ack = (st->seq + 1);
  228. tcp_st.snd_wnd = 0x1000;
  229. tcp_st.rcv_wnd = 0x1000;
  230. tcp_st.snd_una = tcp_st.seq;
  231. st->state = CONNECTING;
  232. st->conf->stats.init_connected_hosts++;
  233. // consider this a non-blocking, but completed "connect()". heh.
  234. if (set_sock_state(sock, &tcp_st) != 0) {
  235. log_error("forge_socket", "set_sock_state failed\n");
  236. decrement_cur_running(st);
  237. return;
  238. }
  239. evutil_make_socket_nonblocking(sock);
  240. bev = bufferevent_socket_new(st->conf->base, sock, BEV_OPT_CLOSE_ON_FREE);
  241. bufferevent_set_timeouts(bev, &read_to, &read_to);
  242. bufferevent_setcb(bev, read_cb, NULL, event_cb, st);
  243. bufferevent_enable(bev, EV_READ);
  244. // Send data
  245. if (st->conf->send_str) {
  246. struct evbuffer *evout = bufferevent_get_output(bev);
  247. // HACK!!! TODO: make some messy parser that replaces ${IP} with IP etc
  248. // and allow null characters
  249. evbuffer_add_printf(evout, st->conf->send_str,
  250. inet_ntoa(addr.sin_addr), inet_ntoa(addr.sin_addr),
  251. inet_ntoa(addr.sin_addr), inet_ntoa(addr.sin_addr));
  252. log_trace("forge-socket", "sent str to %s", inet_ntoa(addr.sin_addr));
  253. }
  254. // Update state/stats
  255. st->state = CONNECTED;
  256. st->conf->stats.connected_hosts++;
  257. log_trace("forge-socket", "go %s go! read a byte!!", inet_ntoa(addr.sin_addr));
  258. }
  259. void stdin_eventcb(struct bufferevent *bev, short events, void *ptr) {
  260. struct config *conf = ptr;
  261. if (events & BEV_EVENT_EOF) {
  262. log_debug("forge-socket",
  263. "received EOF; quitting after buffer empties");
  264. conf->stdin_closed = 1;
  265. if (conf->current_running == 0) {
  266. log_info("forge-socket", "done");
  267. print_status(0, 0, conf);
  268. exit(0);
  269. }
  270. }
  271. }
  272. void stdin_readcb(struct bufferevent *bev, void *arg)
  273. {
  274. struct evbuffer *in = bufferevent_get_input(bev);
  275. struct config *conf = arg;
  276. log_debug("forge-socket", "stdin cb %d < %d ?",
  277. conf->current_running, conf->max_concurrent);
  278. while (conf->current_running < conf->max_concurrent &&
  279. evbuffer_get_length(in) > 0) {
  280. size_t line_len;
  281. char *line = evbuffer_readln(in, &line_len, EVBUFFER_EOL_LF);
  282. struct state *st;
  283. if (!line)
  284. break;
  285. log_debug("forge-socket", "line: '%s'", line);
  286. //synack, 77.176.116.205, 141.212.121.125, 443, 49588, 3628826326, 3441755636, 0, 0,2013-08-11 19:16:05.799
  287. char synack[12];
  288. char srcip[INET_ADDRSTRLEN], dstip[INET_ADDRSTRLEN];
  289. uint32_t seq, seq_ack;
  290. uint16_t sport, dport;
  291. int cooldown, repeat=1;
  292. int ret = sscanf(line, "%11[^,], %15[^,], %15[^,], %hu, %hu, %u, %u, %d, %d,%*s",
  293. synack, srcip, dstip, &sport, &dport, &seq, &seq_ack, &cooldown, &repeat);
  294. log_trace("forge-socket", "%d '%s' sip: '%s', dip: '%s', sport: %d, dport: %d, seq: %d, seq_ack: %d",
  295. ret, synack, srcip, dstip, sport, dport, seq, seq_ack);
  296. if (ret==9 && !repeat && strcmp(synack, "synack") == 0) {
  297. st = malloc(sizeof(*st));
  298. st->conf = conf;
  299. st->src_ip = inet_addr(srcip);
  300. st->dst_ip = inet_addr(dstip);
  301. st->sport = sport;
  302. st->dport = dport;
  303. st->seq = seq;
  304. st->seq_ack = seq_ack;
  305. conf->current_running++;
  306. grab_banner(st);
  307. }
  308. }
  309. }
  310. int main(int argc, char *argv[])
  311. {
  312. struct event_base *base;
  313. struct event *status_timer;
  314. struct timeval status_timeout = {1, 0};
  315. int c;
  316. struct option long_options[] = {
  317. {"concurrent", required_argument, 0, 'c'},
  318. {"read-timeout", required_argument, 0, 'r'},
  319. {"verbosity", required_argument, 0, 'v'},
  320. {"format", no_argument, 0, 'f'},
  321. {"data", required_argument, 0, 'd'},
  322. {0, 0, 0, 0} };
  323. struct config conf;
  324. int ret;
  325. FILE *fp;
  326. log_init(stderr, LOG_INFO, 1, "forge-socket");
  327. ret = ulimit(4, 1000000); // Allow us to open 1 million fds (instead of 1024)
  328. if (ret < 0) {
  329. log_fatal("forge-socket", "cannot set ulimit");
  330. perror("ulimit");
  331. exit(1);
  332. }
  333. base = event_base_new();
  334. conf.base = base;
  335. // buffer stdin as an event
  336. conf.stdin_bev = bufferevent_socket_new(base, 0, BEV_OPT_DEFER_CALLBACKS);
  337. bufferevent_setcb(conf.stdin_bev, stdin_readcb, NULL, stdin_eventcb, &conf);
  338. bufferevent_enable(conf.stdin_bev, EV_READ);
  339. // Status timer
  340. status_timer = evtimer_new(base, print_status, &conf);
  341. evtimer_add(status_timer, &status_timeout);
  342. // Defaults
  343. conf.max_concurrent = 1;
  344. conf.current_running = 0;
  345. memset(&conf.stats, 0, sizeof(conf.stats));
  346. conf.read_timeout = 4;
  347. conf.stdin_closed = 0;
  348. conf.format = FORMAT_BASE64;
  349. conf.send_str = NULL;
  350. // Parse command line args
  351. while (1) {
  352. int option_index = 0;
  353. c = getopt_long(argc, argv, "c:t:r:v:f:d:",
  354. long_options, &option_index);
  355. if (c < 0) {
  356. break;
  357. }
  358. switch (c) {
  359. case 'c':
  360. conf.max_concurrent = atoi(optarg);
  361. break;
  362. case 'r':
  363. conf.read_timeout = atoi(optarg);
  364. break;
  365. case 'v':
  366. if (atoi(optarg) >= 0 && atoi(optarg) <= 5) {
  367. log_init(stderr, atoi(optarg), 1, "forge-socket");
  368. }
  369. break;
  370. case 'f':
  371. if (strcmp(optarg, "hex") == 0) {
  372. conf.format = FORMAT_HEX;
  373. } else if (strcmp(optarg, "base64") == 0) {
  374. conf.format = FORMAT_BASE64;
  375. } else if (strcmp(optarg, "ascii") == 0) {
  376. conf.format = FORMAT_ASCII;
  377. } else {
  378. log_fatal("forge-socket", "Unknown format '%s'; use 'hex', 'base64', or 'ascii'",
  379. optarg);
  380. }
  381. break;
  382. case 'd':
  383. fp = fopen(optarg, "r");
  384. if (!fp) {
  385. log_error("forge-socket", "Could not open send data file '%s':", optarg);
  386. perror("fopen");
  387. exit(-1);
  388. }
  389. fseek(fp, 0L, SEEK_END);
  390. conf.send_str_size = ftell(fp);
  391. fseek(fp, 0L, SEEK_SET);
  392. //assert(conf.send_str_size < 10000); // jumbo frames?
  393. conf.send_str = malloc(conf.send_str_size+1);
  394. if (!conf.send_str) {
  395. log_fatal("forge-socket", "Could not malloc %d bytes", conf.send_str_size+1);
  396. }
  397. if (fread(conf.send_str, conf.send_str_size, 1, fp) != 1) {
  398. log_fatal("forge-socket", "Couldn't read from send data file '%s':", optarg);
  399. }
  400. conf.send_str[conf.send_str_size] = '\0';
  401. fclose(fp);
  402. break;
  403. case '?':
  404. printf("Usage:\n");
  405. printf("\t%s [-c max_concurrency] [-r read_timeout] \n\t"
  406. "[-v verbosity=0-5] [-d send_data_file] [-f ascii|hex|base64]\n", argv[0]);
  407. exit(1);
  408. default:
  409. log_info("forge-socket", "hmmm..");
  410. break;
  411. }
  412. }
  413. log_info("forge-socket", "Using max_concurrency %d, %d s read timeout",
  414. conf.max_concurrent, conf.read_timeout);
  415. event_base_dispatch(base);
  416. return 0;
  417. }