CompTIA Pentest+ PT0-002 – Section 21: Scripting Basics Part 2
March 18, 2023

203. Loops (OBJ 5.1)

In this lesson, we’re going to talk about loops. Loops are one type of flow control that allows you to be able to control which order the code is going to be executed in a given program. For example, if I wanted to run a piece of code 10 different times, I can either copy and paste that code 10 times in a row, or I can use a loop and have this logic takeover that will allow it to continue doing that function until it’s passed through 10 times. Now, when it comes to programming, there are three basic types of loops that you can use. There is a for loop, a while loop, and a do loop. Now the for loop is the most simple type that you’re going to use. For loops are used when you know exactly how many times you want to repeat a certain block of code. For example, I just gave you the example of repeating a piece of code 10 times so, you could set up some code that will go through that loop 10 times and then stop because you already know you want it to happen 10 times. To do this, you’ll simply write For i = 1 to 10 OUTPUT i, Endfor. This means we’re going to start with 1 and go through the loop and each time we’re going to output or print to the screen the value of i. So what you’re going to see is that on your screen, it’s going to go 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. It simply count up until it hits 10, and then it’ll stop executing that loop. Now, another kind of loop we have is what’s called a while loop With a while loop, we don’t know exactly how many times we want to run through the code. Instead, we want the code to go until something happens.

Now, what we’re going to do is set up a condition and we’re going to test for that condition at the beginning of the loop. For example, if I have the code, i = 0 and then While i is less than 10 OUTPUT i, i = i + 1, Endwhile, this will perform that while loop anytime the value of i is less than 10. In the case of this code we initially set the value of i at zero, zero is less 10. So we’ll go through this loop. We’re going to display zero to the screen. And then the value of i will equal zero plus one or one. So we now have i equals one. We test the condition again, is one less than 10. Yes it is. So we’ll output one to the screen. We’ll then add one plus one, giving us two. And we’ll continue to do this every time until that value of i is now set to 10. So what would be the last number on the screen? Well, it’s going to be nine, because we’re going to go through this loop from zero, one, two, three, four, five, six, seven, eight, nine. Once we’ve outputted nine to the screen we now have i equals nine plus one.

That means i is going to equal 10. When we go to check the condition again at the top we see that 10 is not less than 10 and therefore we are going to end that while loop. The third type of loop we have is known as a do loop. Now do loops are used whenever you have an indefinite iteration that you want to happen. This means we’re going to do this thing until some condition is met. But the difference is we’re testing the condition at the end of the loop, instead of the beginning of the loop. Now, why would we want to do this? Well, we would want to do this if we always want to make sure that code runs at least once. For example, if I had the code Do OUTPUT i, i equals i plus one until i is greater than 10, I can then make sure that I’m always going to go through this loop at least once no matter what the value of i already was. For example, if I set, i equals 50 right before the do statement I would still go and output 50 to the screen. Then i becomes 51, 50 plus 1.

Now I check it and say, is i greater than 10? Yes. 51 is greater than 10. I’m going to stop and only have that one number printing to the screen. Conversely, if I set i to equal zero it’s going to look a lot like the while loop that I already did. I’m going to output zero to the screen. Then I’m going to have zero plus one equals one. Is one greater than 10. No. So I’m going to go and do that again. And I’ll output one to the screen and I’m going to continue to do that until I output 10 to the screen because 10 is still not greater than 10. It’s equal to 10, but not greater than 10. So I’m going to go through that loop output of i being output of 10 and then getting 10 plus one being 11. And now when I check it I’m going to stop because 11 is greater than 10. Now, really these are some really simple examples but I just wanted to show you how these for, while, and do loops can be used. Now in the real world you’re going to find much better uses for these three tools. For example, when I use a do loop often, I’ll do it when I’m reading a file. This way I can do something like Do OUTPUT the file as I’m reading each line until the file is at the end of the file. Once I reach the end of the file I can stop because I don’t want to read anymore. That’s a great use case for doing a do loop instead of using something like a for or a while. Now as we move into some of our examples for each of the different programming languages you’re going to see the different for, while, and do loops that are being used in a real world context. And this will help you better understand when to use each one during your penetration test.

