views:

266

answers:

3

Is there any way in C++ on windows to monitor a program and redirect any outgoing requests it makes on a specific port? I have a simple C++ http proxy and want it to be able to automatically redirect all browser requests on port 80 through itself.

A: 

The program would have to be run with administrative privileges in kernel mode of the host OS.

While I don't have extensive experience with windows kernel hooks, in BSD and linux its trivial to install a kernel module that over-writes the system calls for creating sockets and could easily redirect all sockets to a proxy socket of choice.

Harley Green
If everyone used linux, our jobs would be a lot easier in general.
silverbandit91
+4  A: 

The simple way to do it is to create a Windows kernel hook to trap socket requests and reroute them to your proxy.

Some useful documentation on this is:

http://www.internals.com/articles/apispy/apispy.htm

If you're using Windows Vista or better, consider Windows Filtering Platform (WFP):

http://www.microsoft.com/whdc/device/network/wfp.mspx

Also consider looking at Detours (commercial) and EasyHook (free). They significantly simplify the process of writing hooks and redirecting API calls (both Win32 and Application).

0xfe
+1  A: 

If you mean [any destination port] to [one port] then you will have to rely on special drivers. The problem with windows is the inability to natively block [drop] packets. For example a common solution is winpcap. However, while you can monitor traffic, you cannot stop the traffic or modify it in a useful way.

On windows the only solution I've seen would be to use some open TUN/TAP adapter. With that, you would be able to modify every packet that leaves your system.

If you know beforehand the destination port you will be using then it gets rather simple. Simply write a passthrough c++ socket program that will only change the destination port.

If you want to redirect browser requests then you can simply edit the settings in your browser.

Eric