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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
| #define _CRT_SECURE_NO_WARNINGS #include <pcap.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <stdint.h> #include <winsock2.h> #include <ws2tcpip.h>
#pragma comment(lib, "wpcap.lib") #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "Packet.lib")
#define LOG_FILE "traffic.log" #define HEX_PER_LINE 16
#pragma pack(push, 1) struct ether_header { u_char ether_dhost[6]; u_char ether_shost[6]; u_short ether_type; }; #pragma pack(pop)
#pragma pack(push, 1) struct ipv4_header { #if BYTE_ORDER == LITTLE_ENDIAN u_char ihl : 4; u_char version : 4; #elif BYTE_ORDER == BIG_ENDIAN u_char version : 4; u_char ihl : 4; #endif u_char tos; u_short tot_len; u_short id; u_short frag_off; u_char ttl; u_char protocol; u_short checksum; uint32_t saddr; uint32_t daddr; }; #pragma pack(pop)
#pragma pack(push, 1) struct ipv6_header { uint32_t version_tc_flow; uint16_t payload_length; uint8_t next_header; uint8_t hop_limit; uint8_t saddr[16]; uint8_t daddr[16]; }; #pragma pack(pop)
#pragma pack(push, 1) struct tcp_header { u_short src_port; u_short dst_port; uint32_t seq_number; uint32_t ack_number; #if BYTE_ORDER == LITTLE_ENDIAN u_char reserved : 4; u_char data_offset : 4; #elif BYTE_ORDER == BIG_ENDIAN u_char data_offset : 4; u_char reserved : 4; #endif u_char flags; u_short window; u_short checksum; u_short urgent_pointer; }; #pragma pack(pop)
#pragma pack(push, 1) struct udp_header { u_short src_port; u_short dst_port; u_short length; u_short checksum; }; #pragma pack(pop)
void packet_handler(u_char* param, const struct pcap_pkthdr* header, const u_char* pkt_data) { FILE* logfile = fopen(LOG_FILE, "a"); if (logfile == NULL) { printf("Unable to open log file.\n"); return; }
char timestamp[64]; time_t local_tv_sec = header->ts.tv_sec; struct tm ltime; localtime_s(<ime, &local_tv_sec); strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", <ime);
fprintf(logfile, "Timestamp: %s.%.6ld\n", timestamp, header->ts.tv_usec); fprintf(logfile, "Packet length: %d bytes\n", header->len);
struct ether_header* eth_header = (struct ether_header*)pkt_data;
uint16_t ether_type = ntohs(eth_header->ether_type);
if (ether_type == 0x0800) { const struct ipv4_header* ip_header = (struct ipv4_header*)(pkt_data + sizeof(struct ether_header)); char src_ip_str[INET_ADDRSTRLEN]; char dst_ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &(ip_header->saddr), src_ip_str, INET_ADDRSTRLEN); inet_ntop(AF_INET, &(ip_header->daddr), dst_ip_str, INET_ADDRSTRLEN);
u_short src_port = 0; u_short dst_port = 0; const u_char* transport_header = pkt_data + sizeof(struct ether_header) + ip_header->ihl * 4;
if (ip_header->protocol == IPPROTO_TCP) { const struct tcp_header* tcp_header = (struct tcp_header*)transport_header; src_port = ntohs(tcp_header->src_port); dst_port = ntohs(tcp_header->dst_port); fprintf(logfile, "Protocol: TCP\n"); } else if (ip_header->protocol == IPPROTO_UDP) { const struct udp_header* udp_header = (struct udp_header*)transport_header; src_port = ntohs(udp_header->src_port); dst_port = ntohs(udp_header->dst_port); fprintf(logfile, "Protocol: UDP\n"); } else { fprintf(logfile, "Protocol: Other (%d)\n", ip_header->protocol); }
fprintf(logfile, "Source IP: %s:%d\n", src_ip_str, src_port); fprintf(logfile, "Destination IP: %s:%d\n", dst_ip_str, dst_port);
} else if (ether_type == 0x86DD) { const struct ipv6_header* ip6_header = (struct ipv6_header*)(pkt_data + sizeof(struct ether_header)); char src_ip_str[INET6_ADDRSTRLEN]; char dst_ip_str[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, ip6_header->saddr, src_ip_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, ip6_header->daddr, dst_ip_str, INET6_ADDRSTRLEN);
u_short src_port = 0; u_short dst_port = 0; const u_char* transport_header = pkt_data + sizeof(struct ether_header) + sizeof(struct ipv6_header);
if (ip6_header->next_header == IPPROTO_TCP) { const struct tcp_header* tcp_header = (struct tcp_header*)transport_header; src_port = ntohs(tcp_header->src_port); dst_port = ntohs(tcp_header->dst_port); fprintf(logfile, "Protocol: TCP\n"); } else if (ip6_header->next_header == IPPROTO_UDP) { const struct udp_header* udp_header = (struct udp_header*)transport_header; src_port = ntohs(udp_header->src_port); dst_port = ntohs(udp_header->dst_port); fprintf(logfile, "Protocol: UDP\n"); } else { fprintf(logfile, "Protocol: Other (%d)\n", ip6_header->next_header); }
fprintf(logfile, "Source IP: %s:%d\n", src_ip_str, src_port); fprintf(logfile, "Destination IP: %s:%d\n", dst_ip_str, dst_port);
} else { fprintf(logfile, "Non-IP packet. EtherType: 0x%04X\n", ether_type); }
for (u_int i = 0; i < header->len; i++) { fprintf(logfile, "%02X ", pkt_data[i]); if ((i + 1) % HEX_PER_LINE == 0) fprintf(logfile, "\n"); } fprintf(logfile, "\n\n");
fclose(logfile); }
int main() { pcap_if_t* alldevs; pcap_if_t* d; pcap_t* adhandle; char errbuf[PCAP_ERRBUF_SIZE]; int i = 0, inum; char filter_exp[] = ""; struct bpf_program fp; bpf_u_int32 netmask = 0xffffff;
WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { fprintf(stderr, "WSAStartup failed.\n"); return 1; }
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1) { fprintf(stderr, "Error in pcap_findalldevs_ex: %s\n", errbuf); printf("Make sure Npcap is installed and run the program as Administrator.\n"); return 1; }
printf("Available network interfaces:\n"); for (d = alldevs; d != NULL; d = d->next) { printf("%d. %s", ++i, d->name); if (d->description) printf(" (%s)\n", d->description); else printf(" (No description available)\n"); }
if (i == 0) { fprintf(stderr, "No interfaces found! Make sure Npcap is installed.\n"); return 1; }
printf("Enter the interface number (1-%d): ", i); scanf("%d", &inum); if (inum < 1 || inum > i) { fprintf(stderr, "Interface number out of range.\n"); pcap_freealldevs(alldevs); return 1; }
for (d = alldevs, i = 1; i < inum; d = d->next, i++);
if ((adhandle = pcap_open(d->name, 65536, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf )) == NULL) { fprintf(stderr, "Unable to open adapter %s: %s\n", d->name, errbuf); pcap_freealldevs(alldevs); return 1; }
printf("\nListening on %s...\n", d->description ? d->description : d->name);
if (pcap_compile(adhandle, &fp, filter_exp, 1, netmask) == -1) { fprintf(stderr, "Error compiling filter: %s\n", pcap_geterr(adhandle)); pcap_freealldevs(alldevs); return 1; } if (pcap_setfilter(adhandle, &fp) == -1) { fprintf(stderr, "Error setting filter: %s\n", pcap_geterr(adhandle)); pcap_freealldevs(alldevs); return 1; }
pcap_loop(adhandle, 0, packet_handler, NULL);
pcap_freealldevs(alldevs); pcap_close(adhandle); WSACleanup(); return 0; }
|