18
Things You Should Know for Network Programming Anry Lu 2014/10/22

Things you should know for network programming

  • Upload
    anry-lu

  • View
    116

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Things you should know for network programming

Things You Should Know for Network Programming

Anry Lu2014/10/22

Page 2: Things you should know for network programming

TCP & UDP

Page 3: Things you should know for network programming

UDP (just IP + Port Number)

Page 4: Things you should know for network programming

TCP is far more complicated!

Page 5: Things you should know for network programming

TCP Quick ReviewTCP provides reliable, ordered and error-checked

delivery of data.

Page 6: Things you should know for network programming

3-Way Handshaking

Page 7: Things you should know for network programming

Ack, Retransmission & Sliding Window

Page 8: Things you should know for network programming

Flow Control (AIMD)

● check the available flow control on your system ○ sysctl -a | grep tcp_allowed_congestion_control

Page 9: Things you should know for network programming

TCP Options

● Maximum Segment Size● Select Ack● Explicit Congestion Notification● Window Scaling● Timestamp● Keepalive● ...

Page 10: Things you should know for network programming

The life-cycle of a TCP connection.(use netstat or lsof to see the state)

Page 11: Things you should know for network programming

What the state means?● SYN_SENT

○ packets are droppediptables -t filter -t filter -A OUTPUT -p tcp --dst 192.168.68.8 -j DROPnc 192.168.68.8 80

○ solution■ check your network

● ESTABLISHED○ usually means the connections is valid○ if the connection is dead, it takes 7,200 seconds to know

■ net.ipv4.tcp_keepavlid_time

Page 12: Things you should know for network programming

● CLOSE_WAIT○ your code doesn’t handle connection well

(note: all data sent in this state are just dropped)○ server

import socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_address = ('127.0.0.1', 10000)sock.bind(server_address)sock.listen(1)connection, client_address = sock.accept()connection.close()

○ clientimport socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_address = ('127.0.0.1', 10000)sock.connect(server_address)

○ solution■ check your source code

Page 13: Things you should know for network programming

● TIME_WAIT○ it’s a normal state to avoid sending RST and

interfering with new connections○ lasts for 2*MSL after close○ may cause port starvation on server○ solution

■ linger option(use this carefully)

■ net.ipv4.tcp_tw_resuse■ SO_REUSEADDR or SO_REUSEPORT

Page 14: Things you should know for network programming

How to Debug - Sniffer Tools

● wireshark or tcpdump○ for normal socket

sudo tcpdump -i eth5

● socat○ for unix socket

cd /share/CACHEDEV1_DATA/.qpkg/CloudLink/tmp/mv tunnel_agent_monitor.sock tunnel_agent_monitor.sock.origsocat -t 100 -x -v UNIX-LISTEN:./tunnel_agent_monitor.sock,mode=777,reuseaddr,fork UNIX-CONNECT:./tunnel_agent_monitor.sock.orig

Page 15: Things you should know for network programming

What if no sniffer available?

● Sniffer Machine○ sysctl -w net.ipv4.ip_forward=1○ tcpdump -i eth0 host 192.168.68.8

● Target Machine○ polite way

sudo sysctl -w net.ipv4.conf.all.accept_redirects=0sudo sysctl -w net.ipv4.conf.eth5.accept_redirects=0sudo route add -host 192.168.68.8 gw 192.168.68.80

○ hacker way (execute on the sniffer machine)arpspoof -t 192.168.68.51 192.168.68.254

Page 16: Things you should know for network programming

What if SSL is enabled?● Man in the middle proxy

○ http://mitmproxy.org/○ only works if certificate is not checked

mitmproxy -T --hostiptables -t nat -A PREROUTING -s 192.168.68.51 -d 192.168.68.8 -p tcp --dport 8080 -j REDIRECT --to-port 8080iptables -t nat -A PREROUTING -s 192.168.68.51 -d 192.168.68.8 -p tcp --dport 443 -j REDIRECT --to-port 8080

Page 17: Things you should know for network programming

How to disconnect a connection?

● ARP Spoofing + iptables● Faking TCP packets

○ use tcpdump to observer connectiontcpdump -S -n host 192.168.68.63 and tcp

○ use the libnet sample code to fake packets sudo ./tcp2 -s 192.168.68.63.8080 -d 192.168.68.51.52351 -n $SEQ_NO -a $ACK_NO -f "TH_FIN|TH_ACK" sudo ./tcp2 -s 192.168.68.63.8080 -d 192.168.68.51.52351 -n $SEQ_NO -f "TH_RST"

Page 18: Things you should know for network programming

Reference

● dsniff○ http://www.monkey.org/~dugsong/dsniff/

● ettercap○ http://ettercap.github.io/ettercap/