SlideShare a Scribd company logo
1 of 23
1
libpcaplibpcap
Packet Sniffing for Security
Alisa Neeman
2
IntroductionIntroduction
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
AgendaAgenda
Installing libpcap
C stuff
Basic libpcap program
– Grab a device to sniff
– Filters/Event Loops
– Packet structure
4
Getting the libraryGetting the library
Linux:
http://sourceforge.net/projects/libpcap/
VC++:
Winpcaphttp://winpcap.polito.it/install/
default.htm
Cygwin: Wpcap (haven’t tried this)
http://www.rootlabs.com/windump/
5
Install on LinuxInstall on Linux
gunzip libpcap-0.7.1.tar.gz
tar -xvf libpcap-0.7.1.tar
cd libpcap-0.7.1
./configure
make
6
Install for Windows VC++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 FilesMicrosoft Visual
StudioVC98Include
(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’dVC++, cont’d
You also need to add the lib files.
Copy everything from wpdpack/Lib to
C:Program FilesMicrosoft Visual
StudioVC98Lib
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 GotchasAvoiding 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 workn");
exit(-1);
} /* if (thing_didnt_work) */
9
C cont’dC cont’d
Output is formatted.
char person[ ] = “baby”;
printf(“give me %d, %sn”, 5, person);
%d: int
%x: hex
%s: string
%f: double
10
Get to the point!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…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 libpcapOverview 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 toWhat to include and how to
compilecompile
gcc sniff.c -lpcap –o sniff
You must be root or admin
 Some headers I’ve used.
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include<netinet/if_ether.h>
#include<netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
For Windows:
#include <winsock.h>
14
Getting onto the NICGetting 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("%sn",errbuf); exit(1); }
printf("DEV: %sn",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(): %sn",errbuf);
exit(1);
}
Going Live!
16
Once live, capture a packet.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…Hmmm…
18
Main Event LoopMain 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?What is an ethernet header?
From #include<netinet/if_ether.h>
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!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 packetn",
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 packetn”,
ntohs(eptr->ether_type), ntohs(eptr->ether_type));
}
21
Filter – we don’t need to seeFilter – we don’t need to see
every packet!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_compilen");
exit(1);
}
if (pcap_setfilter(descr,&fp) == -1) {
fprintf(stderr,"Error setting filtern");
exit(1);
}
22
Some typical filtersSome typical filters
./sniff "dst port 80"
./sniff "src host 128.226.121.120"
./sniff "less 50"
(grab all packets less than 50 bytes, such
as???)
./sniff "ip proto udp“
(must use the escape character,  , for
protocol names)
23
ReferencesReferences
• http://www.cet.nau.edu/~mc8/Socket/Tutorials/section1.h
• http://www.tcpdump.org/pcap.htm
• http://mixter.void.ru/rawip.html
Windows:
• http://www.coders.eu.org/manualy/win/wskfaq/e
xamples/rawping.html

More Related Content

What's hot

Intel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsIntel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsHisaki Ohara
 
Kernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysisKernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysisAnne Nicolas
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageKernel TLV
 
Kernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver frameworkKernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver frameworkAnne Nicolas
 
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
Kernel Recipes 2015: Speed up your kernel development cycle with QEMUKernel Recipes 2015: Speed up your kernel development cycle with QEMU
Kernel Recipes 2015: Speed up your kernel development cycle with QEMUAnne Nicolas
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and DriversKernel TLV
 
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverterKernel Recipes 2014 - NDIV: a low overhead network traffic diverter
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverterAnne Nicolas
 
Introduction to DPDK
Introduction to DPDKIntroduction to DPDK
Introduction to DPDKKernel TLV
 
Lagopus presentation on 14th Annual ON*VECTOR International Photonics Workshop
Lagopus presentation on 14th Annual ON*VECTOR International Photonics WorkshopLagopus presentation on 14th Annual ON*VECTOR International Photonics Workshop
Lagopus presentation on 14th Annual ON*VECTOR International Photonics WorkshopLagopus SDN/OpenFlow switch
 
DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...
DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...
DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...Jim St. Leger
 
How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.Naoto MATSUMOTO
 
Linux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesLinux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesKernel TLV
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDPlcplcp1
 
DPDK Summit 2015 - RIFT.io - Tim Mortsolf
DPDK Summit 2015 - RIFT.io - Tim MortsolfDPDK Summit 2015 - RIFT.io - Tim Mortsolf
DPDK Summit 2015 - RIFT.io - Tim MortsolfJim St. Leger
 
Make Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance ToolsMake Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance ToolsKernel TLV
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptablesKernel TLV
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureAnne Nicolas
 

What's hot (20)

Intel DPDK Step by Step instructions
Intel DPDK Step by Step instructionsIntel DPDK Step by Step instructions
Intel DPDK Step by Step instructions
 
Kernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysisKernel Recipes 2015 - Kernel dump analysis
Kernel Recipes 2015 - Kernel dump analysis
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 
DPDK In Depth
DPDK In DepthDPDK In Depth
DPDK In Depth
 
Kernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver frameworkKernel Recipes 2015 - So you want to write a Linux driver framework
Kernel Recipes 2015 - So you want to write a Linux driver framework
 
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
Kernel Recipes 2015: Speed up your kernel development cycle with QEMUKernel Recipes 2015: Speed up your kernel development cycle with QEMU
Kernel Recipes 2015: Speed up your kernel development cycle with QEMU
 
Linux Network Stack
Linux Network StackLinux Network Stack
Linux Network Stack
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and Drivers
 
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverterKernel Recipes 2014 - NDIV: a low overhead network traffic diverter
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
 
Introduction to DPDK
Introduction to DPDKIntroduction to DPDK
Introduction to DPDK
 
Lagopus presentation on 14th Annual ON*VECTOR International Photonics Workshop
Lagopus presentation on 14th Annual ON*VECTOR International Photonics WorkshopLagopus presentation on 14th Annual ON*VECTOR International Photonics Workshop
Lagopus presentation on 14th Annual ON*VECTOR International Photonics Workshop
 
DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...
DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...
DPDK Summit - 08 Sept 2014 - Futurewei - Jun Xu - Revisit the IP Stack in Lin...
 
How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.How to Speak Intel DPDK KNI for Web Services.
How to Speak Intel DPDK KNI for Web Services.
 
Linux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesLinux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use Cases
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDP
 
DPDK Summit 2015 - RIFT.io - Tim Mortsolf
DPDK Summit 2015 - RIFT.io - Tim MortsolfDPDK Summit 2015 - RIFT.io - Tim Mortsolf
DPDK Summit 2015 - RIFT.io - Tim Mortsolf
 
Make Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance ToolsMake Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance Tools
 
Dpdk applications
Dpdk applicationsDpdk applications
Dpdk applications
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
 
Kernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architectureKernel Recipes 2015 - Porting Linux to a new processor architecture
Kernel Recipes 2015 - Porting Linux to a new processor architecture
 

Similar to Libpcap

Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018DevOpsDays Tel Aviv
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCKernel TLV
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Andriy Berestovskyy
 
PACKET Sniffer IMPLEMENTATION
PACKET Sniffer IMPLEMENTATIONPACKET Sniffer IMPLEMENTATION
PACKET Sniffer IMPLEMENTATIONGoutham Royal
 
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)Igalia
 
Spying on the Linux kernel for fun and profit
Spying on the Linux kernel for fun and profitSpying on the Linux kernel for fun and profit
Spying on the Linux kernel for fun and profitAndrea Righi
 
Andrea Righi - Spying on the Linux kernel for fun and profit
Andrea Righi - Spying on the Linux kernel for fun and profitAndrea Righi - Spying on the Linux kernel for fun and profit
Andrea Righi - Spying on the Linux kernel for fun and profitlinuxlab_conf
 
BSides London - Scapy Workshop
BSides London - Scapy WorkshopBSides London - Scapy Workshop
BSides London - Scapy WorkshopAdam Maxwell
 
Start Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New RopeStart Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New RopeYung-Yu Chen
 
Berkeley Packet Filters
Berkeley Packet FiltersBerkeley Packet Filters
Berkeley Packet FiltersKernel TLV
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from PythonYung-Yu Chen
 
BUD17-300: Journey of a packet
BUD17-300: Journey of a packetBUD17-300: Journey of a packet
BUD17-300: Journey of a packetLinaro
 
Linux Tracing Superpowers by Eugene Pirogov
Linux Tracing Superpowers by Eugene PirogovLinux Tracing Superpowers by Eugene Pirogov
Linux Tracing Superpowers by Eugene PirogovPivorak MeetUp
 
Plan 9カーネルにおけるTCP/IP実装(未完)
Plan 9カーネルにおけるTCP/IP実装(未完)Plan 9カーネルにおけるTCP/IP実装(未完)
Plan 9カーネルにおけるTCP/IP実装(未完)Ryousei Takano
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Jian-Hong Pan
 

Similar to Libpcap (20)

Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
Pcapy and dpkt - tcpdump on steroids - Ran Leibman - DevOpsDays Tel Aviv 2018
 
Pycon Sec
Pycon SecPycon Sec
Pycon Sec
 
libpcap
libpcaplibpcap
libpcap
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)Network Programming: Data Plane Development Kit (DPDK)
Network Programming: Data Plane Development Kit (DPDK)
 
PACKET Sniffer IMPLEMENTATION
PACKET Sniffer IMPLEMENTATIONPACKET Sniffer IMPLEMENTATION
PACKET Sniffer IMPLEMENTATION
 
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
D. Fast, Simple User-Space Network Functions with Snabb (RIPE 77)
 
Spying on the Linux kernel for fun and profit
Spying on the Linux kernel for fun and profitSpying on the Linux kernel for fun and profit
Spying on the Linux kernel for fun and profit
 
Andrea Righi - Spying on the Linux kernel for fun and profit
Andrea Righi - Spying on the Linux kernel for fun and profitAndrea Righi - Spying on the Linux kernel for fun and profit
Andrea Righi - Spying on the Linux kernel for fun and profit
 
BSides London - Scapy Workshop
BSides London - Scapy WorkshopBSides London - Scapy Workshop
BSides London - Scapy Workshop
 
Start Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New RopeStart Wrap Episode 11: A New Rope
Start Wrap Episode 11: A New Rope
 
Berkeley Packet Filters
Berkeley Packet FiltersBerkeley Packet Filters
Berkeley Packet Filters
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from Python
 
BUD17-300: Journey of a packet
BUD17-300: Journey of a packetBUD17-300: Journey of a packet
BUD17-300: Journey of a packet
 
Linux Tracing Superpowers by Eugene Pirogov
Linux Tracing Superpowers by Eugene PirogovLinux Tracing Superpowers by Eugene Pirogov
Linux Tracing Superpowers by Eugene Pirogov
 
Plan 9カーネルにおけるTCP/IP実装(未完)
Plan 9カーネルにおけるTCP/IP実装(未完)Plan 9カーネルにおけるTCP/IP実装(未完)
Plan 9カーネルにおけるTCP/IP実装(未完)
 
Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021Let's trace Linux Lernel with KGDB @ COSCUP 2021
Let's trace Linux Lernel with KGDB @ COSCUP 2021
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
Debug generic process
Debug generic processDebug generic process
Debug generic process
 

More from liu qiang

Erlang培训
Erlang培训Erlang培训
Erlang培训liu qiang
 
46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)
46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)
46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)liu qiang
 
9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52c9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52cliu qiang
 
9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52c9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52cliu qiang
 
Work of liuqiang
Work of liuqiangWork of liuqiang
Work of liuqiangliu qiang
 
Work Of Liuqiang
Work Of LiuqiangWork Of Liuqiang
Work Of Liuqiangliu qiang
 
Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)
Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)
Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)liu qiang
 
Architect Dec By Infoq
Architect  Dec By InfoqArchitect  Dec By Infoq
Architect Dec By Infoqliu qiang
 

More from liu qiang (9)

Erlang培训
Erlang培训Erlang培训
Erlang培训
 
46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)
46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)
46bcbf7a 1d08-4ac6-8084-245a80fef5ab(2)
 
9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52c9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52c
 
9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52c9082e973 7403-4939-8860-e979214fa52c
9082e973 7403-4939-8860-e979214fa52c
 
Work of liuqiang
Work of liuqiangWork of liuqiang
Work of liuqiang
 
Rack
RackRack
Rack
 
Work Of Liuqiang
Work Of LiuqiangWork Of Liuqiang
Work Of Liuqiang
 
Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)
Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)
Analytics Www.Iyuwa.Com 20091118 20091218 (Pageviews Report)
 
Architect Dec By Infoq
Architect  Dec By InfoqArchitect  Dec By Infoq
Architect Dec By Infoq
 

Recently uploaded

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Recently uploaded (20)

Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

Libpcap

  • 1. 1 libpcaplibpcap Packet Sniffing for Security Alisa Neeman
  • 2. 2 IntroductionIntroduction 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. 3 AgendaAgenda Installing libpcap C stuff Basic libpcap program – Grab a device to sniff – Filters/Event Loops – Packet structure
  • 4. 4 Getting the libraryGetting the library Linux: http://sourceforge.net/projects/libpcap/ VC++: Winpcaphttp://winpcap.polito.it/install/ default.htm Cygwin: Wpcap (haven’t tried this) http://www.rootlabs.com/windump/
  • 5. 5 Install on LinuxInstall on Linux gunzip libpcap-0.7.1.tar.gz tar -xvf libpcap-0.7.1.tar cd libpcap-0.7.1 ./configure make
  • 6. 6 Install for Windows VC++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 FilesMicrosoft Visual StudioVC98Include (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. 7 VC++, cont’dVC++, cont’d You also need to add the lib files. Copy everything from wpdpack/Lib to C:Program FilesMicrosoft Visual StudioVC98Lib 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. 8 Avoiding C GotchasAvoiding 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 workn"); exit(-1); } /* if (thing_didnt_work) */
  • 9. 9 C cont’dC cont’d Output is formatted. char person[ ] = “baby”; printf(“give me %d, %sn”, 5, person); %d: int %x: hex %s: string %f: double
  • 10. 10 Get to the point!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. 11 Finally…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. 12 Overview of libpcapOverview 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. 13 What to include and how toWhat to include and how to compilecompile gcc sniff.c -lpcap –o sniff You must be root or admin  Some headers I’ve used. #include <pcap.h> #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include<netinet/if_ether.h> #include<netinet/in.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <arpa/inet.h> For Windows: #include <winsock.h>
  • 14. 14 Getting onto the NICGetting 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("%sn",errbuf); exit(1); } printf("DEV: %sn",dev);
  • 15. 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(): %sn",errbuf); exit(1); } Going Live!
  • 16. 16 Once live, capture a packet.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
  • 18. 18 Main Event LoopMain 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. 19 What is an ethernet header?What is an ethernet header? From #include<netinet/if_ether.h> 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. 20 NO!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 packetn", 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 packetn”, ntohs(eptr->ether_type), ntohs(eptr->ether_type)); }
  • 21. 21 Filter – we don’t need to seeFilter – we don’t need to see every packet!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_compilen"); exit(1); } if (pcap_setfilter(descr,&fp) == -1) { fprintf(stderr,"Error setting filtern"); exit(1); }
  • 22. 22 Some typical filtersSome typical filters ./sniff "dst port 80" ./sniff "src host 128.226.121.120" ./sniff "less 50" (grab all packets less than 50 bytes, such as???) ./sniff "ip proto udp“ (must use the escape character, , for protocol names)
  • 23. 23 ReferencesReferences • http://www.cet.nau.edu/~mc8/Socket/Tutorials/section1.h • http://www.tcpdump.org/pcap.htm • http://mixter.void.ru/rawip.html Windows: • http://www.coders.eu.org/manualy/win/wskfaq/e xamples/rawping.html

Editor's Notes

  1. What is the difference?
  2. Questions? We want more than one packet, right? How would we do that? What is a packet? What packet do we want?