CS4231 Local Area Networks HW1 – IP Packet Sniffer

Slides:



Advertisements
Similar presentations
Datalink Access.
Advertisements

Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
指導教授:陳淑媛 學生:李宗叡 李卿輔.  利用下列三種方法 (Edge Detection 、 Local Binary Pattern 、 Structured Local Edge Pattern) 來判斷是否為場景變換,以方便使用者來 找出所要的片段。
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
Binary Search (I) Date: June 17, 2009 Introducer: Hsing-Yen Ann.
國立中山大學財產管理系統 線上報廢、盤點系統 總務處保管組 策劃 計算機與網路中心 分析設計 2008/03.
Introduction to Java Programming Lecture 15 Objects and Classes.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
© The McGraw-Hill Companies, Inc., 2006© The McGraw-Hill Companies, Inc., 2007 Chapter 7 位址解析通訊協定及 反向位址解析通訊協定.
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
Tutorial 8 Socket Programming
: Happy Number ★ ? 題組: Problem Set Archive with Online Judge 題號: 10591: Happy Number 解題者:陳瀅文 解題日期: 2006 年 6 月 6 日 題意:判斷一個正整數 N 是否為 Happy Number.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
從此處輸入帳號密碼登入到管理頁面. 點選進到檔案管理 點選「上傳檔案」上傳資料 點選瀏覽選擇電腦裡的檔案 可選擇公開或不公開 為平台上的資料夾 此處為檔案分類,可顯示在展示頁面上,若要參加 MY EG 競賽,做品一律上傳到 “ 98 MY EG Contest ” 點選此處確定上傳檔案.
資料結構實習-一 參數傳遞.
1 libpcap Packet Sniffing for Security Alisa Neeman.
實驗三 sniffer 的使用 介紹 sniffer 、 抓取 packets. 實驗設備與材料 PC 2 台 自己是 對方是 安裝 sniffer 與相關軟體.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
資料結構實習-十 Binary Tree Traversal.
Network Analyzer For Ethereal. 基本設備 Hardware pc network card can connect INTERNET Software OS Winpcap Ethereal.
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
Chapter 11 Strings and Vectors. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview An Array Type for Strings (11.1) The Standard.
網路介紹及其運用 講師陳炯勳. 5-2 IP 協定 ( 一 ) IP 協定運作 (1) – 網路成員:主機 (Host) 與路由器 (Router) – 路由表 – 電報傳輸運作.
Packet Capture & Analyze
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2010.
論文研討 2 學分 授課教師:吳俊概. 第一節 論文發表的目的 第二節 論文發表的歷程 第三節 投稿過程 第四節 退稿處理 學術期刊論文的製作與發表.
組合語言與系統程式 軟體使用教學. 軟體下載及安裝 下載 到課程網頁下載 tw/~jiun/ 安裝 解壓縮後將 System Software 資料夾放在 D 槽 (C 槽版本放在 C 之下 )
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
計算機概論 第6章 數位邏輯設計.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
VHDL語法(3).
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
POSTECH DP&NM Lab. Internet Traffic Monitoring and Analysis: Methods and Applications (1) Programming with Libpcap.
Lab-4 ICMP Ping Programming Speaker : Yi-Fong Lin Date:
UNIX Socket Programming CS 6378
CSCD433 Advanced Networks Fall 2011 Raw vs. Cooked Sockets.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Introduction to Socket Programming Advisor: Quincy Wu Speaker: Kuan-Ta Lu Date: Nov. 25, 2010.
Agostinho L S Castro Telecommunications and Multimedia Unit BPF - BSD Packet Filter.
Overview LAN 1LAN 2 Summary 目的 連接 LAN1 與 LAN2 Frame forwarding Frame filtering Hint Data link layer socket programming Library: libpcap libnet.
TamKang University Computer Science and Information Engineering Member: 林玫君 資工 3D 李俊毅 資工 2B 陳 懷 資工 2B 李承恩 資工 2B 指導老師 : 石貴平, 張志勇 助教 : Kevin Chang.
POSTECH 1/17 CSED702D: Internet Traffic Monitoring and Analysis James Won-Ki Hong Department of Computer Science and Engineering POSTECH, Korea
Java Tutorial /10/21. Java Resource Java SDK –Download from –Install Jdk windows-i586.exe –
Raw Sockets Vivek Ramachandran. A day in the life of Network Packet.
Socket 实验汇总 1. 2 Lab1( 需验收 ) Run all the sample programs today and answer the mentioned questions. Learn about the O_APPEND flag used in open(). Question:
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 IPTABLES and NAT on Fedora Core 6 Speaker : Rex Wu Date :
0x440 Network Sniffing.
PLUS 내부 세미나 1/22 The Libnet Library 이병영 2004/05/06.
王子健 Overview LAN 1LAN 2.
Lab 1 Introduction MMLAB. Environment Install Linux OS in a computer( VirtualBox or Vmware or Native) It’s all up to you for selecting Ubuntu Server or.
Mobile Packet Sniffer Ofer Borosh Vadim Lanzman Dr. Chen Avin
CSCD433/533 Advanced Networks Spring 2016 Lecture 15
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Introduction to Socket Programming
CSCD433/533 Advanced Networks Winter 2019 Lecture 13
Packet Sniffing and Spoofing
Presentation transcript:

CS4231 Local Area Networks HW1 – IP Packet Sniffer 張詠承 passtaiker@gmail.com

Summary 目的 Hint 啟用網路卡之 promiscuous mode (混雜模式) 從網路卡抓取封包 分析封包 顯示分析結果 Raw socket/libpcap/winpcap Data link layer socket programming/Packet capture library Network packet capture IP Packet Sniffer employs a “raw” socket to capture IP protocol packets traveling over your local network. These packets contain data and communications information traveling on your local network. The packets may originate from or be addressed to your computer. On the other hand, the data could be passing between computers that are not yours.

Example of screen shot (after # sudo ./a.out)

What’s Raw Socket Simply put raw sockets provide a way to bypass the whole network stack traversal of a packet and deliver it directly to an application. Raw socket r/w packets from Data Link Layer 利用 Raw socket 可以讀寫 IPv4 packet 的 header Read/write 那些 kernel 不處理的 protocol 的 IP packet ARP (Address Resolution Protocol) RARP (Reverse ARP) 一般我們開始socket接收tcp或udp的packet 時, 我們收到的內容就直接是資料內容 . kernel 己經幫我們把ethernet header 和 ip 或 arp header 都拿掉了. 但是在寫網路封包程式時, 我們需要header . 因此我們在開啟socket 的時候,就可以針對我們要收集的封包總類做過濾. 要求接收到的資料裡要包涵完整的封包標頭檔...

Why Raw Socket TCP/UDP packets received from a socket only the data is shipped to the application layer TCP/UDP packets received from a socket contains only payload part of a IP packet ETH/IP/ARP hdrs are removed by kernel Use raw socket to r/w the header of a IPv4 packet

How to Use Raw Socket // 宣告一個socket,第二個參數指出這是raw socket,第三個參數指出這是ARP封包 sd = socket(PF_PACKET , SOCK_RAW , htons(ETH_P_ALL)); // 第一個參數 PF_PACKET // It is a software interface to send/receive packets at layer 2 of the OSI // All packets received will be complete with all headers and data. // Supports filtering using Berkley Packet Filters. // 第二個參數 PF_PACKET 支援兩個 socket type: SOCK_DGRAM // return packets with the link-layer header removed SOCK_RAW // return complete link-layer packet // 最後一個參數 ETH_P_ALL // return frames for all protocols that the data link receives ETH_P_IP // return IPv4 frames ETH_P_ARP // return ARP Protocol frames ETH_P_IPV6 // return IPv6 frames

How to Use Raw Socket addr.sll_family = PF_PACKET; addr.sll_protocol = htons(ETH_P_ARP); recvfrom(sd, rcvbuffer, sizeof(rcvbuffer), 0, (struct sockaddr*)&addr, &len) /* 第一個參數為 socket descriptor 第二個參數為接收內容的 buffer, 第三個參數為此內容的長度, 第四個參數不會用到設為 0, 第五個參數設定 address 的封包種類、接收的 protocol 等等 第六個參數為 addr 的長度 */

Example int main(int argc, char *argv[]) { // 省略變數宣告 // create raw socket for sniffing sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if(sd == -1){ perror("socket error\n"); return; } // set address addr.sll_family = PF_PACKET; addr.sll_protocol = htons(ETH_P_ALL);

Example for(;;) { len = sizeof(addr); // receive packets ret = recvfrom(sd, rcvbuffer, sizeof(rcvbuffer), 0, (struct sockaddr*)&addr, &len); if (ret == -1) continue; /* 於此處按照 IP protocol的格式 parse rcvbuffer 先判斷 Ethernet 是否是 IP 的封包,若是的話就parse 並印出封包的內容 */ } } // end of main

Network API - libpcap libpcap (Packet CAPture) provides a portable framework for low-level network monitoring. Applications include network statistics collection, security monitoring, network debugging, etc. libpcap is the library we are going to use to grab packets right as they come off of the network card Tutorial http://yuba.stanford.edu/~casado/pcap/section1.html

Libpcap - Functions pcap_t * pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) int pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)

libpcap - Open up NIC for PCAP dev = pcap_lookupdev(errbuf); // 亦可寫成 dev = “eth0” If (dev == NULL) { fprintf(stderr,“%s\n”, errbuf); return -1; } descr = pcap_open_live(dev, BUFSIZ, promisc, pcap_time_out, errbuf); If (descr == NULL) printf(“pcap_open_live(): %s\n”, errbuf);

libpcap - Capture a Packet int ret; ret = pcap_dispatch( pt_a, 0, dev_a_handle, NULL); if ( ret == -1 ) { pcap_perror( pt_a, "pcap_dispatch err:"); } void dev_a_handle( u_char *devId, const struct pcap_pkthdr *hdr, const u_char *packet )

Network API - libnet Designed by Mike Schiffman, libnet is a portable, open source, C-language library for creating and injecting network packets. libnet supports packet creation at all network levels with the TCP/IP network model.

libnet - Functions libnet_t *libnet_init(int injection_type, char *device, char *err_buf); int libnet_write_link(struct libnet_link_int *l, const u_char *device, u_char *packet, int packet_size);

libnet - Initialization net_b = libnet_init( LIBNET_LINK, "eth0", errbuf ); if( net_a == NULL ) { fprintf(stderr, "libnet_init fail:%s ", errbuf ); return; }

libnet - Send a Packet c = libnet_write_link( net_b, (u_char*)packet, hdr->caplen );

pthread int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h) attr - Set to NULL if default thread attributes are used. void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void. *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.

