-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading data from socket and send it again #24
Comments
On Thu, Jan 21, 2016 at 09:37:21AM -0800, amuessig wrote:
procket calls setsockopt(SO_BINDTODEVICE) for the interface option.
So we need to use bind(2). There's a version of bind() in the procket
Because we're using PF_PACKET sockets, we can't call gen_tcp:accept/1. The For sending the frame, use packet:send/3. Something like this:
If the device is in the middle between the client and server, instead of If any of that isn't clear or if you have any questions, please let me |
Thanks for your helpful hints! init({Parent, Port}) ->
I am getting the output "Method accept is called", but never "Frame: [...]". Thanks! EDIT: Yes, the interface is run in promisc. mode. |
Here's a working example: -module(resend).
-export([init/1]).
init(Dev) ->
{ok, ListenSock} = procket:open(0, [{protocol, 16#0008}, {type, raw}, {family, packet}]),
IfIndex = packet:ifindex(ListenSock, Dev),
ok = packet:bind(ListenSock, IfIndex),
Port = erlang:open_port({fd, ListenSock, ListenSock}, [binary,stream]),
accept(ListenSock, IfIndex, Port).
accept(ListenSock, IfIndex, Port) ->
io:fwrite("Method accept is called\n"),
receive
{Port, {data, Data}} ->
Frame = pkt:decapsulate(Data),
error_logger:info_report(Frame),
ok = packet:send(ListenSock, IfIndex, Data),
accept(ListenSock, IfIndex, Port);
_Error ->
ok
end. A few things to note:
Hope that clears things up a bit, feel free to post here if you run into any other problems. |
Thanks a lot, works perfectly. Appreciating your support! 👍 |
There's an old example here: https://gist.github.com/msantos/446057#file-rst-erl You'll probably need to swap the MAC address of your MITM host as the source MAC address when sending the RSTs. |
It's working very well. however, I couldn't find anything how to forward frames/packets/segments to the applications of the host where my Erlang is running. |
Great!
Probably the simplest way is to match any packets with the source or destination set to the IP address of the MITM host and ignore them. Maybe something like this: https://github.com/msantos/herp/blob/master/src/herp.erl#L127 |
Yeah, it worked perfectly. |
Sure, it is pretty simple. Construct an ARP packet then write it to a PF_PACKET or BPF socket: To populate the ARP cache, open any network connection to the host. For example, send a UDP packet to some random port on the host. If you want more control, you could do an ARP probe or gratuitous ARP using the target host's IP address then sniff the reply from the host. |
Hey,
I am trying to build up a middlebox device which is reading packets from a socket, check some stuff like IP address and (if the check matches) send the packet out through the socket.
Later, I will be migrating the socket, thus I need to use raw sockets.
My code is as follow:
How can I bind the opened socket to a interface?
I tried this:
{ok, ListenSock} = procket:open(Port, [{protocol, 16#0008}, {type, raw}, {family, packet}, {interface, "vnf-eth0"}]),
but it didn't work.
Furthermore, trying the listening method, I am not sure how to write back the data..:
Any suggestions/help? Thanks!
The text was updated successfully, but these errors were encountered: