pcap 예제 코드에서...

noish의 이미지

http://www.joinc.co.kr/modules/moniwiki/wiki.php/article/Pcap_%C6%D0%C5%B6%C4%B8%C3%C4%C0%C0%BF%EB 에서

"2.4절. 예제코드" 에 있는
콜백함수를 보던 중 궁금한 것이 있습니다!

1. tcph = (struct tcp *)(packet + iph->ip_hl * 4);
??? ip_hl 헤더의 길이이고... 4는 4바이트-_-a를 의미하는 듯 한데... 왜 ip_hl의 값이 혹시 그냥 순수 데이타의 갯수
(string의 갯수를 의미해서...)를 의미해서 여기에 4를 곱한 것인지요?^^aaa

2. packet += (iph->ip_hl * 4) + TCPHEADERSIZE + (sizeof(struct ether_header));
??? 현재 IP헤더의 첫부분을 가리키고 있으니 IP헤더+TCP헤더만 해야 유저 데이타를 가리지키지 않나요?
뒤에 나오는 이더넷 헤더의 정체는 왜 더해줬는지-_-aa

흐흐흐^^;; ;TCP/IP 책을 보긴 봐야 할 것 같은데-_-;;;

void callback(u_char *useless, const struct pcap_pkthdr *pkthdr,
const u_char *packet)
{
struct ether_header *ep;
unsigned short ether_type;

char *uname = NULL;
char *pass = NULL;
char buf[80];

// 이더넷 헤더를 가져온다.
ep = (struct ether_header *)packet;

// IP 헤더를 가져오기 위해서
// 이더넷 헤더 크기만큼 offset 한다.
packet += sizeof(struct ether_header);
ether_type = ntohs(ep->ether_type);

// Network Layer 의 Protocol 타입을 알아낸다.
// 만약 IP 프로토콜을 사용한다면
// IP 정보를 얻어온다.
if (ether_type == ETHERTYPE_IP)
{
iph = (struct ip *)packet;
if (iph->ip_p == IPPROTO_TCP)
{
tcph = (struct tcp *)(packet + iph->ip_hl * 4);
}

memset(buf, 0x00, 80);

// 유저 데이타를 얻어오기 위해서
// IP, TCP, Ethernet 헤더 크기만큼 offset 한다.
packet += (iph->ip_hl * 4)+TCPHEADERSIZE+(sizeof(struct ether_header));

// 패킷에 ID와 password 관련 문자열을 포함하는지 확인한다.
if ( ((uname = strstr(packet, "uname=")) != NULL)
&& ((pass = strstr(packet, "&pass=")) != NULL))
{
// 헤더 정보를 출력한후
printf("HEADER INFO\n");
printf("Src Address : %s\n", inet_ntoa(iph->ip_src));
printf("Dst Address : %s\n", inet_ntoa(iph->ip_dst));

printf("Src Port : %d\n" , ntohs(tcph->source));
printf("Dst Port : %d\n" , ntohs(tcph->dest));

// 문자열에서 필요한 정보 즉 ID와 Password 만을
// 추출해 낸다.
strncpy(buf, uname+6, strstr(uname, "&") - (uname + 6));
printf("Uname : <%s>\n", buf);
memset(buf, 0x00, 80);
strncpy(buf, pass+6, strstr(pass+6, "&") - (pass + 6));
printf("Pass : <%s>\n", buf);
printf("======================\n\n");
}
}
}

haze11의 이미지

1. tcph = (struct tcp *)(packet + iph->ip_hl * 4);
??? ip_hl 헤더의 길이이고... 4는 4바이트-_-a를 의미하는 듯 한데... 왜 ip_hl의 값이 혹시 그냥 순수 데이타의 갯수
(string의 갯수를 의미해서...)를 의미해서 여기에 4를 곱한 것인지요?^^aaa

->
IHL: Specifies the length of the IP packet header in 32 bit words. The minimum value for a valid header is 5.
라고 나오네요. 32빗 워드기 때문에 4바이트.. 그래서 4를 곱해줍니다.

2. packet += (iph->ip_hl * 4) + TCPHEADERSIZE + (sizeof(struct ether_header));
??? 현재 IP헤더의 첫부분을 가리키고 있으니 IP헤더+TCP헤더만 해야 유저 데이타를 가리지키지 않나요?
뒤에 나오는 이더넷 헤더의 정체는 왜 더해줬는지-_-aa

->
pcap에서는 이더넷 헤더부터 떼옵니다.
eth ( ip ( tcp ) )

noish의 이미지

제가 생각하기에는요

콜백함수의 인자에서 맨끝에 있는 인자(유저 데이타??? packet)에 있는 값이

첫번째! 아래 것을 통과하면서..... packet은 IP헤더를 가리키게 되고!
packet += sizeof(struct ether_header);

두번째!로 패킷에 대한 처리를 할때는... 현재 ip헤더를 가리키고 있기 때문에 이더넷 헤더를 제외하고 포인팅하는 작업이 이루어져야 하지 않나 싶은데요?^^;;
packet += (iph->ip_hl * 4) + TCPHEADERSIZE + (sizeof(struct ether_header));

꼬랑지
Specifies the length of the IP packet header in 32 bit words. The minimum value for a valid header is 5.
이거 출처 좀 알려주시면 감사하겠습니다^^;;;;

답글 감사합니다^_^

;(-______-);

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.