Source Code Security Review
by @quochungto
Perform a systematic white-box security review of web application source code to find exploitable vulnerabilities. Use this skill when: you have authorized a...
Scenario: Penetration test with source access β Java banking application
Trigger: "We're granting you source access for this pentest. The application handles fund transfers and user account management."
Process:
1. Phase 1: Grep for getParameter β finds 47 call sites. Note request.getParameter("title") stored in m_pageTitle field in PageController.java:88.
2. Phase 2.1 (XSS): Grep for InnerHtml β finds objCell.InnerHtml = link in ReportView.java:204. Trace link backward β constructed by string concatenation from HttpUtility.UrlDecode(Request.QueryString["refURL"]) without HTML-encoding. Confirmed reflected XSS (CWE-79, High). Also trace m_pageTitle forward β finds it written into element in template renderer without encoding. Second XSS confirmed, conditionally triggerable (requires type=3).
3. Phase 2.2 (SQL injection): Grep for createStatement β finds s.executeQuery("SELECT name, accno FROM TblCustomers WHERE " + SqlWhere) in CustomerSearch.java:156. SqlWhere is built from Request.QueryString["CID"]. Confirmed SQL injection (CWE-89, Critical).
4. Phase 2.6 (Backdoor): Line-by-line review of AuthService.java β finds if (checkCredentials(up, password) || "oculiomnium".equals(password)) return up;. Hardcoded backdoor password grants access to any account (CWE-798, Critical).
Output: 3 findings β Critical SQL injection, Critical backdoor password, High XSS (x2). Countermeasures: replace createStatement with prepareStatement; remove hardcoded password; HTML-encode all output via HtmlUtils.htmlEscape().
Scenario: Pre-launch PHP e-commerce application review
Trigger: "We're launching next month. Please review our PHP codebase for security issues before we go live."
Process:
1. Phase 1: Grep for PHP input sources β finds $_GET, $_POST, $_COOKIE in 23 files. Check php.ini β register_globals = On on their dev server; flag immediately.
2. Phase 2.3 (Path traversal + RFI): Grep for include( β finds include($_GET['page'] . '.php') in main.php:12. No allow_url_include check in code. Check php.ini β allow_url_include = 1. Confirmed Remote File Inclusion (CWE-98, Critical). Also: allow_url_fopen = 1 and display_errors = On in production config.
3. Phase 2.2 (SQL injection): Grep for mysql_query( β finds mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'") in login.php:34. Variables from $_POST without escaping. Confirmed SQL injection (CWE-89, Critical). magic_quotes_gpc = Off confirms no runtime escaping active.
4. Phase 2.5 (OS command injection): Grep for exec( β finds exec("convert " . $_POST['filename'] . " -resize 100x100 output.jpg") in image.php:67. Confirmed OS command injection via shell metacharacters (CWE-78, Critical).
5. Phase 4 (Config): display_errors = On in php.ini β leaks stack traces and DB credentials to users (Low). register_globals = On β creates uninitialized variable injection vectors (High).
Output: 3 Critical findings, 1 High, 1 Low. Countermeasures: disable allow_url_include and allow_url_fopen; replace mysql_query with mysqli->prepare; replace shell exec with ImageMagick PHP extension API; set display_errors = Off + log_errors = On; set register_globals = Off.
Scenario: Security audit of a PHP/JavaScript SPA β focus on client-side and database tier
Trigger: "Our application is a single-page app with a PHP API backend. We've had a report of potential DOM-based XSS and we want to understand our stored procedure security posture."
Process:
1. Phase 2.1 (DOM XSS): Grep JavaScript for DOM sources and sinks β finds url = document.URL; index = url.indexOf('?redir='); target = unescape(url.substring(index + 7, url.length)); document.location = target; in redirect.js:22. Script checks for // to block absolute URLs but calls unescape() afterward. Confirmed DOM-based open redirect and XSS via ?redir=%2500javascript:alert(1) (CWE-601 + CWE-79, High). Post-validation canonicalization bypass.
2. Phase 2.2 (Stored procedure SQL injection): Review .sql migration files β finds CREATE PROCEDURE show_current_orders (@name varchar(400) = NULL) AS DECLARE @sql nvarchar(4000) SELECT @sql = 'SELECT id_num, searchstring FROM searchorders WHERE ' + 'searchstring = ''' + @name + ''''; EXEC (@sql) GO. Even if the application calls this procedure with a parameterized API, the procedure itself constructs dynamic SQL from @name β confirmed stored procedure SQL injection (CWE-89, High).
3. Phase 3 (Session management): Line-by-line review of TokenGenerator.java β uses java.util.Random (not cryptographically secure) to generate session tokens. Session tokens are predictable given sufficient samples (CWE-338, High).
Output: 3 High findings β DOM-based XSS/redirect, stored procedure SQL injection, predictable session tokens. Countermeasures: remove unescape() call from redirect script; rewrite stored procedure using sp_executesql with parameterized query; replace java.util.Random with java.security.SecureRandom.
clawhub install bookforge-source-code-security-review