Security Vulnerabilities and Secure Coding

Test your knowledge of common security vulnerabilities including XSS, SQL injection, broken access control, command injection, memory security issues, and secure coding practices for web applications and system software.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

In the following code snippet, how should a pointer be deleted int main (int argc, char argv[]) { char j=new char[100]; j=argv[1]; int k=atoi(j); /delete here/ return 0; }

  1. delete j
  2. free j
  3. it is not supposed to be deleted
  4. delete [] j
Question 2 Multiple Choice (Single Answer)

A program wants to do some activities with root privileges when it starts up and shuts down. Other activities it does can be done as a lesser privileged user. Which of the options given below is most secure?

  1. The program should be started with root privileges. Then it should use setuid(UID) to change privileges between root and another account.
  2. The program should be started with root privileges. Then it should use seteuid(UID) to change privileges between root and another account.
  3. Starting the program as root is a security risk. The program should run with least privileges and obtain root using seteuid(UID) whenever necessary.
  4. The program has to run with root privileges entirely. Once root privileges are dropped they cannot be regained.
Question 3 Multiple Choice (Single Answer)

What value is stored in *leaf in the program given below? int *myfunc(int tree) { int *node; int i=rand(); if(0==tree/pow(2,i)) node=&tree; else node=&i; return node; } int main(int argc, char * argv[]) { int *leaf; leaf=myfunc(7); }

  1. value of tree
  2. value of node
  3. value of i
  4. garbage-- its a dangling pointer
Question 4 Multiple Choice (Single Answer)

The following code is part of a system daemon that is run with elevated privileges. It opens a temp file in /tmp directory as a cache. Is there an issue in this code sample? Please assume that filling up /tmp is not an issue here. int outfile = fopen(“/tmp/cache_data”, O_WRONLY | O_CREAT | O_TRUNC, 0600);

  1. Since the file name is hard coded, fopen() will fail if the file already exists
  2. 0600 is not a secure option. The parameter 0600 should be changed to 0666
  3. Attackers can exploit by creating a symboling link /tmp/cache_data that points to a system file
  4. Attackers can exploit the application's cache by writing directly to /tmp/cache_data
Question 5 Multiple Choice (Single Answer)

Is writing to an already freed memory a vulnerability? x = malloc(200); /* do something with x / free (x); / do something else */ strcpy(x, “somedata”);

  1. Overwriting freed memory is a security vulnerability
  2. Depends on the application and how important “somedata” is
  3. This will result in a buffer overflow since the freed memory location cannot handle 8 characters of data “somedata”
  4. strcpy() will fail as it cannot write to already freed memory, and the application will crash
Question 6 Multiple Choice (Single Answer)

What attacks can get realized due to below code? ... Connection con = null; Statement stmt = null; try{ String personName = req.getParameter("PName"); String personAddress = req.getParameter("PAddress"); String personEmail = req.getParameter("PEmail"); String personPhone = req.getParameter("PPhone"); con= UtilDAO.make_con(); stmt = con.createStatement(); String sql = "INSERT INTO PersonDetails values ('"+personName+"', '"+personAddress+"', '"+personEmail+"', '"+personPhone+"')"; stmt.executeUpdate(sql); con.commit(); stmt.close(); UtilDAO.close(con); } catch(Exception e) { log.debug(“Exception is:”+e); } ...

  1. Cross Site Scripting
  2. SQL Injection
  3. Improper Resource Release
  4. Option 1 AND Option 2
  5. Option 1 AND Option 2 AND Option 3
  6. Option 2 AND Option 3
Question 7 Multiple Choice (Single Answer)

