Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 359
Eyal Avatar asked Mar 29 '09 18:03

Eyal


People also ask

How do I view TCP connections in Windows?

Press the Windows key + R, then type "cmd.exe" and click OK. Enter "telnet + IP address or hostname + port number" (e.g., telnet www.example.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command in Command Prompt and test the TCP port status.

How do I view TCP?

Find Your TCP/IP Properties in Microsoft Windows 10In the Cortana search box, type command. The Command Prompt dialog box appears. At the command prompt, type ipconfig /all and press Enter. Note the values that you see for the primary network adapter.


3 Answers

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.

like image 155
froh42 Avatar answered Sep 23 '22 12:09

froh42


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.

like image 20
Frank Schwieterman Avatar answered Sep 23 '22 12:09

Frank Schwieterman


C# code to do this is here

like image 45
JoshJordan Avatar answered Sep 22 '22 12:09

JoshJordan