/* Sockcheck.c By BigDawg dawg@nuthin.nu - [ http://www.rootshell.com/ ]
 * Use: put a list of ips (one per line) in ips.in then run sockcheck. 
 * The list of unsecure socks servers will be saved to ips.out
 * Compile: gcc sockcheck.c -o sockcheck
 * 
 * [20:01] <Skeeter> put my name in the source ;) 
 * 
 * I'd like to say thanks to all who have helped me throughout the past years.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include <ctype.h>
#include <arpa/inet.h>
extern int hexstr;
static int sockfd2;
int toscan = 0;
int gatenum = 0;
#define DEF_FILE "ips.in"
#define OUT_FILE "ips.out"
#define socktimeout 10
#define longtime 3000

int sigalrm_handler3(int sig)
{
    close(sockfd2);
   return;
}

int main()
{
   
    FILE *in;
        FILE *server_file;
        int i;
         time_t  ct;
        char *sockfile;
        char sockip[1024];
        sockfile = DEF_FILE;  
     printf("========================\n");
     printf("sockcheck 0.3 by BigDawg\n");
     printf("========================\n");
      in = fopen(sockfile,"r"); 
     while(fgets(sockip,80,in))
     {
          if(sockip[0] == '\0')
{
                fclose(in);
                exit(1);
}
     sockip[strlen(sockip) -1] = '\0';
     toscan++;
}

fclose(in);
     printf("Loaded %i ips to scan\n",toscan);
     printf("\n");
     in = fopen(sockfile,"r");
     while(fgets(sockip,80,in))
     {
          if(sockip[0] == '\0')
{
                fclose(in);
                exit(1);
}
          sockip[strlen(sockip) -1] = '\0';
      signal(SIGALRM, sigalrm_handler3);
      alarm(socktimeout);
     toscan--;
     printf("Scanning %s (%i more to go)\r",sockip,toscan);
      fflush(stdout);
    printf("                                                                  \r");
    sockcheck(sockip);
     signal(SIGALRM, sigalrm_handler3);
    alarm(longtime);
}
printf("Done checking!\n");
printf("%i unsecure socks servers found\n",gatenum);
fclose(in);
exit(1);
}

int sockcheck(char *host)
{
        FILE *ips;
        int s = 1000;
        int d;
        int r;
        int e;
        int f;
        int i = 0;
        int c;
        char keystring[] = {0x04, 0x01, 0x1A, 0x0B, 0xC7,
                      0x02, 0xD2, 0xF1, 0x62, 0x6C,
                      0x61, 0x68, 0x74, 0x65, 0x73, 0x74, 0x00};
        char hexstr[20];
        char tmpstr2[20] = "\0\0\0\0\0\0";
        char buf[20];
        fd_set gateset;
        struct timeval tv;
        struct in_addr MyHostAddr;
        struct hostent *he;
        struct sockaddr_in sin;
        sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
        sin.sin_family = AF_INET;
        sin.sin_port = htons(1080);
        sin.sin_addr.s_addr = inet_addr(host);
        if(sin.sin_addr.s_addr == INADDR_NONE)
        {
                he = gethostbyname(host);
                if(!he)
                {
                        close(sockfd2);
                        return;
                }
                memcpy(&sin.sin_addr, he->h_addr, he->h_length);
         }
        signal(SIGALRM, sigalrm_handler3);
        alarm(socktimeout);
        e = connect(sockfd2, (struct sockaddr *)&sin, sizeof(sin));
  	if (e < 0)
  	{
        	close(sockfd2);
        	return;
  	}
	 signal(SIGALRM, sigalrm_handler3);   
  	alarm(longtime);
  	FD_ZERO(&gateset);
  	FD_SET(sockfd2, &gateset);
  	tv.tv_sec = 10;
  	tv.tv_usec = 0;  
  	d = select(sockfd2+1, NULL, &gateset, NULL, &tv); 
   	if(d == 0)
   	{
     		close(sockfd2);
	        return;
   	}
        send(sockfd2, keystring, 17, 0);
  	tv.tv_sec = 10;
        tv.tv_usec = 0;
        f = select(sockfd2+2, &gateset, NULL, NULL, &tv);
        if (f)   
	{
        read(sockfd2, tmpstr2, 10);
        strcpy(hexstr, "HEX:");
           sprintf(buf, "%d ", tmpstr2[i]);
           strcat(hexstr, buf);
        if((tmpstr2[0] == 0x00)&&(tmpstr2[1] == 0x5a)&&(tmpstr2[2] == 0x00))
        {
        	close(sockfd2);
		              gatenum++;
                printf("Unsecure socks server found on %s (server #%i) (%i left to scan)\n",host,gatenum,toscan);
                ips = fopen(OUT_FILE,"a");
                fputs(host, ips);
                fputs("\n", ips);
                fclose(ips);
          	return;
        }
close(sockfd2);
return;
}
}            
