In this lesson,
we're going to talk about object-oriented programming,
and specifically the idea of functions,
procedures, classes, and libraries.
Now, object-oriented programming is a programming paradigm
based on the concept of objects,
which basically can contain data and code
in the form of different fields or properties
that we can then reference.
By doing this,
this allows us to be able to use procedures or methods
to be able to call different functions
based on the different contents of the objects themself.
Now, I know this sounds very complicated,
but essentially the whole idea here
is that we can have each object,
and have its own procedures that can be accessed
and modified for the data fields itself.
Think about it this way.
I might have an entire class of something such as a car.
There's lots of different kinds of cars.
I might have a red car, or a blue car,
or a fast car, or a slow car,
but I can create one single object known as a car,
and now I can have different variations of that car
and reuse it in my different programs
inside of my programming languages.
Now, most of the modern programming languages
are going to be object-oriented.
This includes languages like Python, Ruby, Perl,
JavaScript, and even PowerShell.
Unfortunately though, Bash is considered
a scripting language that does not support
object-oriented programming,
so you're going to have to use one of the other languages
if using object-oriented programming is important to you.
The first type of code that we're going to talk about
inside of object-oriented programming is functions.
Now functions are essentially a block of code
that you're going to give a special name to.
This allows you to call that name function
at any time you want, and then it will go ahead and perform
all the pieces of code inside of its block.
This allows us to make our programs very modular,
and we can then reuse those blocks of code
across different programs by simply calling those functions.
Now, these functions are usually going to be defined
in some different method depending on what you're using
as your programming language.
For example, if you're using Python,
you can define your function by using the term D-E-F,
and then a name for that function.
So if I wanted to create a function called area,
that's going to take radius as its input
and then output the area of a circle,
I can define that by using def area (radius):
This will then return radius as the final result,
and then I'm going to use some comments here
to make sure people can understand what my code is doing.
To do a comment in something like Python,
you can put the hashtag.
Anything following the hashtag on that line
will be ignored by the computer and the compiler.
This is just something for us as humans to read.
So I'm going to put comment,
"This is a comment to state what the code will do,"
comment, "The next line calculates the area of a circle."
Then I'm going to define a variable called circle_area,
and it's going to be equal to 3.14 times the radius
I received as an input, times that radius again,
because the area of a circle is Pi r2.
Then I'm going to create a string
that is called display output, and in the string,
I'm going to set it equal to the text string,
the area of the circle with a radius of plus,
the number of the radius I received is an input
plus the word is plus the area of the circle
that I calculated.
This will then display something like the area of the circle
with radius 3 is 28.26, which is 9 Pi.
Then we're going to print that to the screen
by using print privacy (display_output).
Now, anytime I want to call this function
inside of a program, I'll simply enter area, parentheses,
and the radius that I want to give it.
This will then allow us to calculate any area
of any circle using whatever radius we give
because we've now used modular programming.
Now the second thing we're going to talk about
is known as a procedure.
Now, procedures can be anything such as functions, methods,
routines, or subroutines that are going to take input,
generate output, and manipulate data.
In most modern languages, the idea of a procedure
and a function are used equivalently, but essentially,
procedures are a little bit of a higher level group,
and they can contain multiple functions within them.
Next, we have classes.
Now classes are going to be the definitions
for the data format
and the available procedures for a given type
or class of object.
This can also contain data and procedures
or functions underneath those classes too.
Think about classes as a way to group
other types of objects.
A class is really a user-defined prototype or template
from which other objects can be created.
Think again to my example of a car.
I can create a car once that's a class.
Then you can have a red car, I can have a blue car,
somebody else can have an electric car,
another person might have a diesel car.
All of these are types of cars
because we've defined the basic data structure
of what a car is.
Now, how did we define that?
Well, that's going to depend on your programming language,
but for our case that we're talking right now,
we might say that a car is any kind of vehicle
that has some kind of motor,
whether it's electric or gas or something else,
and has at least four wheels
that allows it to roll down the road.
So if I had a vehicle with two wheels like a motorcycle,
that is not a type of car.
But if I had an SUV or a truck,
those would also be a type of car
based on my class definition.
The final thing we need to talk about is libraries.
Now, when you go and leave your environment,
all of your functions and variables and classes you created
would end up being lost.
There's no way to reuse them,
but that's why we have libraries.
Libraries allow us to take all of this code
that we've created and put it into reusable areas.
Now, anytime you start up a new environment,
you can simply reference your library
and it's going to load in all of those functions for you.
In each language, there's a different way to do this,
and so we'll talk about that as we go through
some of our code examples for Python, Pearl, Ruby,
PowerShell, Bash, and JavaScript.
But for now, I just want you to realize
that a library is essentially an external collection
of all of these different classes and functions
and procedures that we just wrote
and that we're going to want to reuse again later on.
Let me give you a great example of this.
If you're starting to make your own
penetration testing tools,
writing something in Python, for example,
you don't want to have to go and figure out
how to interact with the TCP/IP stack.
Instead, you can just import those functions
that already know how to communicate using TCP/IP.
This will save you a ton of work
because you don't have to start all over from ground zero.
Instead, you can just import that library
and that's going to bring in all the modules
and functions that you need to interact with the network
to be able to do your port scans and other things like that.
This is the benefit of object-oriented programming,
and the ability for you to reuse code
as written by other penetration testers
and other software developers in the field.