All right welcome to the last lesson in video.
So in this video we are going to be scripting with Bash.
Now this may seem a little overwhelming at first.
What we're going to be doing is building out a ping sweeper script.
It's going to be pretty basic.
And what I want you to do is maybe just watch this video once and then the second time through really
try to follow along with it.
Take notes etc. I think a first time watch through you know just to get the concepts.
Understand what I'm doing.
And then the second time really really get hands on with it.
And I think that will be probably the best way to learn.
But if you have a different learning style please do go ahead and try that as well.
The only thing I can say is don't let this intimidate you.
Hopefully by the time the video's over with you will have a pretty good understanding on a basic scripting
and how scripting can really be beneficial and improve our timing improve automation etc..
So let's go ahead and quickly talk about some of the things are we learning in this video faces some
of the commands and items are going to learn.
We're going to talk about grep which is going to allow us to narrow down some results.
We're also going to talk about cut and T are both of those are also going to help us narrow down some
results.
So basically we're going to start with a ping that we're going to send out and we're going to want to
gather information back that says that ping was valid.
So we want to know any computer that responded to us.
Well how are we going to do that.
Well we need to identify what a valid ping looks like.
We're going to use grep to to kind of narrow down a string or a sentence or something that identifies
with a positive ping and then we're going to cut out everything we don't want and use TR as well to
cut some things out that we don't want.
So we're going to put this into a script when it's all said and done.
And we're also going to be talking about the usage of for loops and their importance not only with using
them in this script but how we can use simple one liners to do everyday things for us.
So I'll show you at the end of the video how we're going to use that and map and use a for loop with
n map to run through and map script.
So stay tuned through the video again if you are a hands on learner maybe first time through you.
You try hands on but my recommendation here is to just sit back watch the video understand what's going
on.
Second time really follow along with it.
Take notes and get the most that you can.
This lesson.
So let's go ahead and get started.
All right.
So now we're going to be covering bash scripting so the first thing we're going to do to cover before
we get into writing our scripts is how to narrow down results.
And we say narrowing down results.
What we're saying is forgiving a block of text and we want to extract some information from that block
of text.
How are we going to do that.
So that's all we're going to cover in this lesson.
OK.
So let's go ahead and get started.
Well we're going to be doing today is narrowing down a ping result.
So if we come into here and we need to paying an IP address within our system.
So I'm going to be pinging 1 9 2 1 6 8 dot 1 dot 90 and you're going to see that it returns a sixty
four bytes from that address.
So it looks like we're getting a response.
I'm going to hit control see here.
So if you remember from the networking section we actually get a response unlimited until we cancel
it right.
Like we just controlled see here from ping.
So there's another thing that we can do if we only want to send one packet and see if it's alive or
not we can do a dash C of one.
So we're just gonna do that so that's account of one we're sending one packet over if we set 10.
It would send 10 packets over if that makes sense.
So now we're going to do is we're gonna put this into a text file.
So if you remember from previous video we just use this little carrot here and I'm going to call this
IP that text OK.
And if we can IP that text you'll see the same results there.
So now what we're gonna want to do is we're gonna want to narrow this down.
What do I want to extract from here.
Well I want to extract this IP address and it'll make more sense when we get into the scripting part.
But what we're gonna do is we're gonna get out specifically of this this IP address from this line.
Now we're interested in actually is a returned IP address that has a valid response.
Right.
So if I were to attack or if I were to enter in here say 15 dot 90 OK there's no response there.
So this is kind of what it looks like when it doesn't get a response back.
It just kind of lingers.
And then we hit control C and it says zero pinging statistics right.
Nothing was was received back.
So what we see when we get a response back is we see sixty four bytes when there's no response back.
We don't have any bytes.
So if we're doing a sweep through a network which we're going to be doing later we need to be able to
narrow down these results.
So if we're sending say 1 9 to that 1 6 8 that one that one all the way through 255 and we want to see
who responds back and then take that list and narrow it down to the IP addresses which is exactly we're
going to be doing we need to know how to narrow that down.
So what we're gonna be narrowing down on specifically is the sixty 64 bytes.
So lets tab up a couple times to this cat IP address and we're gonna do a pipe in the pipe just means
we're gonna add an additional command here.
So the additional command we're going to do is called grep grep is going to grab any line with what
you specify.
So let's specify sixty four bytes and see what happens see now if you noticed we had all of these lines
before and now a group is doing is grep is taking only the lines that contain sixty four bytes.
So again a valid response.
So we have sixty four bytes here and we have achieved a response.
So what we're going to do now is we're going to narrow this down some more.
OK we've got this line here.
But again we're still trying to extract this IP address.
So how can we do that.
Well there is a tool called cut.
So if we tap up again and we do another pipe because they're sending a new command we're going to say
cut and cut syntax.
It looks like this and I'll explain it once I type it out here.
OK so we have cut and then this hyphen D.
That's a delimiter.
So the delimiter is what we're going to be cutting on.
So we're giving a delimiter of a space meaning here's a space here's the space here's a space.
So we're gonna be cutting on these spaces and then we give a field.
We say OK what field do we want to retrieve back from this cut.
OK.
We want to field for.
If you look one two three four in the fourth field is our IP address.
So it's gonna say OK I'm going to cut on this space I'm gonna cut in this space I'm going to cut on
this space and then I can take it right here.
Now if we identified field five we would be taking this fully identified field three we were taking
from let's go ahead and just hit enter and see what that looks like OK.
So now we are narrow down even more.
But there's an issue here.
If we were to try to send this IP address we would have this little colon here attached onto it and
you can't ping with that IP address.
So we're gonna be doing a sweep or narrowing down this list then we're gonna actually need to remove
this guy here.
So let's take a look at how we do that.
So if we tap up again and again we're going to add a pipe.
Now we're gonna use a command called TR and TR just means translate what we're gonna be doing is another
delimiter.
So dash D there and we're going to be taking out that call.
So it should look something like this.
If we hit Enter now you can see that that Colon has been removed.
OK now let's talk about how we can use this information to write out a script.
We're going to start with a basic script and we can add upon it as we go.
So I've gone ahead and written out a script but we're going to talk about it very slowly so you can
actually look at it and copy it down.
So what we're going to do is go ahead and say get it and I called this IP sweep that S H.
So go ahead and do the same or something similar to enter so let's take a look at this script so at
the very top here we have to declare what we're doing.
So we're going to give a hash bang and we do this with any scripting language.
So if was or Python we'd be entering Python here but this is bash.
So we're gonna be doing a forward slash bean forward slash bash that declares that we're running a bash
script and the DSH also indicates that we're running a bash script so I want you to ignore this line
here in this line here for now.
Let's talk about this line.
This should look very very familiar.
So we're doing in this line we are saying we're going to ping with a count of one which we talked about
and then we're going to do something here.
We've got a dollar sign one and a dollar sign.
IP let's just ignore that for now OK so we're gonna go ahead and do the rest.
That should look familiar.
We're gonna say run a grab sixty bytes.
We're gonna cut the delimiter of a space feel the floor and then we're going to do a translate right
and take off that little bit at the end.
OK I added Ampersand here at the end.
So an ad first hand allows us to do threading.
So that's exactly why it's in there.
If we didn't we'd have to let the process go.
One IP at a time and we'll talk about that in a second as well.
So let's talk about this for line so a for loop is what we're actually running here and the loop is
saying I want to do an IP address.
We're just declaring a variable here you can call it whatever you want.
Or we're saying hey for this IP in a sequence of 1 through 254 we're gonna do something in that do as
a pain.
So what this means is for IP and if we think about it in sequence 1 through 250 for what it's saying
is 1 2 3 4 all the way up to 254.
OK.
So think about it this way if we say 4 1 in this ping sweep we're gonna do that right.
So we're gonna say 4 1 4 2 4 3 all the way through 254.
That's what this loop is doing.
So it's very very simple loop.
Now IP is replaced down here at the very end.
Now we're also calling out this dollar sign 1.
Now this dollar sign 1 is user input.
So we're going to actually do something.
This is called IP sweep right.
So we're gonna have to call out this IP sweep something like this sage and then we're going to have
to give out some information.
It's going to request information if we don't provide it.
It won't know how to ping.
So we're gonna do is we're gonna say the first three octet of our home network.
So of yours is 1 9 2 1 6 8.
That one like mine is then you just enter 1 9 2 1 6 8 1.
Now we could also for simplicity just hard code this so we can say 1 9 2 1 6 8 dot 1 dot IP address
and it would be fine.
We don't have to build the set.
The only reason we're not hard coding it is because you can do multiple pings slaves is going to be
a ping sleep script for you in the future if you're on a different network or you just want to write
something out really quick dollar sign one.
Works perfect.
But if you want to leave it hard coded like this it will also work.
So let's go ahead and just delete this out.
I'm going to put this into dollar sign 1 again to have a proof of concept and then we are going to run
this.
So all we do here at the end is declare done if we didn't run this with an ampersand here we would have
to add a semicolon similar to this but because we are having an ampersand we can actually get rid of
that and just put that back just for some syntax clarity here.
OK so let's go ahead and save this
and remember from our lesson we're gonna have to change the mode of our script because it's not executable
by default.
So we're gonna do a plus X here and then we're gonna call our IP sweep the h we can l s and make sure
it's green.
Here's IPC if the S H.
And then we'll do a dot for it slash IP sweep to S H.
And I'm not going to enter anything in as one to show proof of concept so see it ran through every single
number here and threading and name or service not knowing because we didn't provide an IP address.
So what we can do here is we need to provide that IP address 1 6 8 that one hit Enter okay and it pulls
back some information so what we can do with this information.
Let's write this out to a file so let's just call this IP list.
That text.
Something like this OK.
It's done.
Now let's just cut out the IP list.
Text OK.
Now we have an IP list of the IP is in our network that we just swept right and we can use this information
later on.
But before we go into that I do want to go back and just improve our script a very very tiny bit.
So let's go ahead and just tab up a couple of times so we get to our edit and let me show you how we
can improve this script.
It doesn't have to be overly complicated.
What we can do is we can say something like this we can come in here and give an if statement.
So if his condition.
All right we're gonna say if this exists then do something.
And if it doesn't exist do something else.
So we're gonna say if dollar sign 1 is equal to nothing we're going to give it blank space here and
then we're gonna say then we want to do something.
So what are we going to do.
We can echo out something similar to you forgot an IP address and then echo out what you need to do
like syntax
that slash ping suite or IP suite is what we call this IP suite.
That s h and then IP or we can even write it out.
An example right.
Something like that.
So that way our user if they're using it we wrote this for somebody else.
They know we're talking about it at the very end.
We just need to write if backwards.
We'll put it FDI.
So what this is saying is if this is not an echo here right we're going to echo these and then we need
to add one other thing here which is it.
Else go ahead and hit enter just the spaces out of it.
So if we have no dollar sign one declared.
Right.
If we have nothing entered in we're gonna say echo you forgot an IP address and syntax here.
If we do have something in dollar sign one.
OK.
We're gonna do something else we're gonna do our pink suite and that's it.
So this is very similar and modified from Georgia wide means that I've got a long time ago so full credit
goes to her for this little script.
It was very very easy to learn and it's also very easy to teach.
So let's go ahead and just run this one more time we're gonna say IP sweep the s h and look what happens
now it says you forgot an IP address.
Now we can declare a number and there anything in the script is not perfect right.
We're expecting three octet and we could give it a 1 and it's still going to do something like that
which is incorrect right.
So we need to be able to to modify this down more but that gets really advanced scripting into declaring
that needs to be three act tatts and if you don't provide those then what.
And that just gets more advanced so we really just need to know hey what we're doing here.
Very basic script for ourselves and something to remember.
OK.
Let's clear our screen.
Last thing I want to show you.
So we've been able to write a script out but we can also do looping in one line.
And this is where it becomes kind of fun.
So we have this IP list right.
We did a cat IP list that Tex.
Now let's say we've got this IP list together and we want to do an end map scan on all of these IP.
Now we could just say and map and type in the syntax you want and the IP address of it for every single
one of these starting new tab.
And let that happen or you could do something in a for loop so we can write the same for loop that we
did before.
We're gonna say for IP and then we're we're gonna do is we're gonna write a dollar sign and we're gonna
cat the IP list that text.
So all it does is is bringing out this list that we have here and it's saying OK for this IP this IP
this IP set we're going to run through all of these.
That's what the loop does.
Right.
OK.
We're gonna do our semicolon then we're going to tell it to do something right.
So what do we want to do.
OK let's run it and map script.
So we're gonna say do and map and we'll ping or we'll do a port Port 80 on all of these and we'll say
stealth scan that port 80 and we'll do T4 for a speed and that should be it.
So then we can do.
Done like this and let it run through one at a time.
We can also do the ampersand and let it run multiples like this.
We can hit enter and see what that does.
But I also forgot one thing here forgot to declare the IP address.
So let's talk about it real quick.
So we've got a very very simple one liner and you're going to see one liners a lot.
If you get into ethical hacking and just allows us to do things quickly and scripted.
So we got four IP address in this list.
So every IP in this list we're gonna loop through when we do that loop.
We're gonna do and map we do a quick stealth scan of port 80 T4 and we're gonna say declare the IP address
if you're not comfortable and map.
That's OK.
This is purely an example and something you will see in your career.
I will make a scripting video at some point for and map and we'll cover and map more in detail.
So let's go ahead and just hit enter.
OK.
I messed up my syntax.
So what's going on.
OK.
So for IP in this cut deleted for IP and cat IP let's try this one more time so we just did all these
scans seven scans at once and what we did is you see these are the processes that are starting.
So it started up eight processes actually not seven and it's just running through this list doing the
scans and we're gonna go ahead and just kind of look at it.
So it says Okay on one dot Seventy four is eighty open it's open.
Okay.
And then on two fifty four is eighty open it's open and then you see filter down here where it's not
running filtered filtered same thing.
So we can hit control C or hit enter for done.
And that's pretty much it.
So what we just did is we ran AMF scans at one time instead of having to copy and paste these.
So it's just a little bit of what scripting can do.
It does get more advanced but be able to know a one line for loop is a really really important.
If you go into penetration testing and it's just important in basic bash scripting as well.
So that's it for this lesson.
I will catch you guys over in the next lesson when we start on introductory Python.