In this lesson, we are going to talk
about some of the basic scripting tools
that you should know.
Now, when you issue commands individually,
this can be useful,
especially when you're doing one-time analysis,
but by using scripting, this will allow you
to set up recurring searches to be repeated easily,
and you can even automate them,
so every day at midnight, this particular thing will happen.
All of that can be done with scripting.
Now, when I talk about a script, what is that?
Well, a script is really basically just a list of commands
that are executed by a certain program
or a scripting engine.
There's lots of different scripting languages out there
that you can use, depending on your use case.
We're going to talk a lot about these in this lesson.
Now, before we dive too deep into scripting,
I do want to bring up a quick exam tip for you.
An in-depth ability to conduct scripting
is not needed for the PenTest+ exam, but in the real world,
this is really, really helpful to have.
For the PenTest+ exam, you are expected to be able to read
and understand six languages.
That is Bash, PowerShell, Python,
Ruby, Perl, and JavaScript.
Again, you don't have to be an expert here,
but you do need to be able to read a script
and understand basically what it's doing.
For example, you might be asked
in a performance-based question to put a script together
based on a basic concept or idea using different blocks,
and you're going to move them around the screen
into their correct portions,
but no one is going to expect you to create a script
on your own out of thin air.
That's not the way the exam works.
So, as we go through this section of the course
and the next section of the course,
we're going to be focused on your ability to understand
what is happening in a script and the basic syntax
for these different programming languages.
You may get a multiple choice question
that shows you a script and asks you to read it
and understand what it's doing.
That's fair game, but again, if you can read English,
you can pretty much read the scripts the way
they're going to give it to you on the exam.
Now, the first language we want to talk about is Bash,
and Bash is a scripting language
and command shell for Unix-like systems.
So, that's going to be the default shell
on Linux and Mac OS X,
and you can set it up as your shell on a Unix system, too.
Now, when we're dealing with Bash,
it supports lots of different elements,
such as variables and loops, and conditional statements,
and functions, and a lot more.
When you create a script inside of Bash,
you always start out with what we call the shebang,
which is that hashtag and the exclamation, then /bin/bash.
This says this is a script that is written in Bash.
That's all that's telling the computer.
Then, we're going to give it some information
of what we want it to do.
In this case, I have a three-line script.
First, echo "Pulling NetworkManager entries...".
All that means, echo, is print this to the screen.
So, if I ran this inside the Unix command shell,
you would end up seeing "Pulling NetworkManager entries"
once you hit Enter.
So, really, the benefit of having that echo statement
is to tell your user who just ran this script,
what's going on, saying, "Hey, I'm working,
I'm getting the entries for you, I'm doing something,"
then it's going to start doing those entries,
and in the background, it's going to do this grep.
It says grep "NetworkManager" /var/log/syslog |,
we're piping that over into the cut command, -d " "
fl-5.
Then, we have this greater-than sign,
and the greater-than sign says take the output of cut,
and instead of passing it to another tool,
put it into a log file, and in this case, we're going to
log it into this text file called netman-log.txt.
So, we've grepped something, we've formatted it using cut,
and now we're going to save it as a text file.
Then, we're going to put a message to the screen
for our user, echo "NetworkManager log file created!".
That's it, and then it goes back to the command prompt.
All this is doing is basically three simple things,
print something to the screen,
do a search and save it to a log file,
and then print to the screen saying,
"I created the file and now you can go get it."
That's all this Bash does, but it saves me a lot of time
because I can just run this file every single time I want,
or even set it up on a scheduled task
so it does this once a day maybe, and then I'd have
all those different files logging up over time.
Next, let's talk about PowerShell.
Now, PowerShell is a scripting language
and command shell for Windows systems.
You're not going to find it on Linux or Unix or Mac.
Now, PowerShell supports elements such as variables,
loops, conditional statements, functions,
and cmdlets that use a verb-noun syntax.
So, just like Bash, all that stuff is the same.
The only difference here is we're now going to be able
to use cmdlets as well.
Now, what is a cmdlet?
Well, I'm going to show you that right now.
As we look at a basic PowerShell script, here is one,
Write-Host, that is a verb-noun syntax, right?
Write-Host is just like echo.
It says print this to the screen.
So, write to the host "Retrieving logon failures...".
Then, Get-EventLog, which says, I want to get,
I want to pull, information from this thing, the event log.
I want to get the newest ones, the five newest ones.
I want to get it from the security log,
and I want to get anything that has the Instance ID of 4625,
and that's the ones that I'm going to select.
So, essentially, this is a search command right now.
Now, what is this search command doing for us?
Well, it's saying, I want to go into the event log,
check the five newest entries that meet these conditions.
They're inside the security log,
and they have the instance of 4625.
Now, 4625 is a logon failure code,
so what is the last five people
who tried to log on and failed?
That's all I'm asking here.
Then, I have the next line,
timewritten, message | Out-File C:\log-fail.txt.
So, I want to write the message of those things
from the logs that I just got to this output file,
and then the last thing I'm doing
is writing to the screen again, Write-Host
"Log log-fail.txt has been created.".
So, essentially, we're doing the exact same thing
we were doing back in Bash, except in Bash,
we were looking for network manager entries,
and in this case, we're looking for failed login entries.
The next thing we need to talk about
is Windows Management Instrumentation Command, or WMIC.
This is a program that's used to review log files
on a remote Windows machine.
So, if I'm sitting on my machine as an administrator,
I can actually remotely go onto your machine
and check your logs if you're part of my domain.
Now, this looks something like this, wmic,
and then what I'm searching, in this case, NTEVENT.
NTEVENT, given a certain input, will return log entries
that match your parameters.
So, what am I looking for?
I'm looking at NTEVENTs where, the condition,
LogFile='Security' and EventType=5.
Then I want to get the source name,
the time generated, and the message.
So, essentially, this is doing
the same thing I was doing before.
I'm selecting all the security event log entries
whose events are type five, in this case, an audit failure,
meaning they couldn't log in.
Then, I'm going to output the source,
the time the event was generated,
and a brief message about that event.
This is really useful if you're trying to find events
based on specific details,
and this is more of that one-time thing,
but you can use this language inside of PowerShell as well
and call the WMIC if you need to.
Now, at the beginning of the lesson,
I mentioned Python and Ruby.
Now, Python and Ruby are interpreted, high-level,
general-purpose programming languages,
and they're used heavily by cybersecurity analysts
and penetration testers because they're easy
to write things in and they can do a great job
of going through and searching files and dissecting things
and finding what you need.
Now, in addition to that, they can do all sorts
of other things because they are full programming languages,
but they are scripting languages,
meaning they're interpreted line by line
as you go through the scripts and they are not compiled.
And since they're not compiled, you're not going to have
a binary file associated with them, just a text file,
so you can actually read all the source code very easily
if you find one on a machine.
The next programming language
we're going to discuss is Perl.
Perl is another general-purpose,
interpreted programming language that can be used
as a scripting language, much like Python and Ruby.
Now, Perl was actually created a long time ago,
back in the late 1980s,
as a general-purpose Unix scripting language
that was used for text manipulation.
Oftentimes, back in the late 1990s,
when I first started doing coding,
Perl was used on web servers to be able
to do dynamic content generation for the display
of webpages on a given Linux server.
In general, Perl code is very practical,
easy to use, and efficient,
and it is one of those languages where there are a lot
of third-party modules that you can download
to use with your code.
The last programming language we're going to talk about
is actually a scripting language only,
and this is JavaScript.
Now, JavaScript is a scripting language
that allows developers to do all sorts of fancy
and complex things when you're visiting a webpage.
Initially, JavaScript was added to HTML pages
to be able to have things like popups
and on-click actions, and things like that.
Now, over time, JavaScript has become more complex,
and there are many different versions of JavaScript.
For example, you might be using ReactJS,
which is React JavaScript,
for the front-end of your website,
or NodeJS, which is Node JavaScript,
for the back-end of your website.
If you're going to be doing a lot
of web application penetration testing,
then you're going to have to get familiar
with using JavaScript because you're going to have to read
through that code that's been written by those developers
to find what vulnerable components exist
that could be exploited during your attacks.
Now, in this lesson, all we did was provide you
with a very high level overview
of the six different languages.
Don't worry, we are going to return to all of them
in the next section of the course as we go through each
and every language to provide you with the basic syntax
and functions that these languages have,
and then going through a coding example
with each of these six languages.
For the rest of this section, though,
we're going to focus on using pseudocode to discuss things
like variables, flow control, logic control,
data structures, and object-oriented programming
so you can understand the basics of scripting
before we dive too deep into any singular language.