<p>It's not just a capture I'm looking to do here. I want to first capture the packet, then in real time, check the payload for specific data, remove it, inject a signature and reinject the packet into the stack to be sent on as before.</p>

<p>I had a read of the ipfw divert sockets using IPFW and it looks very promising. What about examples in modifying packets and reinjecting them back into the stack using divert sockets? Also, as a matter of curiosity, would it be possible to read the data from the socket using Java or would this restrict me with packing mangling and reinjecting etc?</p>

[{'answer_id': 63284, 'author': 'diciu', 'author_id': 2811, 'author_profile': 'https://Stackoverflow.com/users/2811', 'pm_score': 2, 'selected': False, 'text': '<p>See divert sockets: <a href="http://www.faqs.org/docs/Linux-mini/Divert-Sockets-mini-HOWTO.html" rel="nofollow noreferrer">Divert Sockets mini HOWTO</a>.</p>\n\n<p>They work by passing traffic matching a certain ipfw rule to a special raw socket that can then reinject altered traffic into the network layers.</p>\n'}, {'answer_id': 63304, 'author': 'Stephen Pellicer', 'author_id': 360, 'author_profile': 'https://Stackoverflow.com/users/360', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re just looking for packet capture, libpcap is very popular. It\'s used in basic tools such as tcpdump and ethereal. As far as "hooking into the stack", unless you plan on fundamentally changing the way the way the networking is implemented (i.e. add your own layer or alter the behavior of TCP), your idea of using IPF for packet modification or intervention seems like the best bet. In Linux they have a specific redirection target for userspace modules, IPF probably has something similar or you could modify IPF to do something similar.</p>\n\n<p>If you are just interested in seeing the packets, then libpcap is the way to go. You can find it at: <a href="http://www.tcpdump.org/" rel="nofollow noreferrer">http://www.tcpdump.org/</a></p>\n'}, {'answer_id': 107590, 'author': 'MarkR', 'author_id': 13724, 'author_profile': 'https://Stackoverflow.com/users/13724', 'pm_score': 1, 'selected': False, 'text': "<p>It's possible to do this in userspace with the QUEUE or NFQUEUE iptables target I think. The client application attaches to a queue and receives all matching packets, which it can modify before they're re-injected (it can also drop them if it wants).</p>\n\n<p>There is a client library libnetfilter_queue which it needs to link against. Sadly documentation is minimal, but there are some mailing list posts and examples knocking around.</p>\n\n<p>For performance reasons, you won't want to do this to every packet, but only specific matching ones, which you'll have to match using standard iptables rules. If that doesn't do enough, you'll need to write your own netfilter kernel module.</p>\n"}, {'answer_id': 107669, 'author': 'Andrew Edgecombe', 'author_id': 11694, 'author_profile': 'https://Stackoverflow.com/users/11694', 'pm_score': 1, 'selected': True, 'text': '<p>I was going to echo other responses that have recommended iptables (depending on the complexity of both the patterns that you\'re trying to match and the packet modifications that you want to make) - until I took notice of the BSD tag on the question.</p>\n\n<p>As <a href="https://stackoverflow.com/users/360/stephen-pellicer">Stephen Pellicer</a> has already mentioned, libpcap is a good option for capturing the packets. I believe, though, that libpcap can also be used to send packets. For reference I\'m pretty sure that <a href="http://tcpreplay.synfin.net/" rel="nofollow noreferrer">tcpreplay</a> uses it to replay pcap formatted files.</p>\n'}]
['https://Stackoverflow.com/questions/63157', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/368855/']