Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Shell 

[PowerShell] TCP Port Listener

To start off, this script is not what you are thinking it is. This was just something I coded up to double check that ports were open on some servers/client systems that I manage.

I didn't really need to make this, but it was a lot of fun trying to get it to work. You learn more by finding a problem and then trying to code a solution.

Code
# to start listener Listener.start();
# to sop listener Listerner.stop();
function Listener {
$input = Read-Host -Prompt "What port do you want to listen on: "
#convert variable string to integer
$port = [int]$input
#create socket for TCP listener
$listener = [System.Net.Sockets.TCPListener]$port
$listener.Start();
#listen for traffic on $port and accept connections made
$listener.AcceptTCPclient()
$listener.stop();
}
# on remote host use
# (New-Object System.Net.Sockets.TcpClient).Connect('target', $port)

Now, this little guy, when called, will listen on whatever port you designated. It will holed at the $listener.AcceptTCPClient() method until something makes a connection on that port, as it is awaiting traffic.

I left the code in the comments for the command to run on the remote system (or vice versa) to connect on the port you are listening. It should return something to the effect of this, if it was successful.

Code
Client              : System.Net.Sockets.Socket
Available           : 0
Connected           : True
ExclusiveAddressUse : False
ReceiveBufferSize   : 65536
SendBufferSize      : 65536
ReceiveTimeout      : 0
SendTimeout         : 0
LingerState         : System.Net.Sockets.LingerOption
NoDelay             : False

Your focus here should be the "Connected : True" statement, showing you that the connection was successful (true).

Feedback and questions always welcome. 🤝
[Image: pia_banner_3.png]
...
$listener.AcceptTCPclient()
$listener.stop(); WHY TOU CLOSE IT ?
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
Can you please explain to me this?

Code
$port = [int]$input

It seems you're giving port a value, but why all of these $?

It's either built into the language itself, or OP is subtly telling us he's "ballin"
(Aug 13, 2017 02:38 PM)ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ Wrote:  Can you please explain to me this?

Code
$port = [int]$input

It seems you're giving port a value, but why all of these $?

It's either built into the language itself, or OP is subtly telling us he's "ballin"

the $ symbol in PS is to declare vars
so he declare port as integer
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Aug 13, 2017 02:01 PM)HFDone Wrote:  ...
$listener.AcceptTCPclient()
$listener.stop(); WHY TOU CLOSE IT ?

You close it because after you establish a connection, there's no need to leave it open. This is just a test to check if a port status is up or down, not a l33t h4x0r tool.

(Aug 13, 2017 02:38 PM)ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ Wrote:  Can you please explain to me this?

Code
$port = [int]$input

It seems you're giving port a value, but why all of these $?

It's either built into the language itself, or OP is subtly telling us he's "ballin"

Yes, the $ symbol in PowerShell is to define a variable.

The reason this line is coded as so is because when we take the value of $input from the user, it is read as a string and must be passed to the listener as an integer. Placing [int] in front of the variable $input converts it from a string to an integer and stores it in the $port variable.

Does that make sense?
[Image: pia_banner_3.png]
(Aug 13, 2017 03:11 PM)Travelers Wrote:  You close it because after you establish a connection, there's no need to leave it open. This is just a test to check if a port status is up or down, not a l33t h4x0r tool.


Yes, the $ symbol in PowerShell is to define a variable.

The reason this line is coded as so is because when we take the value of $input from the user, it is read as a string and must be passed to the listener as an integer. Placing [int] in front of the variable $input converts it from a string to an integer and stores it in the $port variable.

Does that make sense?

Yes it does! Thank you very much for your explaination. And thanks to HFDone as well for another good question. I also was wondering the same thing! So I've learned a lot from this thread so far. Travelers, you don't know it yet, but you're going to be teaching me PS if you keep releasing (ya know, with all my questions).
(Aug 13, 2017 04:34 PM)ɩᴄᴀʀᴜȿ ᴌוᴠᴇѕ™ Wrote:  Yes it does! Thank you very much for your explaination. And thanks to HFDone as well for another good question. I also was wondering the same thing! So I've learned a lot from this thread so far. Travelers, you don't know it yet, but you're going to be teaching me PS if you keep releasing (ya know, with all my questions).

Haha. No worries, dude. Maybe one day I can make something a little bit better. For now though, these little scripts are all I've got.
[Image: pia_banner_3.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by samir5000Mar 13, 2025 01:32 PM 3 324 Mar 13, 2025 01:32 PM
Last Post: Bastille
[Shell] by PELockDec 9, 2024 09:42 AM 3 657 Dec 9, 2024 09:42 AM
Last Post: PELock
[Source] by QismonNov 27, 2024 04:29 AM 2 305 Nov 27, 2024 04:29 AM
Last Post: mikeyp