How can I tell what host a packet comes from using netcat? - bash

I'm trying to write a server using netcat and bash. to recieve asynchronous packets, i'm using the command
netcat -lu 6791
How can I tell what host a packet came from? Is there a better tool i should be using then netcat (socat maybe?)

wireshark (GUI tool), tshark (text-based version of wireshark), and/or tcpdump (very similar to tshark, at least until you start messing with filtering out specific packets or searching for certain patterns or anything more complex) come immediately to mind...

#twalberg's suggestion,
netcat -vv -lu -p 6791 worked.

Related

How to display the port numbers of open connections for a specific process with tcpvcon.exe (Windows 10)?

I have both TCPView and Tcpvcon on my Windows 10 machine and I wonder how to get all the information (port numbers, etc.) displayed in TCPView in the output of the Tcpvcon program? TCPView has the process name, PID, protocol, remote address, remote port, etc. in its output to the GUI. Tcpvcon, on the other hand, only contains the process name, protocol, remote and local address. I would like to have all information that can be read in the TCPView GUI in the command line output of Tcpvcon (especially the port numbers). Tcpvcon seems to have only the three switches -a -c -n but no matter how I combine them, I do not reach my goal. Can anyone help me?
Below is a sample output when I use all three switches. In TCPView I see much more information about the specified process.
I was also very surprised that tcpvcon does not show port numbers (maybe we should ask Mark R. to add them ;-)
BUT you could use
netstat -a -o -n
or with an admin shell even
netstat -a -o -n -b
switches meaning:
-a ... Displays all active TCP connections and the TCP and UDP ports
on which the computer is listening.
-o ... Displays active TCP connections and includes the process ID (PID)
for each connection.
-n ... Displays active TCP connections, however, addresses and port numbers
are expressed numerically and no attempt is made to determine names.
-b ... Displays the executable involved in creating each connection or
listening port. (Note that this option can be time-consuming and
will fail unless you have sufficient permissions.)
To get all available switches just use netstat -? (there are other interesting ones) or https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/netstat
swobi
Up until the 2011 release of TCPVCON, it used to show port info.
The newer versions don't any more.
If you could get your hand on version 2.54, you would be able to get port info.
Tested with tcpvcon-v2.34 (I couldn't find 2.54) and it shows the ports but it doesn't show the process, all conections appear as from System. Also TCPV6 and UDPV6 are missing.
This is an example:
C:\WINDOWS\system32>"C:\My Program Files\TCPView-v4.13\tcpvcon-v2.34.exe" -a -c
TCP,System,-1,LISTENING,WXP-OR7507156:epmap,WXP-OR7507156:0
TCP,System,-1,LISTENING,WXP-OR7507156:microsoft-ds,WXP-OR7507156:0
TCP,System,-1,LISTENING,WXP-OR7507156:sms-rcinfo,WXP-OR7507156:0
TCP,System,-1,LISTENING,WXP-OR7507156:5040,WXP-OR7507156:0
TCP,System,-1,LISTENING,WXP-OR7507156:wsd,WXP-OR7507156:0
..
UDP,System,-1,,192.168.56.1:137,*:*
UDP,System,-1,,192.168.56.1:138,*:*
UDP,System,-1,,192.168.56.1:2177,*:*
UDP,System,-1,,192.168.56.1:5353,*:*
EDIT:
I correct myself. ASB was right.
I just got TCPView v2.54 and it does indeed show the application, the ports and also TCPV6 and UDPV6.
So I confirm that the "good" version is v2.54.
Tcpvcon.exe -a -c
TCPView v2.54 - TCP/UDP endpoint viewer
Copyright (C) 1998-2009 Mark Russinovich
Sysinternals - www.sysinternals.com
TCP,dnscrypt-proxy.exe,4188,LISTENING,WXP-XXX:domain,WXP-XXX:0
TCP,[System Process],0,TIME_WAIT,WXP-XXX:domain,localhost:62240
..
UDP,Teams.exe,12632,*,WXP-XXX:58950,*:*
TCPV6,svchost.exe,1232,LISTENING,wxp-XXX:135,wxp-XXX:0
..
UDPV6,svchost.exe,19712,*,wxp-XXX:50836,*:*
UDPV6,System,4,*,wxp-XXX:56736,*:*
To display the port numbers (and the process names) you need the old v2.54 version of tcpvcon.exe
This SysinternalsSuite.zip Archive from the Wayback Machine contains this version:
https://web.archive.org/web/20100201154325/http://download.sysinternals.com/Files/SysinternalsSuite.zip

Verify IP address?

I need to write a script in which I have to verify if an IP destination respond (read from keyboard).
I supposed I have to use ping, but I'm not sure how.
To learn about the ping command, you may execute the command
man ping
or the command
ping --help
After executing the ping command in your script, you will need to process its return value and/or its output, somehow. Honza gave you a few commands that can be what you need, although you may want to do it differently, for several normal reasons.

Redirect modified output of a program back to its input

For fun, I want to type something in Bash that will connect to an IRC server and automatically respond to PING messages
For example, I have the following output from telnet
Connected to irc.example.com.
Escape character is '^]'.
PING :12341234
If I pipe this to grep and then sed, I can easily turn this output into PONG :12341234, but how do I send it back to the standard input stream of telnet?
Additionally, I'd like to still being to manually send input using the keyboard
What you're trying to do is known as an "echo server". It is fairly easy to do with netcat and it's variants (nc or netcat, ncat, etc.).
For example:
ncat -l 2000 --keep-open --exec "/bin/cat"
In another shell
telnet localhost 2000
Whatever you send will be replied by ncat.

where to send daemon optional output so it's readable

My daemon has option
-r WhereShouldIOutputAdditionalData
daemon is listening on port 26542 and writes on the same port , I want additional data to output to 26542 as well, I tried using
-r /dev/tcp/127.0.0.1/26542
and it doesn't work, When I do
> /dev/tcp/127.0.0.1/26542
I get connection refused. Deamon that I use: vowpal_wabbit, machine learning library.Any ideas?
Per an unoffical man page at
https://github.com/JohnLangford/vowpal_wabbit/wiki/Command-line-arguments
I see
-r [ --raw_predictions ] arg File to output unnormalized predictions to
So I think the -r argument is expecting a sort of /path/to/logs/raw_preds.log argument.
With this, you'll have "captured the optional output so it is readable." You could open a separate window and use the dev/admins old friend tail -f /path/to/logs/raw_preds.log to see info as it is written to the file.
If you really want it all to appear on one port, (which isn't exactly clear from your question), you'd need a separate program that can multi-plex the outputs, AND has control of your required port number. Also you'll need to be concerned about correct order of output.
IHTH.
I'm sorry, what you want to do it's impossibile for two reasons:
First, bash cannot listen on a given TCP port.
For example you cannot write a TCP server daemon in plain bash (you could use netcat for that), you can only connect() to a TCP port in bash.
Also, it is impossibile to listen on the same TCP ip:port that is already in LISTEN state by another process.

Process UDP packets and forward them at the same time

I have a question that is very similar to this question, but the solution provided is not working for me. And actually I want to do something a little different, so maybe there's a better way.
My hope is to send UDP unicast packets (a video stream) to a server. The server will forward these packets to another computer. Optionally, someone logged into the server will be able to watch the video stream using something like mplayer. I'm pretty sure I could stream the video both to the server and to the final destination from the video source, but I really want the server to be in control of that.
First Attempt:
Downloaded nmap to get the ncat tool. Thought I could use that to download and pipe bytes to mplayer and forward with another ncat process. I can pipe to mplayer and it works great, I just can't forward at the same time. If I was using Linux, apparently I could use tee and process substitution, but I'm looking for a Windows solution.
Second Attempt
I found a solution that kind of implied piping to multiple programs would be possible in PowerShell. But what I found is that binary piping wasn't going to work.
Third Attempt
I attempted to program a tee-like executable that would tee to two programs. This was about as close as I got, but the video streams seemed to get a bit corrupted. I think it was more of a performance issue than anything.
Fourth Attempt
Now I'm at the point where I've discovered socat which led me to the question I linked to. It seems odd, but I thought a solution would be for my server to forward packets to the destination AND forward packets to a different port on the server which would be used with netcat or socat to pipe into mplayer. I'd rather not use up another port on the server, but if that's what I have to do, then ok. But like I said, the solution from the other question didn't seem to work. Mplayer started to buffer, then stopped very shortly.
Ran each of these in a separate cmd window in this order. Notice I wasn't trying to forward to the destination computer yet, just trying to get mplayer to work on the server.
socat UDP4-LISTEN:5000 UDP-DATAGRAM:224.10.10.10:5001
socat UDP4-RECVFROM:5001,ip-add-membership=224.10.10.10:0.0.0.0,reuseaddr,fork UDP-DATAGRAM:192.168.16.33:5002
ncat -l -u -p 5002 | mplayer -vo direct3d -cache 1024 -
I seem to get about 924 bytes received according to MPlayer output.
I'm fresh out of ideas, though I will continue learning about socat. I assume I am not running socat correctly, but I'm not really sure where the issue is. If anyone can point out my mistake or offer an alternative solution I'd really appreciate it. Thanks.
Edit:
I did some more testing and if I change the second socat to do this:
socat UDP4-RECVFROM:5001,ip-add-membership=224.10.10.10:0.0.0.0,reuseaddr,fork - | mplayer -vo direct3d -cache 1024 -
I find that it will play the stream from the multicast address. But apparently forwarding to the unicast address on port 5002 is what's not working.
Ok, I'm not really sure why this is, but the netcat process that worked fine to receive the UDP packets from the sender and pipe to MPlayer does not work here. I'm not sure what the problem is.
So instead of:
ncat -l -u -p 5002 | mplayer -vo direct3d -cache 1024 -
I had to use:
socat UDP4-RECVFROM:5002,fork - | mplayer -vo direct3d -cache 1024 -
Though I do still have one small problem. If I close MPlayer, I get a ton of messages like:
2015/02/24 11:14:04 socat[5888] E write(1, 0x800432e8, 986): Broken
pipe
Also if I hit Ctrl-C from the command line it takes a few tries and then when MPlayer stops, my keyboard no longer works and I have to start a new cmd window.

Resources