Skip to content

Instantly share code, notes, and snippets.

@Omegatcu
Created October 24, 2015 15:12
Show Gist options
  • Select an option

  • Save Omegatcu/d8d26b8d0102666104eb to your computer and use it in GitHub Desktop.

Select an option

Save Omegatcu/d8d26b8d0102666104eb to your computer and use it in GitHub Desktop.
SQLiShield

The idea behind the SQLiShield feature in Admin Tools is checking each POST and GET variable against a regular expression. The regular expression only matches SQL code typically found in SQL injections. Considering that it's a bad practice sending raw SQL commands over an HTTP request this works very well without throwing false positives. The only false positives we are aware of is someone posting an article or a support ticket which includes a raw SQL command.

The regular expression we use (version 5 of the regex) is the following:

#(union([\s]{1,}|/\*(.*)\*/){1,}(all([\s]{1,}|/\*(.*)\*/){1,})?select|select(([\s]{1,}|/\*(.*)\*/|`){1,}([\w]|_|-|\.|\*){1,}([\s]{1,}|/\*(.*)\*/|`){1,}(,){0,})*from([\s]{1,}|/\*(.*)\//){1,}[a-z0-9]{1,}_|select([\s]{1,}|/\*(.*)\*/|\(){1,}(COUNT|MID|FLOOR|LIMIT|RAND|SLEEP|ELT)|select([\s]{1,}|/\*(.*)\*/|`){1,}.*from([\s]{1,}|/\*(.*)\//){1,}INFORMATION_SCHEMA\.|EXTRACTVALUE([\s]{1,}|\(){1,}|(insert|replace)(([\s]{1,}|/\*(.*)\*/){1,})((low_priority|delayed|high_priority|ignore)([\s]{1,}|/\*(.*)\*/){1,}){0,}into|drop([\s]{1,}|/\*(.*)\*/){1,}(database|schema|event|procedure|function|trigger|view|index|server|(temporary([\s]{1,}|/\*(.*)\*/){1,}){0,1}table){1,1}([\s]{1,}|/\*(.*)\*/){1,}|update([\s]{1,}|/\*[^\w]*\/){1,}(low_priority([\s]{1,}|/\*[^\w]*\/){1,}|ignore([\s]{1,}|/\*[^\w]*\/){1,})?`?[\w]*_.*set|delete([\s]{1,}|/\*(.*)\*/){1,}((low_priority|quick|ignore)([\s]{1,}|/\*(.*)\*/){1,}){0,}from|benchmark([\s]{1,}|/\*(.*)\*/){0,}\(([\s]{1,}|/\*(.*)\*/){0,}[0-9]{1,}){1,}#i

Please note that you should run this regular expression after any kind of URL decoding or query parameter value preprocessing / filtering. In Joomla!, this means that we run it against the filtered input made available by JInput. If you were to adapt it for your web application consult the documentation of the framework you are using.

If you've found a legitimate SQL injection case not addressed by this RegEx please send me a sample of the offending request, including query parameter values, so I can improve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment