Capture and redirect TCP traffic

H

Hugh Janus

Hi all,

Does anybody know if it is possible with VB.NET to capture and redirect
traffic that flows through a particular port? i.e. the app either
listens or watches port 1234 for outgoing traffic. It then captures or
copies all the traffic and simalteanously send it out of port 4321.

Another possible example: telnet sends info out on port 23. I want
that information captured and sent out in real-time (or almost) of port
80. And likewise, as info flows into port 80, my app sends it to port
23. Perhaps the telnet would be "telnet localhost 80" and my app is
already configured as "whatever arrives/leaves port 23, copy it and
transmit on port 80."

Sort of like bridging two ports together. Am I making it clear?

Thanks in advance.
 
H

Hugh Janus

OK, so it seems that no-one can help with this. :-( I am not giving
up though, and if I get any results I'll post here on this thread.


Does anybody have any suggestions to get me started on this? Any ideas
of where to look, who to ask or ideas of the fundamentals required that
i'd need to link with etc etc?

All help is appreciated!
 
H

Hugh Janus

one other thing. i think that the solution that I am looking to write
would function in a similar way (if not identical perhaps) to how the
port forwarding works on a router. except that this all works on the
local machine. So as the traffic goes out, my app forwards (or
redirects) that traffic out through port 80
 
R

Ray Cassick \(Home\)

Yes, this is not that hard actually. Look into the System.Net namespace and
look at things like TcpListeners, Network Streams and BinaryWriters.

That's what I can give you quickly (just about to head into a meeting), if
you need more details let me know.
 
H

Hugh Janus

Ray said:
Yes, this is not that hard actually. Look into the System.Net namespace and
look at things like TcpListeners, Network Streams and BinaryWriters.

Hi Ray, thanks for your response! You have me intreged now. I have
been looking at these classes and am not really sure where to start.
Is there any more help you can give me?

Thanks.
 
R

Ray Cassick \(Home\)

Hmmm, ok this might throw a wrinkle into things a bit. It sounded like you
wanted to build something like a NAT proxy or something, but since you want
to run this process on the same machine that might take some doing.

(BEWARE - Below is off the cuff, might be broken, might have syntax errors
code examples)

It is simple to write a function that listens on a port:

Dim mSideAListenerPort As Int32
Dim mSideAListener As TcpListener
Dim mSideAClient As TcpClient
Dim mSideAIp As IPAddress
Dim mSideANetworkStream As NetworkStream
Dim mSideAReader As BinaryReader
Dim mSideAWriter As BinaryWriter

mSideAIp = IPAddress.Parse("192.168.0.1")
mSideAListener = New TcpListener(mSideAIp, 23)
mSideAListener.Start()
mSideAClient = New TcpClient

mSideAClient = mSideAListener.AcceptTcpClient 'Listens for a client to
connect to it

mSideANetworkStream = mSideAClient.GetStream

mSideAReader = New BinaryReader(mSideANetworkStream)
mSideAWriter = New BinaryWriter(mSideANetworkStream)

Then create another function to make an outbound connection:

Dim mSideBServerPort As Int32
Dim mSideBClient As TcpClient
Dim mSideBIp As IPAddress
Dim mSideBNetworkStream As NetworkStream
Dim mSideBWriter As BinaryWriter
Dim mSideBReader As BinaryReader

mSideBIp = IPAddress.Parse("192.168.0.2")

mSideBClient = New TcpClient
mSideBClient.Connect(mSideBIp, 8000)

mSideBNetworkStream = mSideBClient.GetStream

mSideBReader = New BinaryReader(mSideBNetworkStream)
mSideBWriter = New BinaryWriter(mSideBNetworkStream)

Then use the Binary Readers and Writers (in the correct order) to read from
the input and write that data to the output:

While Connected 'In one thread

mSideBWriter.Write(mSideAReader.Read)

Loop

While Connected 'In another thread

mSideAWriter.Write(mSideBReader.Read)

Loop

Of course you would need to do the cross reading and writing in different
threads because they need to happen at the same time and you will need to
make sure that you keep your readers and writers matched up otherwise you
will be crossing data.

The problem is that you want to run this on the same machine. I am not sure
how you would accomplish this to be honest. This might really take something
lower level to grab network packets before they get to the interface and
then send them on to the interface on a different port number. Something
that plugs into the IP stack. This is not something that I think you can
develop in .NET because I don't think it would be doable with managed code
at that level.

You could try this out on a second machine and see if you could get it
working then just figure out how to shoehorn it into the same machine. Keep
in mind that the one thing that keeps the processes of routers (relatively)
simple is that they are separate machines with their own destination
addresses and operate at a much lower level on the stack than this.

Also keep in mind that this example would only really cover TCP, no UDP
streams, they would have to be taken care of differently because they are a
different protocol.

Good luck. Hope I helped put this into perspective a bit.
 
R

Ray Cassick \(Home\)

Oh, and since this thread is getting a bit old and might fall off your news
reader feel free to email me if you need to. I help out where I can.
 
H

Hugh Janus

Ray, thanks for all this. It is fantastic. I am going to work with it
for a few days and then let you know the results. You are right, it is
best to contact you via email as this thread is pretty old now.

Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top