Identify the line on which the vulnerability exists: 1 public class performSearchAction extends HttpServlet{ 2 // Servlet for Search Action 3 public void doPost(HttpServletRequest req, HttpServletResponse res) 4 { 5 try 6 { 7 ArrayList arrSearch = Util.performSearchAction(req, res); 8 req.setAttribute(“SearchResults”,arrSearch); 9 RequestDispatcher rd = getServletContext().getRequestDispatcher("/SearchResult.jsp"); 10 rd.forward(req,res); 11 } catch (Exception e) { 12 log.debug(“Exception occurred:”+e); 13 } 14 } //End of doPost method 15 public void doGet(HttpServletRequest req, HttpServletResponse res) 16 { 17 doPost(req,res); 18 } //End of doGet method 19 } //End of Class

  1. Line # 12
  2. Line # 9
  3. Line # 17
  4. Line # 8
  5. Line # 14, 18 & 19
Question 8 Multiple Choice (Single Answer)

Give the name of the vulnerability resides in the below code: 1 <% 2 if(null==resultArr) 3 { 4 %> 5 6 Your Search for '<%=request.getParameter("searchID")%>' has not returned any records 7 8 <% 9 } 10 %>

  1. Information Leakage
  2. Cross Site Scripting
  3. Cross Site Tracing
  4. Option 1 AND Option 2
  5. Option 1 AND Option 3
  6. Command Injection
Question 9 Multiple Choice (Single Answer)

What is wrong in the below code? public void doPost(HttpServletRequest req, HttpServletResponse res) { try { String language = req.getParameter("language"); res.sendRedirect("/doc/"+language+”/index.html”); } catch (Exception e) { } }

  1. Request Redirection is vulnerable and not a good practice
  2. Exception is not logged
  3. Input parameter “language” is not validated
  4. Option 1 AND Option 2
  5. Option 1 AND Option 3
  6. Option 2 AND Option 3
Question 10 Multiple Choice (Single Answer)

In the following code, which is the location of vulnerability? 1 String username = req.getParameter("loginID"); 2 String password = req.getParameter("loginPassword"); 3 String sql = "SELECT UserID from Employee WHERE Emp_ID = ? AND Password=?"; 4 pstmt = con.prepareStatement(sql); 5 pstmt.setString(1,username); 6 pstmt.setString(2,password); 7 pstmt.execute(); 8 user = pstmt.getResultSet(); 9 if(user!=null) 10 { 11 while (user.next()) 12 { 13 userInfo.add(user.getString(1)); 14 } 15 } 16 else 17 { 18 log.debug(“Invalid Login: Login ID-”+ username+” Password-”+ password); 19 }

  1. Line 5
  2. Line 4
  3. Line 11
  4. Line 18
Question 11 Multiple Choice (Single Answer)

In the following code, which is the location of vulnerability? 1 bIsAdmin = true; 2 try 3 { 4 function (); 5 bIsAdmin = isAdminUser(userName); 6 } 7 catch (Exception ex) 8 { 9 log.write(ex.toString()); 10 }

  1. Line 1
  2. Line 5
  3. Line 7
  4. Line 9
Question 12 True/False

Is SQL injection possible in the below code? String username = request.getParameter(“username”); String password = request.getParameter(“password”); conn = pool.getConnection( ); PreparedStatement pstmt = conn.prepareStatement(“select * from user where username=”+username+” and password=”+password); pstmt.execute(); rs = pstmt.getResultSet();

  1. True
  2. False
Question 13 Multiple Choice (Single Answer)

Give the name of the vulnerability resides in the below code: ... Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("cmd.exe /c type "+request.getParameter("path")); //path is an Input Parameter and contains the file name. InputStream stdin = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stdin); BufferedReader br = new BufferedReader(isr); ...

  1. Race Condition
  2. Command Injection
  3. Denial of Service
  4. Cross Site Request Forgery
  5. HTML Injection
Question 14 Multiple Choice (Single Answer)

Give the reason(s) of Information leakage in the below code: 1 ... 2

3 4 Please enter your Login ID and Password to get into the Web Site. 5 6 Enter Login ID : 7 Enter Password : 8 9 10 11
12 ...

  1. auto-complete ON
  2. Improper usage of HTTP Method
  3. Developer Comments
  4. Option 2 AND Option 3
  5. Option 1 AND Option 3
  6. All
Question 15 Multiple Choice (Single Answer)

Which attack(s) are possible in the below code: protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { String name = req.getParameter("name"); ... out.println("hello " + name.trim()); }

  1. Reflected Cross Site Scripting
  2. Improper Error Handling
  3. Directory Listing
  4. Phishing
  5. Option 1 AND Option 2 AND Option 4
  6. Option 1 AND Option 2
Question 16 Multiple Choice (Single Answer)

Which attack(s) are possible in the below code: <% response.sendRedirect("/with_lang.jsp?lang="+request.getParameter("language")); %>

  1. Content Spoofing
  2. HTTP Response Splitting
  3. Directory Listing
  4. Option 1 AND Option 2
  5. Option 2 AND Option 3
Question 17 Multiple Choice (Single Answer)

Identify the name of the vulnerability exist in the below code: 1 ... 2 public class ShowUserDetailsAction extends HttpServlet 3 { 4 private String currentUser; 5 public void doPost(HttpServletRequest req, HttpServletResponse res) 6 { 7 try 8 { 9 currentUser = req.getParameter("userID"); 10 RequestDispatcher rd = getServletContext().getRequestDispatcher ("/ShowDetails.jsp"); 11 if (!"".equals(currentUser)) 12 { 13 14 ArrayList userInfo = new ArrayList(); 15 LoginDAO objLoginDAO = new LoginDAO(); 16 userInfo = objLoginDAO.getUserInfo(currentUser); 17 18 if (userInfo!=null && (userInfo.size()!= 0)) 19 { 20 req.setAttribute("UserInfo", userInfo); 21 } 22 else 23 { 24 req.setAttribute("NoUser", "true"); 25 } 26 } 27 rd.forward(req,res); 28 } catch (Exception e) 29 { 30 log.debug(“Error Occurred:”+ e); 31 } 32 } 33 } 34 ...

  1. URL Tampering
  2. Brute Forcing
  3. Race Condition
  4. HTML Injection
  5. XSS
Question 18 Multiple Choice (Single Answer)

Identify the weakness in the below JSP file: 1 ... 2 3 4 <% 5 if("Admin".equals(session.getAttribute("user-type"))) 6 { 7 %> 8 9 <% 10 }%> 11 12 13 24 ...

  1. SQL Injection
  2. Cross Site Scripting
  3. Broken Access Control
  4. Improper Resource Initialization
Question 19 Multiple Choice (Single Answer)

Identify the weakness in the below JSP file: 1 2 ... 3 Dear User, 4 5 If you liked our services, then you would like to refer it to your friends. 6 7 Click on the below link: 8 9 <a href="/CWE/ReferAFriendAction?pageRedirect=<%=new String( Base64.encode("jsp/ ReferAFriend.jsp".getBytes()))%>";> "Refer a Friend"! 10 ... 11

  1. Information Disclosure
  2. Cross Site Scripting
  3. Usage of Risky Encryption
  4. All of the above
Question 20 Multiple Choice (Single Answer)

Identify the Vulnerable Line # in the below code: 1 ... 2 public static Connection getConnection() 3 { 4 Connection con = null; 5 try 6 { 7 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 8 con = DriverManager.getConnection("jdbc:odbc:Lookup","admin","admin"); 9 10 }catch (ClassNotFoundException e) 11 { 12 if(con!=null) 13 close(con); 14 log.debug(“Error Occurred:” + e); 15 16 } catch(SQLException ex) 17 { 18 19 if(con!=null) 20 close(con); 21 log.debug(“Error Occurred:” + ex); 22 } 23 return con; 24 } 25 ...

  1. Line # 4
  2. Line # 13 & 20
  3. Line # 7 & 8
  4. None of the above