在Mac和Linux下感觉没有好用的http capture的工具。
其实需求很简单:可以monitor某个ip,监控其中发生的http的traffice,并实时的在Web UI中显示出来。
用过Windows平台的Fiddler,感觉非常易用。
想用Rust来写,不过之前对于Rust和网络编程都没有什么经验。
用此gist来一步一步的记录整个学习过程。
2022年6月20号,新加坡。
在Mac和Linux下感觉没有好用的http capture的工具。
其实需求很简单:可以monitor某个ip,监控其中发生的http的traffice,并实时的在Web UI中显示出来。
用过Windows平台的Fiddler,感觉非常易用。
想用Rust来写,不过之前对于Rust和网络编程都没有什么经验。
用此gist来一步一步的记录整个学习过程。
2022年6月20号,新加坡。
才知道:
还是要对socket底层的东西有所了解,不然每次碰到address family,protocol,socket都不知道具体是什么关系。摘自The Open Group Base Specifications Issue 6.
(可能翻译的不准确)
所有的网络协议(protocol)都会关联到一个具体的address family。每个address family都会提供一些基础的services来支持协议(protocol)在特定网络设备中的具体实现。这些services包括packet fragmentation and reassembly, routing, addressing, and basic transport. 所以一个address family会包含多个protocols,每个protocol会对应到一个socket type。。。
...所有的network address都有一个通用的格式,叫sockaddr,被定义在
sys/socket.h
的文件中。然而每个address family都会在此基础上引入各自不同的结构。。。The field sa_family in the sockaddr structure contains the address family identifier, specifying the format of the sa_data area.
例如下面的例子,来自socket.h:
struct sockaddr {
sa_family_t sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
};
Socket有四种类型:SOCK_STREAM, SOCK_SEQPACKET, SOCK_DGRAM, and SOCK_RAW。
其中SOCK_STREAM
和SOCK_SEQPACKEt
功能相似,提供reliable, sequenced, full-duplex octet streams between the socket and a peer to which the socket is connected. 这里应该就是指TCP那种面向连接的。SOCK_DGRAM
和SOCK_RAW
比较相似,supports connectionless data transfer which is not necessarily acknowledged or reliable. 这里应该是指UDP那种没有链接的。
关于socket()
函数。
int socket(int domain, int type, int protocol);
// domain
// Specifies the address family used in the communications domain.
// e.g.
// AF_INET - Internet IP Protocol.
// AF_INET6 - IP version 6.
// AF_UNIX - Unix domain sockets.
// 大概还有100多个AF。。。
// type
// Specifies the type of socket to be created.
// protocol
// Specifies a particular protocol to be used with the socket.
// e.g.
// TCP
// UDP
// IIP
// Internet Control Message Protocol (ICMP)
程序是从run_scapy开始的。
$ sudo ./run_scapy -H
# Welcome to Scapy (2.4.4.dev221)
从run_scapy -> SCAPY/__init__.py -> SCAPY/main.py,可以看到所有传给run_scapy
的arguments最终都是在main.py
中的interact()
函数处理的。 所以从interact()看起。
interact()
做了一下几件事:
getopt
.init_session()
。将libs/all.py
中的所有modules都读取到SESSION
中。同时加入config
的信息,SESSION = {"conf": conf}
。其中SESSION
是一个dictionary。code.interact(banner=banner_text, local=SESSION)
,因为SESSION中存了scapy中所有的modules,所以可以直接在这个console中使用这些模块了。上面提到./run_scapy
会启动一个可交互的console,下面是一个如何在console中操作数据包的例子:
send(IP(dst="1.2.3.4")/TCP(dport=502, options=[("MSS", 0)]))
其中
class
,定义在scapy/layers/inet.py
中。scapy/sendrecv.py
中。(to be continue ...)
libpcap是
tcpdump
用来抓取数据包的函数库。你可以在tcpdump的官方网站中找到对libpcap的详尽介绍。这里面推荐Programming with pcap by Tim Carstens.这里有一段对libpcap的介绍