1 libpcap Packet Sniffing for Security Alisa Neeman.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Datalink Access.
Network Performance Measurement
Programming Languages and Paradigms The C Programming Language.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
TCPDUMP Network-Based Intrusion Detection. Description  Packet sniffing is the heart of intrusion detection and of understanding what is actually occurring.
C Programming - Lecture 5
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
C For Java Programmers Tom Roeder CS sp. Why C? The language of low-level systems programming  Commonly used (legacy code)  Trades off safety.
11- IP Network Layer4-1. Network Layer4-2 The Internet Network layer forwarding table Host, router network layer functions: Routing protocols path selection.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Network Analyzer Example
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
POSTECH DP&NM Lab. Internet Traffic Monitoring and Analysis: Methods and Applications (1) Programming with Libpcap.
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
University of Calgary – CPSC 441.  Wireshark (originally named Ethereal)is a free and open-source packet analyzer.  It is used for network troubleshooting,
Chapter 19 Data Structures Data Structures A data structure is a particular organization of data in memory. We want to group related items together.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
CPSC 441 Tutorial TA: Fang Wang The content of these slides are taken from CPSC 526 TUTORIAL by Nashd Safa (Extended and partially modified)
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
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.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
POSTECH 1/17 CSED702D: Internet Traffic Monitoring and Analysis James Won-Ki Hong Department of Computer Science and Engineering POSTECH, Korea
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Lecture 1: Introduction. Pick Your Version of GLUT OpenGL and the GLUT are available for Windows, Linux and many other Operating Systems and platforms.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Lab 2: SSL Security Attack June 17, 2008 Hyun Jin Kim.
© 2010 Cisco Systems, Inc. All rights reserved. 1 CREATE Re-Tooling Exploring Protocols with Wireshark March 12, 2011 CREATE CATC and Ohlone College.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
GUI With GTK+ Under Linux Fanfan Xiong. Introduction GTK+ (GIMP toolkit) : A library for creating graphical user interfaces(GUI) Two examples developed.
Writing a Channel Access Client in EPICS Bob Dalesio, April 5, 2000.
Writing a Channel Access Client in EPICS Bob Dalesio, April 5, 2000.
Navigating Directories
Sniffer, tcpdump, Ethereal, ntop
Introduction Systems Programming Overview  Systems programming is the programming activity to create software used by other programmers and users. System.
PLUS 내부 세미나 1/22 The Libnet Library 이병영 2004/05/06.
王子健 Overview LAN 1LAN 2.
Department of Computer Science & Engineering 5. Acknowledgments 4. Conclusions 3. Evaluation2. Contribution 1. Introduction REU 2008-Packet Sniffer Jose.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
1 2 2 Call The Project Dynamic-Memory 4 4 # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo)
January 9, 2001 Router Plugins (Crossbow) 1 Washington WASHINGTON UNIVERSITY IN ST LOUIS Exercises.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
Lab 2: Packet Capture & Traffic Analysis with Wireshark
Intro to Ethical Hacking
C Primer.
Pointers & Dynamic Memory
Traffic Analysis with Ethereal
Intro to Ethical Hacking
Intro to Ethical Hacking
C Basics.
Pointers.
Pointers.
Network Analyzer :- Introduction to Wireshark
C Programming - Lecture 5
15213 C Primer 17 September 2002.
Pointers.
Packet Sniffing and Spoofing
Presentation transcript:

1 libpcap Packet Sniffing for Security Alisa Neeman

2 Introduction libpcap is an open source C library for putting your NIC in promiscuous mode. Today I’ll go over a few C gotchas and how to use the libpcap API Any C programmers? Planning to go to grad school?

3 Agenda Installing libpcap C stuff Basic libpcap program –Grab a device to sniff –Filters/Event Loops –Packet structure

4 Getting the library Linux: VC++: Winpcaphttp://winpcap.polito.it/install/ default.htm Winpcaphttp://winpcap.polito.it/install/ default.htm Cygwin: Wpcap (haven’t tried this)

5 Install on Linux gunzip libpcap tar.gz tar -xvf libpcap tar cd libpcap /configure make

6 Install for Windows VC++ Get both Developer's pack download and Windows 95/98/ME/NT/2000/XP install package. Run install and reboot (this installs the.dll and inserts a link in your registry). You need to insert a copy of pcap.h into C:\Program Files\Microsoft Visual Studio\VC98\Include (There is a copy of pcap.h in the Winpcap developer's pack in wpdpack/Include. In fact you can copy over all the.h files )

7 VC++, cont’d You also need to add the lib files. Copy everything from wpdpack/Lib to C:\Program Files\Microsoft Visual Studio\VC98\Lib go to Project -> Settings -> click on the Link tab, and type in wpcap.lib and wsock32.lib in addition to the lib files that are already there.

8 Avoiding C Gotchas Always declare variables at the beginning of a block (no Java/C++ messiness!!) Nothing ‘new’: Always free what you malloc malloc( sizeof ( thingYouWantToAllocate )); Always check the return value (no Exceptions!) if (thing_didnt_work()) { fprintf(stderr, "ERROR: thing didn't work\n"); exit(-1); } /* if (thing_didnt_work) */

9 C cont’d Output is formatted. char person[ ] = “baby”; printf(“give me %d, %s\n”, 5, person); %d: int %x: hex %s: string %f: double

10 Get to the point! Pass by reference explicitly - Pass-by-reference prototype int doSomething( Thing *); Choice 1: Thing * t; doSomething( t ); Choice 2: Thing t; doSomething( &t ); Arrays are always in reference mode: char * is like char[0]

11 Finally… C is NOT an object-oriented language Most frequent data structure is a struct. Under the covers this is an array of contiguous bytes. struct pcap_pkthdr { struct timeval ts; //time stamp bpf_u_int32 caplen; // length of //portion present bpf_u_int32; //packet length }

12 Overview of libpcap What to include and how to compile Going Live Main Event Loop Reading from a packet Filters ARP IP ICMP Open live ether TCP UDP

13 What to include and how to compile gcc sniff.c -lpcap –o sniff You must be root or admin Some headers I’ve used. #include #include #include #include #include #include #include #include #include For Windows: #include

14 Getting onto the NIC int main(int argc, char **argv) { char *dev; /* name of the device to use */ pcap_t* descr; /* pointer to device descriptor */ struct pcap_pkthdr hdr; /* struct: packet header */ const u_char *packet; /* pointer to packet */ bpf_u_int32 maskp; /* subnet mask */ bpf_u_int32 netp; /* ip */ char errbuf[PCAP_ERRBUF_SIZE]; /* ask pcap to find a valid device to sniff */ dev = pcap_lookupdev(errbuf); if(dev == NULL) { printf("%s\n",errbuf); exit(1); } printf("DEV: %s\n",dev);

15 /* ask pcap for the network address and mask of the device */ pcap_lookupnet(dev,&netp,&maskp,errbuf); descr = pcap_open_live(dev,BUFSIZ, 0, -1,errbuf); /* BUFSIZ is max packet size to capture, 0 is promiscous, -1 means don’t wait for read to time out. */ if(descr == NULL) { printf("pcap_open_live(): %s\n",errbuf); exit(1); } Going Live!

16 Once live, capture a packet. packet = pcap_next(descr, &hdr); if (packet == NULL) { printf(“It got away!\n"); exit(1); } else printf(“one lonely packet.\n”); return 0; } //end main

17 Hmmm…

18 Main Event Loop void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char* packet) { //do stuff here with packet } int main(int argc, char **argv) { //open and go live pcap_loop(descr,-1,my_callback,NULL); return 0; }

19 What is an ethernet header? From #include struct ether_header { u_int8_t ether_dhost[ETH_ALEN]; /* 6 bytes destination */ u_int8_t ether_shost[ETH_ALEN]; /* 6 bytes source addr */ u_int16_t ether_type; /* 2 bytes ID type */ } __attribute__ ((__packed__)); Some ID types: #define ETHERTYPE_IP 0x0800 /* IP */ #define ETHERTYPE_ARP 0x0806 /* Address resolution */ Is this platform independent?

20 NO! So we may need to swap bytes to read the data. struct ether_header *eptr; /* where does this go? */ eptr = (struct ether_header *) packet; /* Do a couple of checks to see what packet type we have..*/ if (ntohs (eptr->ether_type) == ETHERTYPE_IP) { printf("Ethernet type hex:%x dec:%d is an IP packet\n", ntohs(eptr->ether_type), ntohs(eptr->ether_type)); } else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP) { printf("Ethernet type hex:%x dec:%d is an ARP packet\n”, ntohs(eptr->ether_type), ntohs(eptr->ether_type)); }

21 Filter – we don’t need to see every packet! Filters are strings. They get “compiled” into “programs” struct bpf_program fp; //where does it go? Just before the event loop: if (pcap_compile(descr,&fp,argv[1],0,netp) == -1) { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); } if (pcap_setfilter(descr,&fp) == -1) { fprintf(stderr,"Error setting filter\n"); exit(1); }

22 Some typical filters./sniff "dst port 80"./sniff "src host "./sniff "less 50" (grab all packets less than 50 bytes, such as???)./sniff "ip proto \udp“ (must use the escape character, \, for protocol names)

References Windows: xamples/rawping.htmlhttp:// xamples/rawping.html