-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (58 loc) · 2.18 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pcap.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/ether.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
void packet_handler(u_char *args, const struct pcap_pkthdr* header, const u_char* packet);
int main(int argc, char *argv[]) {
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "No device could be found: %s\n", errbuf);
return EXIT_FAILURE;
}
pcap_t *handle = pcap_open_live(dev, BUFSIZ, 0, 5000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Failed to open device %s: %s\n", dev, errbuf);
return EXIT_FAILURE;
}
struct bpf_program arp_filter;
char *arp_filter_exp = "arp";
if (pcap_compile(handle, &arp_filter, arp_filter_exp, 0, 0) == -1) {
fprintf(stderr, "Failed to compile filter expression: %s\n", errbuf);
return EXIT_FAILURE;
}
if (pcap_setfilter(handle, &arp_filter) == -1) {
fprintf(stderr, "Failed to set filter: %s\n", errbuf);
return EXIT_FAILURE;
}
printf("Listing for arp packages on device: %s\n", dev);
pcap_loop(handle, 0, packet_handler, NULL);
return EXIT_SUCCESS;
}
void packet_handler(u_char *args, const struct pcap_pkthdr* header, const u_char* packet) {
struct ether_header *eth;
eth = (struct ether_header *) packet;
char* mac_dashbutton = "50:f5:da:2f:4c:d1";
char *cmd = "etherwake -i wlan0 90:2b:34:a1:c0:67";
if(ntohs(eth->ether_type) == ETHERTYPE_ARP) {
struct ether_addr source;
memcpy(&source, eth->ether_shost, sizeof(source));
char* mac_source = ether_ntoa(&source);
if(strcmp(mac_dashbutton, mac_source) == 0) {
printf("Amazon dash button (%s) press detected. Execute command \"%s\".\n", mac_source, cmd);
pid_t pid = fork();
if(pid == 0){
if(execl("/bin/sh", "sh", "-c", cmd, (char*)0) == -1) {
fprintf(stderr, "Failed run command \"%s\": %s\n", cmd, strerror(errno));
}
}
}
}
}