/*
   
   (C) BrainStorm - [ElectronicSouls] 

   i bet you all know what this is,
   or how to use it..if not go shoot yourselfe..
   made for private use..may be distributed
   with my permission.

   ..its just like 0x90 always says :
   "the louder the music, the better the code"

   yea..i know the code looks kinda fucked up..
   but who gives a fuck as long as it worx perfect ;)
 
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/file.h>
            
#define MAXPORT 65000
#define STARTPORT 1
#define COLOR "\E[32m"
#define WHITE "\E[m"
#define COLORS "\E[33m"
#define GR "\E[32m"
#define RD "\E[31m"

void checkport(char *host);

main(int argc, char *argv[])
{
int sockfd, port;
struct sockaddr_in sin;
struct hostent *h;
struct servent *s;
int x, y;
int secport;
char b[100];
char buffer[100];
struct sockaddr_in sins;
int socks;
if(argc < 2) 
{
fprintf(stderr, GR    " [ElectronicSouls] Simple Port Scan \n");
fprintf(stderr, GR    " +++++++++++++++++++ (C) BrainStorm \n\n");
fprintf(stderr, WHITE " +++ Usage: %s -h <host> [start-port] [end-port]\n", argv[0]);
exit(1);
}
if(strcmp(argv[1], "-h") == 0) 
{
printf(COLOR "\n  help for a simple tcp port scanner?!@*\n\n");
exit(0);
}
if((h = gethostbyname(argv[1])) == NULL) 
{
herror(argv[1]);
exit(1);
}
if(argc > 2)
x = atoi(argv[2]);
else
x = STARTPORT;
if(argc > 3)
y = atoi(argv[3]);
else
y = MAXPORT;
printf(GR    "\n\n[ElectronicSouls] Simple Port Scan \n");
printf(COLOR "*** Open Ports on %s:\n", argv[1]);
printf(RD    "*** Scanning...\n\n");
sin.sin_family = AF_INET;
sin.sin_addr = *((struct in_addr *)h->h_addr);
printf(GR "Port			Protocol		  Service\n");                
printf(WHITE "**************************************************************\n");              
for(port = x; port <= y; port++) 
{                   
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_port = htons(port);
s = getservbyport(htons(port), "tcp");
                                            
if(connect(sockfd, (struct sockaddr *)&sin, sizeof(sin)) == 0)
                             
printf("%i			%s  			    %s\n", port, (s == NULL) ? "NULL" : s->s_proto, (s == NULL) ? "UNKNOWN" : s->s_name);      
close(sockfd);
}              
close(sockfd);
printf("**************************************************************\n");                 
checkport(argv[1]);             
close(sockfd);                             

return 0;
}
void checkport(char *host) 
{                      
char buff[100];
char buf[100];
int port;
int port2;
int sock;

struct sockaddr_in newsin;
struct hostent *hostent;
                        
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
{
perror("--- socket failed!");
exit(1);
}
if((hostent = gethostbyname(host)) == NULL) 
{
herror(host);
exit(1);
}
printf("- Would you like more information?\n");
scanf("%s", &buff);
if(strstr(buff, "y")) 
{
printf("- What Port?\n");
scanf("%d", &port);

newsin.sin_family = AF_INET;
newsin.sin_addr = *((struct in_addr *)hostent->h_addr);
newsin.sin_port = htons(port);

if(connect(sock, (struct sockaddr *)&newsin, sizeof(newsin)) == 0) 
{
while(read(sock, buf, sizeof(buf)) > 0) 
{
printf(COLOR "%s\n", buf);      
printf("------------------------------------------------------");
printf("------------------------------------------------------");
printf(WHITE "       [ S c a n - D o n e.] \n");
exit(0);
}
exit(0);
}
 else 
{
printf("+++ Connection Failed ! +++\n");
exit(0);
}
}
}


