Skip to content

Instantly share code, notes, and snippets.

@keegoo
Last active August 27, 2022 16:10
Show Gist options
  • Save keegoo/25ad3e01e416bb597725c0b4c9dfef68 to your computer and use it in GitHub Desktop.
Save keegoo/25ad3e01e416bb597725c0b4c9dfef68 to your computer and use it in GitHub Desktop.
记录如何写一个http proxy in Rust的学习笔记

Briefing

在Mac和Linux下感觉没有好用的http capture的工具。

其实需求很简单:可以monitor某个ip,监控其中发生的http的traffice,并实时的在Web UI中显示出来。

用过Windows平台的Fiddler,感觉非常易用。

想用Rust来写,不过之前对于Rust和网络编程都没有什么经验。

用此gist来一步一步的记录整个学习过程。

2022年6月20号,新加坡。

@keegoo
Copy link
Author

keegoo commented Aug 24, 2022

程序是从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()看起。

@keegoo
Copy link
Author

keegoo commented Aug 24, 2022

interact()做了一下几件事:

  • parse and check arguments with getopt.
  • init_session()。将libs/all.py中的所有modules都读取到SESSION中。同时加入config的信息,SESSION = {"conf": conf}。其中SESSION是一个dictionary。
  • 千方百计的要在终端上打出一个logo,logo的大小会根据终端的column来决定。
  • 打开一个可以交互的console。使用code.interact(banner=banner_text, local=SESSION),因为SESSION中存了scapy中所有的modules,所以可以直接在这个console中使用这些模块了。

@keegoo
Copy link
Author

keegoo commented Aug 27, 2022

上面提到./run_scapy会启动一个可交互的console,下面是一个如何在console中操作数据包的例子:

send(IP(dst="1.2.3.4")/TCP(dport=502, options=[("MSS", 0)]))

其中

  • IP和TCP都是class,定义在scapy/layers/inet.py中。
  • send应该是定义在scapy/sendrecv.py中。

(to be continue ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment