linux下簡單抓包分析,linux抓包
有時候會遇到一些問題需要我們來抓包分析,當手頭又沒有專業的抓包工具的時候,可以用tcpdump來替代一下(一般的發行版都內建這個工具)
比如我們要分析一下eth0介面下跟192.168.7.188 這個目的IP地址22連接埠的發包情況
tcpdump -i eth0 dst 192.168.7.188 and port 22
tcpdump -i eth0 dst 192.168.7.188 and port 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
16:51:55.861670 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 2209699250:2209699442, ack 582840950, win 249, options [nop,nop,TS val 428468844 ecr 517889], length 192
16:51:55.868590 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 192:416, ack 1, win 249, options [nop,nop,TS val 428468851 ecr 517897], length 224
16:51:55.870577 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 416:624, ack 1, win 249, options [nop,nop,TS val 428468853 ecr 517899], length 208
16:51:55.872602 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 624:832, ack 1, win 249, options [nop,nop,TS val 428468855 ecr 517899], length 208
16:51:55.874576 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 832:1040, ack 1, win 249, options [nop,nop,TS val 428468857 ecr 517900], length 208
16:51:55.876573 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 1040:1248, ack 1, win 249, options [nop,nop,TS val 428468859 ecr 517900], length 208
16:51:55.878574 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 1248:1456, ack 1, win 249, options [nop,nop,TS val 428468861 ecr 517901], length 208
16:51:55.880574 IP 192.168.7.26.ssh > 192.168.7.188.37540: Flags [P.], seq 1456:1664, ack 1, win 249, options [nop,nop,TS val 428468863 ecr 517901], length 208
怎在linux下編寫抓包程式
你用SOCK_RAW的模式建立原始通訊端然後接收包。
剩下的就全是分析內容的事情了。。。。最多再用一下多線程。
用到linux下socket編程的最基本知識, 以及對網路通訊協定細節的瞭解。 前者隨便找本socket編程的書就很詳細了, 後者你因該懂。
——————
例如, 你需要include以下這些頭:
stdio.h,stdlib.h, unistd.h, sys/socket.h, sys/types.h, netinet/if_ether.h, netinet/in.h,
然後建立socket的時候用
socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP)),這樣就能用這個socket來監聽乙太網路的包。
然後迴圈調用recvfrom函數來聽這個socket的接受到的資料, 再分析就好了。
如果要嗅探別人機器的包, 就要用ARP欺騙了。。呵呵
給個Linux下抓包軟體
看Linux中強大的網路資料擷取分析工具——TcpDump