WEBVTT

00:00:00.660 --> 00:00:02.410
So let's go back into our environment,

00:00:02.410 --> 00:00:05.230
and we'll check for open ports and devices.

00:00:05.280 --> 00:00:09.360
What we'll do first is create what's called an echo server using PowerShell.

00:00:09.360 --> 00:00:12.850
Effectively, this means we open a port using PowerShell,

00:00:12.850 --> 00:00:16.750
and then we can test that port or any number of ports.

00:00:16.760 --> 00:00:19.530
It just means we don't have to install software that

00:00:19.540 --> 00:00:21.010
is actively using those ports.

00:00:21.010 --> 00:00:22.560
We can kind of pseudo create this.

00:00:23.090 --> 00:00:26.250
Then we'll use the .NET API to check for open ports.

00:00:26.250 --> 00:00:29.760
We'll then use Nmap in PowerShell to check for an open port.

00:00:29.940 --> 00:00:32.040
And then we'll look at using a third‑party module,

00:00:32.040 --> 00:00:35.140
which will use PowerSploit to check for an open port.

00:00:37.360 --> 00:00:40.610
So the first thing we have to do here is to create a listener.

00:00:40.620 --> 00:00:43.360
So this is going to be what's called the echo server,

00:00:43.370 --> 00:00:47.470
which effectively you create a port, you tell it to open it,

00:00:47.470 --> 00:00:50.570
it doesn't really exist necessarily because we've told it to open,

00:00:50.570 --> 00:00:54.960
and then it just returns a response back in the screen that we're in.

00:00:54.960 --> 00:00:58.960
Now to create this initial listener.

00:00:59.070 --> 00:01:03.250
We can do this by creating a listener variable,

00:01:03.250 --> 00:01:07.140
and we're going to utilize the .NET API.

00:01:07.140 --> 00:01:10.620
So we can use new object, or we could just put it in square brackets.

00:01:10.620 --> 00:01:15.820
But either way, we're going to be using System.Net.Sockets.TcpListener.

00:01:15.820 --> 00:01:22.840
And then we have a variable called or a property called ArgumentList here,

00:01:22.840 --> 00:01:25.940
and we can say what the port is going to actually be.

00:01:25.940 --> 00:01:27.770
Now, we could use anything.

00:01:27.770 --> 00:01:28.860
But of course, remember,

00:01:28.860 --> 00:01:33.780
the communication between the two devices that you're testing,

00:01:33.780 --> 00:01:36.650
you'll probably need to make sure the firewall on the

00:01:36.650 --> 00:01:39.560
machine allows that specific port to be open.

00:01:39.580 --> 00:01:45.170
So I'm going to use something on the high end here, so let's say 16,000, and I'm

00:01:45.170 --> 00:01:49.610
going to press Enter. Now, when I go into the listener to check, you can see

00:01:49.610 --> 00:01:53.870
it's now been bound to the IP address. Now, of course, if I try to initiate a

00:01:53.870 --> 00:01:58.330
connection to that, it's probably going to fail because 16,000 probably not a

00:01:58.330 --> 00:01:59.880
port that you would normally open.

00:02:00.100 --> 00:02:04.260
So while we're here on the server, we can say Windows Defender

00:02:04.260 --> 00:02:08.009
Firewall. I can go into Inbound Rules, and I already created one, but

00:02:08.009 --> 00:02:10.520
I wanted to show you here that I have a rule,

00:02:10.530 --> 00:02:15.380
a port rule, and basically, it's allowing every single port to come in in

00:02:15.380 --> 00:02:18.390
my test environment. Now you may not have to do that because it might be

00:02:18.390 --> 00:02:21.460
a standard port like port 80 or port 443.

00:02:21.520 --> 00:02:25.260
So I have that running. Now, the difficulty is here

00:02:25.260 --> 00:02:26.890
that when we make the connection,

00:02:26.970 --> 00:02:31.700
nothing really gets returned. Or when a connection is created,

00:02:31.700 --> 00:02:36.120
so if I say Listener.Pending and do this,

00:02:36.460 --> 00:02:37.660
it comes back and says,

00:02:37.670 --> 00:02:41.000
well hold on a minute. You can't check the status of it because it's not

00:02:41.070 --> 00:02:45.970
actually running. So we have to come first here and say Start.

00:02:46.050 --> 00:02:49.420
So I'm going to start the listener. Then I can check the pending phase.

00:02:49.420 --> 00:02:53.250
But the pending phase isn't anything to do with the service starting.

00:02:53.250 --> 00:02:59.320
Pending is was there a pending connection made to the specific port?

00:02:59.320 --> 00:03:02.810
Which in this instance, there isn't because we haven't actually tried

00:03:02.820 --> 00:03:09.470
connecting to that port. So now that we have that, we need to adjust a

00:03:09.470 --> 00:03:10.980
couple of settings here.

00:03:11.030 --> 00:03:14.340
The first thing we want to do is allow the connection to be made.

00:03:14.390 --> 00:03:18.330
So I'm going to create a new variable on here, and I'm going to

00:03:18.330 --> 00:03:23.580
use the listener, and there's a option here called Accept, if

00:03:23.580 --> 00:03:25.840
we just loop through, TcpClient.

00:03:25.850 --> 00:03:28.910
So what that means is that when the connection is made,

00:03:28.920 --> 00:03:32.660
my listener is going to say oh, I accept you, instead of