204. Logic Control (OBJ 5.1)

In this lesson, we’re going to talk about logic control. Now logic control is used to provide conditions based on different logical tests. This may be a test of a Boolean operator, an arithmetic operator, or a string operator. Let’s take a look at each of these and how we can use different logical conditions to migrate the flow of a program that we write in our scripts. First, let’s consider the following example, IF x equals 1 THEN OUTPUT “The statement was true.” ELSE OUTPUT “The statement was false.” ENDIF. What this code is saying is we’re doing basically an if-then-else statement. If this condition is true, and remember in Boolean, 1 is true, 0 is false, then we’re going to say, “The statement was true.” Otherwise, we’re going to output, “The statement was false.” This is a very simple way to test a condition and get a Boolean value of either true or false. Second, we can do things with arithmetic. This might be something like IF balance less than 10.00 THEN OUTPUT “The account has insufficient funds.” ELSE OUTPUT “The account has at least $10.” ENDIF.

Essentially, this is simply testing, do you have enough money in your bank account if the minimum balance required is at least $10. This is a simple arithmetic operation and we’re just using a less than sign. Similarly, you can use a greater than sign or an equal sign if you’d like as well. Another test we can do is based on string values. So IF x equals “Jason” THEN OUTPUT “The user was Jason.” ELSE OUTPUT “The user was someone else.” ENDIF. Again, this is a very simple case. We were simply testing the value of the variable and seeing does it match the string Jason. Now note, these conditional checks are going to be case sensitive so if your x was equal to lowercase, j-a-s-o-n, this would not be a true statement and you would get the output that the user was someone else. A final thing we want to look at here is how we can combine things to do additional checks. For example, IF minutes greater than 120 THEN OUTPUT “You have studied for 2 hours.” ELSE IF minutes greater than 60 THEN OUTPUT “You should continue to study for another hour.” ELSE OUTPUT “You need to study for at least 2 hours today.” ENDIF. What this is saying is we’re going to test the first condition, are the minutes greater than 120. If they are, then we’re going to output “You have studied for 2 hours,” and stop right there. If they’re not, we’re going to go to the first else condition. Now in that first else condition, we have another condition, if the minutes are greater than 60, then we would output “You should continue to study for another hour,” if they’re not, we’re going to go to its else condition which says, “You need to study for at least 2 hours today.” So if your minutes value is set at 75 going into this condition, it’s going to choose, “You should continue to study for another hour” because you don’t have at least 120 minutes but you do have more than 60. If you had 45 minutes, that would put you in the third condition, “You need to study for at least 2 hours today” because you haven’t even reached the hour point. That is what we’re doing in this nested if that we’re using in this if-else if-else-end if condition.

Now, the last thing I want to mention is that when you’re doing these different conditions, you can actually combine them as well. For example, I can combine Boolean conditions with arithmetic operations. Take a look at this one, IF minutes greater than 60 AND minutes less than 120 THEN OUTPUT “You have between 60 and 120 minutes completed.” ELSE OUTPUT “You have more than 120 minutes or less than 60 minutes completed.” ENDIF. Notice, I’m testing the condition inside those parentheses, the minutes are greater than 60 and the minutes are less than 120. This tells me I’m somewhere between 61 and 119 minutes. By using these different types of conditions, you can start excluding or including different areas. For example, if I wanted to find anybody who had more than 120 minutes and less than 60 minutes, that might tell me people who are over studying or under studying, I can do that by using minutes less than 60 and minutes greater than 120. This will allow you to do these type of logical conditions and testing conditions using ANDs, ORs, and NOT conditions which are all Boolean operations.

205. Data Structures (OBJ 5.1)

In this lesson, we’re going to talk about the different data structures that you can use when you’re writing your own scripts and programs. This includes data structures like JSON, the JavaScript Object Notation format, key value pairs, arrays, dictionaries, comma-separated values or CSVs, lists and trees. The first data structure we have is known as JSON or the JavaScript Object Notation format. JSON is an open data file format and data exchange standard that uses human readable text to store and transmit data objects that consists of different key value pairs and arrays inside of that JavaScript notation. JSON is very commonly used in a lot of different electronic data interchanges between clients and servers, such as web applications with their servers or different scripts with an underlying server. JSON is a language independent data format but it was originally developed for use by JavaScript.

These days though, JavaScript object notation, or JSON format is used by most modern programming languages including ones like Python, Ruby, and Pearl when they’re going to be able to send data or receive data from different systems or different servers. As that data comes in, it is easy to parse that format and take the data and then load it up into other variables, arrays, lists, dictionaries and other data formats. Let’s take a look at a simple JSON representation of data about a given person. Here you can see there are a bunch of key value pairs, notice on the left side, we have the type of key and on the right side, we have the value. So first name, Jason, last name, Dion, is alive, true children, true, spouse, true address. And here we have an array being kept inside of this key value pair. Now this array is going to contain multiple sub key value pairings. For example, under address, you might have street address 123 Main street, city, Davie, state, Florida, zip code 33317. And you can see how we break up these key value pairs inside of this array that is assigned to the address key value. The second data structure we have are key value pairs.

Now I’ve already showed you key value pairs inside of the JavaScript Object Notation, but let’s cover what they are just a little bit more. When you’re dealing with key value pairs. Essentially, this is going to be something on the left defined as the key and something on the right defined as the value. In these key value pairs, you are assigning some value to some type of title or key that we might use as a variable name later on. For example, in the JavaScript notation that I just showed you, we had one piece of that. That was a single key value pair. That single key value pair was, for example, first name colon Jason. Jason was the value, first name was the key and you saw there were many different key value pairs that were being shown inside that JavaScript data notation. Now, it’s also important to note that when you are defining key value pairs you’re going to do this in a particular format. If you’re using a string that value is going to be surrounded by quote marks.

If you’re using a Boolean value like true or false you don’t need those quote marks because true and false are considered the data type and the expected value to be received. If you’re dealing with something that’s a number like a zip code, you can just put that as the number itself, you don’t need to put quotes around it. When you enter numbers in this way without the quotes, they are treated as integers. And in that case you can use them for mathematical operations. If you put quotes around them, they’re going to be treated as a tech string and you can’t use mathematical operations on them. The third data structure we have is a arrays. Now a arrays are a type of data structure that are used to hold multiple values of the same type. For example, if I wanted to hold all of the different names that a person has such as their first name, middle name, and last name I could put that into an array that contains three different values. The first position will always be the first name. The second position will always be their middle name. And the third position will always be their last name. That way, I can have somebody like John Michael Smith. And if I wanted to call out the particular part of that array, I can do that by putting in name, bracket, one, bracket, and that’ll pull out the second position.

In this case, Michael. Now, why is the second position being pulled when I use the number one? Well, it’s because computers always begin count at zero. So a three item array is actually counted as zero, one, two for the first, second and third position. Normally when you’re setting the value of an array, you’re going to be setting those things as a list with commas in between each value. For example, if I want to set the name array, I can set it as name equals John, comma, Michael, comma, Smith. And that will put these things into position zero, one, and two respectively inside of that array. When you want to get data back out of an array you can do that as well by simply putting the name of the array, in this case name, and then a bracket and the number that you want to get for the position. So if I did name bracket two end bracket that’s going to be referring to the third name, in this case the person’s last name, which was Smith. The fourth type of data structure we have is known as a dictionary.

Now a dictionary looks a lot like an array, but the difference is instead of storing a single value inside of each position of the array we’re going to store key value pairs. So think about this like you would a phone directory. You’re going to have somebody’s name and their phone number. In this case, I wouldn’t want to create an array because I’d have different things in each position. I might have one array for all of the names and another array for all of the phone numbers, but now I have to use both of those arrays every time I want to find somebody’s name and phone number. Instead, it would be better to use a dictionary where I have a key value pair stored for that name and phone number. For example, if I wanted to set up a dictionary containing my phone book I can do that by typing in phone book equals curly bracket, and then each of the key value pairs separated by commas such as John, 111-1111, then Mary: 222-2222, Mark:333-3333 and then end the brackets at the end.

This gives me three people with their name and number stored in a key value pair. Now, if I wanted to find out information about mark I could simply do that by calling up phone book bracket, quote and then the key value in this case, mark, and then end quote and end bracket, that would return the value of Mark’s phone number which would be 333-3333. This is the idea of using a dictionary instead of an array. I’d like to think about dictionaries as essentially an array of key value pairs. The fifth data structure we have is known as CSV or comma separated values. Normally comma separated values are going to be stored in a text unlimited format using a comma to separate each of those values. Each line of that file is considered its own record and it’s almost like a flat database. You can read or write to these files from many different programs including Microsoft Excel and many logging applications. For this reason, it is a great way for you to send and receive data across multiple systems because almost every type of application will support CSV import or export capabilities instead (indistinct) on a proprietary format that may not work with other systems.

The biggest limitation with CSV files is that everything is separated by commas. And so if you have commas inside of the data itself this can actually throw off data inside of your records if you don’t escape those first. Now, when you’re dealing with CSV files these are very interoperable ways of working with things because they can store things like numbers and text in plain text, and you can have different areas for each of the fields and all of those are separated by commas. For example, let’s take my phone book example. I can have a CSV file with all the people in my phone book that would be John, Mary and Mark. With John, I would put John, Smith, 111-1111. Then I go to the next line, Mary, Jones, 222-2222. Then I go to the third line, Mark, Williams, 333-3333.

Now I can import that data into any programming language or script that I want. And I can know when I read the first line that is one record and it contains my first person. I have three fields here, the first name, the last name and the phone number. In some CSV files, you may use the first line to actually define what those three categories are. In this case, I might want to put first, last, phone and then have my three lines of data. This way, whoever is importing that data will know what columns to expect as they’re reading the data. And it’s going to continually read until it reach is that deliminator which is the comma in a CSV file. The six type of data structure we have is known as a list. Now, a list works a lot like an array, but the big difference is you can store different data types inside of that list in a sequential manner. When we used arrays, we had to have the same data type. So everything in that had to be a string or had to be an integer or had to be a float. But here on a list, I can actually mix and match things.

When you’re using a list, every element of the list is going to be called an index and the index values again, start from zero and goes up to the last element, which is called the positive index. Now in some programming languages you can also use negative indexing, which starts at negative one and enables you to access elements from the last thing to the first inside of that list. Now, let’s take a look at lists a little bit more so we can understand how they work. Let’s say, for example, I’m going to create a list and I’m going to call it my_list equals bracket and then I’m going to list out all of the data inside of my list. So I might have things like [1,2,3,”example”, 3.14,] Now here, I have five things in my list. Notice that in my list, I have threes integers, one string and one floating point decimal number. Now that I have things in my list, I can read that data or replace that data using different commands in the programming language. When we get into our programming languages we’ll talk a little bit more about how you can do this, but for now just realize you’re going to call them almost like you would in array.

If I wanted to get out the word example from this list I would use my_list[3] and that’s going to reference the fourth position in this particular list. The seventh data structure we have is known as trees. Now, trees are non-linear data structures and they essentially have a root and nodes. The root is going to be the baseline for which all the other node are going to originate from. All of these extra nodes that come off of it are going to be considered other data points that are available to us. Now, again, this is going to depend on which language you’re using whether or not they support trees, but if they do we essentially have parents at the root. And then it goes down to the next node which is a child.

That child can have other children. In which case it itself is considered a parent. And those underneath it are considered children to that node. As we continue to go down these nodes we can go down to the last node which is called the Leafs or the end nodes. Essentially these trees are used to create a hierarchy that can be used in a lot of different real world applications. If you’re programming webpages, for example, HTML uses trees to distinguish which tag comes under which block. And so it knows exactly where it is in the directory structure when it’s displaying a particular webpage. There’s lots of different uses for using trees, especially when you’re searching or organizing data. But again, it is only supported by some of the programming languages and not all of them out there.

206. Object Oriented Programming (OBJ 5.1)

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 themselves. 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, Pearl, 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 named 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 def 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 parentheses radius colon. 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 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 r squared. 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 the radius of plus the number of the radius I received as 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 three is 28.26 which is nine pi. Then we’re going to print that to the screen by using print parentheses 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 do 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 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 that’s written by other penetration testers and other software developers in the field.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!