Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.87 KB | None | 0 0
  1. /*
  2. ** ssh_detect.c
  3. */
  4.  
  5. #include <arpa/inet.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <fcntl.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17.  
  18.  
  19. #define MAX_SOCKETS 1000
  20. #define TIMEOUT 4
  21.  
  22. #define S_NONE 0
  23. #define S_CONNECTING 1
  24. #define S_READ 2
  25.  
  26.  
  27. struct conn_t {
  28. int s, n;
  29. unsigned char buffer[4096];
  30. char status;
  31. time_t a;
  32. struct sockaddr_in addr;
  33. };
  34. struct conn_t connlist[MAX_SOCKETS];
  35.  
  36. void init_sockets(void);
  37. void check_sockets(void);
  38. void fatal(char *);
  39.  
  40. FILE *outfd;
  41. int tot = 0;
  42.  
  43. int tt = 0; // new
  44.  
  45. int Send(int sock, char *words, ...) {
  46. static char textBuffer[1024];
  47. va_list args;
  48. va_start(args, words);
  49. vsprintf(textBuffer, words, args);
  50. va_end(args);
  51. return write(sock,textBuffer,strlen(textBuffer));
  52. }
  53.  
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. int done = 0, i, cip = 1, bb = 0, ret, k, ns, x;
  58. time_t scantime;
  59. char ip[20], outfile[128], last[256];
  60.  
  61. if (argc < 3)
  62. {
  63. printf("Usage: %s <b-block> <port> [c-block]\n", argv[0]);
  64. exit(EXIT_FAILURE);
  65. }
  66.  
  67. memset(&outfile, 0, sizeof(outfile));
  68. if (argc == 3)
  69. snprintf(outfile, sizeof(outfile) - 1, "%s.drupal.%s", argv[1], argv[2]);
  70. else if (argc >= 4)
  71. {
  72. snprintf(outfile, sizeof(outfile) - 1, "%s.%s.drupal.%s", argv[1], argv[3], argv[2]);
  73. bb = atoi(argv[3]);
  74. if ((bb < 0) || (bb > 255))
  75. fatal("Invalid b-range.\n");
  76. }
  77.  
  78. if (!(outfd = fopen(outfile, "a")))
  79. {
  80. perror(outfile);
  81. exit(EXIT_FAILURE);
  82. }
  83. printf("[ sockets -> %d ] [ timeout -> %ds ][ output -> %s ]\n"
  84. "Currently scanning: ", MAX_SOCKETS, TIMEOUT, outfile, argv[1]);
  85. fflush(stdout);
  86.  
  87. memset(&last, 0, sizeof(last));
  88. init_sockets();
  89. scantime = time(0);
  90.  
  91. while(!done)
  92. {
  93. //usleep(1);
  94. for (i = 0; i < MAX_SOCKETS; i++)
  95. {
  96. usleep(1);
  97. if (cip == 255)
  98. {
  99. if ((bb == 255) || (argc >= 4))
  100. {
  101. ns = 0;
  102. for (k = 0; k < MAX_SOCKETS; k++)
  103. {
  104. if (connlist[k].status > S_NONE)
  105. {
  106. ns++;
  107. break;
  108. }
  109. }
  110.  
  111. if (ns == 0)
  112. done = 1;
  113.  
  114. break;
  115. }
  116. else
  117. {
  118. cip = 0;
  119. bb++;
  120. for (x = 0; x < strlen(last); x++)
  121. putchar('\b');
  122. memset(&last, 0, sizeof(last));
  123. snprintf(last, sizeof(last) - 1, "%s.%d.* (total: %d) (%.1f%% done)",
  124. argv[1], bb, tot, (bb / 255.0) * 100);
  125. printf("%s", last);
  126. fflush(stdout);
  127. }
  128. }
  129.  
  130. if (connlist[i].status == S_NONE)
  131. {
  132. connlist[i].s = socket(AF_INET, SOCK_STREAM, 0);
  133. if (connlist[i].s == -1)
  134. printf("Unable to allocate socket.\n");
  135. else
  136. {
  137. ret = fcntl(connlist[i].s, F_SETFL, O_NONBLOCK);
  138. if (ret == -1)
  139. {
  140. printf("Unable to set O_NONBLOCK\n");
  141. close(connlist[i].s);
  142. }
  143. else
  144. {
  145. memset(&ip, 0, 20);
  146. sprintf(ip, "%s.%d.%d", argv[1], bb, cip);
  147. connlist[i].addr.sin_addr.s_addr = inet_addr(ip);
  148. if (connlist[i].addr.sin_addr.s_addr == -1)
  149. fatal("Invalid IP.");
  150. connlist[i].addr.sin_family = AF_INET;
  151. connlist[i].addr.sin_port = htons(atoi(argv[2]));
  152. connlist[i].a = time(0);
  153. connlist[i].status = S_CONNECTING;
  154. cip++;
  155. }
  156. }
  157. }
  158. }
  159. check_sockets();
  160. }
  161.  
  162. printf("\nDscan completed in %u seconds. (found %d ips)\n", (time(0) - scantime), tot);
  163. fclose(outfd);
  164. exit(EXIT_SUCCESS);
  165. }
  166.  
  167. void init_sockets(void)
  168. {
  169. int i;
  170. for (i = 0; i < MAX_SOCKETS; i++)
  171. {
  172. connlist[i].status = S_NONE;
  173. memset((struct sockaddr_in *)&connlist[i].addr, 0, sizeof(struct sockaddr_in));
  174. }
  175. return;
  176. }
  177.  
  178. void check_sockets(void)
  179. {
  180. int i, ret, v;
  181. char temp[19];
  182.  
  183. for (i = 0; i < MAX_SOCKETS; i++)
  184. {
  185. usleep(1);
  186. if ((connlist[i].a < (time(0) - TIMEOUT)) && (connlist[i].status == S_CONNECTING || connlist[i].status == S_READ))
  187. {
  188. close(connlist[i].s);
  189. connlist[i].status = S_NONE;
  190. }
  191. else if (connlist[i].status == S_CONNECTING)
  192. {
  193. ret = connect(connlist[i].s, (struct sockaddr *)&connlist[i].addr,
  194. sizeof(struct sockaddr_in));
  195. if (ret == -1)
  196. {
  197. if (errno == EISCONN)
  198. {
  199.  
  200. connlist[i].n = Send(connlist[i].s,"GET / HTTP/1.1\r\nHOST: %s\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Ubuntu/10.10 Chromium/8.0.552.237 Chrome/8.0.552.237 Safari/534.10\r\nAccept:*/*\r\nConnection: close\r\n\r\n", (char *)inet_ntoa(connlist[i].addr.sin_addr));
  201.  
  202.  
  203. if (connlist[i].n < 0)
  204. error("ERROR writing to socket");
  205.  
  206. // connlist[i].a = time(0);
  207. connlist[i].status = S_READ;
  208. }
  209.  
  210. if ((errno != EALREADY) && (errno != EINPROGRESS))
  211. {
  212. close(connlist[i].s);
  213. connlist[i].status = S_NONE;
  214. }
  215.  
  216. }
  217. else
  218. {
  219.  
  220. connlist[i].n = Send(connlist[i].s,"GET / HTTP/1.1\r\nHOST: %s\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Ubuntu/10.10 Chromium/8.0.552.237 Chrome/8.0.552.237 Safari/534.10\r\nAccept:*/*\r\nConnection: close\r\n\r\n", (char *)inet_ntoa(connlist[i].addr.sin_addr));
  221.  
  222. if (connlist[i].n < 0)
  223. error("ERROR writing to socket");
  224.  
  225.  
  226. // connlist[i].a = time(0);
  227. connlist[i].status = S_READ;
  228. }
  229. }
  230.  
  231.  
  232. if (connlist[i].status == S_READ)
  233. {
  234.  
  235. int n;
  236.  
  237. if ((n = recv(connlist[i].s, connlist[i].buffer, 1024, 0)) > 0)
  238. {
  239. if (n <= 0)
  240. break;
  241. else {
  242. connlist[i].buffer[n] = '\0'; // NEW
  243. // printf("%s", connlist[i].buffer);
  244.  
  245.  
  246. /*
  247. for (v=0;v<strlen(connlist[i].buffer) && connlist[i].buffer[v] != ' ';v++);
  248. strncpy(temp, connlist[i].buffer+v+1, 3);
  249. temp[3] = '\0';
  250. */
  251. // if (strcmp(temp, "200") == 0) {
  252.  
  253. if(strstr(connlist[i].buffer, "Server: CouchDB") != NULL) {
  254. tot++;
  255.  
  256.  
  257. fprintf(outfd, "%s\n",
  258. (char *)inet_ntoa(connlist[i].addr.sin_addr));
  259.  
  260.  
  261. // printf("%s\n", (char *)inet_ntoa(connlist[i].addr.sin_addr));
  262. }
  263. // }
  264.  
  265.  
  266. close(connlist[i].s);
  267. connlist[i].status = S_NONE;
  268.  
  269. }
  270. }
  271.  
  272. }
  273. }
  274. }
  275.  
  276. void fatal(char *err)
  277. {
  278. int i;
  279. printf("Error: %s\n", err);
  280. for (i = 0; i < MAX_SOCKETS; i++)
  281. if (connlist[i].status >= S_CONNECTING)
  282. close(connlist[i].s);
  283. fclose(outfd);
  284. exit(EXIT_FAILURE);
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement