Perdona la tardanza... Tienes ante ti el hK 2.0, ni más ni menos jeje...
¿Y que estabas usando? Claro que tienes que usar sockets!
¿Y que estabas usando? Claro que tienes que usar sockets!
Código (c) [Seleccionar]
#include <time.h> //Hora
#include <stdio.h> //Funciones basicas entrada/salida
#include <string.h> //StrCmp
#include <stdlib.h> //Exit y otras
#include <arpa/inet.h> //struct sockaddr_in
#include <sys/socket.h> //Socket, Connect...
struct sockaddr_in host, client; //Declaraciones
int a=sizeof (struct sockaddr);
char buffer [1024];
int cont = 2;
int newsock;
int sockfd;
int k = 0;
int listens (int port); //Funciones
int conect (char *IP, int port);
int scan (char *IP);
int times () //Esta funcion escribira la fecha en pantalla...
{
time_t now=time (0);
struct tm *ahora;
char buffer [40];
ahora= (struct tm*)localtime ((const time_t*)&now);
strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
printf ("%s ", buffer);
return 0;
}
int finalizar (int state) //Esta funcion detendra el programa en caso de problemas...
{
close (sockfd);
if (state == 1)
{
printf ("Ocurrio un error en tiempo de ejecucion...\n");
exit (1);
}
else exit (0);
}
int help () //Mal uso de la linea de comandos...
{
printf ("Use:\thK <options> <target_ip // port> [port]\n");
printf ("Options:+d // +v [Daemon // Verbose]\n");
printf ("\t+l // +c // +z [Listen // Connect // Scan]\n\n");
printf ("\t./hK -l <port>\n");
printf ("\t./hK -c <target_ip> <port>\n");
printf ("\t./hK -z <target_ip>\n\n");
printf ("Exp:\t./hK dl 31337\n");
printf ("\t./hK vc 192.168.0.1 31337\n");
printf ("\t./hK vz 192.168.0.1\n\n");
exit (1);
}
int main (int argc, char *argv [])
{
time_t now=time (0);
struct tm *ahora;
char hora [40];
ahora=localtime ((const time_t*)&now);
strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
printf ("hK 2.0 - By Sagrini (2010) - %s\n", hora); //Esto escribira la presentacion.
if (argc < 3 || argc > 4) help (); //Diferentes opciones y mal uso de linea de comandos.
if (strlen (argv [1]) != 2) help ();
if (argv [1][0] != 'd' && argv [1][0]!= 'v') help ();
if (argv [1][1] != 'l' && argv [1][1]!= 'c' && argv [1][1]!= 'z') help ();
if (argv [1][1] == 'l' && argv [2] == NULL) help ();
if (argv [1][1] == 'c' && argv [3] == NULL) help ();
if (argv [1][1] == 'z' && argv [2] == NULL) help ();
if (argv [1][0] == 'd') daemon (1, 0);
if (argv [1][1] == 'l') listens (atoi (argv [2]));
if (argv [1][1] == 'c') conect (argv [2], atoi (argv [3]));
if (argv [1][1] == 'z') scan (argv [2]);
return 0;
}
int listens (int port) //Esta funcion esperara una conexion y printara todo lo que reciba...
{
if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
host.sin_port=htons(port);
host.sin_family=AF_INET;
host.sin_addr.s_addr=0;
memset (host.sin_zero, 0, 8);
if(bind(sockfd,(struct sockaddr*)&host,sizeof(host))==-1) finalizar (1);
if(listen(sockfd,5)==-1) finalizar (1);
if((newsock=accept(sockfd, (struct sockaddr*)&client, &a))==-1) finalizar (1);
times ();
printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
close (sockfd);
int ID = fork ();
if (ID != 0)
{
do
{
fgets (buffer, 1024, stdin);
cont=send (newsock, &buffer, strlen (buffer), 0);
}
while (cont>1);
close (newsock);
close (sockfd);
printf ("Finishing connection with %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
}
else
{
cont=recv (newsock, &buffer, 1024, 0);
while (cont>1)
{
buffer [cont-1]='\0';
printf ("RECV %d bytes: %s \n", cont, buffer);
cont=recv (newsock, &buffer, 1024, 0);
}
close (newsock);
close (sockfd);
}
finalizar (0);
}
int conect (char *IP, int port) //Se conecta a una IP y a su puerto determinado para mandar datos...
{
if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
host.sin_port=htons(port);
host.sin_family=AF_INET;
host.sin_addr.s_addr=inet_addr (IP);
memset (host.sin_zero, 0, 8);
if((connect (sockfd, (struct sockaddr*)&host, sizeof (host)))==-1) finalizar (1);
times ();
printf ("Got connection with %s:%d\n", inet_ntoa (host.sin_addr), ntohs (host.sin_port));
int ID = fork ();
if (ID != 0)
{
do
{
fgets (buffer, 1024, stdin);
cont=send (sockfd, &buffer, strlen (buffer), 0);
}
while (cont>1);
close (sockfd);
printf ("Finishing connection with %s:%d\n\n", inet_ntoa (host.sin_addr), ntohs (host.sin_port));
}
else
{
cont=recv (sockfd, &buffer, 1024, 0);
while (cont>1)
{
buffer [cont-1]='\0';
printf ("RECV %d bytes: %s \n", cont, buffer);
cont=recv (sockfd, &buffer, 1024, 0);
}
close (sockfd);
}
finalizar (0);
}
int scan (char *IP) //Sencillo escaner de puertos.
{
host.sin_addr.s_addr=inet_addr (IP);
host.sin_family=AF_INET;
memset (host.sin_zero, 0, 8);
for (cont = 0; cont < 65535; cont ++)
{
if ((sockfd=socket (2, 1, 0))==1) finalizar (1);
host.sin_port = htons (cont);
if (connect (sockfd, (struct sockaddr*)&host, sizeof (host)) != -1)
{
printf ("Port %d open\n", cont);
k++;
}
close (sockfd);
}
if (k == 0) printf ("All ports closed...\n");
else printf ("%d port(s) open(s)...\n", k);
finalizar (0);
}