Skip to content

Commit 9acdd3b

Browse files
committed
ascanrules: SQLi PostgreSQL rename scan rule (all time based)
Signed-off-by: kingthorin <kingthorin@users.noreply.github.com> # Conflicts: # addOns/ascanrules/CHANGELOG.md # addOns/ascanrules/src/main/resources/org/zaproxy/zap/extension/ascanrules/resources/Messages.properties
1 parent 24d305c commit 9acdd3b

File tree

5 files changed

+16
-38
lines changed

5 files changed

+16
-38
lines changed

addOns/ascanrules/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1212
- SQL Injection - MsSQL
1313
- SQL Injection - MySQL
1414
- SQL Injection - Hypersonic
15+
- SQL Injection - PostgreSQL
1516

1617
### Added
1718
- Rules (as applicable) have been tagged in relation to HIPAA and PCI DSS.
Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.net.SocketException;
2424
import java.util.Collections;
2525
import java.util.HashMap;
26-
import java.util.LinkedHashMap;
2726
import java.util.Map;
2827
import java.util.concurrent.atomic.AtomicReference;
2928
import org.apache.commons.configuration.ConversionException;
@@ -42,11 +41,11 @@
4241
import org.zaproxy.zap.model.TechSet;
4342

4443
/**
45-
* The SqlInjectionPostgreScanRule identifies Postgresql specific SQL Injection vulnerabilities
46-
* using Postgresql specific syntax. If it doesn't use Postgresql specific syntax, it belongs in the
47-
* generic SQLInjection class! Note the ordering of checks, for efficiency is : 1) Error based (N/A)
48-
* 2) Boolean Based (N/A - uses standard syntax) 3) UNION based (N/A - uses standard syntax) 4)
49-
* Stacked (N/A - uses standard syntax) 5) Blind/Time Based (Yes)
44+
* This scan rule identifies Postgresql specific SQL Injection vulnerabilities using Postgresql
45+
* specific syntax. If it doesn't use Postgresql specific syntax, it belongs in the generic
46+
* SQLInjection class! Note the ordering of checks, for efficiency is : 1) Error based (N/A) 2)
47+
* Boolean Based (N/A - uses standard syntax) 3) UNION based (N/A - uses standard syntax) 4) Stacked
48+
* (N/A - uses standard syntax) 5) Blind/Time Based (Yes)
5049
*
5150
* <p>See the following for some great specific tricks which could be integrated here
5251
* http://www.websec.ca/kb/sql_injection
@@ -60,11 +59,9 @@
6059
*
6160
* @author 70pointer
6261
*/
63-
public class SqlInjectionPostgreScanRule extends AbstractAppParamPlugin
62+
public class SqlInjectionPostgreSqlTimingScanRule extends AbstractAppParamPlugin
6463
implements CommonActiveScanRuleInfo {
6564

66-
private boolean doTimeBased = false;
67-
6865
private int doTimeMaxRequests = 0;
6966

7067
private int sleepInSeconds;
@@ -80,23 +77,6 @@ public class SqlInjectionPostgreScanRule extends AbstractAppParamPlugin
8077
private static final double TIME_CORRELATION_ERROR_RANGE = 0.15;
8178
private static final double TIME_SLOPE_ERROR_RANGE = 0.30;
8279

83-
/**
84-
* create a map of SQL related error message fragments, and map them back to the RDBMS that they
85-
* are associated with keep the ordering the same as the order in which the values are inserted,
86-
* to allow the more (subjectively judged) common cases to be tested first Note: these should
87-
* represent actual (driver level) error messages for things like syntax error, otherwise we are
88-
* simply guessing that the string should/might occur.
89-
*/
90-
private static final Map<String, String> SQL_ERROR_TO_DBMS = new LinkedHashMap<>();
91-
92-
static {
93-
SQL_ERROR_TO_DBMS.put("org.postgresql.util.PSQLException", "PostgreSQL");
94-
SQL_ERROR_TO_DBMS.put("org.postgresql", "PostgreSQL");
95-
// Note: only Postgresql mappings here.
96-
// TODO: is this all?? we need more error messages for Postgresql for different languages.
97-
// PHP, ASP, JSP(JDBC), etc.
98-
}
99-
10080
/**
10181
* The sleep function in Postgresql cast it back to an int, so we can use it in nested select
10282
* statements and stuff.
@@ -209,7 +189,8 @@ public class SqlInjectionPostgreScanRule extends AbstractAppParamPlugin
209189
}
210190

211191
/** for logging. */
212-
private static final Logger LOGGER = LogManager.getLogger(SqlInjectionPostgreScanRule.class);
192+
private static final Logger LOGGER =
193+
LogManager.getLogger(SqlInjectionPostgreSqlTimingScanRule.class);
213194

214195
@Override
215196
public int getId() {
@@ -252,16 +233,12 @@ public void init() {
252233

253234
// set up what we are allowed to do, depending on the attack strength that was set.
254235
if (this.getAttackStrength() == AttackStrength.LOW) {
255-
doTimeBased = true;
256236
doTimeMaxRequests = 3;
257237
} else if (this.getAttackStrength() == AttackStrength.MEDIUM) {
258-
doTimeBased = true;
259238
doTimeMaxRequests = 5;
260239
} else if (this.getAttackStrength() == AttackStrength.HIGH) {
261-
doTimeBased = true;
262240
doTimeMaxRequests = 10;
263241
} else if (this.getAttackStrength() == AttackStrength.INSANE) {
264-
doTimeBased = true;
265242
doTimeMaxRequests = 100;
266243
}
267244
// Read the sleep value from the configs
@@ -289,7 +266,6 @@ public void scan(HttpMessage originalMessage, String paramName, String paramValu
289266
int countTimeBasedRequests = 0;
290267
for (int timeBasedSQLindex = 0;
291268
timeBasedSQLindex < SQL_POSTGRES_TIME_REPLACEMENTS.length
292-
&& doTimeBased
293269
&& countTimeBasedRequests < doTimeMaxRequests;
294270
timeBasedSQLindex++) {
295271
countTimeBasedRequests++;

addOns/ascanrules/src/main/javahelp/org/zaproxy/zap/extension/ascanrules/resources/help/contents/ascanrules.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ <H2 id="id-40022">SQL Injection - PostgreSQL (Time Based)</H2>
422422
<br>
423423
Post 2.5.0 you can change the length of time used for the attack by changing the <code>rules.common.sleep</code> parameter via the Options 'Rule configuration' panel.
424424
<p>
425-
Latest code: <a href="https://github.com/zaproxy/zap-extensions/blob/main/addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SqlInjectionPostgreScanRule.java">SqlInjectionPostgreScanRule.java</a>
425+
Latest code: <a href="https://github.com/zaproxy/zap-extensions/blob/main/addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SqlInjectionPostgreSqlTimingScanRule.java">SqlInjectionPostgreSqlTimingScanRule.java</a>
426426
<br>
427427
Alert ID: <a href="https://www.zaproxy.org/docs/alerts/40022/">40022</a>.
428428

addOns/ascanrules/src/main/resources/org/zaproxy/zap/extension/ascanrules/resources/Messages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ ascanrules.sqlinjection.mssql.name = SQL Injection - MsSQL (Time Based)
186186
ascanrules.sqlinjection.mysql.name = SQL Injection - MySQL (Time Based)
187187
ascanrules.sqlinjection.name = SQL Injection
188188
ascanrules.sqlinjection.oracle.name = SQL Injection - Oracle (Time Based)
189-
ascanrules.sqlinjection.postgres.name = SQL Injection - PostgreSQL
189+
ascanrules.sqlinjection.postgres.name = SQL Injection - PostgreSQL (Time Based)
190190
ascanrules.sqlinjection.refs = https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
191191
ascanrules.sqlinjection.soln = Do not trust client side input, even if there is client side validation in place.\nIn general, type check all data on the server side.\nIf the application uses JDBC, use PreparedStatement or CallableStatement, with parameters passed by '?'\nIf the application uses ASP, use ADO Command Objects with strong type checking and parameterized queries.\nIf database Stored Procedures can be used, use them.\nDo *not* concatenate strings into queries in the stored procedure, or use 'exec', 'exec immediate', or equivalent functionality!\nDo not create dynamic SQL queries using simple string concatenation.\nEscape all data received from the client.\nApply an 'allow list' of allowed characters, or a 'deny list' of disallowed characters in user input.\nApply the principle of least privilege by using the least privileged database user possible.\nIn particular, avoid using the 'sa' or 'db-owner' database users. This does not eliminate SQL injection, but minimizes its impact.\nGrant the minimum database access that is necessary for the application.
192192
ascanrules.sqlinjection.sqlite.alert.errorbased.extrainfo = The following known SQLite error message was provoked: [{0}].
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@
4141
import org.zaproxy.zap.model.TechSet;
4242
import org.zaproxy.zap.testutils.NanoServerHandler;
4343

44-
/** Unit test for {@link SqlInjectionPostgreScanRule}. */
45-
class SqlInjectionPostgreScanRuleUnitTest extends ActiveScannerTest<SqlInjectionPostgreScanRule> {
44+
/** Unit test for {@link SqlInjectionPostgreSqlTimingScanRule}. */
45+
class SqlInjectionPostgreSqlTimingScanRuleUnitTest
46+
extends ActiveScannerTest<SqlInjectionPostgreSqlTimingScanRule> {
4647

4748
@Override
48-
protected SqlInjectionPostgreScanRule createScanner() {
49-
return new SqlInjectionPostgreScanRule();
49+
protected SqlInjectionPostgreSqlTimingScanRule createScanner() {
50+
return new SqlInjectionPostgreSqlTimingScanRule();
5051
}
5152

5253
@Test

0 commit comments

Comments
 (0)