Ct8ozzl TECfDs85f0xAjw Project3av3
Ct8ozzl TECfDs85f0xAjw Project3av3
Ct8ozzl TECfDs85f0xAjw Project3av3
1. Whether the learner learns how to set up web site and demonstrates command injection
on the web server of their instance.
2. Whether the learner finds the valuable information.
3. Whether the learner can patch the web app to remove the command injection
vulnerabilities.
Assignment Topic:
In this exercise, we will use the Linux instance you already set up for Projects 1 and 2 in the
MOOCs on “Design and Analyze Secure Networked Systems” and “Basic Cryptography and
Programming with Crypto API”. If you have not done so because you have taken this MOOC
separately or for other reasons, then please follow the instructions in
http://ciast.uccs.edu/coursera/pub/project1aV3.pdf to set up the Linux instance.
Preparation Step:
First since we are importing a web site with command injection vulnerability into your AWS
Linux Instance, make sure you set your security group by choosing “My IP” as the source so
that only your local machine can reach your instance, not any hacker out there!! I also enter my
workplace IP as source for the HTTP, HTTPS, and SSH ports. Do not proceed without the
firewall is set up properly. Your instance will become easy target once the vulnerable web site is
deployed!
cd to the directory with your private key to your AWS Linux instance
Login to instance with
$ curl -O https://ciast.uccs.edu/coursera/pub/csr591.tbz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) Peer's Certificate issuer is not recognized.
More details here: https://curl.haxx.se/docs/sslcerts.html
curl was not able to find the CA certificate that signs the ciast web server certificate.
By examining the ciast web server certificate on chrome browser, we find it is signed by
The google search find the CA certificate at COMODO support web site
https://support.comodo.com/index.php?/Knowledgebase/Article/View/968/108/intermediate-ca-
2-comodo-rsa-organization-validation-secure-server-ca-sha-2
By saving the related CA certificate pem file, as comodo2_ca.pem, we were able to download
the document securely with curl --cacert comodo2_ca.pem
without any warning message.
We also download the gpg key of this csr591.tbz software and my public key which used to sign
this software package.
Now you can verify this software is indeed signed by me with following commands we learned
from project 1c.
To clear away the warning, you can trust my public key by signing my public key using
Please decide how far you trust this user to correctly verify other users' keys
(by looking at passports, checking fingerprints from different sources, etc.)
Your decision? 4
gpg> quit
Using tar command we recover the directory structure of the web site.
Copy the www directory to where the apache web server documents/cgi scripts reside
To allow the submission of midterm exam answers, the upload of semester project files, and the
phd/master reports, we change the ownership and access rights of three directories in
/var/www/html: midterm, gsc, and studentproj with the following commands
$ cd /var/www/html
$ sudo chown -R apache:apache midterm gsc studentproj
$ sudo chmod -R 755 midterm gsc studentproj
Now we can test the midterm submit web app.
Type http://<your instanceIPAddr>/CS526S2012midterm.php
into your browser.
To set up upload report web app and AWS key retrieve web app, let execute the following
command to allow the credential files to be read by apache.
Now let us explore the web page that allows a project group to retrieve their AWS Access Key
and Secret key based on the login/password credential.
Type http://<yourInstanceIPAddr>/keyaccess.html You will see the follow web page. Hit
“retrieve keys” button.
You will see the related keys are retrieved and display.
Note that the url of the response web page shows the web form is processed by vul.py a python
script in /var/www/cgi-bin directory.
Figure 5. Key Access Web App Response Web page.
Now that we have shown you three working web apps written in different programming
languages (perl, php, python). We will demonstrate they have the same command injection
vulnerabilities.
Command Injection Attacks:
Now you are ready for launching command injection attacks on your instance.
Enter the https://<yourInstanceIPAddr>/keyaccess.html into the url address box of your local
browser.
Enter &ls& right after the csnet in Login: text input box. Hit “retrieve keys”
You should see a list of files displayed as shown below. If you examine the content of
/var/www/cgi-bin on your instance. You will find the same list. This is due to the fact that the
command was executed by vul.py python server side script on its directory.
Capture browser image with the command injection results of &ls& on your instance as your
first project3a deliverable. Make sure to include the url with your instanceIPAddr in your image.
Treasure Hunt:
Now that we can listing files in a victim server. We can hunt for valuable information there.
By examining the files in the above listing. We find there is a passwd file. To view the content
of the passwd file, we can replace “&cat passwd&” with “&ls&”
Capture browser image with the command injection results of &cat password& on your instance
as your second project3a deliverable. Make sure to include the url with your instanceIPAddr in
your image.
/var/www/html/ is the apache web document root directory it hosts a lot of directories.
If we were to leave behind some trajon files, we need a directory where apache web server can
perform read and write. To list directory content with detail access rights. We use
the malicious string to show these files and their access rights.
Here is what was displayed. I highlight one of the directories, midterm, that Apache account
can write to it.
Figure 9. Reveal Apache Writable Directories for hosting Trojan.
Q1. What are the other two directories the apache account can write into them? Can you spot
them? Submit the answer as your 3rd deliverables of project3a.
Given that we know /var/www/html/midterm directory is writable with command injection and
the vul.py is executed in /var/www/cgi-bin/, we come up with the following malicious string.
The echo > command allow us to write a single line php script (trajon file) as a file call sh6.php
../html/midterm is a file system navigation maneuver from /var/www/cgi-bin to
/var/www/html/midterm.
Now enter the following malicious string to the login entry right after csnet
https://<yourInstanceIPAddr>/midterm/
to see if sh6.php is there.
Try
https://<yourInstanceIPAddr>/midterm/sh6.php?cmd=ls
to see if it will display the content of midterm directory.
Try
https://<yourInstanceIPAddr>/midterm/sh6.php?cmd=cat ../php/reg.php
Q2. What is the most valuable info after your examine the source code of php/reg.php?
Submit the answer as your 4th deliverables of project3a.
Now that we have hacked the vul.py. Let us exploit midterm web app.
sudo vi /var/www/html/hackv2.php
Type http://<yourInstanceIPAddr>/hackv2.php
Visit any text input, hit Enter. That’s it.
sudo ls /var/www/html/gsc
you should see sh6b.php there.
It is a trajon and you can launch any command with
http://<yourInstanceIPAddr>/gsc/sh6b.php?cmd=<any command you like to run there>
Capture the above sh6b.php execution result as image and submit it as your 5th deliverables of
project3a.
Capture the above sh7.php execution result as image and submit it as your 6th deliverables of
project3a.
cp vul.py vul.py.bak
sudo vi vul.py
and then insert the following line to include regex library after line 5:
import re
Then after the line 30 which is “passwd=form['passwd'].value” insert the following five lines:
print htmlHead
m=re.match(r"^[a-zA-Z0-9]{4,8}$", login)
if m is None:
print '<h3>Detect ilegal input for login </h3>'+htmlTail
sys.exit()
Make sure you type tab in front the print and sys.exit() statement
Python is picky about how they identify the if branches.
Capture the above keyaccess.html with &ls& execution result as image and submit it as your 7th
deliverables of project3a.
The vulnerability of midterm.cgi is due to the exploit of hidden tag exam. To fix that, we input
validate $examine right after it reads in the hidden tag content.
Right after line 33 ($examine = $answers{'exam'};) Let add the following eight lines:
if ($login !~ /^[a-zA-Z0-9]{4,8}$/) {
print "login $login format not correct! Potential hacking activity.";
exit(1);
}
if ($examine !~ /^(CS\d{3}|CS\d{4|)([SMFW])(\d{4})(midterm|quiz1|quiz2|final)$/) {
print "examine id $examine not correct! Potential hacking activity.";
exit(1);
}
To complete fix this, we need to substitute system($command) at the end of the script with mail
command
First add sendmail library module right after the 2nd line.
use Mail::Sendmail;
One solution is to detect the file extension .php and not allow it.
Upload sh7.php again and capture browser displaying the response web page indicating “file
type php not allowed”. Submit the image as your 8th deliverables of project3a.