Threat mapping and vulnerability detection
Discovering entry points narrows the focus for threat mapping and vulnerability detection. An entry point is essential to a trace. It is important to unearth where this variable goes (execution flow) and its impact on the application.
The previous scan found a
Request object entry in the application:
22 : NameValueCollection nvc=Request.QueryString;
Running the script with the
-t option will help to trace the variables. (For full coverage, trace it right through to the end, using all possible iterations).
D:\PYTHON\scancode>scancode.py -t details.aspx nvc Tracing variable:nvc NameValueCollection nvc=Request.QueryString; String arr1=nvc.AllKeys; String sta2=nvc.GetValues(arr1);
This assigned a value from
sta2, so that also needs a trace:
D:\PYTHON\scancode>scancode.py -t details.aspx sta2 Tracing variable:sta2 String sta2=nvc.GetValues(arr1); pro_id=sta2;
Here's another iteration; tracing
D:\PYTHON\scancode>scancode.py -t details.aspx pro_id Tracing variable:pro_id String pro_id=""; pro_id=sta2; String qry="select * from items where product_id=" + pro_id; response.write(pro_id);
Finally, this is the end of the trace. This example has shown multiple traces of a single page, but it is possible to traverse multiple pages across the application. Figure 3 shows the complete output.
Figure 3. Vulnerability detection with tracing
As the source code and figure show, there is no validation of input in the source. There is a SQL injection vulnerability:
String qry="select * from items where product_id=" + pro_id;
The application accepts
pro_id and passes it as is to the
SELECT statement. It is possible to manipulate this statement and inject SQL payload.
Similarly, another line exposes a cross-site scripting (XSS) vulnerability:
Throwing back the (unvalidated)
-sG option executes the global search routine. This routine looks for file objects, cookies, exceptions, etc. Each has potential vulnerabilities, and this scan can help you to identify them and map them to the respective threats:
D:\shreeraj_docs\perlCR>scancode.py -sG details.aspx Dependencies: 13 : Request Object Entry: 22 : NameValueCollection nvc=Request.QueryString; SQL Object Entry: 49 : String qry="select * from items where product_id=" + pro_id; SQL Object Entry: 50 : SqlCommand mycmd=new SqlCommand(qry,conn); Response Object Entry: 116 : response.write(pro_id); XSS Check: 116 : response.write(pro_id); Exception handling: 122 : catch(Exception ex)
This code review approach takes minimal effort by detecting entry points, vulnerabilities, and variable tracing.
Mitigation and Countermeasure
After you have identified a vulnerability, the next step is to mitigate the threat. There are various ways to do this, depending on your deployment. For example, it's possible to mitigate SQL injection by adding a rule to the web application firewall to bypass a certain set of characters such as single and double quotes. The best way to mitigate this issue is by applying secure coding practices--providing proper input validation before consuming the variable at the code level. At the SQL level, it is important to use either prepared statements or stored procedures to avoid SQL
SELECT statement injection. For mitigation of XSS vulnerabilities, it is imperative to filter out characters such as greater than (>) and less than (<) prior to serving any content to the end-client. These steps provide threat mitigation to the overall web application.
Code review is a very powerful tool for detecting vulnerabilities and getting to their actual source. This is the "whitebox" approach. Dependency determination, entry point identification, and threat mapping help detect vulnerability. All of these steps need architecture and code reviews. The nature of code is complex, so no single tool can meet all of your needs. As a professional, you need to write tools on the fly when doing code review and put them into action when the code base is very large. It is not feasible to go through each line of code.
In this scenario, one of the methods is to start with entry points, as discussed earlier in this article. You can build complex scripts or programs in any language to grab various patterns in voluminous source code and link them together. Tracing the variable or function is the key that can show up the entire traversal and greatly help in determining vulnerabilities.
Exhibit 1 - scancode.py
Shreeraj Shah is the founder of Blueinfy, a company that provides application security services.
Return to O'Reilly SysAdmin