00:03:32.660 --> 00:03:35.150
just allowing it connect and do nothing.

00:03:35.330 --> 00:03:38.820
So I'm going to say client listener AcceptTcpClient.

00:03:38.830 --> 00:03:42.650
So now we have this ability for it to say accept

00:03:42.650 --> 00:03:44.900
the connection that's coming in.

00:03:44.910 --> 00:03:49.830
And then from here, we can specify other properties to kind of go

00:03:49.830 --> 00:03:55.860
in and say I want this message to be written back to me, etc. So,

00:03:55.860 --> 00:03:58.090
let's go over to the Windows machine.

00:03:58.090 --> 00:03:59.640
Now that I have this running that says

00:03:59.640 --> 00:04:03.440
client=listener.AcceptTcpClient, it's not going to return

00:04:03.440 --> 00:04:06.200
anything because no one's made a connection.

00:04:06.210 --> 00:04:11.230
So let's flick to the Windows 10 machine and create that connection.

00:04:12.780 --> 00:04:14.500
So this is our Windows 10 machine.

00:04:14.510 --> 00:04:17.860
And so what we're going to do this time is we're going to create that

00:04:17.860 --> 00:04:24.610
connection, so connect to it. And we do that by simply creating what is

00:04:24.610 --> 00:04:27.220
going to be a variable for the endpoint.

00:04:27.230 --> 00:04:34.680
So I'm going to call it client, and I'm going to say instead of a TcpListener,

00:04:34.690 --> 00:04:40.780
I want to create a TcpClient. And then, for my client properties, I

00:04:40.780 --> 00:04:45.940
can go in and say connect to a specific IP or port. I'm going to set

00:04:45.940 --> 00:04:51.970
my IP address. Now I can go back and say connect to that specific

00:04:51.970 --> 00:04:57.040
IP. So, now I've connected. Let's see what happened back on the

00:04:57.040 --> 00:04:58.920
Windows Server itself.

00:04:58.930 --> 00:05:00.260
So let's change to that.

00:05:01.850 --> 00:05:06.700
So what happened is my client or my accepting TCP client

00:05:06.830 --> 00:05:10.020
stopped. It received a connection.

00:05:10.030 --> 00:05:12.620
So if I now loop back and say listener is pending,

00:05:12.620 --> 00:05:15.690
it will say well no, no there's no listener pending,

00:05:15.690 --> 00:05:18.860
because now I have a specific connection.

00:05:18.880 --> 00:05:22.270
So now we need to get information about the client that's connecting.

00:05:22.290 --> 00:05:27.070
So let me create a variable called connection, and I'll

00:05:27.070 --> 00:05:30.730
populate that with the value from client.

00:05:31.020 --> 00:05:34.360
And what I can then do is I want to retrieve the data

00:05:34.360 --> 00:05:37.150
stream that's coming from that client.

00:05:37.160 --> 00:05:41.730
So let's create a variable called stream, and I can say stream, and I'm going

00:05:41.730 --> 00:05:48.580
to say client.GetStream. So that will return any value.

00:05:48.580 --> 00:05:50.360
So let's look what stream looks like.

00:05:50.370 --> 00:05:53.690
So right now, you can see that there's a connection that was

00:05:53.690 --> 00:05:57.080
created, and we're allowed to read, and we're allowed to write, and

00:05:57.080 --> 00:06:00.220
there's a timeout. So we're starting to grab values. I'm going to

00:06:00.220 --> 00:06:01.870
clear that so we have more space.

00:06:01.880 --> 00:06:09.600
What I can then do is say write a message, so Write‑Verbose. And from here, I

00:06:09.600 --> 00:06:17.980
can tell it to write a specific message into the console here.

00:06:19.640 --> 00:06:26.680
So from here, I'm going to say New Connection, let me just change that to

00:06:26.990 --> 00:06:33.720
quotes, New Connection from, and then, of course, we'll just use standard

00:06:33.720 --> 00:06:40.760
PowerShell here to just write a message out using the properties that are

00:06:40.760 --> 00:06:45.070
part of the client object itself.

00:06:45.080 --> 00:06:50.060
Now you'll notice when we do client.Client, we then have access to things

00:06:50.060 --> 00:06:56.750
like remote endpoint, the address, . I can then say same thing again,

00:06:56.750 --> 00:07:03.290
client.Client.Remote, and I want to use the port this time. And then, maybe I

00:07:03.290 --> 00:07:07.470
just want to write that out as Verbose. And you'll see the message gets

00:07:07.470 --> 00:07:12.150
returned with those values saying a connection was made from a different IP

00:07:12.160 --> 00:07:16.690
address using a different port, which was the remote connection that came

00:07:16.700 --> 00:07:22.180
out, but it was retrieved and accepted on the port that was targeted when it

00:07:22.180 --> 00:07:22.940
came across.

00:07:23.300 --> 00:07:26.420
Now, of course, you may see that the remote endpoint port is different, and

00:07:26.420 --> 00:07:30.550
that's because it's an outgoing port that's making a connection coming in. So

00:07:30.550 --> 00:07:33.740
our echo server allows us to initiate that connection.

00:07:33.750 --> 00:07:37.140
So at any point, we can just rerun this as we need to. So

00:07:37.140 --> 00:07:41.420
that's how we create an echo server or piece of PowerShell that

00:07:41.420 --> 00:07:44.220
will return specific values for us.
