Skip to content
Snippets Groups Projects
Commit 08c86103 authored by Uwais's avatar Uwais
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
File added
#include <stdio.h>
#include <pcap.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <netinet/if_ether.h>
#define ARP_REQUEST 1
#define ARP_RESPONSE 2
typedef struct _arp_hdr arp_hdr;
struct _arp_hdr
{
uint16_t htype;
uint16_t ptype;
uint8_t hlen;
uint8_t plen;
uint16_t opcode;
uint8_t sender_mac[6];
uint8_t sender_ip[4];
uint8_t target_mac[6];
uint8_t target_ip[4];
};
int print_available_interfaces()
{
char error[PCAP_ERRBUF_SIZE];
pcap_if_t *interfaces, *temp;
int i = 0;
if (pcap_findalldevs(&interfaces, error) == -1)
{
printf("Cannot aquire the devices\n");
return -1;
}
printf("The available interfaces are : \n");
for (temp = interfaces; temp; temp = temp->next)
{
printf("#%d: %s\n", ++i, temp->name);
}
return 0;
}
void print_version()
{
printf("ARP Spoof Detector Project\n");
exit(1);
}
void print_help(char *bin)
{
printf("ARP Spoof Detector\n");
printf("Available arguments\n");
printf("--------------------------------------------------------\n");
printf("-h or --help\t\t Print the help\n");
printf("-l or --lookup\t\t Print the available interfaces\n");
printf("-i or --interfaces\t\t Provide the interface to sniff on\n");
printf("-v or --version\t\t Check the name of the tool\n");
printf("Usage : %s -i <interface> [You can look for the available interfaces using -l / --lookup]\n", bin);
exit(1);
}
char *get_hardware_address(uint8_t mac[6])
{
char *m = (char *)malloc(20 * sizeof(char));
sprintf(m, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
printf("\n");
return m;
}
char *get_ip_address(uint8_t ip[4])
{
char *m = (char *)malloc(20 * sizeof(char));
sprintf(m, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
printf("\n");
return m;
}
int sniff_arg(char *device_name)
{
char error[PCAP_ERRBUF_SIZE];
pcap_t *pack_desc;
const u_char *packet;
struct pcap_pkthdr header;
struct ether_header *eptr;
int i;
u_char *hardptr;
arp_hdr *arpheader = NULL;
char *t_mac, *t_ip, *s_mac, *s_ip;
int counter = 0;
time_t ctimer, ltimer;
long int diff=0;
pack_desc = pcap_open_live(device_name, BUFSIZ, 0, 1, error);
if (pack_desc == NULL)
{
printf("%s\n", error);
return -1;
}
else
{
printf("Listening on %s...\n", device_name);
}
while (1)
{
packet = pcap_next(pack_desc, &header);
if (packet == NULL)
{
printf("Error : Cannot Capture Packet\n");
return -1;
}
else
{
eptr = (struct ether_header *)packet;
if (ntohs(eptr->ether_type) == ETHERTYPE_ARP)
{
ctimer = time(NULL);
diff = ctimer - ltimer;
arpheader = (arp_hdr *)(packet + 14);
if (diff > 20)
counter = 0;
printf("Recieved a packet with length %d\n", header.len);
printf("Recieved at %s\n", ctime((const time_t *)&header.ts.tv_sec));
printf("Ethernet Header length : %d\n", ETHER_HDR_LEN);
printf("Operation Type : %s\n", (ntohs(arpheader->opcode) == ARP_REQUEST) ? "ARP Request" : "ARP Response");
s_mac = get_hardware_address(arpheader->sender_mac);
printf("Sender MAC : %s\n", s_mac);
s_ip = get_ip_address(arpheader->sender_ip);
printf("Sender IP : %s\n", s_ip);
t_mac = get_hardware_address(arpheader->target_mac);
printf("Target MAC : %s\n", t_mac);
t_ip = get_ip_address(arpheader->target_ip);
printf("Target IP : %s\n", t_ip);
printf("---------------------------------------------------------------\n");
counter++;
ltimer = time(NULL);
if (counter > 20)
{
printf("ARP Spoof Alert\n");
}
}
}
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2 || strcmp("-h", argv[1]) == 0 || strcmp("--help", argv[1]) == 0)
{
// print_version();
print_help(argv[0]);
}
else if (argc < 2 || strcmp("-v", argv[1]) == 0 || strcmp("--version", argv[1]) == 0)
print_version();
else if (argc < 2 || strcmp("-l", argv[1]) == 0 || strcmp("--lookup", argv[1]) == 0)
print_available_interfaces();
else if (argc < 2 || strcmp("-i", argv[1]) == 0 || strcmp("--interface", argv[1]) == 0)
{
if (argc < 3)
{
printf("Please provide an interface\n");
// print_available_interfaces();
}
else
{
sniff_arg(argv[2]);
}
}
else
{
printf("Invalid Argument\n");
print_help(argv[0]);
}
return 0;
}
\ No newline at end of file
File added
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main()
{
char *device_name, *net_addr, *net_mask;
int rcode;
char error[PCAP_ERRBUF_SIZE];
bpf_u_int32 net_addr_int, net_mask_int;
struct in_addr addr;
device_name = pcap_lookupdev(error);
if(device_name == NULL){
printf("%s\n", error);
return -1;
}
else{
printf("Interface : %s\n", device_name);
}
rcode = pcap_lookupnet(device_name, &net_addr_int, &net_mask_int, error);
if(rcode == -1)
{
printf("%s\n", error);
return -1;
}
addr.s_addr = net_addr_int;
net_addr = inet_ntoa(addr);
if(net_addr == NULL)
{
printf("Error converting ip address to stirng");
return -1;
}
else
{
printf("NET: %s\n", net_addr);
}
addr.s_addr = net_mask_int;
net_mask = inet_ntoa(addr);
if(net_mask == NULL)
{
printf("Error Converting net mask to string\n");
return -1;
}
else
printf("Mask : %s\n", net_mask);
return 0;
}
lookup 0 → 100755
File added
lookup.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
char error[PCAP_ERRBUF_SIZE];
pcap_if_t *interfaces, *temp;
int i = 0;
if(pcap_findalldevs(&interfaces, error) == -1)
{
printf("Cannot acquire the devices\n");
return -1;
}
printf("The available interfaces are :\n");
for(temp = interfaces ; temp; temp = temp->next)
printf("#%d: %s\n ", ++i, temp->name);
}
pcapture 0 → 100755
File added
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <netinet/if_ether.h>
int main()
{
char *device_name;
char error[PCAP_ERRBUF_SIZE];
pcap_t *pack_desc;
const u_char *packet;
struct pcap_pkthdr header;
struct ether_header *eptr;
u_char *hardptr;
device_name = pcap_lookupdev(error);
if (device_name == NULL)
{
printf("%s\n", error);
return -1;
}
else
{
printf("Interface : %s\n", device_name);
}
pack_desc = pcap_open_live(device_name, BUFSIZ, 0, 1, error);
if (pack_desc == NULL)
{
printf("%s\n", error);
return -1;
}
while (1)
{
packet = pcap_next(pack_desc, &header);
if (packet == NULL)
{
printf("Error : Cannot Capture Packet");
return -1;
}
else
{
printf("Recieved a packet with length %d\n", header.len);
printf("Recieved at %s\n", ctime((const time_t *)&header.ts.tv_sec));
printf("Ethernet Header length : %d\n", ETHER_HDR_LEN);
eptr = (struct ether_header *)packet;
if (ntohs(eptr->ether_type) == ETHERTYPE_IP)
{
printf("Ethernet type hex : 0x%x\n Decimal : %d is an IP Packet \n\n", ETHERTYPE_IP, ETHERTYPE_IP);
}
else if (ntohs(eptr->ether_type) == ETHERTYPE_ARP)
{
printf("Ethernet type hex : 0x%x\n Decimal : %d is an ARP Packet \n\n", ETHERTYPE_ARP, ETHERTYPE_ARP);
}
else
printf("Ethernet type hex : 0x%x\n Decimal : %d is not an IP or ARP Packet \n\n", ntohs(eptr->ether_type), ntohs(eptr->ether_type));
hardptr = eptr->ether_dhost;
int i = ETHER_ADDR_LEN;
printf("Destination Address : ");
do
{
printf("%s%x", (i == ETHER_ADDR_LEN) ? " " : ":", *hardptr++);
} while (--i > 0);
printf("\n");
hardptr = eptr->ether_shost;
i = ETHER_ADDR_LEN;
printf("Source Address : ");
do
{
printf("%s%x", (i == ETHER_ADDR_LEN) ? " " : ":", *hardptr++);
} while (--i > 0);
printf("\n");
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment