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.
Questions
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; }
- delete j
- free j
- it is not supposed to be deleted
- delete [] j
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?
- The program should be started with root privileges. Then it should use setuid(UID) to change privileges between root and another account.
- The program should be started with root privileges. Then it should use seteuid(UID) to change privileges between root and another account.
- Starting the program as root is a security risk. The program should run with least privileges and obtain root using seteuid(UID) whenever necessary.
- The program has to run with root privileges entirely. Once root privileges are dropped they cannot be regained.
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); }
- value of tree
- value of node
- value of i
- garbage-- its a dangling pointer
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);
- Since the file name is hard coded, fopen() will fail if the file already exists
- 0600 is not a secure option. The parameter 0600 should be changed to 0666
- Attackers can exploit by creating a symboling link /tmp/cache_data that points to a system file
- Attackers can exploit the application's cache by writing directly to /tmp/cache_data
Is writing to an already freed memory a vulnerability? x = malloc(200); /* do something with x / free (x); / do something else */ strcpy(x, “somedata”);
- Overwriting freed memory is a security vulnerability
- Depends on the application and how important “somedata” is
- This will result in a buffer overflow since the freed memory location cannot handle 8 characters of data “somedata”
- strcpy() will fail as it cannot write to already freed memory, and the application will crash
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); } ...
- Cross Site Scripting
- SQL Injection
- Improper Resource Release
- Option 1 AND Option 2
- Option 1 AND Option 2 AND Option 3
- Option 2 AND Option 3
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
- Line # 12
- Line # 9
- Line # 17
- Line # 8
- Line # 14, 18 & 19
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 %>
- Information Leakage
- Cross Site Scripting
- Cross Site Tracing
- Option 1 AND Option 2
- Option 1 AND Option 3
- Command Injection
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) { } }
- Request Redirection is vulnerable and not a good practice
- Exception is not logged
- Input parameter “language” is not validated
- Option 1 AND Option 2
- Option 1 AND Option 3
- Option 2 AND Option 3
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 }
- Line 5
- Line 4
- Line 11
- Line 18
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 }
- Line 1
- Line 5
- Line 7
- Line 9
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();
- True
- False
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); ...
- Race Condition
- Command Injection
- Denial of Service
- Cross Site Request Forgery
- HTML Injection
Give the reason(s) of Information leakage in the below code: 1 ... 2
12 ...- auto-complete ON
- Improper usage of HTTP Method
- Developer Comments
- Option 2 AND Option 3
- Option 1 AND Option 3
- All
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()); }
- Reflected Cross Site Scripting
- Improper Error Handling
- Directory Listing
- Phishing
- Option 1 AND Option 2 AND Option 4
- Option 1 AND Option 2
Which attack(s) are possible in the below code: <% response.sendRedirect("/with_lang.jsp?lang="+request.getParameter("language")); %>
- Content Spoofing
- HTTP Response Splitting
- Directory Listing
- Option 1 AND Option 2
- Option 2 AND Option 3
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 ...
- URL Tampering
- Brute Forcing
- Race Condition
- HTML Injection
- XSS
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 ...
- SQL Injection
- Cross Site Scripting
- Broken Access Control
- Improper Resource Initialization
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
- Information Disclosure
- Cross Site Scripting
- Usage of Risky Encryption
- All of the above
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 ...
- Line # 4
- Line # 13 & 20
- Line # 7 & 8
- None of the above