pthread int pthread_join(pthread_t * thread, void **value_ptr); The pthread_join() function suspends execution of the calling thread until the target thread terminates

libpcap, libnet Installation sudo apt-get install libpcap0.8-dev libnet sudo apt-get install libnet1-dev Remember to Install these two library first

Programming Environment You have to write your program on Linux platform. You can install VMware to run Linux on it.

Promiscuous Mode We can only receive frames destined to us (Unicast) , to everyone (Broadcast) and to some selected addresses we subscribe to (Multicast). If we could receive the frames for all computers connected to our broadcast domain – Promiscuous mode

Promiscuous Mode It is the “See All, Hear All” Wizard mode  Tells the network driver to accept all packets irrespective of whom the packets are addressed to. Used for Network Monitoring – both legal and illegal monitoring  We can do this by programmatically setting the IFF_PROMISC flag or by using the ifconfig utility (ifconfig eth0 promisc) #include <sys/ioctl.h> #include <net/if.h> struct ifreq ifrq; strncpy(ethreq.ifr_name,"eth0",IFNAMSIZ); ioctl(sock,SIOCGIFFLAGS,&ifrq); ifrq.ifr_flags|=IFF_PROMISC; ioctl(sock,SIOCSIFFLAGS,&ifrq);

The making of a Sniffer Create Raw socket – socket() Set interface you want to sniff on in promiscuous mode. Bind Raw socket to this interface – bind() optional Receive packets on the socket – recvfrom() Process received packets Close the raw socket().

Internet Address Manipulation in_addr_t inet_addr(const char *cp) convert the Internet host address cp from numbers-and-dots notation into binary data in network byte order char *inet_ntoa(struct in_addr in) convert the Internet host address in given in network byte order to a string in standard numbers-and-dots notation (a.b.c.d) The string is returned in a statically allocated buffer, which subsequent calls will overwrite.

Internet Address Manipulation

Network Byte Ordering Network is big-endian, host may be big- or little-endian Functions work on 16-bit (short) and 32-bit (long) values htons() / htonl() convert host byte order to network byte order ntohs() / ntohl() convert network byte order to host byte order Use these to convert network addresses, ports, …

Ethernet Header Format Destination 目的地的 MAC address Source 傳送方的 MAC address Message Type (#define ETH_P_ARP 0x0806) 封包種類,如果該值是0x0806,則表示為ARP封包 Data 封包內容

IP Header Format Protocol IPPROTO_ICMP 1 IPPROTO_IGMP 2 IPPROTO_TCP 6 IPPROTO_UDP 17

Requirements packet number time elapsed since capture was initiated (with microsecond resolution) packet size packet type (protocol) ETHERNET packet: ARP IP packet: TCP/UDP/ICMP/IGMP source and destination IP addresses summary information about the IP packet

Bonus GUI Filter IP address Protocol Additional IP protocols etc

Debugging

Grading Correctness (60%) Report (30%) Coding Style (10%) How to run your program. What you’ve learned? What are you suffer from this HW? Any feedback? Coding Style (10%)

Hand in your program Deadline: 2009/5/20 PM 23:59:59 Write a simple report in text file. Please tar/zip/rar your files (including code and report) named as 學號.tar (ex: 9762560.tar) and login to ftp:// 140.114.71.48:4231, cs4231/cs4231s10 Change directory to Project1_upload and create a directory named your 學號, then upload your file in this directory.

Appendix Raw socket 收封包: WinPcap函式庫使用入門: WinPcap函式庫下載與文件: http://blog.roodo.com/thinkingmore/archives/554037.html WinPcap函式庫使用入門: WinPcap函式庫下載與文件: http://www.winpcap.org/devel.htm LibPcap函式庫使用入門: http://yuba.stanford.edu/~casado/pcap/section1.html http://www.tcpdump.org/pcap.htm VMWare Server 2.0安裝入門: http://full827.pixnet.net/blog/post/24011324

Appendix libpcap / libnet pthread http://web.zyline.com.cn/prolist.asp?id=4916 http://dev.csdn.net/article/21/21009.shtm pthread http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

Appendix WireShark-the open source Ethernet 封包格式: IP 封包格式: Study-Area http://www.wireshark.org/ Ethernet 封包格式: http://en.wikipedia.org/wiki/EtherType IP 封包格式: http://www.networksorcery.com/enp/protocol/ip.htm Study-Area http://www.study-rea.org/network/network_ip_arp.htm 鳥哥的Linux http://linux.vbird.org/linux_server/0110network_basic/0110network_basic.php

Demo