How do I hook the TCP stack in Windows to sniff and modify packets?
Asked Answered
Y

7

12

I'd like to write a packet sniffer and editor for Windows. I want to able to see the contents of all packets entering and leaving my system and possibly modify them. Any language is fine but I'd like it to run fast enough that it won't burden the system.

I've read a little about WinPcap but the documentation claims that you can't use WinPcap to create a firewall because it can't drop packets. What tools will help me write this software?

Yang answered 29/3, 2009 at 18:5 Comment(0)
A
11

Been there, done that :-) Back in 2000 my first Windows program ever was a filter hook driver.

What I did was implementing the filter hook driver and writing a userspace application that prepared a filter table on what to allow and what to disallow. When you get around your initial set of blue screens (see below for my debug tip in kernel mode) the filter mode driver is quite easy to use ... it gives each packet to a function you wrote and depending on the return code drops it or lets it pass.

Unfortunatley packets at that level are QUITE raw, fragments are not reassembled and it looks more like the "network card" end of things (but no ethernet headers anymore). So you'll have quite a bad time decoding the packets to filter with that solution.

There also is the firewall hook driver, as discussed in this codeproject article.

If you are on Vista or Server 2008 you'd better have a look at WFP (Windows Filtering Platform) instead, that seems to be the mandated API of the day for writing firewalls. I don't know about it other than google turing it up some minutes ago when I googled for the filter hook driver.

Update: Forgot the debug tip:

Sysinternals DbgView shows kernel-mode DbgPrint output, and more important - it can also read them from the dump file your last blue screen produced. So sprinkle your code with dbgprint and if it bluescreens just load the dump into dbgview to see what happened before it died ... VERY useful. Using this I managed without having a kernel debugger.

Audwen answered 29/3, 2009 at 21:42 Comment(2)
Filter hook drivers aren't a good choice, because you can only have one of them installed at any time. If you try to install on a system which already has someone elses hook, you're stuffed.Diann
Agreed. I think that the firewall hook driver is the way to go, though it is deprecated in WinXP and beyond for running too high in the network stack. NDIS is recommended for WinXP. NDIS seems like more work, though.Yang
I
1

I'm pretty sure you'd need to write a filter driver. http://en.wikipedia.org/wiki/Filter_driver I don't know much more than that :). It would definitely be a C/C++ Win32 app and you'd likely being doing some kernel side work. Start by downloading the DDK and finding some of the sample filter drivers.

If you just want to monitor what goes in and out of IIS, consider an ISAPI filter. Still C/C++ in Win32, but relatively easier than writing a device driver.

Inexcusable answered 29/3, 2009 at 18:15 Comment(0)
B
1

C# code to do this is here

Biosphere answered 29/3, 2009 at 18:18 Comment(2)
Since it's raw socket based, it won't be able to sniff incoming TCP packets on Windows 7. (Microsoft crippled raw sockets on non-server versions of windows)Freed
I need a WFP solution/example, unfortunately.Electrodialysis
C
0

I actually did this, several years ago. I'm hazy on the details at this point, but I had to develop a filter/pass-thru/intermediate driver using the Windows DDK. I got a lot of good information from pcausa. Here's a url which points to their product that does this: http://www.pcausa.com/pcasim/Default.htm

Coadjutor answered 29/3, 2009 at 18:48 Comment(0)
M
0

If you're doing this for practical reasons, and not just for fun, then you should take a look at Microsoft Network Monitor. The home page talks about the version 3.3 beta, but you can download version 3.2 from the Downloads page. There is also an SDK for NM, and the ability to write parsers for your own network protocols.

Myceto answered 29/3, 2009 at 19:10 Comment(0)
D
0

There's a question you need to ask which you don't know you need to ask; do you want to know which applications sockets belong to? or are you happy to be restricted to the IP:port quad for a connection?

If you want to know applications, you need to write a TDI filter driver, but that makes handling the receive almost impossible, since you can't block on the receive path.

If you're happy with IP:port, go in at the NDIS level, and I believe you can block on receive to your hearts content.

A word of warning; if you have no prior kernel experience, writing either of these drivers (although TDI is significantly harder) will take about two years, full time.

Diann answered 29/3, 2009 at 21:50 Comment(0)
C
0

this:

TdiFw is a simple TDI-Based Open Source Personal Firewall for Windows NT4/2000/XP/2003

http://tdifw.sourceforge.net/

may help you

Cartagena answered 30/3, 2009 at 10:37 Comment(1)
If that's what I think it is, it sucks! the source code is NOT well written or comprehendable. I looked at it when I was writing my own TDI filter and it didn't help me at all.Diann

© 2022 - 2024 — McMap. All rights reserved.