Packet Mastering Hack in the Box, 2004 jose nazario.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Intermediate TCP/IP TCP Operation.
Packet Mastering the Monkey Way Recon, 2005 jose nazario.
Introduction1-1 message segment datagram frame source application transport network link physical HtHt HnHn HlHl M HtHt HnHn M HtHt M M destination application.
UDP & TCP Where would we be without them!. UDP User Datagram Protocol.
CSE551: Computer Network Review r Network Layers r TCP/UDP r IP.
Page: 1 Director 1.0 TECHNION Department of Computer Science The Computer Communication Lab (236340) Summer 2002 Submitted by: David Schwartz Idan Zak.
Source Port # (16)Destination Port # (16) Sequence Number (32 bits) Acknowledgement Number (32 bits) Hdr Len (4) Flags (6)Window Size (16) Options (if.
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)
TCP. Learning objectives Reliable Transport in TCP TCP flow and Congestion Control.
Chapter 3 Review of Protocols And Packet Formats
WXES2106 Network Technology Semester /2005 Chapter 8 Intermediate TCP CCNA2: Module 10.
Bro: A System for Detecting Network Intruders in Real-Time Presented by Zachary Schneirov CS Professor Yan Chen.
Transport Layer TCP and UDP IS250 Spring 2010
Defining Network Protocols Application Protocols –Application Layer –Presentation Layer –Session Layer Transport Protocols –Transport Layer Network Protocols.
IP-UDP-RTP Computer Networking (In Chap 3, 4, 7) 건국대학교 인터넷미디어공학부 임 창 훈.
Sniffing network traffic in Python
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
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.
ECE 4110 – Internetwork Programming Client-Server Model.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
University of Calgary – CPSC 441.  UDP stands for User Datagram Protocol.  A protocol for the Transport Layer in the protocol Stack.  Alternative to.
Agostinho L S Castro Telecommunications and Multimedia Unit BPF - BSD Packet Filter.
LWIP TCP/IP Stack 김백규.
TCP/IP Essentials A Lab-Based Approach Shivendra Panwar, Shiwen Mao Jeong-dong Ryoo, and Yihan Li Chapter 5 UDP and Its Applications.
CSTP FS01CS423 (cotter)1 Protocols 2 References: RFC’s 791, 793, 768, 826.
1 LAN Protocols (Week 3, Wednesday 9/10/2003) © Abdou Illia, Fall 2003.
TCP : Transmission Control Protocol Computer Network System Sirak Kaewjamnong.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
Chapter 6-2 the TCP/IP Layers. The four layers of the TCP/IP model are listed in Table 6-2. The layers are The four layers of the TCP/IP model are listed.
1 IP : Internet Protocol Computer Network System Sirak Kaewjamnong.
Network Programming Eddie Aronovich mail:
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Sniffer, tcpdump, Ethereal, ntop
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
PA3: Improving Performance with I/O Multiplexing Part 1-1: Nov. 7, Part 1-2: Nov. 10 Part 2-1: Nov. 17, Part 2-2: Nov.20.
Slide #1 CIT 380: Securing Computer Systems TCP/IP.
PLUS 내부 세미나 1/22 The Libnet Library 이병영 2004/05/06.
1 CSE 5346 Spring Network Simulator Project.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
Network Layer by peterl. forwarding table routing protocols path selection RIP, OSPF, BGP IP protocol addressing conventions datagram format packet handling.
A network primer (or refresher) Henning Schulzrinne (based on slides from Kurose/Ross)
1 Kyung Hee University Chapter 11 User Datagram Protocol.
IP Internet Protocol. IP TCP UDP ICMPIGMP ARP PPP Ethernet.
Packet Switch Network Server client IP Ether IPTCPData.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Program Training 10/27/2010. What is a Socket ? An interface between an application process and transport layer (TCP or UDP). 2.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
Introduction To TCP/IP Networking Mr. Zeeshan Ali, Asst. Professor
Introduction to TCP/IP networking
or call for office visit, or call Kathy Cheek,
5. End-to-end protocols (part 1)
or call for office visit,
CS 1652 Jack Lange University of Pittsburgh
Transport layer API: Socket Programming
IP : Internet Protocol Surasak Sanguanpong
Wide Area Networks and Internet CT1403
Design of a Diversified Router: November 2006 Demonstration Plans
Network Architecture Models: Layered Communications
Process-to-Process Delivery: UDP, TCP
Socket Programming with UDP
Transport Layer 9/22/2019.
Chapter 4: outline 4.1 Overview of Network layer data plane
Presentation transcript:

Packet Mastering Hack in the Box, 2004 jose nazario

Raw IP vs socket based networking

capturesend drivereassemble

pcapdnet eventnids

