动态重写 TCP 流:有多困难?把所说的流转储怎么样?
我正在尝试编写一个 tcp 流“隧道”(类似于默认情况下的 SSH 句柄),但有一个例外,我必须在流经时重写某些信息。
我确信那里有类似的东西,但我一直没能找到它。 我有三个主要问题:
- 是否有一种简单的方法来保存 TCP 流以供观察? (即使用 netcat,或 ssh -r/-l/-D,或一起使用其他一些实用程序)
- 动态重写流有多难?
编辑:被重写的信息只是初始身份验证。
I'm trying to write a tcp stream 'tunnel' (similar to the ones SSH handles by default) but with one exception, I have to rewrite certain information as it flows through.
I'm certain there's something similar out there but I have not been able to find it.
I have three main questions:
- Is there an easy way to save a tcp stream for observation? (ie using netcat, or a ssh -r/-l/-D, or using some other utility alltogether)
- how hard is it to rewrite the stream on the fly?
Edit: The information being rewritten would be just the initial authentication.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以从现有(或容易找到的)公用设施中拼凑出带有测井功能的直通隧道。
在此示例中,连接到
http://localhost:8080/
将传递到http://localhost:80/
,并将日志数据传输到log
。TCPreen 工具专门用于此目的。
如果你有 root 权限,有很多分析器,例如 tcpdump 和 tcpflow 可以直接从网络捕获数据包,而无需重定向流量。
socat
还可以使用,cr
和,crnl
选项进行一些非常基本的流修改,即剥离/添加/替换\r
字符。无论如何,回到最初的问题……自从我编写任何 Java 以来已经有很长时间了,这完全未经测试,但是可以在重新传输之前修改流量的隧道并不困难。
A straight pass-through tunnel with logging can be cobbled together from existing (or easily found) utilities.
In this example, connecting to
http://localhost:8080/
will pass through tohttp://localhost:80/
, and log data transferred tolog
.The tool TCPreen is specialized for this exact purpose.
If you have root privileges, there are many analyzers such as tcpdump and tcpflow which can capture packets directly from the network, without having to redirect traffic.
socat
can also do some very basic stream modification with the,cr
and,crnl
options, which strip/add/replace\r
characters.In any case, back to the original question… It's been ages since I've written any Java, and this is totally untested, but a tunnel that can modify traffic before retransmitting isn't difficult.
非常肯定 Ettercap 支持 TCP 流的重写。
tcpdump 可以写出数据包捕获,然后您可以使用 Wireshark
如果您想以编程方式执行此操作,您可以检查它们各自的来源以了解从哪里开始。
Pretty sure Ettercap supports rewriting of TCP streams.
tcpdump can write out packet captures, which you could then later analyze using Wireshark
If you want to do it programmatically, you could inspect their respective sources to get ideas of where to start.
不是自吹自擂,但我在很久以前为异步 IO 编写的框架中编写了一些代码来完成此操作。关于代码的很多内容现在都有点过时了,但它确实有效。以下是其网页的链接:
我写的用于隧道的东西你想要的东西叫做PortForward,还有一些东西可以转储TCP流,但我忘了我叫它什么。由于框架的工作原理,它们可以很容易地组合起来。
如果您需要帮助使用它来实现该目标,我会回来。正如其他人指出的那样,不可能动态重写 SSL 流。因此,如果您的连接使用加密和/或 MAC(其中一种情况是 SSL),那么您就不走运了。
Not to toot my own horn, but I wrote some code to do exactly this in a framework I wrote a long time ago for asynchronous IO. There are a lot of things about the code that are kind of dated now, but it does work. Here's a link to the web page on it:
The thing I wrote that does the tunnel thing you want is called PortForward, and there's also something there that will dump out a TCP stream, but I forgot what I called it. They can be easily combined because of how the framework works.
I'll come back if you want help using it to accomplish that goal. As others have pointed out, it is impossible to re-write an SSL stream on the fly. So if your connection is using encryption and/or MACs (one way this would be true is if it were SSL) you're out of luck.
我不确定这是否是您所要求的,但是...
除非您拥有服务器 SSL 证书的私钥,否则您无法动态重写 SSL 流...或者您可以在某个时刻拦截它(在客户端或服务器地址空间),不受 SSL 保护。如果可以的话,SSL 就是浪费时间。
同样,如果您捕获 SSL 流的全部内容(双向),这对您没有任何好处,除非您拥有相关的私钥。
I'm not sure if this is what you are asking, but ...
You cannot rewrite an SSL stream on the fly unless you have the private key for the server's SSL cert ... or you can intercept it at some point (in the client or server address space) where it is not SSL protected. If you could, SSL would be a waste of time.
Similarly, if you capture the entire contents of an SSL stream (in both directions), it will do you no good, unless you have the relevant private keys.