210. Coding in PowerShell (OBJ 5.2)
In this lesson, we’re going to take a look at how you can do coding inside of PowerShell. First, let’s talk about commenting your code. Just like we did in Bash, we can use the hashtag to comment a line of code. So if I use hashtag, This is the first line of my script, that piece of code, everything beyond that hashtag on that line is going to be ignored. Now, in addition to having a single hashtag, you can also use a less than sign and a hashtag as an opening tag. Then everything you write after that point will be ignored until you end or close that tag by doing hashtag, greater than. So, for example, if I wanted to have a comment block that said, This is a comment block, you can use this to comment out large sections of text or code in your scripts. I would simply put less than hashtag, all of that text, and then hashtag, greater than to end that block of code. The next thing we’re going to talk about is variables. In PowerShell, all variables are going to start with a dollar sign. So if you want to assign a variable, you’ll simply do dollar sign, the variable name, equals the value.
For example, $CustomerName = Jason. Now, if you want to display the contents of that variable or interact with that variable using other commands, you’re simply going to call it using the dollar sign and its name. So in this case, it’s $CustomerName to be able to read data from that variable or display it to the screen. You can also declare variable types inside a PowerShell too if you want. For example, if you want a variable to only hold integers, you can define it by putting [int]$ and the variable name, like AnswerNumber, then equals 42, or whatever the value is you want. In this case, I want to make sure that this AnswerNumber will only take a number like 42 or 52 or 1 as its input. If I want to do the same thing with a string, I’ll simply use [string]$AnswerString, equals, quotes, and the string I want to save. For example, the life, the universe, and everything. If you want to declare a constant in PowerShell, you do this by simply making a variable read only. To do this, you’re going to use the commandlet Set-Variable, the name of the constant, in this case Pi, Option and the option you want, in our case ReadOnly, then -Value and the value you want to set, in my case, 3.14159. This gives me a single complete line of code that says Set-Variable Pi -Option ReadOnly -Value 3.14159 to set the constant of Pi to the value of 3.14159. PowerShell also supports the use of basic or indexed arrays. This will allow you to store multiple values and then reference them from a single name that essentially make a list of variables. For example, if you use $tempArray, equals @, parenthesis, end parenthesis, this will create a blank array called tempArray that is ready to start storing data. If you want to go ahead and create that array and put data in it at the same time, you can do that as well. By using $tempArray, equals, @, parenthesis, quote, Jason, quote, comma, quote, Sahra, quote, comma, quote, Eduardo, quote, comma, quote, Linda, quote, end parenthesis. And this is store the four names of Jason, Sahra, Eduardo and Linda into an array called tempArray. If I want to get data back out of that array, I can read it by using $tempArray, bracket, and the position number.
For example, I’m going to use $tempArray[1] and that would return the value of Sahra. You could also use named arrays or associative arrays inside of PowerShell. For example, let’s create one called PhoneBook to store names and numbers. To do this, we’re going to use $PhoneBook, equals, @, and then a set of curly brackets. If I want to save the first name as Jason, I can do this by doing $PhoneBook.name, equals, quote, Jason, end quote. If I want to store the number for that, I can then use $PhoneBook.number, equals, quote, 321-123, quote. If I want to be able to read the information of Jason back out, I can do that by doing $PhoneBook.name, and essentially referencing that key of name to the word Jason. If I wanted to save all of this at once using one command when I initialize it, I can do that too. By doing $PhoneBook, equals, @, curly bracket, quote, name, quote, equals, quote, Jason, quote, comma, quote, number, quote, equals, quote, 321-1234, quote, end curly brace. Now, if I wanted to get the number out of that, I can simply type in $PhoneBook.number, and the number of 321-123 would come back out. Next, we need to talk about comparisons in PowerShell. Just like in Bash, we are going to use textual letters to define what we are comparing, and in fact, they’re the exact same ones as Bash.
This would be things like -eq for equal, ne for not equal, gt for greater than, ge for greater than or equal to, lt for less than, or -le for less than or equal to. If you want to use these statements, you simply are going to put the variable, dash, the thing you want to use, and then the variable name. For example, to compare variable a against variable b to see if they’re equal, I would simply type in $a -eq $b, and that will compare a against b. If they are equal, I’ll get a value back of true. If they are not equal, I will get a value back of false. Next, we have to talk about our conditional statements like ifs. Now, when we deal with an if inside a PowerShell, it’s going to look just a little bit different than we used in Bash. When we’re using a conditional statement of if, we’re not actually going to have to type out the word then. To create a conditional statement inside a PowerShell, we’re going to do if, parenthesis, the condition we want to test, end parenthesis, and then a curly bracket to open up the then part of it. Anything in between this set of curly brackets is going to be considered the then commands that we’re going to operate. Now, if you want to be able to use an else condition, you would use if, condition, curly bracket, the commands you want to run, end curly bracket, else, curly bracket, the you want to run, end curly bracket. If you want to do multiple cases, you can do things like if, elseif, else, and in this case, it’ll be if, the condition, the curly bracket with the code, elseif, the condition, the curly brackets with the code, else, the curly brackets, and the code you want to run. As you can see, they’re not very complicated, but they are just a little bit different than what you learned in Bash. Next, let’s talk about flow control like for, do-while, and while loops.
First, we’re going to talk about a for loop. Now, inside of PowerShell, we’re going to use a for loop to perform a set of commands for each item in a list. For example, if I use the format of for, parentheses, and then the initialization value, semicolon, the condition I want to test, semicolon, and how often I want to repeat that would be my setup for the for. Then I’m going to use a curly bracket, and the statement or commands I want to run, and then end that curly bracket. For example, if I wanted to print some value to the screen multiple times as I go through the for cycle, I could do something like for, parenthesis, $i=1, to initialize the i variable with a value of 1, semicolon, $i -lt 5, semicolon, which test the condition is the current value of i less than 5. Then we have $i++, and end the parentheses. This will then, at the end of each loop, increment the value of i by one. Then we have a set of curly braces, and inside of that, we’re going to have the command we want to run. In this case, Write-Host $i, which will display the value of i to the console on the screen.
Then, when we’re done with that for loop, we will Write-Host All done to the screen. So if I ran this code, what you’re going to see on your screen is 1234All done. Now, why did it stop at four? Well, the first time through the loop, i equals one. we test is I less than five? Yes, it is. We run through it and then increment by one. Now we’re at two. We do the same thing for two. Two is less than five. We print it to the screen. We did increment. We then go to three, three is less than five. We print it to the screen and increment it. Now we’re at four, four is less than five. We increment it. Now we make it five. Is five less than five? No, it’s not. So we’re not going to print five to the screen and instead we’ll print All done. The next loop we have is what’s called a do-while loop. A do-while loop is going to be used to perform a set of commands while a test is true. So we’re always going to run things at least once. And then we’re going to test the condition. The way this format works is do, curly bracket, the commands you want to run, end curly bracket, while, parentheses, the condition you want to test.
So if I set the variable i to equal one and then enter a do-while loop, I’m going to run the command Write-Host $i and print i to the screen. Then I’m going to increment i by using $i++ and then I’m going to check that condition. Now, is i less than or equal to 10? Yes, it is because the value is only two at this point. We’ve run through it once and we incremented our counter. Now we’re going to go and print it again. Two gets printed to the screen. We increment by one. We’re now at three. Is three less than 10? Yes, it is so we’ll go through the loop again. So we print three to the screen. We increment to four. We test that and we continue on until we get to nine. Once i equals nine, we test that condition, it is still less than 10 so we’ll print nine to the screen and then increment to 10. Now is 10 less than 10? No, it’s not. So we’re going to stop and print All done. So what you would see on the screen is 123456789All done. The third type we have is the until-do. With until-do, we’re going to perform a set of commands until a test is considered true. So this takes the format of do, curly bracket, the commands you want to run, end curly bracket, until, parenthesis, the condition you want to test, end parenthesis. So again, if I use $i = 1, and I set up a do, Write-Host of I, i++, until, i is greater than five, Write-Host All done, what would you expect to see on the screen? Well, you should expect it to have 12345All done. Now, why is that? Because we’re going to continue to do this loop of printing something and incrementing by one until i is actually larger than five. Once that statement becomes true, that’s when we’re going to stop.
And that statement won’t become true until i is equal to six which happens right after five is printed to the screen. Next, we need to talk about string operations. Now with string operations, you’re going to be able to set values of a variable to a string by putting that content inside of quote marks. For example, $testString, equals, quote, Test, space, String. Now, if you want to display this to the screen, you would simply use Write-Host $testString. But you can also concat or add things to strings. For example, if I wanted to make this say Test, space, String2, I can do that by typing in Write-Host $testString, plus, quote, 2, end quote. And that would display Test String2 on the screen. Also, when you’re working with a string, you can also pull out just certain pieces of a string based on their position and the length you want. To do this, you’ll use the Substring command. When you want to display a variable with a Substring, you’ll put $TestString.Substring, parenthesis, 2, comma, 4, as an example. This is going to start at number two position of the TestString and pull out the next four characters. So if I ran this against my testString, what would I expect to get on the screen? I would expect to get st, space, S because I’m going to start at the second position which, if you remember computers start counting at zero, we’re going to go zero, one, two, that gets me to Tes, so it’s going to start at s.
Then I’m going to go for four places, st, space, S, and that gives me the four places I want to display to the screen. Next, let’s talk about input and output from the keyboard and monitor. If you want to put something out onto the screen, you’re going to use Write-Host, and then put that thing on the screen. If you want to read, you’re going to use Read-Host. So, putting this together, I might have something that looks like this. Write-Host, quote, Please enter your name, colon, end quote. Then Read-Host $UserName. This is going to print something to the screen and ask for the username from the person to enter it. When they do that, I’m then going to use Write-Host, quote, Hello, space, quote, plus, $UserName, plus, quote, exclamation mark, quote. So in total, on the screen, you should see, Please enter your name.
You’ll type in your name, such as Jason and hit Enter. Once you do that, it’s going to combine or concatenate these three strings. Hello, the username you entered, and the exclamation mark to print, Hello, Jason, exclamation mark, onto the screen. Now, in addition to reading from the keyboard and writing to the monitor, you can also read and write files. To do this, you can read a file by setting it into a variable, such as $TempFile, equals, Get-Content – Path, and the file name you want, such as C:\test.txt. If I want to display the contents of that file to the screen, I can use something like Write-Host $TempFile and that will show up on my screen. In addition to this, we can also redirect information into files by using the greater than sign or the double greater than sign. If you use a single greater than sign, it’s going to overwrite your file. So, for example, if I use Write-Host, quote, This is the beginning of a new script log file, end quote, and then greater than, script.log, it’s going to overwrite any content inside of script.
log and only write the text that I just listed inside of this command. Now, if I wanted to append something to the end of this script, I can do that by using the double greater than sign. For example, let’s say I wanted to run a script called enumerate.ps1. I would then say .\enumerate.ps1, and then I can append its results into the log file I just created it. So I would use .\enumerate.ps1 >> script.log. As you can see, a lot of PowerShell looks very similar to Bash with just a few slight differences. One of the big giveaways that you’re going to see when you’re dealing with PowerShell is inside the script, you’re going to see commandlets listed such as Write-Host, which have a verb-noun format. For example, Read-Host, Write-Host. This is an action followed by the thing you’re doing the action on. Most commandlets inside a PowerShell are going to be written this way and so that tends to be the giveaway when you’re looking at a script to determine if it’s a PowerShell script or not.
211. PowerShell Example (OBJ 5.2)
All right, we’re at PowerShell. This is a PowerShell script example. For all of my people who love Windows, this one’s for you. So PowerShell is used as a command scripting language inside of Windows. What do you think this script here is going to do for us? Well, we’re going to Access = Get-Date. We’re going to get the date. We’re going to write the output you ran the script on, whatever the access date was, and we’re going to put that to our output. Next, we have ComputerName = envcomputername. OS = Get-WmiObject of the Class Win32_OperatingSystem dash ComputerName $ComputerName, select the caption, select the string windows, split that, replace it and select the string windows. That’s a lot of words, right? And then it goes into this if statement, if this, if this, if this, if this, right? So what does that look like it’s doing to you? Well, to me, what that looks like is that we are getting the computer name and then we’re trying to determine what operating system this computer is running. So if it matches 10, then you’re running OS 10. If you’re matching eight, you’re running eight. If you’re matching seven, you’re matching seven, right? And it’s going to tell you what operating system this person is running.
Next, we’re going to use a Write-Output the following users are currently logged in and then it’s going to do if it matches version seven, Windows 7, we’re going to query the user using that command. If we’re using Windows 10, we’re going to use this other thing, so else do this. If this, then that. Then we have the next thing. We’re going to write our output and it’s going to go to PSdrive, PowerShell drive, select the object, exclude used, free, provider, credential, and currentlocation. What is this doing? Well, this is trying to list out any share drives that person has. And finally, we have SMBCheck = Get-ItemProperty dash Path HKLM, which is a registry key, that long registry key, Name SMB1, SelectObject SMB1. If SMBCheck dash match 0, Write-Host SMBv1 is currently disabled. Otherwise, Write SMBv1 is enabled. So putting all that together, what did this PowerShell script do? Well, it’s an enumeration script and this is only a small piece of it. This comes from a much larger script of about four or 500 lines that you can run on a victim.
Once you’ve exploited a Windows machine and get access, you can run this PowerShell script and it will enumerate that system. It’ll tell you which users are logged in, what version of Windows it is. It’s going to tell you what shares there are, what printers are hooked up to it, if it’s running SMBv1, if it’s missing patches, all of that information can be run from this one script. And this is just a small piece of that script. So that’s all this is, it’s an enumeration script. We’re finding out information about a machine that we’ve just exploited. Now, how do you know this is PowerShell? Well, for one, it talks a lot about Windows. The other thing I noticed is that there’s these get things, right? Get this, select that. That is very common in Powershell. They use a lot of words that have two or three or four words strung together with dashes as part of their operations. You can start seeing what PowerShell looks like the more you look at these type of scripts. PowerShell is very, very distinctive and much different than Python, Bash, or Ruby and so it’s pretty easy to identify if they give you a PowerShell script.
212. Coding in Python (OBJ 5.2)
In this lesson, we’re going to start coding in Python. Now, we’re going to go a little bit quicker this time because we’ve already spent a lot of time talking about Bash and PowerShell and we’re really going to talk about all the same functions. The only difference is what does the syntax look like? Now in Python if you want to comment something, you’re again going to use the hashtag. In Python though, there is no block commenting. So, you have to put the hashtag in front of every single line that you want to be commented out and ignored by the computer. Next, let’s talk about variables. In Python, they try to make a very stripped down and easy to use language. So, it’s not a lot of extra fluff or extra character when you’re typing things out. In the case of variables, you simply give the variable a name and then equal a value. For example, if I want to create a variable called Price and set it equal to 10, I would simply type Price = 10. Now, if you don’t use quotes around the value, this is going to be treated as a mathematical number. And in this case an integer because I used the number 10 as the first value assigned. Now if instead, I want to treat something as a string I would then put quotes around it. I could do something like Vendor = “CompTIA”. Now, in Python you can also use a single quote or a double quote. So, I could also do Vendor = ‘CompTIA’ and that will be treated as a string. If I put a number inside of quotes that’s also going to be treated as a string. So if I set Vendor = “123.” That 123 is no longer a mathematical number, but instead is treated as asking text for maybe will do string manipulations with it. Now by default, Python is going to assign what it thinks is the best type of variables for you, when you do the initial assignment like we did with price and vendor. But if you want to specify a certain type for a variable, like a string integer or float, you can do that when you initially assign the value.
You can do this by putting the Variable = the type, parenthesis and then the value. So price = int(42) would say the price is now set to the integer 42. Everything else that’s ever going to be stored inside that price variable would now be considered an integer unless I re initialize it with a different type. For example, if I now wanted to use floating point numbers or decimals, I could do Price = float(42.00). If I want to make the price now a string, I could do that as well. By typing it. I would do Price = str(“The life, the universe and everything”). This now makes that a string variable instead of being an in integer or float variable. Now in Python, when you want to display or interact with a variable, you simply call it by its name. So price, vendor, customer name, all of these are variables. You don’t need any special characters in front of it, like a dollar sign to call them like you did in some of the other languages. Now you may be wondering how do I set a constant inside of Python? Well, there actually, isn’t the idea of a constant inside of Python. So if you want to make a constant, you basically are just going to treat it like a variable. Now by convention, if we’re going to treat a variable as a constant, we’ll simply make it in all capital letters.
For example, PI= 3.14159 would now be treated as a constant pie. But the computer will allow you to write other values to this because after all it is only a variable. The only reason we’re treating it as a constant is because we made it all capitalized for us as humans to realize that we wanted this to be a constant and not something we’re going to change throughout the rest of our program. The next thing we need to talk about in Python is Arrays. Now, if you want to create an Array, you can create a blank Array by using something like tempArray =[]. Now, if you want to put something in the Array, when you create it, you can do that as well. By simply typing in something like tempArray= [ Value1, Value2, Value3]. Now, if those are numbers, you could put numbers in there, but if there’s strings, you’re going to have to use quote marks around each of those values. For example, if I wanted to create a name Array, that’s going to contain the four names of Jason, Mary, Joe, and Susan. I can do that by typing in nameArray= [“Jason”, “Mary”, “Joe”, “Susan”] Next, we have to understand how to pull things out of an Array. And to do that, we’re going to use the name of the Array[Position]. So if I used name array[0] that’s going to give me the name, Jason, which was the first position or position zero inside of that Array. Now in the other languages, we talked about associative and named Arrays. In Python, we don’t use those terms. Instead, we call it a dictionary. If I wanted to create a dictionary, that’s going to be a phone book for us. Like I’ve done in the other languages. I would simply type in Phone Book= {}. As you can see when you’re using a dictionary, you’re going to use curly brackets instead of the square brackets. So if I’m creating a phone book with a key of name and a value of Jason and a key of number and the value of 321- 1234, I would do Phone book ={“name”= “Jason”, “number”,= “321-1234”} Now, once I’ve done that, you can then call out the thing you want based on its key name such as Phone Book[“Name”] And that would give me Jason, if I did Phone Book [“Name”] that will give me 321- 1234 as my result. Next, let’s talk about comparisons. In Python, they try to keep it very simple. And we use things that look like mathematical symbols, or if we’re using strings and other characters. When we want to do is equal to we test this by using two equal signs.
Now, this is important because if I wrote something like a = b, I’m not actually doing a comparison. If a= b, I just set a to the value of b, just like I’m assigning a variable. So in Python, you have to use the double equal sign when you’re doing the comparison. So a ==b will tell me is a and b equal. If they are, I get true. If they’re not, I get false. Then we have the a is not equal to. There’s two different ways to write this. My preferred way is to use the exclamation equal sign as the not equal to, but you can also use the less than, and greater than sign right next to each other, to be able to say, not equal to. So this would look like this a!= b or a <> b. Next we have is greater than. And to do greater than you’re just simply going to do a > b using the greater than symbol. If you want to do greater than or equal to you’ll simply use the greater than, and then the equal sign. So a >= b. If you want to do less than you’ll just use the less than sign. A < b. If you want to do is less than or equal to you’ll use a <= b. Now, when we get to conditionals, we have the, if statements just like we did before, but in Python, they’re really simple to do. If you want to do an if conditional statement, you’ll simply type in if (the condition): and the next slide will have the thing that you want to do.
For example, if I had the statement, if a == b: print (a) and the value of a and b are equal, I’m going to display a to the screen. Now notice I have indented the print command in just a couple of spaces to make sure it’s easily identifiable, as part of that If statement, this is important when you’re doing Python. Next, you can also do if else statements, these take the form of if (condition): # then do something else: # do something else. If you want to do a multiple, if conditions, you can do that as well. If (condition): then do something. Elif (condition): then do something else. Else: do this thing instead. Next let’s talk about flow control. In flow control, the first one we have is the flow loops. Now flow loops are going to be used to perform a set of commands for each item in a list. For example, if I have four X in list, colon do something. Let’s say, for example, I set up an Array called certification list. It = [A, Network, Security, CySA, PenTest, CASP]. Now for certification name in certification list: print (certification name.) This is going to go through and create a list of A, network, security, CySA, PenTest, and Casp. Now, when it prints to the screen, what is this going to look like? Well, it’s going to look like a mess because they’re all going to be smashed together. In my print command. I only said to print the variable. I never said to print a space, a comma, or a new line between each item in the list. So you’re going to get something that looks like ANetworkSecurityCySAPenTestCasp, all written as one word. Next, we have our while loops. While loops are used to perform a set of commands while a test condition is true. For example, i= 1 while i< 6: print(I). I = i+ 1 print (“All done”). Now on the screen, you should see 12345All done, because we went through that loop five times before the value of i became six and then six was no longer, less than six. So we would stop working. Now in Python, we don’t have the idea of an until loop, but you can instead create that functionality by reversing the logic of your while loop. For example, if you want to perform a set of commands until a test condition is true, you would simply set the condition backwards from what you initially wanted.
Consider the last example where I wanted to print the numbers, starting at one and counting up while I was still less than six. Now, if I wanted to do this as an until statement, I can instead say it this way, print all the numbers, starting at one and count upward until i is greater than five. Notice, this is essentially the same logic as you using an until command. So i= 1. Well, i >5: print(i). i= i + 1 print (All done) and again, I’m going to get the same results, but now I’m testing the opposite condition, but the results are still going to be 12345All done. When I print them to the screen. Next let’s talk about String operations. String operations and Python are pretty easy. You can have something like testString=”Dion Training is helping me learn to code.” If you want to print that to the screen, simply type print (testString) and on the screen, you’ll see Dion Training is helping me learn to code. Now, if I wanted to add multiple strings together, I can that using the plus sign. For example, let’s say I had the command print(testString, “in Python” + “today”) What is this going to do? Well, it’s going to take the variable test String, which says Dion Training is helping me learn how to code. Then add space in Python and then space today to put all that in one line on the screen.
Now, when you’re dealing with variables that contain strings or strings themself, you can also pull out certain parts of that string by referencing the variable name of the string, and then adding the positions that you want to read from. You do this by using brackets after the string. For example, let’s create a variable called my name and it’s going to store a string as its value. To do this in Python. We’re simply going to enter my name= “and your name” in my case Jason’s based Dion. Now, if I wanted to reference just a part of this string from the variable, my name, I can type it as my name [X:Y] X is going to be the position I want to start reading from. And Y is going to be the position that I’m going to stop reading. Now, remember the left most character is always going to be position zero when you’re looking at a string. So let’s say I had my name[0] that is going to give me J because J is in the first position inside that string, which we call position zero. Now, if I used my name [4].
What’s that going to give me? It’s going to give me N because I’m going to count a up 0 1 2 3 4. And that gets me J A S O N. N is the fifth letter, which is in the fourth position inside this string because computers start counting at zero. Let’s say I had something like my name[0:4] What is that going to give me? It’s going to give me J A S O, notice I’m not going to get the N here. Now why is that? Well it’s because I started at position zero, which is J and then I’m going to continue until I reach position four, which is the end. But I’m not going to display position four. I’m going to stop when I get there. So I get J A S O and I stop before printing out the end. Next, let’s say I had my name[6:8] What’s that going to give you? Well, if we look at our string of Jason space Dion, the sixth position is the D so we’re going to have position six and position seven, but we’re going to stop when we get to position eight. So we’re going to have D I and display that to the screen. What do you think is going to happen, if I use a negative number, let’s say I did something like my name[-2]. What will I get there? Well, I’m actually going to get the O from Dion.
Now why is that? Well, because a negative number says, start reading from the end of the string. So when we start reading from the end of the string with position -1 being the last character or the N in my last name. Position -2 is going to be the O in my last name. And we can continue going from right to left. 1, – 2, -3,-4 all the way through until we get to the front of the string. So if I use something like my name[6:-2] What’s that going to give me?
Well, it’s going to start at position six, which was the D it’s going to go to position seven, which is the I, and then it’s going to stop when it gets to the position eight, which is also a collet to the negative two position. If we counted from right to left, instead of left to right. So we get the same thing with 6:8, as we do with 6:-2. Now let’s do one more. What’s going to happen if I do something like my name[-4:-2] Well, this is going to give me the same thing. I’m going to get DI again. Because the -4 position is four positions from the left. So counting from the left, I go N O I D. So I’m going to start with the D and then I’m going to go to the right until I get to position -2 and I’m going to stop before I print out position -2. So really I’m printing -4 and -3 or DI in the case of this string. I know that’s a lot of information.
We talk about strings and how you can read different positions from them. But this is something that you’re going to do a lot as a penetration tester. When you’re trying to get certain pieces of information inside of a string. For example, let’s say you had an IP address of 192. 168.100.100 and you wanted to figure out what the third Okta was. Well, you could do that by reading the string IP bracket, and then use these number positions to be able to pull out just the three digits that make up the third Okta of that IP. And you can then compare that in your script with other things. Next, let’s talk about input and output from your keyboard and monitor. Now in Python, you can get input by using the keyword input.
To do this you’re going to use something like a variable name = input() and then some kind of a prop tier user. For example, userName= input,(“please enter your name:”) Now, when you go and ask for input, you’re saying, please enter your name. When they do that, that’s going to be stored in the username variable. Next, if we had something like print(“Hello” + userName+ “!”) This would then show on your screen as please enter your name: When I entered in my name of Jason is then going to return. Hello, Jason! That way we can combine the little strings that I added inside of the quote marks with the variable name that you just filled in. When you entered your name. Next, let’s talk about files. Inside a Python you can obviously write and read to files. To write to a file. You first need to open that file and assign it to a variable.
So you’ll use something like tempFile= open(‘test.txt’,’w’) Now notice that w there, that means I’m going to set it to a writing condition, write means I’m going to overwrite any existing content in that file. If I wanted to append to the file, instead of overwriting the file, I can change that W to an a and so tempFile= open(‘test.txt’ ,’a’) And that A stands for append. Now you could also read or write from network drives if you want to. Now, if you want to read from a file, you’re simply going to change that W or that A into an R for read. And this is how you can interact with files. You’ll either set it to write mode, read mode or append mode.
Now, if you want to display the file contest to the screen, you can do that using the print command. You’re going to use print() the variable you assigned in my case, tempFile.read () parenthesis. Notice we add this .read to the end of the file, because we want to read from that variable. Now, if I wanted to print maybe just the first 50 characters in a file, I could do that as well. Notice I had that parentheses in the read. That’s where I’m going to put a number. So if I do print(tempFile.read(50)) That is going to say, read the first 50 characters in that file. The other thing you can do instead of reading characters would be to read lines.
And I like to actually read lines from the end of a file, especially if I’m reading some kind of a log file, because I want to see the most recent activity. So let’s say I wanted to read the last five lines of a given log file. I can do that in my script by using print(tempFile.read line(-5)) This will read the last five lines or the most five current lines from that log file that are written at the end of that file and display them to the screen. All right, I know that was a lot of information about Python and I spent a lot of time, especially talking about how to manipulate strings. The reason is those are important concepts for you to know as a penetration tester, and it may be something you see, come up on the exam.
So I want you to feel comfortable with it. If this is your first time dealing with Python and dealing with the way of calling out different pieces of a string, I do recommend that you open up your curly Linux machine because in it, there is a default Python interpreter already built in. Just go into your terminal and type Python at the command prompt. Once you do that, you’ll be in the interactive environment and you can actually set something like my name equals and put your name and start playing with different character combinations and position values.
So you can start trying to guess, are you going to get the right answer? When you say, what is position five? What is position five through seven? And then you can type that command in Python, and it will give you the answer. And so you can create as many of these challenges as you want for yourself and be able to make sure you’re very comfortable with counting out the positions for a given string and identifying exactly where those characters are.