LinuxUN*XMac OS XWindows C/C++/C#PythonTclPerl cross platform cross language

libevent – event wrapper library Abstract event framework uses poll(), select(), epoll(), kqueue() optimized for target platform at libevent compile time write once, optimized everywhere handles signals and alarms, too works on file descriptors libevent programming basics: 1.Initialize event framework: event_init() 2.Create event: event_set() 3.Install event into list to check: event_add() 4.Run the events: event_dispatch()

Readable? Writable? Signal? Alarm? Start The Main Event Loop Wait some period of time before moving on to the next event check

Execute callback, return to main event loop. Pass data to the event. Example: data to write to a file descriptor. When an Event Is Caught Event is removed from queue upon completion, unless EV_PERSIST is set or the event is rescheduled.

Events vs threads Spawn threads for tasks (read, write, process) Any thread can wait until it has input, overall program still moves Threads are difficult to debug Threads can deadlock against each other Not all functions are thread safe, clobbering data Onus is on you to choreograph a careful dance, easy to mess up Main thread of execution loops over possible actions Actions include: read, write, signal, alarm Every possible action has an associated “callback” Callbacks process data Easy to debug, look at active event handler Deadlocks don’t happen, data not clobbered by stray thread Program is always doing something, or looking for something to do Both used in high performance programming Both excellent for high performance packet actions

Networking Stack TCPUDP IP layer Link layer (eg Ethernet, PPP)

Ethernet type (16 bits) Destination addr (8 bits) Source addr (8 bits) Ethernet Header Structure

Header checksum (16 bits) Destination IP (32 bits) Options (if any) Data (payload) Source IP (32 bits) Protocol (8 bits)TTL (8 bits) IPID (16 bits)Fragment offset (13 bits) Flags (3 bits) Total length (16 bits)ToS (8 bits) Header Length (4 bits) Version (4 bits) IP Header Structure

Source port (16 bits)Destination port (16 bits) UDP length (16 bits)UDP checksum (16 bits) Data (payload) UDP Header Structure

Options (if any) TCP Checksum (16 bits) Data (payload) Urgent pointer (16 bits) Window size (16 bits) Acknowledgement number (32 bits) Sequence number (32 bits) Source port (16 bits)Destination port (16 bits) TCP Header Structure Header len (4 bits) Reserved (6 bits) UAPRSF

libpcap – packet capture Platform independent Flexible Relatively decent performance Very standard Order of operations 1. Create a pcap object: pcap_open_live() 2. Get data from the network, send to callback 3. Close pcap object: pcap_close()

pcap_loop() pcap_dispatch() pcap_next() pcap packet header packet (layer 2 and above) Packet processing callback Layer 2Layer 3Layer 4 and above

libdnet – low level networking Simple interface to network, kernel material Cross platform (Win, OS X, UN*X) Easy to use interface Libdnet basics: 1.Open network object: ip_open() 2.Allocate packet memory 3.Construct TCP packet: tcp_pack_header() 4.Construct IP packet: ip_pack_header() 5.Checksum: ip_checksum() 6.Write the packet: ip_send() 7.Close the object: ip_close()

IP Addressing Routing Ethernet Blobs IPv6 Arp Firewalling Interfaces Random Numbers

tcp_pack_hdr(hdr, sport, dport, seq, ack, flags, win, urp)

ip_open()eth_open() Routing table lookup ip_pack_header() ip_checksum() Packet sent ip_send() eth_send() ip_pack_header() ip_checksum()

pcap, event, dnet example: jscan TCP SYN port scanner OS fingerprinting Passive fingerprinting Passive port scanning Active port scanning Can be decoupled

jscan Program flow Open pcap object (for receiving) Open IP object (for sending) Create and set send and receive events Send callback Receive callback –Fingerprint OS using the packet –Report results Loop until all ports scanned or stopped

int main (int argc, char *argv[]) { ctx.rand = rand_open(); ctx.p = pcap_open_live(intf, 1500, (ctx.flags == SCAN_FLAGS_PASSIVE), 500, ebuff); if (ctx.p == NULL) err(1, "pcap_open_live"); ctx.dl_len = pcap_dloff(ctx.p); printf("scan completed in %d seconds.\n", time(NULL) - start); return (1); }

event setup: event_init(); ctx.tv.tv_sec = 0; ctx.tv.tv_usec = 500; p_fd = pcap_fileno(ctx.p); event_set(&ctx.recv_ev, p_fd, EV_READ, _recv, (void *) &ctx); event_add(&ctx.recv_ev, &ctx.tv); if (ctx.flags == SCAN_FLAGS_ACTIVE) { ctx.ip = ip_open(); if (ctx.ip == NULL) err(1, "ip_open() failed.."); event_set(&ctx.send_ev, p_fd, EV_WRITE, _send, (void *) &ctx); event_add(&ctx.send_ev, &ctx.tv); ctx.dport = 1; } event_dispatch();

