Merhaba, bu konuda C Programlama Dili ile bir Packet Sniffer Aracı Kodlayacağız.
Bu program için WinPcap kütüphanesini kullandım.
İlerleyen zamanlarda ;
C++ İle CPU yakma konusu ve bu konu için part2 konuları hazırlamayı da düşünüyorum.
Programın Özelliklerine bakacak olursak :
Ağ Trafiği İzleme:
Gerçek zamanlı paket yakalama
Tüm ağ arayüzlerini otomatik tespit etme
Promiscuous mod desteği (tüm ağ trafiğini yakalama)
Protokol Analizi:
Ethernet paketleri analizi
IP paketleri analizi
TCP paketleri analizi
UDP paketleri analizi
ICMP paketleri tespiti
Paket Detayları:
MAC adresleri (kaynak ve hedef)
IP adresleri (kaynak ve hedef)
Port numaraları
TCP sequence ve acknowledge numaraları
Paket uzunluğu
Zaman damgası
Gelişmiş Filtreleme Sistemi:
Port bazlı filtreleme (örn: 80, 443 gibi belirli portları izleme)
IP adresi bazlı filtreleme (belirli IP'leri izleme)
Protokol bazlı filtreleme:
Sadece TCP paketleri
Sadece UDP paketleri
Sadece ICMP paketleri
Tüm protokoller
İstatistik ve Raporlama:
Toplam yakalanan paket sayısı
Protokol bazlı paket dağılımı:
TCP paket sayısı ve yüzdesi
UDP paket sayısı ve yüzdesi
ICMP paket sayısı ve yüzdesi
Diğer protokollerin sayısı ve yüzdesi
Toplam transfer edilen veri miktarı (MB cinsinden)
Her 100 pakette bir otomatik istatistik raporu
Kullanıcı Dostu Arayüz:
Türkçe dil desteği
Kolay anlaşılır menü sistemi
Detaylı filtre ayarları
Renkli ve düzenli çıktı formatı
Windows Özel Desteği:
WinPcap kütüphanesi entegrasyonu
Windows Sockets (WinSock) desteği
Administrator modunda çalışma
Güvenlik Özellikleri:
Paket içeriğinin hex formatında gösterimi
Detaylı protokol analizi
Şüpheli paket tespiti imkanı
Ağ trafiği anomali tespiti için altyapı
Devam konusunda programa çok daha fazla özellik ekleyeceğim elbette.
Bu program için WinPcap kütüphanesini kullandım.
İlerleyen zamanlarda ;
C++ İle CPU yakma konusu ve bu konu için part2 konuları hazırlamayı da düşünüyorum.
Programın Özelliklerine bakacak olursak :
Ağ Trafiği İzleme:
Gerçek zamanlı paket yakalama
Tüm ağ arayüzlerini otomatik tespit etme
Promiscuous mod desteği (tüm ağ trafiğini yakalama)
Protokol Analizi:
Ethernet paketleri analizi
IP paketleri analizi
TCP paketleri analizi
UDP paketleri analizi
ICMP paketleri tespiti
Paket Detayları:
MAC adresleri (kaynak ve hedef)
IP adresleri (kaynak ve hedef)
Port numaraları
TCP sequence ve acknowledge numaraları
Paket uzunluğu
Zaman damgası
Gelişmiş Filtreleme Sistemi:
Port bazlı filtreleme (örn: 80, 443 gibi belirli portları izleme)
IP adresi bazlı filtreleme (belirli IP'leri izleme)
Protokol bazlı filtreleme:
Sadece TCP paketleri
Sadece UDP paketleri
Sadece ICMP paketleri
Tüm protokoller
İstatistik ve Raporlama:
Toplam yakalanan paket sayısı
Protokol bazlı paket dağılımı:
TCP paket sayısı ve yüzdesi
UDP paket sayısı ve yüzdesi
ICMP paket sayısı ve yüzdesi
Diğer protokollerin sayısı ve yüzdesi
Toplam transfer edilen veri miktarı (MB cinsinden)
Her 100 pakette bir otomatik istatistik raporu
Kullanıcı Dostu Arayüz:
Türkçe dil desteği
Kolay anlaşılır menü sistemi
Detaylı filtre ayarları
Renkli ve düzenli çıktı formatı
Windows Özel Desteği:
WinPcap kütüphanesi entegrasyonu
Windows Sockets (WinSock) desteği
Administrator modunda çalışma
Güvenlik Özellikleri:
Paket içeriğinin hex formatında gösterimi
Detaylı protokol analizi
Şüpheli paket tespiti imkanı
Ağ trafiği anomali tespiti için altyapı
Devam konusunda programa çok daha fazla özellik ekleyeceğim elbette.
C:
#include <stdio.h>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <pcap.h>
#include <time.h>
#include <ctype.h>
struct ethernet_header {
u_char dest[6];
u_char source[6];
u_short type;
};
// Victor77
struct ip_header {
u_char ver_ihl;
u_char tos;
u_short tlen;
u_short identification;
u_short flags_fo;
u_char ttl;
u_char proto;
u_short crc;
u_char source_ip[4];
u_char dest_ip[4];
};
struct tcp_header {
u_short source_port;
u_short dest_port;
unsigned int sequence;
unsigned int acknowledge;
u_char len;
u_char flags;
u_short window;
u_short checksum;
u_short urgent_pointer;
};
struct udp_header {
u_short source_port;
u_short dest_port;
u_short len;
u_short checksum;
};
struct packet_stats {
unsigned long total_packets;
unsigned long tcp_packets;
unsigned long udp_packets;
unsigned long icmp_packets;
unsigned long other_packets;
unsigned long total_bytes;
} stats = {0};
struct filter_options {
int port_filter;
char ip_filter[16];
int protocol_filter; // Victor77
} filter = {0, "", 0};
void print_ethernet_header(const u_char *packet);
void print_ip_header(const u_char *packet);
void print_tcp_header(const u_char *packet);
void print_udp_header(const u_char *packet);
void print_hex_ascii_line(const u_char *payload, int len, int offset);
void print_payload(const u_char *payload, int len);
void print_packet_stats(void);
void setup_filters(void);
int check_filters(const u_char *packet);
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data) {
struct tm *ltime;
char timestr[16];
time_t local_tv_sec;
if (!check_filters(pkt_data)) {
return;
}
stats.total_packets++;
stats.total_bytes += header->len;
local_tv_sec = header->ts.tv_sec;
ltime = localtime(&local_tv_sec);
strftime(timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("\n\n=== Paket #%lu Yakalandı ===\n", stats.total_packets);
printf("Zaman: %s.%.6d\n", timestr, header->ts.tv_usec);
printf("Paket Uzunluğu: %d byte\n", header->len);
print_ethernet_header(pkt_data);
struct ethernet_header *eth = (struct ethernet_header *)pkt_data;
if (ntohs(eth->type) == 0x0800) {
print_ip_header(pkt_data + sizeof(struct ethernet_header));
}
// Victor77
if (stats.total_packets % 100 == 0) {
print_packet_stats();
}
}
void print_ethernet_header(const u_char *packet) {
struct ethernet_header *eth = (struct ethernet_header *)packet;
printf("\n=== Ethernet Başlığı ===\n");
printf("Kaynak MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
eth->source[0], eth->source[1], eth->source[2],
eth->source[3], eth->source[4], eth->source[5]);
printf("Hedef MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
eth->dest[0], eth->dest[1], eth->dest[2],
eth->dest[3], eth->dest[4], eth->dest[5]);
}
void print_ip_header(const u_char *packet) {
struct ip_header *ip = (struct ip_header *)packet;
printf("\n=== IP Başlığı ===\n");
printf("Kaynak IP: %d.%d.%d.%d\n",
ip->source_ip[0], ip->source_ip[1],
ip->source_ip[2], ip->source_ip[3]);
printf("Hedef IP: %d.%d.%d.%d\n",
ip->dest_ip[0], ip->dest_ip[1],
ip->dest_ip[2], ip->dest_ip[3]);
switch(ip->proto) {
case 6:
stats.tcp_packets++;
print_tcp_header(packet + (ip->ver_ihl & 0xf) * 4);
break;
case 17:
stats.udp_packets++;
print_udp_header(packet + (ip->ver_ihl & 0xf) * 4);
break;
case 1:
stats.icmp_packets++;
printf("Protokol: ICMP\n");
break;
default:
stats.other_packets++;
printf("Protokol: Diğer (%d)\n", ip->proto);
}
}
void print_tcp_header(const u_char *packet) {
struct tcp_header *tcp = (struct tcp_header *)packet;
printf("\n=== TCP Başlığı ===\n");
printf("Kaynak Port: %d\n", ntohs(tcp->source_port));
printf("Hedef Port: %d\n", ntohs(tcp->dest_port));
printf("Sequence Number: %u\n", ntohl(tcp->sequence));
printf("Acknowledge Number: %u\n", ntohl(tcp->acknowledge));
}
void print_udp_header(const u_char *packet) {
struct udp_header *udp = (struct udp_header *)packet;
printf("\n=== UDP Başlığı ===\n");
printf("Kaynak Port: %d\n", ntohs(udp->source_port));
printf("Hedef Port: %d\n", ntohs(udp->dest_port));
printf("Uzunluk: %d\n", ntohs(udp->len));
}
void print_packet_stats(void) {
printf("\n=== Paket İstatistikleri ===\n");
printf("Toplam Paket: %lu\n", stats.total_packets);
printf("TCP Paketleri: %lu (%.2f%%)\n",
stats.tcp_packets,
(float)stats.tcp_packets/stats.total_packets * 100);
printf("UDP Paketleri: %lu (%.2f%%)\n",
stats.udp_packets,
(float)stats.udp_packets/stats.total_packets * 100);
printf("ICMP Paketleri: %lu (%.2f%%)\n",
stats.icmp_packets,
(float)stats.icmp_packets/stats.total_packets * 100);
printf("Diğer Paketler: %lu (%.2f%%)\n",
stats.other_packets,
(float)stats.other_packets/stats.total_packets * 100);
printf("Toplam Veri: %.2f MB\n", (float)stats.total_bytes/(1024*1024));
}
void setup_filters(void) {
printf("\n=== Filtre Ayarları ===\n");
printf("Port filtresi girin (0 = filtre yok): ");
scanf("%d", &filter.port_filter);
printf("IP filtresi girin (Enter = filtre yok): ");
getchar();
fgets(filter.ip_filter, sizeof(filter.ip_filter), stdin);
filter.ip_filter[strcspn(filter.ip_filter, "\n")] = 0;
printf("Protokol filtresi seçin:\n");
printf("0: Tümü\n1: Sadece TCP\n2: Sadece UDP\n3: Sadece ICMP\n");
scanf("%d", &filter.protocol_filter);
}
int check_filters(const u_char *packet) {
struct ethernet_header *eth = (struct ethernet_header *)packet;
if (ntohs(eth->type) != 0x0800) return 1;
struct ip_header *ip = (struct ip_header *)(packet + sizeof(struct ethernet_header));
if (filter.protocol_filter != 0) {
switch(filter.protocol_filter) {
case 1: if (ip->proto != 6) return 0; break;
case 2: if (ip->proto != 17) return 0; break;
case 3: if (ip->proto != 1) return 0; break;
}
}
if (strlen(filter.ip_filter) > 0) {
char ip_str[16];
sprintf(ip_str, "%d.%d.%d.%d",
ip->source_ip[0], ip->source_ip[1],
ip->source_ip[2], ip->source_ip[3]);
if (strcmp(ip_str, filter.ip_filter) != 0) {
sprintf(ip_str, "%d.%d.%d.%d",
ip->dest_ip[0], ip->dest_ip[1],
ip->dest_ip[2], ip->dest_ip[3]);
if (strcmp(ip_str, filter.ip_filter) != 0) {
return 0;
}
}
}
if (filter.port_filter > 0) {
if (ip->proto == 6) {
struct tcp_header *tcp = (struct tcp_header *)(packet + sizeof(struct ethernet_header) + (ip->ver_ihl & 0xf) * 4);
if (ntohs(tcp->source_port) != filter.port_filter &&
ntohs(tcp->dest_port) != filter.port_filter) {
return 0;
}
}
else if (ip->proto == 17) {
struct udp_header *udp = (struct udp_header *)(packet + sizeof(struct ethernet_header) + (ip->ver_ihl & 0xf) * 4);
if (ntohs(udp->source_port) != filter.port_filter &&
ntohs(udp->dest_port) != filter.port_filter) {
return 0;
}
}
}
return 1;
}
int main() {
pcap_if_t *alldevs;
pcap_if_t *d;
int i = 0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) {
printf("WSAStartup başarısız oldu. Hata kodu: %d\n", WSAGetLastError());
return 1;
}
printf("Gelişmiş Ağ Paket Dinleyici v2.0\n");
printf("=================================\n");
if (pcap_findalldevs(&alldevs, errbuf) == -1) {
printf("Ağ arayüzleri bulunamadı: %s\n", errbuf);
WSACleanup();
return 1;
}
printf("\nMevcut ağ arayüzleri:\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(" (Açıklama yok)\n");
}
if (i == 0) {
printf("\nAğ arayüzü bulunamadı!\n");
return 1;
}
printf("\nDinlemek istediğiniz arayüzün numarasını girin (1-%d): ", i);
int inum;
scanf("%d", &inum);
if (inum < 1 || inum > i) {
printf("\nArayüz numarası geçersiz.\n");
pcap_freealldevs(alldevs);
WSACleanup();
return 1;
}
setup_filters();
for (d = alldevs, i = 0; i < inum-1; d = d->next, i++);
if ((adhandle = pcap_open_live(d->name, 65536, 1, 1000, errbuf)) == NULL) {
printf("\nArayüz açılamadı: %s\n", errbuf);
pcap_freealldevs(alldevs);
WSACleanup();
return 1;
}
printf("\n%s arayüzünde dinleme başlatılıyor...\n", d->name);
printf("Çıkmak için Ctrl+C tuşlarına basın.\n\n");
pcap_freealldevs(alldevs);
pcap_loop(adhandle, 0, packet_handler, NULL);
pcap_close(adhandle);
WSACleanup();
return 0;
}
Son düzenleme:


