Skip to content

Instantly share code, notes, and snippets.

@RicardoLara
Created May 31, 2016 01:46
Show Gist options
  • Select an option

  • Save RicardoLara/ff3c0fbc76ab70b424bafc47b848249d to your computer and use it in GitHub Desktop.

Select an option

Save RicardoLara/ff3c0fbc76ab70b424bafc47b848249d to your computer and use it in GitHub Desktop.
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <my_global.h>
#include <mysql.h>
int indice;
unsigned char trama[1514], tramaResp[1514]; // Declara Trama
unsigned char MACDestino[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; // Broadcast
unsigned char MACOrigen[6], IPOrigen[4];
unsigned char etherType[] = { 0x08, 0x06 }; // Define ARP
unsigned char arpData[] = { 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01 };
unsigned char arpTarget[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
unsigned char arpTargetIP[] = { 0xc0, 0xa8, 0x00, 0x61 };
unsigned char respIP[4], respMAC[6];
void enviarTrama(int ds, unsigned char * trama){
struct sockaddr_ll interfaz;
//Reseteamos la struct
memset(&interfaz, 0, sizeof(interfaz));
interfaz.sll_family = AF_PACKET;
interfaz.sll_protocol = htons(ETH_P_ALL);
interfaz.sll_ifindex = indice;
if (sendto(ds, trama, 60, 0, (struct sockaddr *)&interfaz, sizeof(interfaz)) < 0) printf("Error al enviar");
//else printf("\nExito al enviar\n");
}
void estrTrama(unsigned char * raw, unsigned char digit){
memcpy(raw + 0, MACDestino, 6);
memcpy(raw + 6, MACOrigen, 6);
memcpy(raw + 12, etherType, 2); // ARP
memcpy(raw + 14, arpData, 8);
memcpy(raw + 22, MACOrigen, 6);
memcpy(raw + 28, IPOrigen, 4);
memcpy(raw + 32, arpTarget, 6);
memcpy(arpTargetIP, IPOrigen, 4);
arpTargetIP[3] = digit;
memcpy(raw + 38, arpTargetIP, 4);
}
void imprimeDatos(){
int i;
printf("Indice: %d\n", indice);
printf("Dir MAC: ");
for (i = 0; i < 6; i++) {
printf("%.2x", MACOrigen[i]);
if (i != 5) printf(":");
}
printf("\nDir IP: ");
for (i = 0; i < 4; i++) {
printf("%d", IPOrigen[i]);
if (i != 3) printf(".");
}
printf("\n");
}
void obtenerDatos(int ds, struct ifreq net){
if (ioctl(ds, SIOCGIFINDEX, &net) < 0) perror("Error al obtener el indice");
else indice = net.ifr_ifindex;
if (ioctl(ds, SIOCGIFHWADDR, &net) < 0) perror("Error MAC");
else memcpy(MACOrigen, net.ifr_hwaddr.sa_data, 6);
if (ioctl(ds, SIOCGIFADDR, &net) < 0) perror("Error IP");
else memcpy(IPOrigen, net.ifr_hwaddr.sa_data + 2, 4);
}
int intento = 0;
int recibirTrama(int ds, unsigned char * tramaResp){
unsigned char server_reply[6000];
int i = 0, j = 0, success = 0;
ssize_t numbytes;
numbytes = recvfrom(ds, server_reply, 6000, 0, NULL, NULL);
if (numbytes > 0) {
printf("Recibi algo. Tamaño %lu bytes. ", numbytes);
if (server_reply[0] == MACOrigen[0] &&
server_reply[1] == MACOrigen[1] &&
server_reply[2] == MACOrigen[2] &&
server_reply[3] == MACOrigen[3] &&
server_reply[4] == MACOrigen[4] &&
server_reply[5] == MACOrigen[5] ) {
printf("DST Correcto. ");
/* Print packet
printf("\tData:");
for (i=0; i<numbytes; i++) printf("%02x:", server_reply[i]);*/
if (server_reply[12] == 0x08 && server_reply[13] == 0x06) {
printf("\n\tPackete recibido es ARP... " );
if (server_reply[21] == 0x02) {
printf("Tipo Respuesta! Verificando...\n" );
if (server_reply[28] == arpTargetIP[0] &&
server_reply[29] == arpTargetIP[1] &&
server_reply[30] == arpTargetIP[2] &&
server_reply[31] == arpTargetIP[3]
) {
memcpy(respIP, server_reply + 28, 4);
memcpy(respMAC, server_reply + 22, 6);
success = 1;
}
}
}else
printf("Packete recibido no es ARP. Buscando...\n" );
}else
printf("DST: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", server_reply[0], server_reply[1], server_reply[2], server_reply[3], server_reply[4], server_reply[5]);
}else{
printf("Timed Out\n");
intento++;
}
return success;
}
void finish_with_error(MYSQL *con){
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
int main(int argc, char *argv[]){
MYSQL *con = mysql_init(NULL);
int packet_socket, j, resp = 0;
struct ifreq net;
unsigned char digit = 0x00;
char resultadoIP[15], resultadoMAC[18], query[500];
if (con == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root", "MAC", 0, NULL, 0) == NULL) finish_with_error(con);
if ((packet_socket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
perror("Error en el socket");
else{
printf("Exito al abrir el socket\n");
struct timeval tv;
tv.tv_sec = 0; /* 30 Secs Timeout */
tv.tv_usec = 10000; // Not init'ing this can cause strange errors
setsockopt(packet_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(struct timeval));
if (argc > 1)
strcpy(net.ifr_name, argv[1]);
else{
printf("Introduzca nombre de la interfaz: ");
scanf("%s", net.ifr_name);
}
obtenerDatos(packet_socket, net);
imprimeDatos();
//imprimeTrama(trama)
for (j = 0; j < 255; j++) {
digit += 0x01; intento = 0, resp = 0;
estrTrama(trama, digit);
enviarTrama(packet_socket, trama);
printf("[Query] Buscando IP %d.%d.%d.%d\n", arpTargetIP[0], arpTargetIP[1], arpTargetIP[2], arpTargetIP[3]);
while (!resp && intento < 10) {
printf("\tIntento #%d: ", intento + 1);
resp = recibirTrama(packet_socket, tramaResp);
}
if (intento == 10) printf("[Error] No se encontro. %d.%d.%d.%d\n", arpTargetIP[0], arpTargetIP[1], arpTargetIP[2], arpTargetIP[3]);
else {
sprintf(resultadoIP, "%d.%d.%d.%d", respIP[0], respIP[1], respIP[2], respIP[3]);
sprintf(resultadoMAC, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", respMAC[0], respMAC[1], respMAC[2], respMAC[3], respMAC[4], respMAC[5]);
printf("[Resp!] Almacenando: %s = MAC: %s\n", resultadoIP, resultadoMAC);
sprintf(query, "INSERT INTO dir VALUES(NULL,'%s','%s')", resultadoIP, resultadoMAC);
if (mysql_query(con, query)) finish_with_error(con);
}
}
mysql_close(con);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment