Questions
Data returned by which of the following methods should be validated before using it
- getParameter ()
- getQueryString ()
- getCookies ()
- getHeaders ()
- 1
- 1 and 2
- 1,2 and 3
- 1,2,3 and 4
Which of the following are countermeasures for XSS
- Releasing Resources after use
- Input Validation
- Running with least privilege
- URL based access control
- Output Encoding
- 1 and 4
- 2 and 4
- 2 and 5
- 3 and 5
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.
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 9
- Line 5
- Line 7
- Line 1
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 18
- Line 11
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
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
Are there any memory issues in the following code? Please assume that variable inputsize has the correct size.
int add_num_array(int inputsize, int num) {
int *newnum = malloc (inputsize * sizeof(int)); /* 1 */
int i;
for (i=0; i<n;i++) { /* 2 */
newnum[i] += num; /* 3 */
}
}
- No vulnerabilities are present
- Line 1 should only use malloc(inputsize);
- Line 2 should be for (i=0; i<=n, i++)
- Line 1 should use calloc() instead of malloc()
What is the vulnerability in this code?
char output[20];
/* Assume data is a character array with value %200d asdf */
sprintf(output, data);
- Buffer overflow
- Off by one error
- Format string vulnerability
- No vulnerabilities are present in this code
What is the vulnerability in this code?
int main(int argc, char * argv[]) {
printf (argv[1]);
}
- Buffer overflow
- Off by one error
- Format string vulnerability
- No vulnerabilities are present in this code
What is the possible vulnerability in this code?
unsigned int total, userinput1, userinput2;
userinput1 = receiveInput();
userinput2 = receiveInput();
total = userinput1 + userinput2;
- Integer overflow
- Buffer overflow
- Stack overflow
- Data type mismatch
Which Compilation switch will you use to check Buffer Overflows?
- /GS on Visual C++ and -fmudflap -fmudflapth -fmudflapir on GCC
- /O in Vc++ and -O2 in GCC
- /S in Vc++ and -fcrossjumping in GCC
- /S in VC++ and -fno-function-cse in GCC
What can go wrong in following code?
#include <stdio.h>
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("usage: %s [source] [dest]\n", argv[0]);
exit(1);
}
char x;
FILE *file[2];
file[0] = fopen(argv[1],"r+");
file[1] = fopen(argv[2],"w+");
for(x = 0; x < 2; x++) {
if(file[x] == NULL) {
printf("error opening file.\n");
exit(1);
}
}
do {
x = fgetc(file[0]);
fputc(x,file[1]);
} while(x != EOF);
for(x = 0; x < 2; x++)
fclose(file[x]);
return 0;
}
- SQL Injection
- Arc Injection
- Buffer Overflow
- both 2 and 3
Which compilation switch should be enabled for stack protection? Choose the best and most secure option.
- fstack-protector
- fstack-protector-all
- fdelete-null-pointer-checks
- Both a and b
unsigned char j,k; j=getchar(); k=getchar(); unsigned char result = j + k; What vulnerability is present in this code:
- Heap Overflow
- Integer overflow
- Buffer overflow
- No Vulnerability
Which statement creates a buffer over flow? (Line numbers are marked using comments /* */)
#include <iostream.h>
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[]) {
int i=0,j=1;
char ipstring[80];
for (;i<=3;i++){
cout<<"\n entering a new character\n";
j=getchar();/*1*/
cout<<”enter a string”;
gets(ipstring);/*2*/
cout<<j<<"\n";
}
return 0;
}
```
- 1
- 2
- Both
- None
What is the vulnerability ?
int main (int argc, char *argv[]) {
char k[3];
int i=0,j=1;
char buffer[50];
strncpy(buffer, argv[1], sizeof(buffer) - 1);
buffer[49]='/0';
unsigned char ch='a';
k[0]=1;
do{
i++;
k[i]=ch+i;
} while(i<3);
return 0;
}
- Heap overflow
- Integer overflow
- Off by one error
- None of the above
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
- a & b
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