- ๊ฐ์: S-dev ๋คํธ์ํฌ ๋ณด์
- ๋ง๊ฐ์ผ: July 13, 2023
- ์ํ: ์๋ฃ
- ์ ํ: ๊ณผ์
๊ณผ์ ๋ด์ฉ
ใด์์ธ
๊ณผ์ ํด๊ฒฐ ๊ณผ์
ใด0. ์ค์ผ๋ ํค ์ฝ๋
ใด1. Ethernet Header์ src mac / dst mac ์ถ๋ ฅ
ใด2. IP Header์ src ip / dst ip
ใด3. TCP Header์ src port / dst port
ใด4. Payload(Data)์ hexadecimal value(์ต๋ 10๋ฐ์ดํธ๊น์ง๋ง)
๊ฒฐ๊ณผ
์ ์ฒด ์ฝ๋
์ก์์ ๋๋ packet์ captureํ์ฌ ์ค์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ C/C++ ๊ธฐ๋ฐ ํ๋ก๊ทธ๋จ์ ์์ฑํ๋ผ.
1. Ethernet Header์ src mac / dst mac
2. IP Header์ src ip / dst ip
3. TCP Header์ src port / dst port
4. Payload(Data)์ hexadecimal value(์ต๋ 10๋ฐ์ดํธ๊น์ง๋ง)
- TCP packet์ด ์กํ๋ ๊ฒฝ์ฐ "ETH + IP + TCP + DATA" ๋ก ๊ตฌ์ฑ์ด ๋๋ค. ์ด ๊ฒฝ์ฐ(TCP packet์ด ์กํ๋ค๊ณ ํ๋จ๋๋ ๊ฒฝ์ฐ๋ง)์๋ง 1~4์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋๋ก ํ๋ค(Data์ ํฌ๊ธฐ๊ฐ 0์ฌ๋ ์ถ๋ ฅํ๋ค).
- ๊ฐ๊ฐ์ Header์ ์๋ ํน์ ์ ๋ณด๋ค(mac, ip, port)๋ฅผ ์ถ๋ ฅํ ๋, ๋ ธ๋ค๊ฐ(packet์ ์์์์น๋ก๋ถํฐ ์ผ์ผ์ด ๋ฐ์ดํธ ์ธ์ด ๊ฐ๋ฉฐ)๋ก ์ถ๋ ฅํด๋ ๋๋๋ฐ ๋ถํธํจ.
- ์ด๋ด ๋ ๊ฐ๊ฐ์ Header ์ ๋ณด๋ค์ด structure๋ก ์ ์ ์ธํ ํ์ผ์ด ์์ผ๋ฉด ์ฝ๋์ ๊ตฌ์ฑ์ด ํ๊ฒฐ ๊ฐ๊ฒฐํด์ง๋ค. ์์ผ๋ก ๊ฐ๊ธ์ ์ด๋ฉด ๋คํธ์ํฌ ๊ด๋ จ ์ฝ๋๋ฅผ ์์ฑํ ํ ๋์๋ libnet ํน์ ์์ฒด์ ์ธ ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธํ์ฌ ์ฌ์ฉํ๋๋ก ํ๋ค.
- http://packetfactory.openwall.net/projects/libnet > Latest Stable Version: 1.1.2.1 ๋ค์ด๋ก๋(libnet.tar.gz) > include/libnet/libnet-headers.h
- libnet-headers.h ์์ ์๋ ๋ณธ ๊ณผ์ ์ ์ง์ ์ ์ผ๋ก ๊ด๋ จ๋ ๊ตฌ์กฐ์ฒด๋ค :
- struct libnet_ethernet_hdr (479 line)
- struct libnet_ipv4_hdr (647 line)
- struct libnet_tcp_hdr (1519 line)
- pcap-test ์ฝ๋๋ฅผ skeleton code์ผ๋ก ํ์ฌ ๊ณผ์ ๋ฅผ ์ํํด์ผ ํ๋ฉฐ, pcap_findalldevs, pcap_compile, pcap_setfilter, pcap_lookupdev, pcap_loop API๋ ์ฌ์ฉํ์ง ์๋๋ค(์ธํฐ๋ท์ ๋์๋ค๋๋ ์ฝ๋์ ํฌํจ๋์ด ์๋ ํจ์๋ค์ด๋ฉฐ, ๋ณธ ํจ์๋ค์ด ๊ณผ์ ์ ์ฝ๋์ ํฌํจ๋๋ ๊ฒฝ์ฐ ๊ณผ์ ๋ฅผ ๋ฒ ๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผํจ).
- Dummy interface๋ฅผ ์ด์ฉํ์ฌ ๋๋ฒ๊น ์ ์ฝ๊ฒํ ์ ์๋ ๋ฐฉ๋ฒ์ ์๋ฉด ๊ณผ์ ์ํ์ ๋์์ด ๋๋ค.
์ฝ๋์ ์ดํด๋ฅผ ๋๊ธฐ ์ํด ์ต๋ํ ์์ธํ๊ฒ ์ฃผ์์ ์์ฑํด๋ดค์ต๋๋ค.
ํ๋ฆฐ ๋ถ๋ถ์ด ์๋ค๋ฉด ๋๊ตฌ๋ ์ง์ ํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.
#include <pcap.h>
#include <stdbool.h>
#include <stdio.h>
void usage() {
printf("syntax: pcap-test <interface>\n");
printf("sample: pcap-test wlan0\n");
}
typedef struct {
char* dev_;
} Param;
Param param = {
.dev_ = NULL
};
bool parse(Param* param, int argc, char* argv[]) {
/* parse()ํจ์ ์ธ์
1. param ๊ตฌ์กฐ์ฒด์ ํฌ์ธํฐ
2. argc: ์
๋ ฅ๋ ์ธ์์ Count
3. argv: ์ค์ ์
๋ ฅ๋ ์ธ์์ Value
*/
if (argc != 2) {
usage();
return false;
// argc๊ฐ 2๊ฐ ์๋๋ผ๋ฉด, usage() ํจ์ ํธ์ถ ํ return false
}
param->dev_ = argv[1];
return true;
// argc๊ฐ 2๋ผ๋ฉด param ๊ตฌ์กฐ์ฒด์ dev_ ๋ณ์์ ์ธ์๊ฐ(argv[1])์ผ๋ก ์ค์ ํ return true;
}
int main(int argc, char* argv[]) {
if (!parse(¶m, argc, argv))
return -1;
// parse ํจ์ ํธ์ถ, param ๊ตฌ์กฐ์ฒด์ ํฌ์ธํฐ, ์
๋ ฅ๋ ์ธ์์ Count, ์
๋ ฅ๋ ์ธ์์ Value๋ฅผ ์ธ์๊ฐ์ผ๋ก ๋๊ฒจ์ค
// parse() ํจ์์์ argc๊ฐ 2๊ฐ ์๋๋ผ๋ฉด false๊ฐ ๋ฆฌํด๋๋ฏ๋ก main()ํจ์์์๋ return -1์ด ๋ฐ์ํจ
char errbuf[PCAP_ERRBUF_SIZE];
// PCAP_ERRBUF_SIZE๋ pcap.h ํ์ผ์์ 256๋ฐ์ดํธ๋ก ์ ์๋์ด ์์
// pcap.h: https://github.com/the-tcpdump-group/libpcap/blob/master/pcap/pcap.h
pcap_t* pcap = pcap_open_live(param.dev_, BUFSIZ, 1, 1000, errbuf);
/*
1. ํจํท ์บก์ฒ๋ฅผ ์ํด ์ด๋ ค๋ ๋คํธ์ํฌ ๋๋ฐ์ด์ค์ ์ด๋ฆ
2. ํจํท๋น ์บก์ฒํ ์ต๋ ๋ฐ์ดํธ ์๋ฅผ ์ง์
BUFSIZ๊ฐ ์ด๋์ ์ ์ ๋์ด ์๋์ง๋ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค..
3. promiscuous mode(1) ์ค์
4. millisecond ๋จ์๋ก Timeout ์ง์
5. ์๋ฌ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ๋ฆฌํดํ ๋ฒํผ
*/
if (pcap == NULL) {
fprintf(stderr, "pcap_open_live(%s) return null - %s\n", param.dev_, errbuf);
return -1;
}
// pcap ๊ฒฐ๊ณผ๊ฐ NULL์ธ ๊ฒฝ์ฐ ํ์ค ์๋ฌ๋ฅผ ์ฅ์น๋ช
๊ณผ ์๋ฌ๋ฉ์ธ์ง์ ํจ๊ป ์ถ๋ ฅ
/* ์๋ฌ๊ฐ ๋ฐ์ํ์ฌ errbuf์ ์๋ฌ ๋ฉ์ธ์ง๊ฐ ์ ์ฅ๋์์ง๋ง, pcap์ด NULL์ด ์๋๋ผ๋ฉด?
์ด๋ผ๋ ์๊ฐ์ ์ ๊น ํ์์ต๋๋ค.
ํ์ง๋ง, pcap doc์ ํ์ธํด๋ณด๋, pcap_open_live ํจ์๊ฐ ์คํ์ ์คํจํ๊ฒ ๋๋ฉด pcap์ ๋ฐ๋์
NULL๊ฐ์ผ๋ก ์ค์ ๋๋ค๊ณ ํฉ๋๋ค. ์ด ๋ถ๋ถ์ ๋์ด์ ์์ ํ ํ์๊ฐ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
doc Link: https://www.ibm.com/docs/en/aix/7.3?topic=p-pcap-open-live-subroutine
*/
while (true) {
struct pcap_pkthdr* header;
/* pcap_pkthdr๋ ํจํท ํค๋๋ฅผ ๋ด๋ ๊ตฌ์กฐ์ฒด
struct pcap_pkthdr {
struct timeval ts; /* time stamp(ํ์์คํฌํ) */
bpf_u_int32 caplen; /* length of portion present(ํ๋ฒ์ ์ฝ์ด๋ค์ธ ํจํท์ ๊ธธ์ด) */
bpf_u_int32 len; /* length this packet (off wire)(์ค์ ํจํท์ ์ ์ฒด ๊ธธ์ด) */
};
*/
const u_char* packet;
// packet์ด๋ผ๋ ์ด๋ฆ์ ๋ณ์๊ฐ unsigned charํ ๋ฐ์ดํฐ์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ฉฐ, ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์
int res = pcap_next_ex(pcap, &header, &packet);
/* pcap_next_ex ํจ์ ์ธ์
1. pcap_open_live๋ฅผ ํตํด ์คํ๋ pcap
2. header ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ณ์
3. packet ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ณ์
return: ์ฑ๊ณต ์ 1์ ๋ฐํํ๋ฉฐ, timeout์ด ๋ ๊ฒฝ์ฐ 0์ ๋ฐํ, ์๋ฌ์ -1์ ๋ฐํ, EOF์ -2๋ฅผ ๋ฐํํจ
*/
if (res == 0) continue;
// timeout์ continue๋ฅผ ํตํด while๋ฌธ ์ฌ์ง์
if (res == PCAP_ERROR || res == PCAP_ERROR_BREAK) {
printf("pcap_next_ex return %d(%s)\n", res, pcap_geterr(pcap));
break;
/* pcap.h์์ PCAP_ERROR์ PCAP_ERROR_BREAK๋ ๋ค์๊ณผ ๊ฐ์ด define ๋์ด์์
#define PCAP_ERROR -1
#define PCAP_ERROR_BREAK -2
๋ฐ๋ผ์, ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ํด๋น ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํด์ฃผ๊ณ , while๋ฌธ์์ ํ์ถ
*/
}
printf("%u bytes captured\n", header->caplen);
// header ๊ตฌ์กฐ์ฒด์ ์ ์ฅ๋ caplen(ํ๋ฒ์ ์ฝ์ด๋ค์ธ ํจํท์ ๊ธธ์ด)๋ฅผ ์ถ๋ ฅ
}
pcap_close(pcap);
// open๋ pcap์ close์ฒ๋ฆฌ
}-
ethernet_hdr ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ํด์ผ ํฉ๋๋ค.
libnet-headers.h์์ ethernet_hdr ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
struct libnet_ethernet_hdr { u_int8_t ether_dhost[ETHER_ADDR_LEN];/* destination ethernet address */ u_int8_t ether_shost[ETHER_ADDR_LEN];/* source ethernet address */ u_int16_t ether_type; /* protocol */ };
-
MAC ์ฃผ์๋ 6๋ฐ์ดํธ์ด๋ฏ๋ก
#difine ETHER_ADDR_LEN 6๋ ์ถ๊ฐํฉ๋๋ค. -
packet ํฌ์ธํฐ๋ฅผ Ethernet Header ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ก ํ๋ณํ ํฉ๋๋ค.
struct libnet_ethernet_hdr* eth_hdr = (struct libnet_ethernet_hdr*)packet; /* struct libnet_ethernet_hdr* eth_hdr -> eth_hdr๋ผ๋ ์ด๋ฆ์ struct libnet_ethernet_hdr ํ์ ์ ํฌ์ธํฐ๋ฅผ ์ ์ธ (struct libnet_ethernet_hdr*)packet -> packet์ด ๊ฐ๋ฆฌํค๋ ๋ฉ๋ชจ๋ฆฌ์ ์์ ์์น์์ struct libnet_ethernet_hdr ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ๋งํผ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ struct libnet_ethernet_hdr ํ์์ผ๋ก ํด์ */
-
src mac / dst mac์ ์ถ๋ ฅํด์ฃผ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
void printMac(u_int8_t* src_mac, u_int8_t* dst_mac){ printf("Source Mac: %02x %02x %02x %02x %02x %02x\n", src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]); printf("Destination Mac: %02x %02x %02x %02x %02x %02x\n", dst_mac[0],dst_mac[1],dst_mac[2],dst_mac[3],dst_mac[4],dst_mac[5]); } /* libnet_ethernet_hdr ๊ตฌ์กฐ์ฒด์์ mac์ฃผ์๋ `ETHER_ADDR_LEN`ํฌ๊ธฐ์ ๋ฐฐ์ด๋ก ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ์, 6๋ฐ์ดํธ๊ฐ์ mac[0] ~ mac[5]๋ก ์ถ๋ ฅํฉ๋๋ค. */
-
mainํจ์์์ printMac ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
printMac(eth_hdr -> ether_shost, eth_hdr -> ether_dhost); //eth_hdr์ ether_shost, ether_dhost๋ฅผ ์ธ์๊ฐ์ผ๋ก ํธ์ถํฉ๋๋ค.
-
Ether Type์ด IPv4๊ฐ ์๋ ๊ฒฝ์ฐ ๋ท ๋ด์ฉ์ ์ถ๋ ฅํ์ง ์์ต๋๋ค.
if(ntohs(eth_hdr -> ether_type) != ETHER_TYPE_IP) continue;
-
IPv4์ ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ํฉ๋๋ค.
libnet-headers.h์์ libnet_ipv4_hdr ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
struct libnet_ipv4_hdr { #if (LIBNET_LIL_ENDIAN) u_int8_t ip_hl:4, /* header length */ ip_v:4; /* version */ #endif #if (LIBNET_BIG_ENDIAN) u_int8_t ip_v:4, /* version */ ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ #ifndef IPTOS_LOWDELAY #define IPTOS_LOWDELAY 0x10 #endif #ifndef IPTOS_THROUGHPUT #define IPTOS_THROUGHPUT 0x08 #endif #ifndef IPTOS_RELIABILITY #define IPTOS_RELIABILITY 0x04 #endif #ifndef IPTOS_LOWCOST #define IPTOS_LOWCOST 0x02 #endif u_int16_t ip_len; /* total length */ u_int16_t ip_id; /* identification */ u_int16_t ip_off; #ifndef IP_RF #define IP_RF 0x8000 /* reserved fragment flag */ #endif #ifndef IP_DF #define IP_DF 0x4000 /* dont fragment flag */ #endif #ifndef IP_MF #define IP_MF 0x2000 /* more fragments flag */ #endif #ifndef IP_OFFMASK #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ #endif u_int8_t ip_ttl; /* time to live */ u_int8_t ip_p; /* protocol */ u_int16_t ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ }; /* ๊ตฌ์กฐ์ฒด์์ ์๋์ ๊ฐ์ #if, #endif ๋ฌธ์ด ๋ณด์ ๋๋ค. #if (LIBNET_LIL_ENDIAN) u_int8_t ip_hl:4, ip_v:4; #endif #if (LIBNET_BIG_ENDIAN) u_int8_t ip_v:4, ip_hl:4; #endif ๊ฒ์์ ํด๋ณด๋, ์ด๋ ์กฐ๊ฑด๋ถ ์ปดํ์ผ ์ง์์๋ผ๊ณ ํฉ๋๋ค. ์ปดํ์ผ ํ๊ฒฝ์ด ๋ฆฌํ์ค๋์ธ์ธ์ง, ๋น ์ค๋์ธ์ธ์ง์ ๋ฐ๋ผ ์ปดํ์ผ์ ๋ค๋ฅด๊ฒ ํ ์ ์๋ ๊ฒ ์ ๋๋ค. ๋ฆฌํ ์ค๋์ธ์ธ ๊ฒฝ์ฐ ํจํท ํค๋๋ ip_hl, ip_v ์์๋๋ก ํด์ํด์ผํ๋ฉฐ, ๋น ์ค๋์ธ์ธ ๊ฒฝ์ฐ ip_v, ip_hl ์์๋๋ก ํด์ํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๊ฒ ํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. 1. `#difine LIBNET_LIL_ENDIAN` ๋๋ `#difine LIBNET_BIG_ENDIAN` ๋งคํฌ๋ก๋ฅผ ์ ์ธ 2. ์ปดํ์ผ์ ํ ๋ `-DLIBNET_LIL_ENDIAN` ๋๋ `-DLIBNET_BIG_ENDIAN` ์ต์ ์ ์ถ๊ฐ ์ ๋ 2๋ฒ์ ํํ์ฌ makefile์ ์์ ํ์ต๋๋ค. makefile code LDLIBS += -lpcap CFLAGS += -DLIBNET_LIL_ENDIAN all: pcap-test pcap-test: pcap-test.c clean: rm -f pcap-test *.o */
-
Ethernet ํค๋ ๋ค์์ ์ค๋ IPv4 ํค๋์ ๋ํ packet ํฌ์ธํฐ๋ฅผ ์์ฑํฉ๋๋ค.
struct libnet_ipv4_hdr* ip_hdr = (struct libnet_ipv4_hdr*)(packet + sizeof(*eth_hdr));
-
src ip / dst ip๋ฅผ ์ถ๋ ฅํด์ฃผ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
void printIP(struct in_addr src_ip, struct in_addr dst_ip) { printf("Source IP: %d.%d.%d.%d\n", src_ip.s_addr & 0xFF, (src_ip.s_addr >> 8) & 0xFF, (src_ip.s_addr >> 16) & 0xFF, (src_ip.s_addr >> 24) & 0xFF); printf("Destination IP: %d.%d.%d.%d\n", dst_ip.s_addr & 0xFF, (dst_ip.s_addr >> 8) & 0xFF, (dst_ip.s_addr >> 16) & 0xFF, (dst_ip.s_addr >> 24) & 0xFF); }
-
mainํจ์์์ printIPํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
printIP(ip_hdr -> ip_src, ip_hdr -> ip_dst);
-
TCP ์ด์ธ์ ํจํท์ ์ถ๋ ฅํ์ง ์๋๋ก ํฉ๋๋ค.
if(ip_hdr -> ip_p != PROTOCOL_TYPE_TCP) continue;
-
TCP์ ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ํฉ๋๋ค.
libnet-headers.h์์ libnet_tcp_hdr ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
struct libnet_tcp_hdr { u_int16_t th_sport; /* source port */ u_int16_t th_dport; /* destination port */ u_int32_t th_seq; /* sequence number */ u_int32_t th_ack; /* acknowledgement number */ #if (LIBNET_LIL_ENDIAN) u_int8_t th_x2:4, /* (unused) */ th_off:4; /* data offset */ #endif #if (LIBNET_BIG_ENDIAN) u_int8_t th_off:4, /* data offset */ th_x2:4; /* (unused) */ #endif u_int8_t th_flags; /* control flags */ #ifndef TH_FIN #define TH_FIN 0x01 /* finished send data */ #endif #ifndef TH_SYN #define TH_SYN 0x02 /* synchronize sequence numbers */ #endif #ifndef TH_RST #define TH_RST 0x04 /* reset the connection */ #endif #ifndef TH_PUSH #define TH_PUSH 0x08 /* push data to the app layer */ #endif #ifndef TH_ACK #define TH_ACK 0x10 /* acknowledge */ #endif #ifndef TH_URG #define TH_URG 0x20 /* urgent! */ #endif #ifndef TH_ECE #define TH_ECE 0x40 #endif #ifndef TH_CWR #define TH_CWR 0x80 #endif u_int16_t th_win; /* window */ u_int16_t th_sum; /* checksum */ u_int16_t th_urp; /* urgent pointer */ };
-
IPv4 ํค๋ ๋ค์์ ์ค๋ TCP ํค๋์ ๋ํ packet ํฌ์ธํฐ๋ฅผ ์์ฑํฉ๋๋ค.
struct libnet_tcp_hdr* tcp_hdr = (struct libnet_tcp_hdr*)(packet + sizeof(*eth_hdr) + ip_hdr -> ip_hl*4); /* TCP ํค๋๋ ํจํท์ ์์๋ถํฐ ์ด๋๋ท ํค๋์ ๊ธธ์ด์ IP ํค๋์ ๊ธธ์ด๋ฅผ ๋ํ ๊ณณ์ ์์นํ๊ฒ ๋ฉ๋๋ค. ๋ฐ๋ผ์, packet + sizeof(*eth_hdr) + ip_hdr -> ip_hl*4์ tcp header์ ์์ ์์น์ ๋๋ค. */
-
src port / dst port๋ฅผ ์ถ๋ ฅํด์ฃผ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
void printPORT(u_int16_t src_port, u_int16_t dst_port){ printf("Soruce Port: %d\n", ntohs(src_port)); printf("Destination Port: %d\n", ntohs(dst_port)); }
-
mainํจ์์์ printPORTํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
printPORT(tcp_hdr -> th_sport, tcp_hdr -> th_dport);
-
TCP ํค๋ ๋ค์์ ์ค๋ Payload(Data)์ ๋ํ packet ํฌ์ธํฐ๋ฅผ ์์ฑํฉ๋๋ค.
u_char *data = (u_char *)(packet + sizeof(*eth_hdr) + ip_hdr->ip_hl * 4 + tcp_hdr->th_off * 4);
-
Payload(Data)์ length๋ฅผ ํ์ธํฉ๋๋ค.
int data_len = ntohs(ip_hdr->ip_len) - (ip_hdr->ip_hl * 4 + tcp_hdr->th_off * 4);
-
Payload(Data)๋ฅผ ์ถ๋ ฅํด์ฃผ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
void printDATA(int data_len, u_char* data){ if (data_len == 0){ printf("Data is Zero Bytes\n"); return; } printf("Data is %d Bytes\n",data_len); printf("Data: "); if(data_len < 10){ for(int i = 0; i <= data_len; i++){ printf("%02x ", data[i]); } } else{ for(int i = 0; i <=9; i++){ printf("%02x ",data[i]); } } printf(". . .\n"); } // data_len์ด 10๋ณด๋ค ์์ ๊ฒฝ์ฐ ์ ์ฒด data๋ฅผ ์ถ๋ ฅ์์ผ์ฃผ๋ฉฐ, data_len์ด 10 ์ด์์ผ ๊ฒฝ์ฐ, data์ 10๋ฐ์ดํธ๋ง ์ถ๋ ฅ์ํต๋๋ค.
-
mainํจ์์์ printDATAํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
printDATA(data_len, data);
