123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <libgen.h>
  9. #include <signal.h>
  10. #include <net/if.h>
  11. #include <net/ethernet.h>
  12. #include <sys/select.h>
  13. #include <sys/socket.h>
  14. #include <sys/stat.h>
  15. #include <sys/un.h>
  16. #include <syslog.h>
  17. #include <getopt.h>
  18. #include <pcap.h>
  19. #define SNAPLEN 1600
  20. struct hijack {
  21. pcap_t *pcap;
  22. int fd;
  23. int datalink;
  24. int filtered;
  25. unsigned long rx_count;
  26. unsigned long tx_count;
  27. };
  28. struct hijack_listener {
  29. struct sockaddr_un sun;
  30. int fd;
  31. };
  32. struct hijack_options {
  33. char interface[IF_NAMESIZE];
  34. int daemonise;
  35. };
  36. static int daemonised = 0;
  37. /**
  38. * Log error message
  39. *
  40. */
  41. static __attribute__ (( format ( printf, 2, 3 ) )) void
  42. logmsg ( int level, const char *format, ... ) {
  43. va_list ap;
  44. va_start ( ap, format );
  45. if ( daemonised ) {
  46. vsyslog ( ( LOG_DAEMON | level ), format, ap );
  47. } else {
  48. vfprintf ( stderr, format, ap );
  49. }
  50. va_end ( ap );
  51. }
  52. /**
  53. * Open pcap device
  54. *
  55. */
  56. static int hijack_open ( const char *interface, struct hijack *hijack ) {
  57. char errbuf[PCAP_ERRBUF_SIZE];
  58. /* Open interface via pcap */
  59. errbuf[0] = '\0';
  60. hijack->pcap = pcap_open_live ( interface, SNAPLEN, 1, 0, errbuf );
  61. if ( ! hijack->pcap ) {
  62. logmsg ( LOG_ERR, "Failed to open %s: %s\n",
  63. interface, errbuf );
  64. goto err;
  65. }
  66. if ( errbuf[0] )
  67. logmsg ( LOG_WARNING, "Warning: %s\n", errbuf );
  68. /* Set capture interface to non-blocking mode */
  69. if ( pcap_setnonblock ( hijack->pcap, 1, errbuf ) < 0 ) {
  70. logmsg ( LOG_ERR, "Could not make %s non-blocking: %s\n",
  71. interface, errbuf );
  72. goto err;
  73. }
  74. /* Get file descriptor for select() */
  75. hijack->fd = pcap_get_selectable_fd ( hijack->pcap );
  76. if ( hijack->fd < 0 ) {
  77. logmsg ( LOG_ERR, "Cannot get selectable file descriptor "
  78. "for %s\n", interface );
  79. goto err;
  80. }
  81. /* Get link layer type */
  82. hijack->datalink = pcap_datalink ( hijack->pcap );
  83. return 0;
  84. err:
  85. if ( hijack->pcap )
  86. pcap_close ( hijack->pcap );
  87. return -1;
  88. }
  89. /**
  90. * Close pcap device
  91. *
  92. */
  93. static void hijack_close ( struct hijack *hijack ) {
  94. pcap_close ( hijack->pcap );
  95. }
  96. /**
  97. * Install filter for hijacked connection
  98. *
  99. */
  100. static int hijack_install_filter ( struct hijack *hijack,
  101. char *filter ) {
  102. struct bpf_program program;
  103. /* Compile filter */
  104. if ( pcap_compile ( hijack->pcap, &program, filter, 1, 0 ) < 0 ) {
  105. logmsg ( LOG_ERR, "could not compile filter \"%s\": %s\n",
  106. filter, pcap_geterr ( hijack->pcap ) );
  107. goto err_nofree;
  108. }
  109. /* Install filter */
  110. if ( pcap_setfilter ( hijack->pcap, &program ) < 0 ) {
  111. logmsg ( LOG_ERR, "could not install filter \"%s\": %s\n",
  112. filter, pcap_geterr ( hijack->pcap ) );
  113. goto err;
  114. }
  115. logmsg ( LOG_INFO, "using filter \"%s\"\n", filter );
  116. pcap_freecode ( &program );
  117. return 0;
  118. err:
  119. pcap_freecode ( &program );
  120. err_nofree:
  121. return -1;
  122. }
  123. /**
  124. * Set up filter for hijacked ethernet connection
  125. *
  126. */
  127. static int hijack_filter_ethernet ( struct hijack *hijack, const char *buf,
  128. size_t len ) {
  129. char filter[55]; /* see format string */
  130. struct ether_header *ether_header = ( struct ether_header * ) buf;
  131. unsigned char *hwaddr = ether_header->ether_shost;
  132. if ( len < sizeof ( *ether_header ) )
  133. return -1;
  134. snprintf ( filter, sizeof ( filter ), "broadcast or multicast or "
  135. "ether host %02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0],
  136. hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5] );
  137. return hijack_install_filter ( hijack, filter );
  138. }
  139. /**
  140. * Set up filter for hijacked connection
  141. *
  142. */
  143. static int hijack_filter ( struct hijack *hijack, const char *buf,
  144. size_t len ) {
  145. switch ( hijack->datalink ) {
  146. case DLT_EN10MB:
  147. return hijack_filter_ethernet ( hijack, buf, len );
  148. default:
  149. logmsg ( LOG_ERR, "unsupported protocol %s: cannot filter\n",
  150. ( pcap_datalink_val_to_name ( hijack->datalink ) ?
  151. pcap_datalink_val_to_name ( hijack->datalink ) :
  152. "UNKNOWN" ) );
  153. /* Return success so we don't get called again */
  154. return 0;
  155. }
  156. }
  157. /**
  158. * Forward data from hijacker
  159. *
  160. */
  161. static ssize_t forward_from_hijacker ( struct hijack *hijack, int fd ) {
  162. char buf[SNAPLEN];
  163. ssize_t len;
  164. /* Read packet from hijacker */
  165. len = read ( fd, buf, sizeof ( buf ) );
  166. if ( len < 0 ) {
  167. logmsg ( LOG_ERR, "read from hijacker failed: %s\n",
  168. strerror ( errno ) );
  169. return -1;
  170. }
  171. if ( len == 0 )
  172. return 0;
  173. /* Set up filter if not already in place */
  174. if ( ! hijack->filtered ) {
  175. if ( hijack_filter ( hijack, buf, len ) == 0 )
  176. hijack->filtered = 1;
  177. }
  178. /* Transmit packet to network */
  179. if ( pcap_inject ( hijack->pcap, buf, len ) != len ) {
  180. logmsg ( LOG_ERR, "write to hijacked port failed: %s\n",
  181. pcap_geterr ( hijack->pcap ) );
  182. return -1;
  183. }
  184. hijack->tx_count++;
  185. return len;
  186. };
  187. /**
  188. * Forward data to hijacker
  189. *
  190. */
  191. static ssize_t forward_to_hijacker ( int fd, struct hijack *hijack ) {
  192. struct pcap_pkthdr *pkt_header;
  193. const unsigned char *pkt_data;
  194. ssize_t len;
  195. /* Receive packet from network */
  196. if ( pcap_next_ex ( hijack->pcap, &pkt_header, &pkt_data ) < 0 ) {
  197. logmsg ( LOG_ERR, "read from hijacked port failed: %s\n",
  198. pcap_geterr ( hijack->pcap ) );
  199. return -1;
  200. }
  201. if ( pkt_header->caplen != pkt_header->len ) {
  202. logmsg ( LOG_ERR, "read partial packet (%d of %d bytes)\n",
  203. pkt_header->caplen, pkt_header->len );
  204. return -1;
  205. }
  206. if ( pkt_header->caplen == 0 )
  207. return 0;
  208. len = pkt_header->caplen;
  209. /* Write packet to hijacker */
  210. if ( write ( fd, pkt_data, len ) != len ) {
  211. logmsg ( LOG_ERR, "write to hijacker failed: %s\n",
  212. strerror ( errno ) );
  213. return -1;
  214. }
  215. hijack->rx_count++;
  216. return len;
  217. };
  218. /**
  219. * Run hijacker
  220. *
  221. */
  222. static int run_hijacker ( const char *interface, int fd ) {
  223. struct hijack hijack;
  224. fd_set fdset;
  225. int max_fd;
  226. ssize_t len;
  227. logmsg ( LOG_INFO, "new connection for %s\n", interface );
  228. /* Open connection to network */
  229. memset ( &hijack, 0, sizeof ( hijack ) );
  230. if ( hijack_open ( interface, &hijack ) < 0 )
  231. goto err;
  232. /* Do the forwarding */
  233. max_fd = ( ( fd > hijack.fd ) ? fd : hijack.fd );
  234. while ( 1 ) {
  235. /* Wait for available data */
  236. FD_ZERO ( &fdset );
  237. FD_SET ( fd, &fdset );
  238. FD_SET ( hijack.fd, &fdset );
  239. if ( select ( ( max_fd + 1 ), &fdset, NULL, NULL, 0 ) < 0 ) {
  240. logmsg ( LOG_ERR, "select failed: %s\n",
  241. strerror ( errno ) );
  242. goto err;
  243. }
  244. if ( FD_ISSET ( fd, &fdset ) ) {
  245. len = forward_from_hijacker ( &hijack, fd );
  246. if ( len < 0 )
  247. goto err;
  248. if ( len == 0 )
  249. break;
  250. }
  251. if ( FD_ISSET ( hijack.fd, &fdset ) ) {
  252. len = forward_to_hijacker ( fd, &hijack );
  253. if ( len < 0 )
  254. goto err;
  255. if ( len == 0 )
  256. break;
  257. }
  258. }
  259. hijack_close ( &hijack );
  260. logmsg ( LOG_INFO, "closed connection for %s\n", interface );
  261. logmsg ( LOG_INFO, "received %ld packets, sent %ld packets\n",
  262. hijack.rx_count, hijack.tx_count );
  263. return 0;
  264. err:
  265. if ( hijack.pcap )
  266. hijack_close ( &hijack );
  267. return -1;
  268. }
  269. /**
  270. * Open listener socket
  271. *
  272. */
  273. static int open_listener ( const char *interface,
  274. struct hijack_listener *listener ) {
  275. /* Create socket */
  276. listener->fd = socket ( PF_UNIX, SOCK_SEQPACKET, 0 );
  277. if ( listener->fd < 0 ) {
  278. logmsg ( LOG_ERR, "Could not create socket: %s\n",
  279. strerror ( errno ) );
  280. goto err;
  281. }
  282. /* Bind to local filename */
  283. listener->sun.sun_family = AF_UNIX,
  284. snprintf ( listener->sun.sun_path, sizeof ( listener->sun.sun_path ),
  285. "/var/run/hijack-%s", interface );
  286. if ( bind ( listener->fd, ( struct sockaddr * ) &listener->sun,
  287. sizeof ( listener->sun ) ) < 0 ) {
  288. logmsg ( LOG_ERR, "Could not bind socket to %s: %s\n",
  289. listener->sun.sun_path, strerror ( errno ) );
  290. goto err;
  291. }
  292. /* Set as a listening socket */
  293. if ( listen ( listener->fd, 0 ) < 0 ) {
  294. logmsg ( LOG_ERR, "Could not listen to %s: %s\n",
  295. listener->sun.sun_path, strerror ( errno ) );
  296. goto err;
  297. }
  298. return 0;
  299. err:
  300. if ( listener->fd >= 0 )
  301. close ( listener->fd );
  302. return -1;
  303. }
  304. /**
  305. * Listen on listener socket
  306. *
  307. */
  308. static int listen_for_hijackers ( struct hijack_listener *listener,
  309. const char *interface ) {
  310. int fd;
  311. pid_t child;
  312. int rc;
  313. logmsg ( LOG_INFO, "Listening on %s\n", listener->sun.sun_path );
  314. while ( 1 ) {
  315. /* Accept new connection */
  316. fd = accept ( listener->fd, NULL, 0 );
  317. if ( fd < 0 ) {
  318. logmsg ( LOG_ERR, "accept failed: %s\n",
  319. strerror ( errno ) );
  320. goto err;
  321. }
  322. /* Fork child process */
  323. child = fork();
  324. if ( child < 0 ) {
  325. logmsg ( LOG_ERR, "fork failed: %s\n",
  326. strerror ( errno ) );
  327. goto err;
  328. }
  329. if ( child == 0 ) {
  330. /* I am the child; run the hijacker */
  331. rc = run_hijacker ( interface, fd );
  332. close ( fd );
  333. exit ( rc );
  334. }
  335. close ( fd );
  336. }
  337. return 0;
  338. err:
  339. if ( fd >= 0 )
  340. close ( fd );
  341. return -1;
  342. }
  343. /**
  344. * Close listener socket
  345. *
  346. */
  347. static void close_listener ( struct hijack_listener *listener ) {
  348. close ( listener->fd );
  349. unlink ( listener->sun.sun_path );
  350. }
  351. /**
  352. * Print usage
  353. *
  354. */
  355. static void usage ( char **argv ) {
  356. logmsg ( LOG_ERR,
  357. "Usage: %s [options]\n"
  358. "\n"
  359. "Options:\n"
  360. " -h|--help Print this help message\n"
  361. " -i|--interface intf Use specified network interface\n"
  362. " -n|--nodaemon Run in foreground\n",
  363. argv[0] );
  364. }
  365. /**
  366. * Parse command-line options
  367. *
  368. */
  369. static int parse_options ( int argc, char **argv,
  370. struct hijack_options *options ) {
  371. static struct option long_options[] = {
  372. { "interface", 1, NULL, 'i' },
  373. { "nodaemon", 0, NULL, 'n' },
  374. { "help", 0, NULL, 'h' },
  375. { },
  376. };
  377. int c;
  378. /* Set default options */
  379. memset ( options, 0, sizeof ( *options ) );
  380. strncpy ( options->interface, "eth0", sizeof ( options->interface ) );
  381. options->daemonise = 1;
  382. /* Parse command-line options */
  383. while ( 1 ) {
  384. int option_index = 0;
  385. c = getopt_long ( argc, argv, "i:hn", long_options,
  386. &option_index );
  387. if ( c < 0 )
  388. break;
  389. switch ( c ) {
  390. case 'i':
  391. strncpy ( options->interface, optarg,
  392. sizeof ( options->interface ) );
  393. break;
  394. case 'n':
  395. options->daemonise = 0;
  396. break;
  397. case 'h':
  398. usage( argv );
  399. return -1;
  400. case '?':
  401. /* Unrecognised option */
  402. return -1;
  403. default:
  404. logmsg ( LOG_ERR, "Unrecognised option '-%c'\n", c );
  405. return -1;
  406. }
  407. }
  408. /* Check there's nothing left over on the command line */
  409. if ( optind != argc ) {
  410. usage ( argv );
  411. return -1;
  412. }
  413. return 0;
  414. }
  415. /**
  416. * Daemonise
  417. *
  418. */
  419. static int daemonise ( const char *interface ) {
  420. char pidfile[16 + IF_NAMESIZE + 4]; /* "/var/run/hijack-<intf>.pid" */
  421. char pid[16];
  422. int pidlen;
  423. int fd = -1;
  424. /* Daemonise */
  425. if ( daemon ( 0, 0 ) < 0 ) {
  426. logmsg ( LOG_ERR, "Could not daemonise: %s\n",
  427. strerror ( errno ) );
  428. goto err;
  429. }
  430. daemonised = 1; /* Direct messages to syslog now */
  431. /* Open pid file */
  432. snprintf ( pidfile, sizeof ( pidfile ), "/var/run/hijack-%s.pid",
  433. interface );
  434. fd = open ( pidfile, ( O_WRONLY | O_CREAT | O_TRUNC ),
  435. ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) );
  436. if ( fd < 0 ) {
  437. logmsg ( LOG_ERR, "Could not open %s for writing: %s\n",
  438. pidfile, strerror ( errno ) );
  439. goto err;
  440. }
  441. /* Write pid to file */
  442. pidlen = snprintf ( pid, sizeof ( pid ), "%d\n", getpid() );
  443. if ( write ( fd, pid, pidlen ) != pidlen ) {
  444. logmsg ( LOG_ERR, "Could not write %s: %s\n",
  445. pidfile, strerror ( errno ) );
  446. goto err;
  447. }
  448. close ( fd );
  449. return 0;
  450. err:
  451. if ( fd >= 0 )
  452. close ( fd );
  453. return -1;
  454. }
  455. int main ( int argc, char **argv ) {
  456. struct hijack_options options;
  457. struct hijack_listener listener;
  458. struct sigaction sigchld;
  459. /* Parse command-line options */
  460. if ( parse_options ( argc, argv, &options ) < 0 )
  461. exit ( 1 );
  462. /* Set up syslog connection */
  463. openlog ( basename ( argv[0] ), LOG_PID, LOG_DAEMON );
  464. /* Set up listening socket */
  465. if ( open_listener ( options.interface, &listener ) < 0 )
  466. exit ( 1 );
  467. /* Daemonise on demand */
  468. if ( options.daemonise ) {
  469. if ( daemonise ( options.interface ) < 0 )
  470. exit ( 1 );
  471. }
  472. /* Avoid creating zombies */
  473. memset ( &sigchld, 0, sizeof ( sigchld ) );
  474. sigchld.sa_handler = SIG_IGN;
  475. sigchld.sa_flags = SA_NOCLDWAIT;
  476. if ( sigaction ( SIGCHLD, &sigchld, NULL ) < 0 ) {
  477. logmsg ( LOG_ERR, "Could not set signal handler: %s",
  478. strerror ( errno ) );
  479. exit ( 1 );
  480. }
  481. /* Listen for hijackers */
  482. if ( listen_for_hijackers ( &listener, options.interface ) < 0 )
  483. exit ( 1 );
  484. close_listener ( &listener );
  485. return 0;
  486. }