What direction should I go in(libraries, documents)?
UPDATE
Can someone illustrate how to use winpcap to do the job?
UPDATE 2
How do I verify whether a packet is an HTTP one?
What direction should I go in(libraries, documents)?
UPDATE
Can someone illustrate how to use winpcap to do the job?
UPDATE 2
How do I verify whether a packet is an HTTP one?
If by "hijack" you meant sniff the packets then what you should do to do it with WinPcap is the following:
Find the device you want to use - See WinPcap tutorial.
Open a device using
pcap_open
Use a function that reads packets from the descriptor like
pcap_loop
This will loop until something wrong has happened or the loop was broken using a special method call. It will call the functionPointer for each packet.
In the function pointed implement something that parses the packets, it should look like a
pcap_handler
:Now all you have left is to parse the packets that their buffer is in the
const u_char*
and their length is in thepcap_pkthdr
structurecaplen
field.Assuming you have HTTP GET over TCP over IPv4 over Ethernet packets, you can:
The rest of the packet should be the HTTP text. The text between the first and second space should be the URI. If it's too long you might need to do some TCP reconstruction, but most URIs are small enough to fit in one packet.
UPDATE: In code this would look like that (I wrote it without testing it):
You can also filter only HTTP traffic by using creating and setting a BPF. See WinPcap tutorial. You should probably use the filter
"tcp and dst port 80"
which would only give you the request your computer sends to the server.If you don't mind using C#, you can try using Pcap.Net, which would do all that for you much more easily, including the parsing of Ethernet, IPv4 and TCP parts of the packet.
try http://www.winpcap.org/
You may want to look at the source code of
tcpdump
to see how it works.tcpdump
is a Linux command-line utility that monitors and prints network activity. You need root access to the machine to use it, though.It may sound like overkill but the Web proxy/cache server Squid does exactly that. A few years ago my company used it and I had to tweak the code locally to provide some special warnings when certain URLs were accessed so I know it can do what you want. You just need to find the code you want and pull it out for your project. I used version 2.X and I see they're up to 3.X now but I suspect that aspect of the code hasn't changed much internally.
You didn't say if windows is a 'requirement' or a 'preference' but according to the site: http://www.squid-cache.org/ they can do both.