Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** ssh_detect.c
- */
- #include <arpa/inet.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <unistd.h>
- #include <stdarg.h>
- #define MAX_SOCKETS 1000
- #define TIMEOUT 4
- #define S_NONE 0
- #define S_CONNECTING 1
- #define S_READ 2
- struct conn_t {
- int s, n;
- unsigned char buffer[4096];
- char status;
- time_t a;
- struct sockaddr_in addr;
- };
- struct conn_t connlist[MAX_SOCKETS];
- void init_sockets(void);
- void check_sockets(void);
- void fatal(char *);
- FILE *outfd;
- int tot = 0;
- int tt = 0; // new
- int Send(int sock, char *words, ...) {
- static char textBuffer[1024];
- va_list args;
- va_start(args, words);
- vsprintf(textBuffer, words, args);
- va_end(args);
- return write(sock,textBuffer,strlen(textBuffer));
- }
- int main(int argc, char *argv[])
- {
- int done = 0, i, cip = 1, bb = 0, ret, k, ns, x;
- time_t scantime;
- char ip[20], outfile[128], last[256];
- if (argc < 3)
- {
- printf("Usage: %s <b-block> <port> [c-block]\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- memset(&outfile, 0, sizeof(outfile));
- if (argc == 3)
- snprintf(outfile, sizeof(outfile) - 1, "%s.drupal.%s", argv[1], argv[2]);
- else if (argc >= 4)
- {
- snprintf(outfile, sizeof(outfile) - 1, "%s.%s.drupal.%s", argv[1], argv[3], argv[2]);
- bb = atoi(argv[3]);
- if ((bb < 0) || (bb > 255))
- fatal("Invalid b-range.\n");
- }
- if (!(outfd = fopen(outfile, "a")))
- {
- perror(outfile);
- exit(EXIT_FAILURE);
- }
- printf("[ sockets -> %d ] [ timeout -> %ds ][ output -> %s ]\n"
- "Currently scanning: ", MAX_SOCKETS, TIMEOUT, outfile, argv[1]);
- fflush(stdout);
- memset(&last, 0, sizeof(last));
- init_sockets();
- scantime = time(0);
- while(!done)
- {
- //usleep(1);
- for (i = 0; i < MAX_SOCKETS; i++)
- {
- usleep(1);
- if (cip == 255)
- {
- if ((bb == 255) || (argc >= 4))
- {
- ns = 0;
- for (k = 0; k < MAX_SOCKETS; k++)
- {
- if (connlist[k].status > S_NONE)
- {
- ns++;
- break;
- }
- }
- if (ns == 0)
- done = 1;
- break;
- }
- else
- {
- cip = 0;
- bb++;
- for (x = 0; x < strlen(last); x++)
- putchar('\b');
- memset(&last, 0, sizeof(last));
- snprintf(last, sizeof(last) - 1, "%s.%d.* (total: %d) (%.1f%% done)",
- argv[1], bb, tot, (bb / 255.0) * 100);
- printf("%s", last);
- fflush(stdout);
- }
- }
- if (connlist[i].status == S_NONE)
- {
- connlist[i].s = socket(AF_INET, SOCK_STREAM, 0);
- if (connlist[i].s == -1)
- printf("Unable to allocate socket.\n");
- else
- {
- ret = fcntl(connlist[i].s, F_SETFL, O_NONBLOCK);
- if (ret == -1)
- {
- printf("Unable to set O_NONBLOCK\n");
- close(connlist[i].s);
- }
- else
- {
- memset(&ip, 0, 20);
- sprintf(ip, "%s.%d.%d", argv[1], bb, cip);
- connlist[i].addr.sin_addr.s_addr = inet_addr(ip);
- if (connlist[i].addr.sin_addr.s_addr == -1)
- fatal("Invalid IP.");
- connlist[i].addr.sin_family = AF_INET;
- connlist[i].addr.sin_port = htons(atoi(argv[2]));
- connlist[i].a = time(0);
- connlist[i].status = S_CONNECTING;
- cip++;
- }
- }
- }
- }
- check_sockets();
- }
- printf("\nDscan completed in %u seconds. (found %d ips)\n", (time(0) - scantime), tot);
- fclose(outfd);
- exit(EXIT_SUCCESS);
- }
- void init_sockets(void)
- {
- int i;
- for (i = 0; i < MAX_SOCKETS; i++)
- {
- connlist[i].status = S_NONE;
- memset((struct sockaddr_in *)&connlist[i].addr, 0, sizeof(struct sockaddr_in));
- }
- return;
- }
- void check_sockets(void)
- {
- int i, ret, v;
- char temp[19];
- for (i = 0; i < MAX_SOCKETS; i++)
- {
- usleep(1);
- if ((connlist[i].a < (time(0) - TIMEOUT)) && (connlist[i].status == S_CONNECTING || connlist[i].status == S_READ))
- {
- close(connlist[i].s);
- connlist[i].status = S_NONE;
- }
- else if (connlist[i].status == S_CONNECTING)
- {
- ret = connect(connlist[i].s, (struct sockaddr *)&connlist[i].addr,
- sizeof(struct sockaddr_in));
- if (ret == -1)
- {
- if (errno == EISCONN)
- {
- 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));
- if (connlist[i].n < 0)
- error("ERROR writing to socket");
- // connlist[i].a = time(0);
- connlist[i].status = S_READ;
- }
- if ((errno != EALREADY) && (errno != EINPROGRESS))
- {
- close(connlist[i].s);
- connlist[i].status = S_NONE;
- }
- }
- else
- {
- 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));
- if (connlist[i].n < 0)
- error("ERROR writing to socket");
- // connlist[i].a = time(0);
- connlist[i].status = S_READ;
- }
- }
- if (connlist[i].status == S_READ)
- {
- int n;
- if ((n = recv(connlist[i].s, connlist[i].buffer, 1024, 0)) > 0)
- {
- if (n <= 0)
- break;
- else {
- connlist[i].buffer[n] = '\0'; // NEW
- // printf("%s", connlist[i].buffer);
- /*
- for (v=0;v<strlen(connlist[i].buffer) && connlist[i].buffer[v] != ' ';v++);
- strncpy(temp, connlist[i].buffer+v+1, 3);
- temp[3] = '\0';
- */
- // if (strcmp(temp, "200") == 0) {
- if(strstr(connlist[i].buffer, "Server: CouchDB") != NULL) {
- tot++;
- fprintf(outfd, "%s\n",
- (char *)inet_ntoa(connlist[i].addr.sin_addr));
- // printf("%s\n", (char *)inet_ntoa(connlist[i].addr.sin_addr));
- }
- // }
- close(connlist[i].s);
- connlist[i].status = S_NONE;
- }
- }
- }
- }
- }
- void fatal(char *err)
- {
- int i;
- printf("Error: %s\n", err);
- for (i = 0; i < MAX_SOCKETS; i++)
- if (connlist[i].status >= S_CONNECTING)
- close(connlist[i].s);
- fclose(outfd);
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement