Packet Sniffing and Spoofing

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP Advanced Unix Programming Raw Sockets Datalink Access Chapters 25, 26.
Advertisements

Socket Programming 101 Vivek Ramachandran.
Datalink Access.
Umut Girit  One of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
Tactics to Discover “Passive” Monitoring Devices
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
1 Fall 2005 Hardware Addressing and Frame Identification Qutaibah Malluhi CSE Department Qatar University.
UDP - User Datagram Protocol UDP – User Datagram Protocol Author : Nir Shafrir Reference The TCP/IP Guide - ( Version Version.
Tutorial 8 Socket Programming
What is an instruction set?
Chapter 9 Classification And Forwarding. Outline.
Gursharan Singh Tatla Transport Layer 16-May
Introduction to InfoSec – Recitation 12 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
1 Introduction to Raw Sockets 2 IP address Port address MAC address TCP/IP Stack 67 Bootp DHCP OSPF protocol frame type UDP Port # TCP Port.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
LWIP TCP/IP Stack 김백규.
Internet Addresses. Universal Identifiers Universal Communication Service - Communication system which allows any host to communicate with any other host.
LWIP TCP/IP Stack 김백규.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CS-334: Computer.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Raw Sockets Vivek Ramachandran. A day in the life of Network Packet.
User Datagram Protocol (UDP) Chapter 11. Know TCP/IP transfers datagrams around Forwarded based on destination’s IP address Forwarded based on destination’s.
Lab 2: SSL Security Attack June 17, 2008 Hyun Jin Kim.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
EECB 473 DATA NETWORK ARCHITECTURE AND ELECTRONICS PREPARED BY JEHANA ERMY JAMALUDDIN Basic Packet Processing: Algorithms and Data Structures.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
DoS Suite and Raw Socket Programming Group 16 Thomas Losier Paul Obame Group 16 Thomas Losier Paul Obame.
Chapter 9 Hardware Addressing and Frame Type Identification 1.Delivering and sending packets 2.Hardware addressing: specifying a destination 3. Broadcasting.
Computer Architecture EKT 422
1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Dynamic Host Configuration Protocol (DHCP)
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
0x440 Network Sniffing.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
TCP/IP Illustrated, Volume 1: The Protocols Chapter 6. ICMP: Internet Control Message Protocol ( 월 ) 김 철 환
UDP : User Datagram Protocol 백 일 우
1 Device Controller I/O units typically consist of A mechanical component: the device itself An electronic component: the device controller or adapter.
UDP: User Datagram Protocol Chapter 12. Introduction Multiple application programs can execute simultaneously on a given computer and can send and receive.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
IP - Internet Protocol No. 1  Seattle Pacific University IP: The Internet Protocol Kevin Bolding Electrical Engineering Seattle Pacific University.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
SESSION HIJACKING It is a method of taking over a secure/unsecure Web user session by secretly obtaining the session ID and masquerading as an authorized.
Introduction to Networks v6.0
Chapter 11 User Datagram Protocol
Chapter 21 Address Mapping
Lab 2: Packet Capture & Traffic Analysis with Wireshark
Instructor Materials Chapter 5: Ethernet
LWIP TCP/IP Stack 김백규.
UNIX Sockets COS 461 Precept 1.
Objective: ARP.
LAN Vulnerabilities.
Layered Architectures
Net 323: NETWORK Protocols
Transport layer API: Socket Programming
UNIX Sockets Outline Homework #1 posted by end of day
COMPUTER NETWORKS CS610 Lecture-10 Hammad Khalid Khan.
ECEG-3202 Computer Architecture and Organization
Chapter 9 Instruction Sets: Characteristics and Functions
Net 323 D: Networks Protocols
ECEG-3202 Computer Architecture and Organization
Ch 17 - Binding Protocol Addresses
Internet Networking recitation #8
Last Class: Communication in Distributed Systems
Transport Layer 9/22/2019.
Format String Vulnerability
Virtual Private Network
Chapter 10 Instruction Sets: Characteristics and Functions
Presentation transcript:

Packet Sniffing and Spoofing

How Packets Are Received NIC (Network Interface Card) is a physical or logical link between a machine and a network Each NIC has a MAC address Every NIC on the network will hear all the frames on the wire NIC checks the destination address for every packet, if the address matches the cards MAC address, it is further copied into a buffer in the kernel After copying into the kernel buffer, the card interrupts the CPU informing it about the availability of a new packet

Promiscuous Mode The frames that are not destined to a given NIC are discarded When operating in promiscuous mode, NIC passes every frame received from the network to the kernel If a sniffer program is registered with the kernel, it will be able to see all the packets In Wi-Fi, it is called Monitor Mode It should be noted that elevated or root privileges are required to put a NIC card in promiscuous mode

BSD Packet Filter (BPF) BPF allows a user- program to attach a filter to the socket, which tells the kernel to discard unwanted packets. An example of the compiled BPF code is shown here. It is very inefficient to discard packets at the application level, so we try to filter packets as early as possible and hence use BPF The filter is often written in human readable format using Boolean operators and is compiled into a pseudo-code and passed to the BPF driver This low level code is then interpreted by the BPD pseudo-machine

BSD Packet Filter (BPF) setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) A compiled BPF pseudo-code can be attached to a socket through setsockopt() When a packet is received by kernel, BPF will be invoked An accepted packet is pushed up the protocol stack. See the diagram on the following slide.

Packet Flow With/Without Filters

Packet Sniffing Packet sniffing describes the process of capturing live data as they flow across a network Let’s first see how computers receive packets.

Receiving Packets Using Socket Create the socket Provide information about server We first create a socket and provide information about the server, including the UDP port and the IP address - The bind() system call configures the socket - By specifying INADDR_ANY, we bind the socket to all available IP addresses - We use recvfrom() to receive UDP packets Receive packets

Receiving Packets Using Raw Socket Creating a raw socket Capture all types of packets Enable the promiscuous mode The code using sockets can only receive packets that are intended for it A sniffer program needs to capture all packets flowing on the network cable (regardless of destination ip and port number) , this can be done use raw socket This program shows how to build a simple sniffer program using raw socket Wait for packets

Limitation of the Approach This program is not portable across different operating systems. Setting filters is not easy. The program does not explore any optimization to improve performance. The PCAP library was thus created. It still uses raw sockets internally, but its API is standard across all platforms. OS specifics are hidden by PCAP’s implementation. Allows programmers to specify filtering rules using human readable Boolean expressions.

Packet Sniffing Using the pcap API Filter Invoke this function for every captured packet Initialize a raw socket, set the network device into promiscuous mode. Compilation: We can compile the code using the following command: $ gcc –o sniff sniff.c -lpcap

Processing Captured Packet: Ethernet Header The packet argument contains a copy of the packet, including the Ethernet header. We typecast it to the Ethernet header structure. When got_packet() is invoked, the third argument, a pointer, points to the buffer that holds the packet The pointer has a unsigned char type, so the contents of the buffer are treated as a sequence of characters This buffer is actually an Ethernet frame Since we are only interested in the IP packet, we would like to check whether the type field of the ethernet header is IP One way is to find the offset of the type field from the beginning of the buffer This is quite inconvenient. Hence we us the concepts of struct and type casting A struct is a group of variables that are stored in contigious memory locations Since we know that the packet buffer sent to got_packet() contains an ethernet frame, we also have the ethernet header structure. We can do a type casting. Now we can access the field of the structure

Processing Captured Packet: IP Header Find where the IP header starts, and typecast it to the IP Header structure. Now we can easily access the fields in the IP header.

Further Processing Captured Packet If we want to further process the packet, such as printing out the header of the TCP, UDP and ICMP, we can use the similar technique. We move the pointer to the beginning of the next header and type-cast We need to use the header length field in the IP header to calculate the actual size of the IP header In the following example, if we know the next header is ICMP, we can get a pointer to the ICMP part by doing the following: We should note that the size of the IP header is not fixed because some packets header may contain an extra IP options field

Packet Spoofing When some critical information in the packet is forged, we refer to it as packet spoofing. Many network attacks rely on packet spoofing. Let’s see how to send packets without spoofing.

Sending Packets Without Spoofing Testing: Use the netcat (nc) command to run a UDP server on 10.0.2.5. We then run the program on the left from another machine. We can see that the message has been delivered to the server machine: The first step creates a socket The second step provides the destination information In the final step, sendto() is used to send out the UDP packet with the provided payload

Spoofing Packets Using Raw Sockets There are two major steps in packet spoofing: Constructing the packet Sending the packet out

Spoofing Packets Using Raw Sockets We use setsockopt() to enable IP_HDRINCL on the socket. For raw socket programming, since the destination information is already included in the provided IP header, we do not need to fill all the fields Using the typic socket to send packets, we do not have much control over the header fields We can only fill in a few header fields, such as the destination IP address and destination port number The other fields are set by operating system We use raw sockets to take control of being able to create the whole packet and asking the operating system to “leave us alone” Step 1: AF_INET indicates that this is for IPv4 For Second argument we typically use SOCK_DGRAM (for UDP) or SOCK_STREAN ( for TCP ). For raw socket, we use SOCK_RAW Third argument we choose IPPROTO_RAW indicating that we will supply the IP header Step 2: It should be noted that by setting the destination IP address, we help the kernel get the correct MAC address corresponding to the destination if the destination is on the same network Step 4: the second argument is a pointer to the buffer containing the whole ip packet Third argument is the size of the packet Fourth argument is to set flags The next two arguments are the pointers to the destination sockadd_in structure and its size For security reasons, only root processes and processes with the CAP_NET_RAW capabilities can create raw sockets Since the socket type is raw socket, the system will send out the IP packet as is.

Spoofing Packets: Constructing the Packet Fill in the ICMP Header Find the starting point of the ICMP header, and typecast it to the ICMP structure Fill in the ICMP header fields

Spoofing Packets: Constructing the Packet Fill in the IP Header Typecast the buffer to the IP structure Fill in the IP header fields Step 1: it should be noted that we are using the type casting technique. The ICMP echo-request header is very simple, we just have to fill in the type and checksum fields. The payload field is optional, we choose not to use that here. Step2: we set source and destination IP address to the values of our choice. We do not need to fill in the checksum field as that is calculated by the OS by itself Finally, send out the packet

Spoofing UDP Packets Constructing UDP packets is similar, except that we need to include the payload data now.

Spoofing UDP Packets (continued) Testing: Use the nc command to run a UDP server on 10.0.2.5. We then spoof a UDP packet from another machine. We can see that the spoofed UDP packet was received by the server machine.

Sniffing and Then Spoofing In many situations, we need to capture packets first, and then spoof a response based on the captured packets. Procedure (using UDP as example) Use PCAP API to capture the packets of interests Make a copy from the captured packet Replace the UDP data field with a new message and swap the source and destination fields Send out the spoofed reply

UDP Packet

UDP Packet (Continued)

Packing Sniffing Using Scapy

Spoofing ICMP & UDP Using Scapy

Sniffing and Then Spoofing Using Scapy

Packet Spoofing: Scapy v.s C Python + Scapy Pros: constructing packets is very simple Cons: much slower than C code C Program (using raw socket) Pros: much faster Cons: constructing packets is complicated Hybrid Approach Using Scapy to construct packets Using C to slightly modify packets and then send packets

Endianness Endianness: a term that refers to the order in which a given multi-byte data item is stored in memory. Little Endian: store the most significant byte of data at the highest address Big Endian: store the most significant byte of data at the lowest address Htons() and ntohs() convert data to appropriate format. Smallest addressable unit of memory – byte. Some datatypes consist of multiple byte in contagious memory eg int = 4 bytes. Short int = 2 bytes. Question: how are multiple bytes arranged in the memory: least significant bytes are put in the lower address or higher address? Different computer architectures does it differently. Eg: Atmel AVR32, IBM z/Architecture mainframes use Big-Endian byte order; x86 uses Little-Endian byte order

Endianness In Network Communication Computers with different byte orders will “misunderstand” each other. Solution: agree upon a common order for communication This is called “network order”, which is the same as big endian order All computers need to convert data between “host order” and “network order” . To solve the endianness-mismatching problem when data is send over the network, IANA defined a common order called network byte order, regardless of what endianness the host has. The conversion function figures out the endianness of the host, and conduct the conversion accordingly. Conversion macros have easy names: Htons means host to network byte order conversion Ntohl meand network to host byte order conversion s -> short integer l ->long integer

Summary Packet sniffing Packet spoofing using raw socket Using PCAP APIs Packet spoofing using raw socket Sniffing and the spoofing Endianness