receive callback: static void _recv(int fd, short event, void *arg) { struct myctx *ctx = (struct myctx *) arg; struct pcap_pkthdr ph; u_char *pread; if ((ctx->flags == SCAN_FLAGS_ACTIVE) && (ctx->dport > 65535)); else /* reschedule */ event_add(&ctx->recv_ev, &ctx->tv); if ((pread = (u_char *) pcap_next(ctx->p, &ph)) != NULL) report(pread, ctx); return; }

send callback: static void _send(int fd, short event, void *arg) { struct myctx *ctx = (struct myctx *) arg; struct jscan_pkt *pkt; u_char buf[BUFSIZ]; int len = IP_HDR_LEN + TCP_HDR_LEN, dport; if (ctx->dport > 65535) return; event_add(&ctx->send_ev, &ctx->tv); pkt = (struct jscan_pkt *) buf; ip_pack_hdr(&pkt->pkt_hdr_i.ip, IP_TOS_LOWDELAY, len, rand_uint16(ctx->rand), 0, 128, IP_PROTO_TCP, ctx->src.addr_ip, ctx->dst.addr_ip); tcp_pack_hdr(&pkt->pkt_hdr_t.tcp, rand_uint16(ctx->rand), ctx->dport, rand_uint32(ctx->rand), rand_uint32(ctx->rand), TH_SYN, rand_uint16(ctx->rand), 0); ip_checksum(pkt, len); ip_send(ctx->ip, pkt, len); ctx->dport += 1;/* we SYNed that port */ return; }

report callback (2 pages): static void report(u_char * packet, void *arg) { struct myctx *ctx = (struct myctx *) arg; static struct ip_hdr *ip_h; u_char *tmp; const char *p; struct addr ip_src; static struct entry *np, *n2; tmp = packet + ctx->dl_len; ip_h = (struct ip_hdr *) tmp; if (ip_h->ip_v != 4) return; p = inet_ntoa(ip_h->ip_src); if ((addr_aton(p, &ip_src)) == -1) return; /* * if it's a passive scan, don't care about the src * address. if it's an active scan, make sure it was the * dest we specified. make sure it's a TCP packet, too, * and has SA set. */

if (((ctx->flags == SCAN_FLAGS_PASSIVE) || ((addr_cmp(&ip_src, &(ctx->dst))) == 0)) && (ip_h->ip_p == IP_PROTO_TCP)) { struct tcp_hdr *tcp_h = (struct tcp_hdr *)(tmp + IP_HDR_LEN); if (tcp_h->th_flags == 0x12) { /* SYN ACK */ struct servent *serv; char *s_name = "unknown", *os = NULL; if (ctx->osfile != NULL) os = osprint(ctx, ntohs(tcp_h->th_win), ip_h->ip_ttl, ip_h->ip_off, ntohs(ip_h->ip_len)); serv = getservbyport(tcp_h->th_sport, "tcp"); if (serv != NULL) s_name = strdup(serv->s_name); printf("%-16s %35s %15s %6d/tcp\n", addr_ntoa(&ip_src), os ? os : “unknown", s_name, htons(tcp_h->th_sport)); } return; }

jscan output $ sudo jscan -t passive -i fxp0 -f compat/pf.os scan started, type is passive, listening on fxp unknown www 80/tcp FreeBSD www 80/tcp $ sudo jscan -t active -s d i fxp0 -f compat/pf.os scan started, type is active, listening on fxp Linux 2.0.3x ssh 22/tcp Linux 2.0.3x whois 43/tcp Linux 2.0.3x auth 113/tcp Linux 2.0.3x bgp 179/tcp scan completed. total execution time was 70 seconds.

libnids – reassemble IP streams NIDS “E” box (event generation box) Userland TCP/IP stack Based on Linux IP stack Uses libpcap, libnet internally IP fragment reassembly

Kernel IP stack Userland

Kernel IP stack Userland IP stack Libnids

libnids Basics Initialize –nids_init() Register callbacks –nids_register_tcp() –nids_regster_ip() –nids_regiser_udp() Run! –nids_run() React –nids_kill_tcp()

nids_run() UDP callbackTCP callbackIP callback TCP stream object: - TCP state - client data - server data - source IP, port - dest IP, port - seq, ack, etc … UDP packet: - source IP, port - dest IP, port - UDP payload IP packet - struct IP packet - contains upper layers

libnids TCP states NIDS_JUST_ESTABLISHED –New TCP connected state (3WHS) –Must set stream->{client,server}.collect=1 to get stream payload collected NIDS_DATA –Data within a known, established TCP connection NIDS_RESET, NIDS_CLOSE, NIDS_TIMED_OUT –TCP connection is reset, closed gracefully, or was lost libnids doesn’t expose SYN_SENT, FIN_WAIT, etc …

Example libnids code: jflow jflow –Pcap to NetFlow summaries –Daemonizes –Sends to a receiving host over UDP Limitations of jflow –Not very lightweight –Inaccurate for some things

int main (int argc, char *argv[]) { nids_init(); nids_register_ip(monitor_ip); nids_run(); return(0); }

ip callback (truncated): void monitor_ip(struct ip *pkt) { struct ip_record rec; int i; rec.rec.srcaddr = (u_int)(pkt->ip_src.s_addr); rec.rec.dstaddr = (u_int)(pkt->ip_dst.s_addr); rec.rec.nexthop = inet_addr(" "); rec.rec.dOctets = htonl(pkt->ip_len); rec.rec.pad = 0x0; rec.rec.prot = pkt->ip_p; rec.rec.tos = 0x0; rec.rec.tcp_flags = 0x0; rec.rec.pad_2 = 0x0; rec.rec.pad_3 = 0x0; for (i = 0; i < 4; i++) rec.rec.reserved[i] = 0x0; export_ip_record(&rec); return; }

export record: void export_ip_record(struct ip_record *rec) { time_t now; /* fill out the header */ now = time(NULL); rec->hdr.version = htons(1); rec->hdr.count = htons(1); rec->hdr.SysUptime = htonl(get_uptime()); rec->hdr.unix_secs = htonl(now); rec->hdr.unix_nsecs = 0; /* XXX */ if (write(ctx.u, rec, sizeof(struct ip_record)) < sizeof(struct ip_record)) warn("ip_export_record(): short write()"); else ctx.count += 1; return; }

jflow output $ sudo tcpdump -lni fxp0 -s1500 -Tcnfp udp port :21: NetFlow v1, uptime, , 2 recs started , last :80 > :37116 >> tos 0, 623 (623 octets) started , last :37116 > :80 >> tos 0, 4851 (4851 octets)... 11:21: NetFlow v1, uptime, , 1 recs started , last :137 > :137 >> tos 0, 1 (50 octets) (ttl 64, id 8693)

Performance considerations 1 … 2…. 3…. 4…. … O(n) List Loop: 1. open file 2. malloc() 3. parse file

Interrupts Hardware checksumming Memory management

Resources jtrace}

Additional Resources Stevens, TCP/IP Illustrated vols 1 and 2 Schiffman, Building Open Source Network Security Tools RFCs from the IETF

pynids example: VersionDetect : 80: Apache/1.3.31(Unix) AxKit/1.61 DAV/1.0.3 mod_perl/1.29 mod_ssl/ OpenSSL/0.9.7d : 80: DCLK-AdSvrAdSvr : 80: Microsoft-IIS/ : 80: Apache : 80: Apache/ (Unix) PHP/ : 80: Microsoft-IIS/ : 80: Apache/ : 80: Apache/ (Unix) : ssh: SSH-2.0-OpenSSH_3.6p1 Microsoft Outlook Small tool to grab client and server banner strings Useful to inventory a network passively

#! /usr/bin/env python # VersionDetect.py, copyright © 2004 jose nazario import os, pwd, string, sys seen = [] def main(): nids.param("scan_num_hosts", 0) if not nids.init(): print "error -", nids.errbuf() sys.exit(1) (uid, gid) = pwd.getpwnam(“nobody”)[2:4] os.setgroups([gid,]) os.setgid(gid) os.setuid(uid) if 0 in [os.getuid(), os.getgid()] + list(os.getgroups()): print "error - drop root, please!“ sys.exit(1) nids.register_tcp(handleTcpStream) try: nids.run() # loop forever except KeyboardInterrupt: sys.exit(1)

def handleTcpStream(tcp): end_states=(nids.NIDS_CLOSE,nids.NIDS_TIMEOUT,nids.NIDS_RESET) if tcp.nids_state == nids.NIDS_JUST_EST: ((src, sport), (dst, dport)) = tcp.addr if dport in (80, 8000, 8080, 22, 2222, 2022, 25, 587): tcp.client.collect = 1 tcp.server.collect = 1 elif tcp.nids_state == nids.NIDS_DATA: # keep all of the stream's new data tcp.discard(0) elif tcp.nids_state in end_states: headers = string.split(tcp.client.data, "\n") for header in headers: if tcp.addr[1][1] in (22, 2222, 2022): if "SSH-" in header: # SSH client string if tcp.addr[0][0] not in seen: print "%16s:%4s: \t%s" \ %(tcp.addr[0][0], "ssh", header) seen.append(tcp.addr[0][0]) break