CSE420+Practical+Lab4A+Code+Automation+and+Cross Site+Scripting+ +mozn1079657
CSE420+Practical+Lab4A+Code+Automation+and+Cross Site+Scripting+ +mozn1079657
Objectives
In this part, you will complete the following objectives:
• Task 1.1: Write a Bash Script to Automate an Nmap Scan and Store the Results.
• Task 1.2: Differentiate between Scripts Written in Bash, Python, Ruby, and PowerShell.
Background / Scenario
Penetration testing often requires repetitive tasks that use various tools to perform reconnaissance,
analyze, and exploit vulnerable systems. Creating scripts to automate these tasks reduces the time
necessary to complete the penetration testing project.
Required Resources
• Kali VM customized for the Ethical Hacker course
Instructions
Task 1.1: Bash Script to Automate an Nmap Scan and Store the Results.
Step 1.1.1: Create a basic Bash script.
The Bash shell has a built-in script interpreter. Bash scripts can be written in any text editor and require
minimal programming experience. The scripts can then be run from the Bash shell prompt. The syntax and
structure of Bash scripts is similar to what you would type at the command prompt if you were doing the
task manually. In this step, you will write a short script named recon.sh to perform a simple Nmap scan.
b. To begin your first script, log in to Kali, with the username kali and password kali. Open a terminal
window and ping the target host at 10.6.6.23 to ensure that it is available on the network.
┌──(kali㉿Kali)-[~]
└─$ ping -c5 10.6.6.23
In the following screenshot above we are pinging the target host which is (10.6.6.23)
d. In this script, the user will enter the IP address of the target as a command line option. If no option
is entered, the user will receive an error message showing the proper command syntax. Enter the
if/then sequence as shown.
2 # Check if IP of target is entered
3 if [ -z "$1" ]
4 then
5 echo "Correct usage is ./recon.sh <IP>"
6 exit 1
7 else
8 echo "Target IP $1"
9 fi
Here we opened the mousepad command which gave us an empty text editor
┌──(kali㉿Kali)-[~]
└─$ chmod +x recon.sh
┌──(kali㉿Kali)-[~]
└─$ ./recon.sh 10.6.6.23
Target IP 10.6.6.23
For this screenshot in order to test the script further we added the above
command using the file name we saved.
For the second test we added a test in the command after we wrote the file
name saved from the previous task, we insured that the text is surrounded on
quotation.
The purpose of the script is to automate Nmap scanning using the target IP address value that is
supplied to the script. What do you think will happen if the value is not a legal IP address?
Answer below and include screenshots.
If the IP address is not legal then most likely the nmap will return an 1
error and terminate the process. Furthermore no action will be taken. In
order to avoid such encounter, the IP address should follow a valid
formatting, and should be valid and legal. [1]
i. Now edit the script file to enter the commands that will run the Nmap scan. Use the variable $1 to
indicate the IP address of the target device you want to scan. The results of the Nmap scan will be
written to a file named scan_results.txt in the current directory.
8 echo "Running Nmap…"
9 # Run Nmap scan on target and save results to file
10 nmap -sV $1 > scan_results.txt
10 echo "Scan complete – results written to scan_results.txt"
The nmap scan that will run this script is the version detection scan “- 2
sV”, in which it will determine the information of the version of the open
ports as well as their service. [2]
By the ports, given that it usually runs on ports 139/tcp and 445/tcp as
4
shown in the below screenshot, as well ass when information is displayed
you can see from the version.
a. Open the recon.sh file in the text editor. Add the following commands.
13 # If the Samba port 445 is found and open, run enum4linux.
14 if grep 445 scan_results.txt | grep -iq open
15 then
16 enum4linux -U -S $1 >> scan_results.txt
17 echo "Samba found. Enumeration complete."
18 echo "Results added to scan_results.txt."
19 echo "To view the results, cat the file."
20 else
The following screenshot shows the extra added code based on the code provided above
to the recon.sh file.
b. Analyze the additional code.
• Line 13 is a comment.
• Line 14 indicates the start of an if/then statement that will search in the Nmap results for open
port 445. The grep command searches lines in the file that match the pattern “445 open.” The
grep command searches for lines that match the pattern “445” first. Then the output is piped
into a second grep command to search again for lines that match the pattern open. With the
option -i, the grep command ignores the case distinctions in the search patterns. The option -
q suppresses standard outputs.
• Line 15 is the "then" clause. This contains the command that will be executed should the if test
return "true".
• Lines 16 - 19 are executed if the SMB file sharing port (445) is found. Line 16 runs enum4linux
with the -U and -S options on the target host specified in $1 and appends the results to the end
of the scan_results.txt file. Lines 17, 18 and 19 display messages when emun4linux finished
the scan and provides directions to view the results.
• Line 20 indicates that the action to take if the logical if the condition fails.
• Line 21 displays a message if the SMB file sharing port (445) is not open.
• Line 22 the fi signifies the end of the if/then clause.
5
CSE420-Practical Lab4A: Code Automation and XSS 11
-U which is user enumeration tries to enumerate the users accounts on
the target system while -S “share enumeration” tries to enumerate shared
resources on the target. [3]
c. Save the recon.sh file in the text editor and exit to the command prompt. Below is the complete
recon.sh script.
#!/bin/bash
# Check if IP of target is entered
if [ -z "$1" ]
then
echo "Correct usage is ./recon.sh "
exit
else
echo "Target IP $1"
echo "Running Nmap…"
# Run Nmap scan on target and save results to file
nmap -sV $1 > scan_results.txt
echo "Scan complete – results written to scan_results.txt"
fi
# If the Samba port 445 is found and open, run enum4linux.
if grep 445 scan_results.txt | grep -iq open
then
enum4linux -U -S $1 >> scan_results.txt
echo "Samba found. Enumeration complete."
echo "Results added to scan_results.txt."
echo "To view the results, cat the file."
else
echo "Open SMB share ports not found."
Fi
┌──(kali㉿kali)-[~]
└─$ ./recon.sh 10.6.6.23
Running Nmap....
Scan complete -- results written to scan_results.txt
Samba found. Enumeration complete.
Results added to scan_results.txt.
To view the results, cat the file.
e. Use the cat command to view the results contained in the scan_results.txt file.
6
Answer below and include screenshots.
┌──(kali㉿Kali)-[~]
└─$ nmap -sn -iL to_scan.txt
We used the above given command to check the ip addresses that are up.
Variables read in as options from CLI Cannot begin with number or capital
are assigned $1, $2, ..., $n letter.
Calling Variables variableName $variableName #variableName $varibleName
In the provided screenshot we have created a new mousepad and added the given code above.
Using the information from the chart, what shebang should be the first line of code?
Answer below.
It should be → #!/usr/bin/python3 7
What port range will be scanned?
Answer below and include screenshots.
Based on the code it will begin at 21 and end at 80, so the range would
be 21-80. 8
Using the information from the chart, what scripting language interpreter will be used to run this
code?
Answer below and include screenshots.
The targets are the devices in the IP range of 10.6.6.0 – 10.6.6.255 10
c. Review the code sample shown. Use the syntax characteristics to determine which scripting
language is used to interpret the code.
1 $nmapExe = "Program Files (x86)Nmap
map.exe"
2 #define nmap targets
3 $target = "10.6.6.0/24", "172.17.0.0/29"
4 #run nmap scan for each target
5 foreach ($target in $target)
6 {
7 $filename = "nmap_results"
8 $nmapfile = ". emp" + $filename + $target +".xml"
9 cmd.exe /c "$nmapExe -p 20-25,80,443,3389,8080 -oX $nmapfile -A -v $target"
10 }
Using the information from the chart, what scripting language interpreter will be used to run this
code?
Answer below and include screenshots.
What options will Nmap use for the scan and what do those options mean?
Answer below and include screenshots.
Objectives
In this part, you will perform Reflected XSS and Stored XSS attacks against the DVWA (Damn Vulnerable
Web Application!) at low, medium, and high security levels.
• Task 2.1: Perform Reflective Cross Site Scripting Exploits
• Task 2.2: Perform Stored Cross Site Scripting Exploits
Required Resources
• Kali VM customized for the Ethical Hacker course
• Internet access
Instructions
In Kali Linux, the default web browser is usually Iceweasel, which is based on Firefox. To change
the title of a tab in Iceweasel or any other browser, you would typically need to use JavaScript. The
below simple script can do that:
You can run this script in the browser's developer console by doing the following:
1. Open your browser and navigate to the webpage whose tab title you want to change.
2. Right-click on the webpage and select "Inspect" or press `Ctrl + Shift + I` (or `Cmd + Option + I`
on Mac) to open the developer tools.
3. Go to the "Console" tab within the developer tools.
4. Paste the JavaScript code mentioned above into the console and press Enter.
My Screenshots:
c. Navigate to the DVWA application and enter the following URL into the browser http://10.6.6.13.
d. At the login prompt, enter the credentials: admin/password.
d. Enter CTRL+U on the keyboard to view the source code of the page.
e. Search for the string Hello Reflected_Test by entering CTRL+F to open a search box.
The presence of the string in the page source HTML indicates that values entered in a user response
text field are inserted into the source code for the page. This indicates to an attacker that the page
may be vulnerable to reflected XSS attacks.
An alert popup box will appear with the words You are hacked!. This means the site is vulnerable
to Reflected XSS attacks and we have successfully exploited the vulnerability.
h. Select and copy the URL for the compromised page. Open a new browser tab and paste the URL
into the URL field and press <Enter>.
You should see the same web page appear displaying the You are hacked! popup box. This means
that if a user opens the URL a malicious script will execute. The alert box is used to simulate a
malicious script in this lab.
c. Again, enter the following payload in the What's your name? box and click Submit.
<script>alert("You are hacked!")</script>
You will see a Hello response, but this time no pop up will appear. This indicates that the script did
not execute. Note that the script is displayed as literal text.
We can analyze the code in the backend of the web site to investigate the reason.
d. Click the View Source button on the bottom right of the page and review the PHP code.
Note: On a real web server, we would not have access to this backend source code, but here on
DVWS we do.
This source code creates a filter, with str_replace() function, that removes the <script> tag in our
payload and replaces it with a null value. This renders the payload script ineffective, so the attack
failed, and no popup window is displayed. Because this script is only filtering out <script> in lower
case, we can try and get around the filter by using a different tag in the payload. We will use
<ScRipt>.
Yes it did appear, which indicates that the sensitive case filters “if there 14
is any” haven’t done their job.
There is a Hello message and no alert pop up box. Again, we can analyze the backend source code
to investigate.
d. Click the View Source button and review the PHP code.
In this code, the developer used a regular expression to replace any form of the <script> tag, no
matter what case of the characters is used, with a null value.
Which character in the script was omitted from the regular expression? How do you know?
Answer below.
The character omitted from the regular expression was “e” which is in
15
“script”, you are able to tell because there is no placeholder for “e” in the
regular expression.
e. To bypass this filter, we must use another HTML tag instead of <script> to attack the site.
Close the source code window and return to the Reflected XSS Vulnerability page.
f. Enter the following payload in the What's your name? box and click Submit. (Note the use of
underscores to replace spaces.)
Review the text that you input into the web form. How did it work?
Answer below.
Because we didn’t use <script> which is what the backend filter was
16
looking for in order to remove it.
Both strings, Test#1 and Stored XSS Test, will be in the page source code indicating that the two
input fields may be vulnerable to a Stored XSS attack.
e. Close the source code window and return to the Stored XSS Vulnerability page.
f. Enter Test#1 in the Name * box and enter the following payload in the Message * field and click
Sign Guestbook.
<script>alert("You are hacked!")</script>
g. Refresh the page. If alerted, click Resend to display the page. The XSS alert popup box will appear
again.
Because the XSS payload is stored in the guestbook, the alert popup box will appear each time the
page is refreshed.
h. Before proceeding to the next step, clear the XSS payload from the page. Click Setup / Reset DB
in the left menu then click Create / Reset Database.
d. Enter CTRL+U on the keyboard to view the page source code. Enter CTRL+F to search for the
Test#1 and Stored XSS Test strings.
Both strings will be in the page source code indicating that the two input fields may be vulnerable to
a Stored XSS attack.
No popup box should appear. Refreshing the page should not cause the alert popup box to appear
either.
This means that there is code in the backend that is sanitizing the user input from the Message *
field to prevent scripts from being submitted. You can see the modified input in the last rectangle
message box below the input fields.
It modified the input by removing raw HTML and JavaScript tags and it 18
converted the special characters to HTML, and neutralized the payload.
In this case the <script> tag was removed when the filter modified the
input.
g. Click the View Source button and review the PHP source code and investigate.
You will see two blocks of code with the word Sanitize. The first block of code, under // Sanitize
message input, contains two PHP functions for performing input sanitization. The strip_tags()
function removes all html tags from the message field before storing them in the database. The
htmlspecialchars() function converts all special characters into equivalent HTML entities so they
are not reflected back in the browser.
The second block of code, under // Sanitize name input, performs input sanitation on the Name *
field. It contains the str_replace() function which replaces any occurrence of the <script> tag with
a null value. This disables the script completely.
We can attempt to bypass the security on the Name * field by using some other payload that does
not contain <script> tags.
Note: Changing the maxlength parameter does not persist. If you refresh the page, for example,
the setting needs to be changed again.
j. Return to the Vulnerability page and enter the following payload in the Name * field.
<ScRipt>alert("You are hacked!")</ScRipt>
k. In the Message * field you can type any text you like and then click Sign Guestbook.
An XSS alert popup box will appear with the words You are hacked!.
Because the XSS payload is stored in the guestbook, the alert popup box will appear each time the
page is refreshed or each time other users visit the page.
The popup confirms you have successfully exploited Stored XSS vulnerability at the Medium level
of security.
l. Before proceeding to the next step, clear the XSS payload from the page. Click Setup / Reset DB
in the left menu then click Create / Reset Database.
c. Type the string Test#1 in the Name* field and type Stored XSS Test in the Message * field. Click
Sign Guestbook.
d. Enter CTRL+U on the keyboard to view the page source code. Enter CTRL+F to search for the
Test#1 and XSS Test strings.
Both Test#1 and XSS Test should be in the page source code indicating that the two input fields
may be vulnerable to a Stored XSS attack.
No popup box will appear. Refreshing the page will not cause the alert popup box to appear either.
This means that there is code in the site backend that is sanitizing the user input from the Message
* field.
You will see two blocks of code. As before with the Medium security, the first block of code, under //
Sanitize message input, contains two php functions for performing input sanitization. The
strip_tags() function removes all html tags from the message field before storing them in the
database. The htmlspecialchars() function converts all special characters into equivalent HTML
characters so they are not reflected back in the browser.
The second block of code, under // Sanitize name input, is performing input sanitation on the Name
* field. It contains the preg_replace() function. This function uses a regular expression to replace
any occurrence of the <script> tag, regardless of character case, with a null value.
We can attempt to bypass the security on the Name * field by using some other payload that does
not contain <script> tags.
h. Before entering any payload into the Name * field, it will be necessary to change the max character
length restriction on the field, as was done above.
With the maxlength restriction changed, the XSS payload can now be entered into the Name * field.
<svg onload=alert("You_are_hacked!")>
i. In the Message * field, you can type any text you like and then click Sign Guestbook.
An XSS alert popup box will appear with the message "You_are_hacked!".
Because the XSS payload is stored in the guestbook, the alert popup box will appear each time the
page is refreshed.
The popup confirms you have successfully exploited a Stored XSS vulnerability at High security
level.
j. Before proceeding to the next step, clear the XSS payload from the page. Click Setup / Reset DB
in the left menu then click Create / Reset Database.
The H4cker website should now be displayed under the iframe test message.
This is a powerful exploit because the threat actor could send the browser to a malicious website.
d. Before proceeding to the next step, clear the XSS payload from the page. Click Setup / Reset DB
in the left menu then click Create / Reset Database.
22
All screenshots are found under each task.
Stealing the cookies of website visitors has security implications. Cookies contain information about how
and when users visit a web site and sometimes authentication information, such as usernames and
passwords. Without proper security measures, a threat actor can capture cookies and use them to
impersonate specific users and gain access to their information and accounts.
a. Select DVWA Security in the left menu and select Low in the Security Level options dropdown.
Click Submit.
<script>alert(document.cookie)</script>
A popup box with the cookie will be presented. This is a cookie that PHP uses to keep of track of
running sessions.
An exploit could modify the XSS script to have the cookie sent to another destination rather than just
displaying it.
1- First is Low
2- Second is Medium
23
All Screenshots are found under each task
References
[1] "Options Summary | NMAP Network Scanning," [Online]. Available:
https://nmap.org/book/man-briefoptions.html.
[2] "Service and version detection | NMAP network scanning," [Online]. Available:
https://nmap.org/book/man-version-detection.html.
[3] C. "GitHub - CiscoCXSecurity/Enum4Linux: Enum4Linux is a Linux alternative to
Enum.exe for enumerating data from Windows and Samba hosts," GitHub, [Online].
Available: https://github.com/CiscoCXSecurity/enum4linux.
[4] P. "GitHub - postmodern/Ruby-NMap: a Ruby interface to NMAP, the exploration tool and
Security / port Scanner. allows automating NMAP and parsing NMAP XML files.," GitHub,
[Online]. Available: https://github.com/postmodern/ruby-nmap.
[5] "PHP: Hypertext Preprocessor," [Online]. Available:
https://www.php.net/manual/en/function.addslashes.php.
Step Max
Part 1- Code Automation 1
Part 2- Cross-Site Scripting 1.5
Total marks: 2.5 marks