472,114 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,114 software developers and data experts.

Capture and redirect TCP traffic

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.

Nov 23 '05 #1
9 7671
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!

Dec 12 '05 #2
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

Dec 12 '05 #3
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.

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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.

Dec 13 '05 #4

Ray Cassick (Home) wrote:
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.

Dec 13 '05 #5
p.s. That should say "intrigued" not "intreged".

Dec 13 '05 #6
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.

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Dec 14 '05 #7
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.

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Dec 14 '05 #8
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.

Dec 14 '05 #9
to learn everything there is to know about forwarding ports in a
router visit:
www.portforward.com

On 12 Dec 2005 06:27:18 -0800, "Hugh Janus"
<my*************@hotmail.com> wrote:
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


Jan 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by jonathan | last post: by
4 posts views Thread by Mariano Drago | last post: by
4 posts views Thread by DGS | last post: by
2 posts views Thread by =?ISO-8859-1?Q?Fran=E7ois_de_Dardel?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.