Assignment No 5 Vulnerability Management
Assignment No 5 Vulnerability Management
1. Concept
Vulnerabilities are flaws (or bugs) in operating systems, software applications, or networking protocols that can be exploited by adversaries to obtain access or elevate access privilege of the computer systems. Vulnerability management is a security practice by which organizations set up procedures to discover vulnerabilities in its systems/applications and to fix the vulnerabilities proactively before being exploited Patches are additional program added to the original software or systems to address the vulnerabilities. Like bandages, patches fix vulnerabilities or reduce the impact of the vulnerabilities once exploited. Applying patches timely is critical since attackers these days usually exploit vulnerabilities soon after they are discovered. Hence, automatic vulnerability scanning and a standard patch management process enable the risk of vulnerable systems/applications.
2. Vulnerability Discovery
Vulnerability scanning is a process of searching for known vulnerabilities in software or computer systems. The scanning can be focused on different aspects of a computer system. For example, port scanning searches for opening ports on a computer system and web application scanning searches for vulnerabilities in web applications/services. Vulnerability scanner such as Nessus looks for vulnerabilities in a computer system against a list of known vulnerabilities, which are mostly well documented in public accessible vulnerability databases. Web vulnerability scanning focuses on discovering vulnerabilities in a web application/service. Instead of checking for a broad range of software or system vulnerabilities, web vulnerability scanners, different from general vulnerability scanners, check for vulnerabilities embedded in a web application such as testing invalid form inputs or exploiting cookies. Both OWASP and WASC web sites maintain a list of web vulnerability scanners. The Web Application Vulnerability Scanner Evaluation Project (WAVSEP) develops a set of test cases for benchmarking web vulnerability scanners.
3. Vulnerability Database
Vulnerability databases provide a list of known vulnerabilities and their description in details. These databases include vendor owned databases, such as X-Force database and VUPEN security advisories, community databases, such as the Exploit Database, Open Source Vulnerability Database (OSVDB) and Bugtraq.
All vulnerabilities represented using SCAP. Web application vulnerabilities as other software vulnerabilities are also included in general purpose vulnerability database such as NVD or OSVDB. OWASP maintains a list of top ten web application vulnerabilities to highlight the prevalence of vulnerabilities in web applications from practitioners perspectives.
In our lab exercises, we have examined the SQL injection vulnerabilities in Badstore.net, in which the database queries are handled in Perl. To sanitize inputs, Perl uses two methods to handle dynamic SQL queries: quote() and bind_param() The purpose of quote() method is to put a pair of quotation marks on the input variables so that any special character in the inputs will be treated as inputs, not part of the SQL metacharacters. For example, in the SQL command below, a quote() method should be applied on the variable to prevent invalid inputs. SELECT * FROM mysql.customer WHERE name = '$username'; To prepare for database inputs, the Perl scripts need to sanitize the variable $username as below. my $Username = "Lisa OConners"; ### $dbh is the database handle my $quotedUsername= $dbh->quote( $Username ); my $sth = $dbh->prepare( "SELECT * FROM mysql.customer WHERE name = $quotedUsername" ); $sth->execute(); exit;
In Perl, parameters, also called binding or placeholders, are the variables that hold inputs. To sanitize parameters, you will need to use a different method called bind_param().The index number, 1, on the method refers to the first input parameter to be bound. my $sth = $dbh->prepare("SELECT * FROM mysql.customer WHERE name = ?"); $sth->bind_param(1, $username); The same method can also bind multiple parameters as the example below. my $sth = $dbh->prepare("SELECT * FROM mysql.customer WHERE name = ? AND pass = ?"); $sth->bind_param(1, $username); $sth->bind_param(2, $password); Instead of using bind_param(), a developer can also use the execute() method to bind values with parameters. The example above can be coded as my $sth = $dbh->prepare("SELECT * FROM mysql.customer WHERE name = ? AND pass =?"); $sth->execute($username,$password); A dynamic SQL statement with user inputs cannot be injected with crafted SQL codes in Perl parameterized queries. A parameterized query specifies the structure of the query and the placeholders for all user input values. User inputs are interpreted as data instead of a part of the SQL statement code.
is often unrealistic since it may greatly limit the functionality of the web site that the users would like to visit . The best solution for the application developers is to adopt secure coding practice and sanitize all user inputs. One of the most common special characters used to define elements within the markup language is the < character, and is typically used to indicate the beginning of an HTML tag. These tags can either affect the formatting of the page or induce a program that the client browser executes, e.g. the <SCRIPT> tag introduces a JavaScript program. The labs in this module will guide students to explore how cross site scripting is executed as well as how the web site developer can fix the vulnerable codes. Objectives In this assignment, you will learn about How to discover SQL injection vulnerabilities, such as authentication weakness, on a web server, and investigate solutions to fix the vulnerable codes; and Investigate XSS vulnerabilities on a web server and revise the vulnerable codes to prevent XSS.
3. From the menu bar on the top of the VM, select Applications > Accessories > Terminal. This will open up a Linux shell command terminal, execute the command ifconfig
4. You will receive several lines of output. You are going to look for the Ethernet interface (i.g. eth0). Find the inet addr: field and write down the IP address in the space below. inet addr:192.168.73.131 Bcast:192.168.73.255 Mask:255.255.255.0 5. Visit Badstore web site on the virtual machine. Open a browser. Type in the URL below: http://localhost/badstore/ 4.2 SQL Injection Risk Area Discovery 1. There are three potential areas on the BadStore web site that is exploitable using SQL Injection. All these areas are related to HTML forms accepting user inputs. 2. Browse the list and input fields on the left panel of the web site. Try to identify at least two risk areas where a malicious SQL query can be injected. a. My Account b. Supplier Login
3. Directly type the following SQL injection string into the Email Address text field under the Login to Your Account section and leave the password field empty. 'OR 1=1 OR' 4. Look at the text directly above Welcome to BadStore,net! again. Aft er the above SQL injection, your user account should have changed. Which user has BadStore.net authenticated you as? Test User 5. Now, you learned that it is possible to login this website without any password and the SQL injection is possible during registered user login. 6. The next step is to try and login as an administrative user. Since we do not know the backend SQL query for the login/password user entries, we will need to guess the correct syntax to achieve the purpose. 7. Let us type in the following SQL injection string in the email field and see what will happen. admin 'OR '1'='1 8. Again, look at the text directly above Welcome to BadStore,net! After the above SQL injection, which user has BadStore.net authenticated you as? Master System Administrator 9. Take a screen shot of the Badstore home page showing the above user login privilege.
10. What is the security implication that someone could use a SQL injection above to login Badstore? It implies that the security of the textboxes in badstore is weak and are not sanitized or validated and it accepts query that can change the result of an action.
11. Assume that you are now the web master of the Badstore website and you would like to fix the SQL injection flaw discovered above. You will need to investigate the CGI program in Badstore. The BadStore CGI program is written in Perl. The program is placed under /var/www/badstore/cgi-bin/badstore.cgi 12. To edit the CGI program, in the terminal window execute command: sudo gedit /var/www/badstore/cgi-bin/badstore.cgi 13. As discussed in the previous sessions, you will need to format all SQL statement in parameterized queries (also known as prepared statements) so that the input values from the browsers will only be treated as values, not SQL meta-characters. The construction of a SQL statement contains user input is performed in two steps: 14. The Perl code specified the structure of the query, leaving placeholders for each place where user input is expected. The application specified the contents of each placeholder. When the parameterized queries are formatted, the crafted SQL code cannot interfere with the structure of the query specified in the attack above. The query structure has already been defined so that user inputs are always interpreted as data rather than a part of the SQL statement. 15. The next steps will guide you to fix the vulnerable Perl script. Please search for the following original authentication query in a sub procedure called authuser in badstore.cgi: Original Query: ### Prepare and Execute SQL Query to Verify Credentials ### my $sth = $dbh->prepare("SELECT * FROM userdb WHERE email='$email' AND passwd='$passwd'") ; $sth->execute() or die "Couldn't execute SQL statement: " . $sth->errstr; 16. Please modify the authentication query into the following to sanitize the parameters. The modified scripts have been placed on the CGI program as comments (star ting with a # sign) below the original query. You can just uncomment the modified query and comment out the original query. Modified Query: ### Prepare and Execute SQL Query to Verify Credentials ### my $sth = $dbh->prepare("SELECT * FROM userdb WHERE email= ? AND passwd= ?"); $sth->bind_param(1, $email); $sth->bind_param(2, $passwd); $sth->execute() or die "Couldn't execute SQL statement: " . $sth->errstr;
17. Save the CGI file. 18. Close the browser that has Badstore web site to clear up the login information. Open another new browser to visit Badstore. Go to Login/Register page and type the SQL injection string into the email address field as you did in the previous steps. The SQL injection attack should have been prevented after the change in the Perl script. 19. What is the response from the web site after SQL injection?
5. What is the range of item numbers after you performed the SQL injection? 1000 to 1014, and 9999 6. What is the risk for a web application when it shows its SQL query to the users? Showing the SQL query too risky for it will help hackers to create the attack, especially the SQL injection. Therefore web admins/developers are advised not to include an SQL query in the result page if in case that no results are found. 7. What should an application developer do to mitigate the risk mentioned above? The developer should patch that security hole in order to prevent the risk of being attacked by an SQL injection attack. The developer should change its authentication methods. 8. The next steps will guide you to fix the code related to item search. To edit the CGI program, in the terminal window execute command: sudo gedit /var/www/badstore/cgi-bin/badstore.cgi 9. The next steps will guide you to fix the vulnerable Perl script. Please search for the following original authentication query in a sub procedure called search in badstore.cgi: Original Search Query: ### Prepare and Execute SQL Query ### $sql="SELECT itemnum, sdesc, ldesc, price FROM itemdb WHERE '1000' or '1'='1IN (itemnum,sdesc,ldesc)"; my $sth = $dbh->prepare($sql) or die "Couldn't prepare SQL statement: " . $dbh->errstr; $temp=$sth; $sth->execute() or die "Couldn't execute SQL statement: " . $sth->errstr; 10. Please modify the query into the following to sanitize the parameters. The modified scripts have been placed on the CGI program as comments (starting with a # sign) below the original query. You can just uncomment the modified query and comment out the original query. Modified Search Query: ### Prepare and Execute SQL Query ### $sql="SELECT itemnum, sdesc, ldesc, price FROM itemdb WHERE ? IN (itemnum,sdesc,ldesc)"; my $sth = $dbh->prepare($sql) or die "Couldn't prepare SQL statement: " . $dbh->errstr; $temp=$sth; $sth->execute($squery) or die "Couldn't execute SQL statement: " . $sth->errstr;
11. In addition, to avoid revealing the SQL command to the users, you should modify the error control script below in the same sub procedure. The modified scripts have been placed on the CGI program as comments (starting with a # sign) below th e original script. You can just uncomment the modified query and comment out the original query. Original Script: if ($sth->rows == 0) { print h2("No items matched your search criteria: "), $sql, $sth->errstr; Modified Script: if ($sth->rows == 0) { print h2("No items matched your search criteria: Please change your search criteria"); 12. Save the CGI file. 13. Close the browser window that has Badstore web site. Open a new browser to visit Badstore again. 14. Click on Whats New to see the items. In the Quick Item Search window, type the previous SQL injection string into the search field as you did in the previous steps. The SQL injection attack should have been prevented after the change in the Perl script.
15. What is the response from the web site after SQL injection?
16. Explain the differences between the original search query and the modified search query. Also explain how the modification can fix the SQL injection flaw. The original search query inserts the entered query into the prepared SQL query before searching it into the database and if it doesnt found any item it will display the SQL query used while the modified search query sanitizes first the query before it will execute. Also it will not display the SQL query when no results are found.
3. Paste the screen shot that the Badstore site authenticates you as a supplier.
4. To fix the SQL injection flaw related to supplier authentication. Again, you need to edit the CGI program, in the terminal window execute command: sudo gedit /var/www/badstore/cgi-bin/badstore.cgi 5. You will need to modify a sub procedure called supplierportal. Please identify the original scripts that need to be fixed in badstore.cgi and provide modified scripts. Original Query: my $sth = $dbh->prepare("SELECT * FROM userdb WHERE email='$email' AND passwd='$passwd' "); eval { $sth->execute(); 1; } or do { print "Location: /cgi-bin/badstore.cgi?action=supplierlogin\n\n"; };
Modified Query: my $sth = $dbh->prepare("SELECT * FROM userdb WHERE email=? AND passwd=? "); eval { $sth->execute($email, $passwd); 1; } or do { print "Location: /cgi-bin/badstore.cgi?action=supplierlogin\n\n"; }; 6. Close the browser that has Badstore web site to clear up the login information. Open another new browser to visit Badstore. Go to Supplier Login page and type the SQL injection string into the email address field as you did in the previous steps. The SQL injection attack should have been prevented after the change in the Perl script. 7. What is the response from the web site after the SQL injection?
4. Once your guestbook entry is submitted, you will be able to view your comment in the guestbook plus any previous comment that were submitted. There is no limit to the amount of comments that visitors can add to the guestbook.
6. Once the script is submitted to the guestbook, whoever views the guestbook will execute the script within their browser. The script will always execute when the guestbook is viewed by you or others. With malicious payloads, the website moderator will need to remove the malicious comment to restore guestbook functionality. 7. What is the security impact of the XSS attack above? New user that will sign to the guestbook will be having some trouble with the script executing and showing the alert every time the guest books comments area opens. Thus if that script is capable of transferring money from the victim to the hacker, every person that will open will become a victim. 8. Now, assume that you are the web master for Badstore. To remove the malicious Javascript, you will need to clean up the guestbook contents. Since there is no guestbook moderator web interface, you will need to go into the BadStore web file directory, locate the guestbook file and delete all the store contents. 9. In the Linux Places menu, select Home Folder. In the left pane of the File Browser, select File System and navigate to the following directory, /var/www/badstore/data/. 10. In the data directory there will be a file called guestbookdb. This is the file where all the comments are stored. Right click the file and select Open with gedit. 11. Once the file is opened in gedit, you will be able to see all the contents of the guestbook comment section.
12. Delete the malicious content and save the file. 13. In additional to deleting malicious input in the database, Please describe what else can a web master do to prevent such an XSS attack. Patching the vulnerability of the textbox in guestbook and sanitize all inputs and prevent any scripts from executing by adding codes that automatically delete comments with script or by replacing script character by safe characters.
10. Go back to the BadStore.net Sign Our Guestbook page and enter a regular comment such as Another Test Comment to make sure the new code does not delete legitimate comments. 11. Submit the comment and check to see if it is displayed correctly.
12. Enter another entry in the guest book but use the malicious script below instead. <script>alert(XSS)</script> 13. Does the sanitizing code delete the malicious script? What has happened after you enter the malicious script?
14. Let us try a different way of fixing the code. Go back to the badstore.cgi source code page and comment out the code that you just used. 15. You will now substitute the < or > characters with their HTML safe characters. The two sections of code which should be uncommented are: $comments=~ s/</\<\;/isg; $comments=~ s/>/\>\;/isg; Just like before, the comment text value that is assigned from the comment textbox within guestbook to the comments variable is searched for characters < and > and replaces them with their HTML safe characters of $lt and %gt respectively. 16. Uncomment the code that was just mentioned and save the badstore.cgi file. 17. Go back to the BadStore.net Sign Our Guestbook page and enter a regular comment such as This is Another Testing to make sure the new code does not delete legitimate comments.
19. Enter another entry in the guest book but use the malicious script below instead. <script>alert(XSS)</script> 20. Does the sanitizing code delete the malicious script? What has happened after you enter the malicious script?
21. Compare the two different ways of sanitizing the user comments. One way of sanitizing comments is to delete comments with script tags and the other is to replace the script tag with a safe characters that will only just print the comment (including the script tag) rather than executing it. 22. Comment out the code that was uncommented and save the badstore.cgi file. 23. Clean out any guestbook entries as instructed previously. 24. Close down all open windows and shut down the Badstore.net Linux virtual machine. 4.9 Turn Off Virtual Machines 1. After finishing this exercise, you should reset Firefox proxy setting so it stops using the proxy server. Otherwise you would not be able to visit web sites without running the proxy server at port 8088. To do so, Launch your Firefox web browser, and follow its menu item path Edit > Preferences > Advanced > Network Tab > Settings button to reach the Connection Settings window. Check the Use System Proxy Settings checkbox. 2. Close Paros (File > Exit). Close Terminal Windows (type exit under command line.) 3. Click on the power button on the VM and turn it off.