Coursera Assignment Steps
Coursera Assignment Steps
Objective :
1. You will learn how to hack web apps with command injection vulnerabilities in a web site
of your AWS Linux instance.
2. You will learn how to search valuable information on a typical Linux systems with LAMP
services, and deposit and hide Trojans for future exploitation.
3. You will learn how to patch these web apps with input validation using regular
expression.
4. You will learn a security design pattern to avoid introducing injection vulnerabilities by
input validation and replacing generic system calls with specific function calls.
5. You will learn how to hack web apps with SQL injection vulnerabilities and retrieve user
profile information and passwords.
6. You will learn how to patch them with input validation and SQL parameter binding.
7. You will learn the hacking methodology, Nessus tool for scanning vulnerabilities, Kali
Linux for penetration testing, and Metasploit Framework for gaining access to vulnerable
Windows Systems, deploying keylogger, and perform Remote VNC server injection.
8. You will learn security in memory systems and virtual memory layout, and understand
buffer overflow attacks and their defenses.
9. You will learn how to clone a Kali instance with AWS P2 GPU support and perform
hashcat password cracking using dictionary attacks and known pattern mask attacks.
Ex 1:
How to hack web apps with command injection
vulnerabilities in a web site of your AWS Linux
instance
Command injections - A type of cyber attacks called injection where operating system
commands are injected by hackers as part of the inputs and executed by a program and
typically in a Web server.
The command injection occurs when the program does not perform proper input
validations. That's a very important technique. General kind of technique, also defense.
The input from the users are used directly or, very importantly, indirectly most of the time
as parameters to the layer system command functions.
For example, exec function code or system function code by the program to send
confirmation maybe e-mail or notification e-mail or create a directory to
host user submitted data in an upload kind of case.
It's listed as the number one vulnerability or application security threat at OWASP's (Open
Web Application Security Project) site was listed as a Top 10 Vulnerabilities in 2012 still in
2017. OWASP is Open Web Applications Security Projects. It is a wonderful
organization. And at creating a thriving global community that drives the visibility and
evolution in safety and security of the world's server.
53. ls
Once we unzip,
Once we copy the www folder to root directoy, we could access the front page
sudo cp -r www /var/
54. check the content of www folder
cd www
ls
We can access all the php and html pages (as all the html and php files are inside the folder
html which is inside the www folder) as unzipped the folder.
Ex:
http://13.126.44.109//CS526S2012midterm.html
http://13.126.44.109//CS526S2012midterm.php
http://13.126.44.109//info.php
http://13.126.44.109//phpinfo.php
http://13.126.44.109//regCorrect.php
http://13.126.44.109//register.php
Now I want to do a new registration with student
Name: kalai, student UFP
login: selvi, Confirm student UFP login:kalai, student SID without
dash:666777888,
Registration failed
Try to upload a file sh7.php in http://13.126.44.109//upload.php
Try to
http://13.126.44.109//CS526S2012midterm.php
chow,chow,#a88,
scroll down and Answer all the questions then click “submit”
Chow can submit the form. Now check the submitted form or details whether stored in
CS526/CS5262012midterm directory
The url of the response web page shows the web form input is processed by midterm.cgi, a
perl server side script in cgi-bin directory.
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.
For that it is required to copy the folder www to root folder.
cd
cd proj
Once the www folder copied into the root folder /var)
Get into the html folder.
cd /var/www/html
ls
change mode – ( 7 means user has the read, write,execute permission, 5 means group has
read, execute permission, 5 means others have read, execute permission)
sudo chmod -R 755 midterm gsc studentproj
ls -l
Now we could see the user name apache against midterm, gsc, studentproj
The url of the response web page shows the web form input is processed by midterm.cgi, a
perl server side script in /var/www/cgi-bin directory.
to change the mode to get rights to all the txt files of folder “data”
type –
to change the mode to get rights to all the csv files of folder “data”
59. Type
sudo chmod 755 /var/www/data/*.csv
(By using the url we are visiting the different pages for example upload page of the same
website)
61. Save a file with name “hai” as image file in desktop and select that file and click send file
As the login is correct it allows to upload a file. By using the url we are visiting different
pages of same website.
The url of the response web page shows the web form input is processed by upload.php
script in /var/www/html directory.
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:///keyaccess.html You
will see the follow web page. Hit “retrieve keys” button
From this we can say all three web apps midterm.php, upload.php, keyaccess.php are working
The /reg web page take in the full name are shown in the web page below.
Full name, email, password and then come the [INAUDIBLE] value of the membership
applicant.
When the user fills in this information and clicks on the register button the web form data
will be submitted to the web server for processing.
The data will be saved in the database and email will also be sent to the user's email.
And then they know that the registration is approved and the provided link they can
access to the member specific variable informations, variable webpage.
The system admin will then look at the application and then try to approve the
membership. This particular set up for this particular web browser, web app.
Here we show the hacker can actually attach a malicious unix command as showed as
ampersand sine os ampersand sine just four capture. Right after the typical email entry.
The register button is then clicked. So this particular demo you can actually tell your
friend the smallest character. Example of Command Injection is four characters.
csnet&ls&
Click “retrieve keys”
If we 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.
&ls& shows the directory content of /var/www/cgi-bin
So we could get the directory content by using the malicious strong &ls&. To verify this we can get
into the cgi-bin directory in putty.
Treasure Hunt
1. &cat passwd&
To view the content of the passwd file
Now we could see the password of all the users. Ex: jupchurc:#a38
Now I could login as jupchurc (after identifying the passwords) in the page http:// 3.110.54.185
/CS526S2012midterm.php
/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 the apache web server can perform
read and write.
1. If we want to know the access rights of all the users. We can use the OS command &ls –al
../html& in the webpage http://3.108.41.212//keyaccess.html
highlighted one of the directories, midterm, that Apache accounts can write to it
&cat vulpy.txt&
HACKING MIDTERM WEB APP:
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 inorder to add the file
sh6.php in the http://ip/midterm
In putty
You must be in html folder. Otherwise again run the command
cd /var/www/html/
then
sudo vi /var/www/html/hackv2.php
Hacking Upload Web App. In your local machine, type “echo '' > sh7.php” to create sh7.php file.
Note that use single quote characters to wrap the one line php file. Try to type it, not copy and
paste, because the word process may use different characters.
http://3.108.41.212//upload.php
choose sh7.php
http://13.127.137.113/gsc/master/jgray/doc/sh7.php?cmd=ls
--END--
Now let's see what the HTTP response coming back. We see a long list of the files, right in
the middle there. Staring with counters PHP action, dot PHP and so on.
And they are returned, followed by the original greeting and status messages.
It turns out, all this middle part is a file in the directory /var/www/html/php.
Which is exactly the same location of the server side script, that are processing the
request. It is located inside the web server location,
see in this case CS 591 x [email protected]. We also see the ampersand LS, ampersand sign is
included in the mail command which are echoed.
Normally you shouldn't print out your mail whatever the command in
your server size script, but here is for the demonstration purpose.
It turns out by adding this right after the AOS.n
We can display the content
in /var/html.
Basically, what we are saying is you can actually change the OS
command by other reprising commands as that would be executed.
And the IOS dot dot means go to the upper directory
of /1/wxtmlpsp which is this one.
Basically, we can now navigate through the file system
in the victim's web service site as long as we have the privilege.
And in our case, the server side script regit.php is run by the account Apache,
so long as Apache can read and execute the file
you can actually display and navigate through that.
The directories of Apache doesn't have the execute or read-write.
Then we will not get any result coming back.
We cannot display those with this attempt.
But still, any of the web directory, since Apache needs to be able to read it.
Therefore it will be able to display the content.
How about if you like to display the action.php? Well, very simple.
Just do a cat, space Action.php and prefix with ampersand sign and then post script with
ampersand sign. Submit it through the webpage. Now you are going to see the content of
that action.php. Normally the php service, we don't want to reveal its content only the
execution result html but you are going to see they are very variable in measures coming
back. I will let you try it and maybe discuss with your classmates. In this MOOC, what other
critical credential information was revealed inside the action.php? Here we show it by
replacing &ls& with &cat ../../cgi-bin/passwd&. Thus doing, when we enter and submit, we
were able to display the content of the password file right in the middle there. You can see
a typical password file format, the login name, followed by the colon which is a separator,
followed by the password string. In this case, unfortunately encrypt in using the parent
text pound sign something. Okay, so is very revealing using CAT, you can actually see the
content, and then capture a lot of information in the Web server side.
So we just demonstrate to you we can run any OS command in the victim site and
navigate up and down in the file system.
Discussion on
Unlike command injection, SQL injection focus on attacking the SQL server,
and therefore it cannot execute arbitrary OS command.
Personally I feel it's less dangerous, but then critical information could be revealed.
MySQL server, which is to be run on Linux, maintain its own password file in MySQL
database, by this specific database called MySQL, inside its user table, a table with
the name USER.
They are different from the password maintained by the operating system in
/etc/shadow, typically /etc/shadow in Linux system.
However, for convenience or for [LAUGH] laziness reason, if the system
administrator is too lazy, often the login and password are exactly the same created.
The hacker can then log in.
If they break into a database, retrieve this credential information, they can
then login through the victim's system. This is called escalating of the privilege.
You steal credentials from one area of the services, you can authorize in the other
service. Escalating privilege is one of the important hacking methodology steps.
Here is a demo of SQL injection attack using a typical Membership Profile Request
web app.
First, let us show the normal operation.
sudo yum update
Download the website http://ciast.uccs.edu/coursera/pub/csr591.tbz
wget http://ciast.uccs.edu/coursera/pub/csr591.tbz
nmap localhost
nmap localhost
Setup mysql database account and database tables for the web apps with
vulnerabilities.
First switch the directory to /var/www/data
cd /var/www/data
Run the following commands to set up the related databases and tables
for the SQL Injection exercises.
sudo mysql/createacc.pl cs00net chow "#Uc2013lions$"
sudo mysql -u root -pcs00net chowdb < admin.sql
sudo mysql -u root -pcs00net chowdb < member1.sql
sudo mysql -u root -pcs00net chowdb < cs591.sql
http://3.110.86.19/showme.php
In the Membership Profile Request web app page, we enter the email address and
the passwords and then we click the Submit button.
If the password check are OK, the status is shown in the activity response web page
coming back.
By just adding a malicious string, single quote, 'or 0=0 or' ,operator,
ending with a single quote.
Now enter ‘ or 0=0 or ‘ in Email and Submit it
If we enter this string right after the email address, we are able to retrieve the profile
information of all the member in this particular database, including the password for
access the website.
Know that missing the two single quote characters or the last or operator in this
malicious string, will not result in the display of those password information.
It's very tricky, but we will explain why is that later on.
The SQL query result will return SQL syntax error at line one, if we're missing those
single quotes or the or operator.
For example, by removing the last or operator, we will have 0= 0 single quote
end up in the SQL query closed statement, where 0 quote is not a legal term in SQL
statement.
Therefore, SQL query processing will reject and print out there's a syntax error.
Let's examine the showme.php code to understand where the vulnerability of the
SQL injection occurs.
First the code, we can spot looking at the pattern, it violates the security design
pattern as we discussed in the previous session. It does not check on the input right
after we read in the input data, as shown in the arrow there.
Furthermore, the input email is used in SQL query.
The query also use star, a wildcard character, to display all the fields in the table,
and that should be changed to make it more specific.
For example, instead of showing the password, we probably should result in a
special request before we show the password. Remember the paradigm to replacing
generic mechanism with specific mechanism, the more specific, the better the
security.
Here we examine how SQL query with malicious string are evaluated.
By turning on the print debugging statements in the showme.php,
we get the following query printout, which shows out we have 'or 0=0 or', called a
malicious string, injected into the SQL query, as shown in the middle there.
The where clause has three expression altogether. And we highlight the malicious
string with red colors. And turn out there are three expressions.
They are connected with or operator. The first operation, first expression,
email='[email protected]'. This is the normal expression, which we see in normal
query.
It would return true as intended, and therefore it only showed one of the row when
we examine all the row.
The second expression, 0=0, is the malicious attack, which as a logic expression will
return always true.
And because anything true or another value is true although, is always returned true,
we simply bypass a previous logic expression. And any of the row in the database
will then be matched and displayed.
The third one is a two single quote character. It doesn't matter in this case, since the
middle logic expression already override that particular way across, to yield the true
expiration for all or. This term, two single quotes, doesn't seem to matter.
Therefore, in this case, all that row information in the member1 table will come back
and display, and all the column will also display, because we use wildcard character
star.
• In this lesson we patch the membership profile retrievable web app which has
the SQL vulnerability with two techniques.
• The first one is Input Validation technique, the other one is Parameterized
Query technique.
Remember the first rule in Security Design Pattern A, we present in previous lesson
for command injection defense. We observed three sub patterns.
1. The first one, always validate input before performing any program specific
operation after we read in the input.
2. Second, we validate the input data using regular expression so to detect the
malicious string.
3. Third, we exit right away once we detect the malicious string or code.
We use a question mark symbol to represent each of the parameter in the query thus
we limit the number of variable input to the query.
Once the query is prepared the hacker can no longer add any additional expressions
or queries statement into the SQL query.
We then use the bind on a scope parameter function to bind the parameter.
We use very specific type use the string number and the actual variable. We can
double check whether the variable contains a value that is exactly the same time.
For example the numerical value we can restrict using I for double value we use the
symbol of D to represent in the first parameter of bind parameter.
S for string and B for blob object. The query processing will detect any for this type
matching with the value to be submitted.
We will use Show me defense pb.psp, which is a modification of showme.psp,
int_slash_y_slash_ww_html your chrome what was mention.
• Line 29 we see the $_email variable is replaced by a question mark in the
query statement we try to formulate.
• Line 34 we see bind_underscore_parameter to bind a string type to the
variable because $_email variable is a string type.
• Line 42 we bind_underscore_the result using this function to return for
variable corresponding to very specific columns which we write to return to form the
response web page.
Hacking methodology
1. Footprinting
• The right column here show the command we use to obtain those related
information in the internet about the organizations.
• We have whois you can type in most of the nicknacks system and it will return
those critical organization information about administrator and the technical
contact information, how many server, name server, are serving that particular
domain.
• And we also see the netscraft as one of the command. Actually one of the website
that provide a query, allow you to type in a domain name, and it track all of the
Web servers they've used for that organization, over the years; including the
operating system and the version. And we were able to find that information very
valuable.
2. Scanning
Here we use and Nmap command or one of the famous tool called nexus.
• Nmap - Network Mapper. It is an open-source Linux command-line tool that is used to
scan IP addresses and ports in a network and to detect installed applications.
Nmap allows network admins to find which devices are running on their network,
discover open ports and services, and detect vulnerabilities.
• It allows us to find out what other machines within that domain or subnet range,
network adjust range, how many machine are active, how many server are active
and what are the ports which represent the specific service that are open.
• The Nexus too can even instructed it to gather and analyze the operating system
and package and identify the vulnerability listed and even tried to launching the
malicious attack.
3. Enumerations
• It is a process of gathering the information about the target machine by actually
actively connect to it.
• hacker try to identify user, system, admin kind of accounts and try to find out what
the active directory, shared directory that may be open.
• And if there's a web server they will try find out what are the web page that is
available for potential injection attacks.
4. Wiretap
• We can wiretap the network or using the legitimate account to analyze the network
traffics and explore the web page to see if they have command or unsecure injection
vulnerability. This is so called low hanging fruit.
5. Escalating privilege
• With the privilege we established in number four, we then- a hacker will then
perform treasure hunting. We do that by exporting variable directory within the
attackers system.
Ex: the passport file, the script, server site script file, which may have embedded
with some credential information, maybe some configuration file. And try to
gather additional credential passport information and we search directory and
see whether they can read or write or even execute. And see whether we can even
modify the access right or its security token if they are using secure index more
for protection. In case the passport file is readable and the passport maybe
encrypted we can read it out and offline. We use password cracking tool such
as, johntheripper showing here to break into the additional account.
6. Pilferting
• Gathering the information on identify the Mackays to allow access to the trops
system. This including examining the configurations of various network
services. And often they include commands and showing how they are configured.
All they actually contains the credential credit card, passport information such as,
server connection information or very typical for intrusion detection system. They
might including database account for access and deposit detecting malicious
information.
7. Covering the track
• System may including logs or intrusion detection system that monitor and knew
your access or modification of critical, file and directory. Such law breakers, the
hacker may attempt to modify, to hide their track if that is possible.
Note that from previous lesson some operating system will prevent this from
happening by only allow a pen right to the lock.
8. Backdoor
• to allow re-entry sometime in the future. The hacker may create scripts to be
excecute by the Cron job, or At job, in a regular interval. And when they are execute
it will examine the remote executable they have deposit and see whether they still
exists.
If they are wiped out by the anti-virus quarantine these At job and Cron job will put it
back. And therefore, that's a reason why, from time to time we see the quarantined virus
reappear. And these also show why a back door attack is very important.
A Trojan kind of attack is very important to detect. Make sure we search.
Once we got attack, make sure we search the start up folder, registry entry and those
At/Cron type and the configuration file to see we have those dangerous Script there.
And sometimes they will hide those file, those script with the DOT prefix widget.
That's called hidden directory or hidden file.
9. Denial of service
• Maybe after the weakened machine has been explored with all the treasure and no
longer useful. Sometime we launching the denial of service attack to shut down the
system. And that can also happen when we can't actually, we cannot
penetrate. And then we use this kind of proof for denial of service attack. And this
can be done from outside or from inside by modifying the configuration of network
routing table and confusing the system interior and resulting in incorrect routing or
database access. And in this case it's very important for the vendor to backup the
file and images. But at the same time the hacker will try to, if they can get access to
this backup up file and images, restore image, they will try to modify and resulting
in failed disaster recovery process. So pick up in multiple places that's important
steps.
What command we can use to display the password file in this directory?
The answer, is cat command, you just say cat. The password and it will show the actual
content of the password file in the cgi-bin directory.
Here we show the actual code of the Python server-side script and discuss its
vulnerabilities. Line 27 through 29 read in the input variable.
And we can spot right away, based on the security design pattern, there's no validation
code for that, right? We need to see some code with if statement and that has contained
regular expression, okay? We didn't find it there.
Line 59 show the formatting of mail command. And here the last variable, we have three
variables there, sharing a library by the ampersand sign.
The last variable is login. And if we look at the corresponding formatting string in the
front, it actually substitutes a -c parameter of the mail command, which is the carbon
copy parameter, which specifies the list of users who were send a copy of the email.
Line 59 shows the os.system function. And this is the way Python script allow you to
execute by providing OS command and has the same effect. It too was too generic.
And it should be asked, according to our security design pattern, any generic kind of
function call should be substituted with more specific function call meet the purpose.
In our case, we should use specific mail command. Since mail command variable contain
the variable or login from the initial user input, this vulnerability is very severe, and often
be classified as such in scanning tool.
Here we show how to patch this Python script.
Between lines 34 and 35, we insert
the regular expression matching functions.
And if the result is not matched, we print out the error message and
using sys.exit() function to exit right away.