在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号,新加坡。
早期的时候kamene是scapy的一个面向Python3的版本;但后面scapy也加入了对Python3的支持,所以kamene渐渐失去维护了。
感觉scapy的代码更规整一点,所以转去读scapy的代码。
对Mac的支持显示在root/scapy/arch/__init__.py
代码中,如下:
from scapy.consts import LINUX, SOLARIS, WINDOWS, BSD
from scapy.config import conf
if LINUX:
from scapy.arch.linux import *
elif BSD:
from scapy.arch.unix import read_routes, read_routes6, in6_getifaddr
from scapy.arch.bpf.core import *
if not conf.use_pcap:
# Native
from scapy.arch.bpf.supersocket import *
conf.use_bpf = True
elif SOLARIS:
from scapy.arch.solaris import *
elif WINDOWS:
from scapy.arch.windows import *
from scapy.arch.windows.native import *
其中,root/scapy/consts.py
LINUX = platform.startswith("linux")
OPENBSD = platform.startswith("openbsd")
FREEBSD = "freebsd" in platform
NETBSD = platform.startswith("netbsd")
DARWIN = platform.startswith("darwin")
SOLARIS = platform.startswith("sunos")
WINDOWS = platform.startswith("win32")
WINDOWS_XP = platform_lib.release() == "XP"
BSD = DARWIN or FREEBSD or OPENBSD or NETBSD
所以BSD,即root/scapy/arch/unix
和root/scapy/arch/bpf/core
中存在的才是支持Mac平台的代码。
libpcap是tcpdump
用来抓取数据包的函数库。你可以在tcpdump的官方网站中找到对libpcap的详尽介绍。这里面推荐Programming with pcap by Tim Carstens.
这里有一段对libpcap的介绍
The libpcap library was written as part of a larger program called TCPDump. The libpcap library allowed developers to write code to receive link-layer packets(Layer 2 in the OSI model) on different flavors of UNIX operating systems without having to worry about the idiosyncrasy of different operating system's network cards and drivers. Essentially, the libpcap library grabs packets directly from the network cards, which allowed developers to write programs to decode, display, or log the packets.
才知道:
还是要对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 ...)
继续看了下
root/kamene/arch/linux.py
中的内容,下面的代码(特别是get_working_if()
)用来获取正在工作中的网口。其中:
/proc/net/dev
中可以获取网络端口统计信息。可以参考what is /proc/<pid>/net/dev 和 Linux下查看网络流量信息两篇文章。from fcntl import ioctl
中,fcntl
模块是基于文件描述符来进行文件控制和I/O控制。它是Unix系统调用fcntl()和ioctl()的接口。可以参见Python官方文档。bits/ioctls.h
和if.h
,查了下应该是Linux代码中的头文件,定义了具体的数据格式。/proc/net/dev
文件。所以此代码应该是仅试用Linux。因为Scapy是跨平台的,所以要找一下Mac下的平台代码在哪里。猜测是在root/kamene/arch/unix.py中 。