如何计算每个 TCP 连接(系统范围)发送和接收的字节数?

发布于 2024-10-12 03:24:52 字数 270 浏览 3 评论 0原文

例如,最新版本的 TCPView 具有这样的功能:显示每个 TCP 连接发送/接收的字节(启动 TCPView 时开始计数)。是否可以不进行数据包嗅探? windows为此提供了任何API吗?我还没有找到这样的性能计数器

此处描述了如何枚举所有连接

编辑:TDI 是否有助于接收每个套接字的传输统计信息?网络BIOS?任何链接在哪里挖掘?

e.g. recent versions of TCPView has such functionality: showing bytes sent/received per TCP connection (counting starts when TCPView is launched). is it possible w/o packet sniffering? does windows provides any API for this? I haven't found such Performance Counter

how to enumerate all connections are described here

EDIT: does TDI help to receive per-socket transfer statistics? NetBIOS? any links where to dig?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

迷乱花海 2024-10-19 03:24:52

所有,我基本上完全逆向了 tcpview 3.0.2 并根据我所学到的实现了与其相同的功能。

tcpview 使用 ETW 监视网络活动。

关键 API 是 StartTrace、OpenTrace、ProcessTrace。

使用 KERNEL_LOGGER_NAME 并启用 EVENT_TRACE_FLAG_NETWORK_TCPIP 标志。

然后,您可以从 EventCallback 检索网络活动数据,然后将其解析为 TcpIp_TypeGroup1 和其他结构。根据文档,这些结构仅支持vista。不过你可以在xp(逆向猜测)和2003(我的环境是2003,xp上没有测试)中调用并使用它。当然,所有这些结构都必须由您自己定义。

从vista开始,win提供了一些API来检索每个连接的统计信息。如GetPerTcpConnectionEStats、GetPerUdpConnectionEStats,您可以从MSDN获取更多详细信息。

另外,从 vista 中,您可以使用 RAW Socket 来完成相同的工作(我认为更精确)。在vista之前,RAW Socket无法检索SEND数据包,很遗憾。

all, I have basically fully reverse tcpview 3.0.2 and implement the same feature as its according to what I have learnt.

tcpview use ETW for monitoring network activity.

The key APIs are StartTrace, OpenTrace, ProcessTrace.

Use the KERNEL_LOGGER_NAME and enable EVENT_TRACE_FLAG_NETWORK_TCPIP flags.

Then you can retrieve network activity data from EventCallback, then parse it as TcpIp_TypeGroup1 and other structures. According to the document, these structures are only supported from vista. However you can call and use it in xp(guess from reverse) and 2003(My environment is 2003, no test on xp). Certainly you have to define all these structures by yourself.

From vista, win provides some APIs for retrieving every connections statistic information. Such as GetPerTcpConnectionEStats, GetPerUdpConnectionEStats, you can get more details from MSDN.

Also, from vista, you can use RAW Socket to finish the same work(more precise I think). Before vista, RAW Socket can't retrieve SEND packets, it's a pity.

稚气少女 2024-10-19 03:24:52

我也想实现这个功能,所以我逆向tcpview 3.0.2。

我发现,tcpview使用了一个WMI性能计数器MSNT_TcpIpInformation。

但MSNT_TcpIpInformation在xp和2003中正式不支持。

这里有说明,你可以参考一下。
http://www.scriptinternals.com/new/us/support/Internal /WMI_MSNT_TcpIpInformation.htm

顺便说一句,MSNT_TcpIpInformation 没有有关数据包的信息,因此 tcpview 每次都会递增已发送和已转发的数据包。
这是反汇编:

CPU Disasm
Address   Hex dump          Command                                           Comments
0040B41B  |.  83E8 02       SUB EAX,2                                         ; Switch (cases 2..3, 3 exits)
0040B41E  |.  74 29         JE SHORT 0040B449
0040B420  |.  83E8 01       SUB EAX,1
0040B423  |.  75 40         JNE SHORT 0040B465
0040B425  |.  8B57 1C       MOV EDX,DWORD PTR DS:[EDI+1C]                     ; Case 3 of switch Tcpview.40B41B
0040B428  |.  0196 90060000 ADD DWORD PTR DS:[ESI+690],EDX
0040B42E  |.  119E 94060000 ADC DWORD PTR DS:[ESI+694],EBX
0040B434  |.  8386 C0060000 ADD DWORD PTR DS:[ESI+6C0],1
0040B43B  |.  119E C4060000 ADC DWORD PTR DS:[ESI+6C4],EBX
0040B441  |.  5E            POP ESI
0040B442  |.  5F            POP EDI
0040B443  |.  5D            POP EBP
0040B444  |.  5B            POP EBX
0040B445  |.  83C4 3C       ADD ESP,3C
0040B448  |.  C3            RETN
0040B449  |>  8B47 1C       MOV EAX,DWORD PTR DS:[EDI+1C]                     ; Case 2 of switch Tcpview.40B41B
0040B44C  |.  0186 78060000 ADD DWORD PTR DS:[ESI+678],EAX
0040B452  |.  119E 7C060000 ADC DWORD PTR DS:[ESI+67C],EBX
0040B458  |.  8386 A8060000 ADD DWORD PTR DS:[ESI+6A8],1
0040B45F  |.  119E AC060000 ADC DWORD PTR DS:[ESI+6AC],EBX
0040B465  |>  5E            POP ESI                                           ; Default case of switch Tcpview.40B41B
0040B466  |.  5F            POP EDI

I want to implement this function also, so I reverse tcpview 3.0.2.

I found, tcpview use a WMI performance counter MSNT_TcpIpInformation.

But MSNT_TcpIpInformation is not supported in xp and 2003 officially.

here is the description, you can reference to.
http://www.scriptinternals.com/new/us/support/Internal/WMI_MSNT_TcpIpInformation.htm

by the way, MSNT_TcpIpInformation have no information about packets, so tcpview just increment sent and revd packets everytime.
here is the disassemble:

CPU Disasm
Address   Hex dump          Command                                           Comments
0040B41B  |.  83E8 02       SUB EAX,2                                         ; Switch (cases 2..3, 3 exits)
0040B41E  |.  74 29         JE SHORT 0040B449
0040B420  |.  83E8 01       SUB EAX,1
0040B423  |.  75 40         JNE SHORT 0040B465
0040B425  |.  8B57 1C       MOV EDX,DWORD PTR DS:[EDI+1C]                     ; Case 3 of switch Tcpview.40B41B
0040B428  |.  0196 90060000 ADD DWORD PTR DS:[ESI+690],EDX
0040B42E  |.  119E 94060000 ADC DWORD PTR DS:[ESI+694],EBX
0040B434  |.  8386 C0060000 ADD DWORD PTR DS:[ESI+6C0],1
0040B43B  |.  119E C4060000 ADC DWORD PTR DS:[ESI+6C4],EBX
0040B441  |.  5E            POP ESI
0040B442  |.  5F            POP EDI
0040B443  |.  5D            POP EBP
0040B444  |.  5B            POP EBX
0040B445  |.  83C4 3C       ADD ESP,3C
0040B448  |.  C3            RETN
0040B449  |>  8B47 1C       MOV EAX,DWORD PTR DS:[EDI+1C]                     ; Case 2 of switch Tcpview.40B41B
0040B44C  |.  0186 78060000 ADD DWORD PTR DS:[ESI+678],EAX
0040B452  |.  119E 7C060000 ADC DWORD PTR DS:[ESI+67C],EBX
0040B458  |.  8386 A8060000 ADD DWORD PTR DS:[ESI+6A8],1
0040B45F  |.  119E AC060000 ADC DWORD PTR DS:[ESI+6AC],EBX
0040B465  |>  5E            POP ESI                                           ; Default case of switch Tcpview.40B41B
0040B466  |.  5F            POP EDI
痴骨ら 2024-10-19 03:24:52

检查 WinSock LSP 示例项目,网址为 http://connect.microsoft.com/WNDP/Downloads

将在 nonifslsp\sockinfo.cpp 中找到一个示例,其中“说明如何开发能够对通过 TCP/IP 套接字传输的所有字节进行计数的分层服务提供程序。

Check the WinSock LSP Sample project at http://connect.microsoft.com/WNDP/Downloads

You will find a sample in nonifslsp\sockinfo.cpp which "illustrates how to develop a layered service provider that is capable of counting all bytes transmitted through a TCP/IP socket."

帥小哥 2024-10-19 03:24:52

netstat (netstatp) 的 sysinternals 版本可以执行此操作。 IIRC,它使用 SNMP 来收集信息。搜索网络并找到您喜欢的版本。文件名为 netstatp.c 和 netstatp.h
据我所知,Sysinternals 不再发布 netstatp。

您还可以访问此处并获取 tcpview 和/或 tcpconv 其中之一可用以源形式。

The sysinternals version of netstat (netstatp) does this. IIRC, it uses SNMP to gather its info. Search the net and find a version you're comfortable with. The file names are netstatp.c and netstatp.h
Sysinternals no longer publishes netstatp that I am aware of.

You can also go here and get tcpview and/or tcpconv one of which is available in source form.

淑女气质 2024-10-19 03:24:52

查看 BitMeterOS 的源代码,它可以在 xp+ 上运行。你们还想看看 TCPDump/Libpcap 。这两个都监视网络网络流量,libpcap 可能会是你想要的,尽管

还有 Winpcap,一个更多的窗口面向“版本”,可以在此处找到有关网络流量统计的简单教程,您还会对感兴趣,用于基于连接进行过滤this 表示原始数据包的大小。

Have a look at the source code for BitMeterOS, it works on xp+. you many also want to look at TCPDump/Libpcap as well. both of these monitor network network traffic, libpcap will probably be what your after though

there is also Winpcap, a more windows orientated 'version', a simple tutorial on network traffic stats can be found here, you'll also be interested in this for filtering based on connection and this for the size of the raw packets.

韬韬不绝 2024-10-19 03:24:52

我最好的选择是挂钩“发送”API 调用并记录每次发送的金额。虽然这看起来确实不值得,但我很确定它会起作用。祝你好运!

My best bet would be hooking the "send" API calls and recording the amount sent each time. Although this really doesn't seem worth it, I'm pretty sure it would work. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文