4 ms·
It's finding some things where placeholders aren't a solution.... 29/11/2016 08:23:13: $update = 'UPDATE '.$table.' SET '; You can't use a placeholder for a t
by tyingq 10y ago
It's finding some things where placeholders aren't a solution....
29/11/2016 08:23:13: $update = 'UPDATE '.$table.' SET ';
You can't use a placeholder for a table name. Using a dynamic table name is pretty standard practice. The reason for that is to allow a table prefix, allowing multiple of the same app within a single database. And, typically, the table name originates from a config file, not web POSTS/GETS or other "taintable" sources.
- oneeyedpigeon 10y agoAlso, one of the examples is: 03/12/2016 19:33:11: $pdo->prepare("UPDATE users SET avatar=? WHERE id=22 ")->execute([$u]) ; Isn't that the correct way to do it?
- DCoder 10y agoThat's what happens when you use Regexes to find problems: https://github.com/laurent22/so-sql-injections/blob/7b9d99c06c5d4e751d48a0d9b6c99ec4fc11d009/src/AppBundle/InjectionFinder.php#L62 https://github.com/laurent22/so-sql-injections/blob/7b9d99c0...
- tyingq 10y agoFound the regex's being used: https://github.com/laurent22/so-sql-injections/blob/master/src/AppBundle/InjectionFinder.php#L62 https://github.com/laurent22/so-sql-injections/blob/master/s... It's pretty basic. For UPDATE, it's matching anything with UPDATE followed by SET, followed by a dollar sign+alphanumeric And, it's specifically going after dynamic table names. Hmm. '/UPDATE\s+.*?\$[a-zA-Z0-9].*?\sSET.*?/i', // UPDATE $table SET ... Edit: You can also escape the scans by using variable names that start with an underscore :) $_dontscanme
- DCoder 10y ago> You can also escape the scans by using variable names that start with an underscore So people who use "WHERE id=" . $_GET['id'] will not get detected?
- oneeyedpigeon 10y agoI just did the same :) It flags, for example, the following as an SQL injection: query("DELETE FROM foo WHERE id=1");$id=2; In other words, it's flagging any line that begins with "DELETE ... FROM" and then later has a variable. Now, that might be a fairly strong correlation, but it's definitely far from perfect.
- tyingq 10y agoI suspect you can fool it by splitting into different lines, which is actually common with longish queries. $query="SELECT whatever from foo WHERE". "id=$bar ". "AND " . "field=$value";
- laurent123456 10y agoYes there are false positives. It would be too time consuming to extract and parse the PHP code from the questions since there are millions of them to process, however I found that the regexes, though not perfect, provide a good estimate and are quick to execute.
- tyingq 10y agoYou could tweak the regexes a bit to fix some things... - it's currently skipping variable names with underscores, and it's matching variable names that start with numbers, which isn't a valid variable name - the false positives coming from ->execute($whatever) could be fixed by not searching after a > character. I don't think that would create any notable amount of false negatives. Like, for example, changing: '/UPDATE\s+.*?\sSET\s.*?\$[a-zA-Z0-9].*?/i' To '/UPDATE\s+.*?\sSET\s[^>]*?\$[a-zA-Z_]/i'
- oneeyedpigeon 10y ago> You can also escape the scans ... ... or by splitting the SQL string over multiple lines. Or building it up by concatenating strings. Let's stop this now, we've demonstrated that the code is very brittle :)
- tyingq 10y agoAnother comment mentions that it would skip $_GET['whatever'] and $_POST['whatever'], which is probably in the most egregiously vulnerable code. I think the underscore thing is worth fixing.
- deleted 10y ago[deleted]
- laurent123456 10y agoYes, that's a false positive. I guess the results could be tweaked to filter out lines that include `->prepare(...`.
- scwoodal 10y agoWhat are the common use cases for this scenario?
- tyingq 10y agoTable prefixes? Running more than one instance of an application in a single database. You'll find it's supported in most php apps and frameworks. Wordpress, Drupal, Laravel, Magento, etc. Not just php either. Ruby/Rails Active Record, for example, supports it. And I imagine dynamic, variable bound table names are probably in every ORM in just about every language.
- DCoder 10y agoVariable table names can also be useful if you have poor man's partitioning – separate tables with historical data , like "logs_201610", "logs_201611", "logs_201612", etc. Another scenario I just thought of: your schema is more or less dynamic. For example, Drupal lets developers/site administrators create entities with custom fields, each field gets its own DB table, and to load an entity you need to SELECT * FROM node LEFT OUTER JOIN field_data_field_name_1 ON .. LEFT OUTER JOIN field_data_field_name_2 ON .. ...
- laurent123456 10y agoGood point, and I don't think it's possible to use prepared statements for table names. I'll remove the regex for this.
- dozzie 10y ago> Using a dynamic table name is pretty standard practice. The reason for that is to allow a table prefix, allowing multiple of the same app within a single database. This is precisely how PHP programmers think: that MySQL is somehow the gold standard in the databases world. Table prefixes are just the stupid way of emulating database schemas where it's not supported, which is only MySQL (SQLite doesn't support this either, but it was never meant for web applications anyway).
- tyingq 10y agoTable prefixes aren't just used in PHP. Ruby/Rails, Grails, etc. And the general idea of dynamic, variable bound table names can be found in almost any language. Schemas are nice, but as you mention, two of the most popularly used databases don't support it. Edit: And, for what it's worth, the affinity for Mysql in PHP code is most likely because both are there, by default, in shared hosting environments. I don't personally know any PHP devs that find Mysql to be a "gold standard". Most popular PHP apps and frameworks support Postrgres as well.
- dozzie 10y ago> Schemas are nice, but as you mention, two of the most popularly used databases don't support it. Yeah, two databases. One that was designed as an embedded storage engine and was never intended for web applications and the other that historically was probably the shittiest one (silently damaging unicode data, silently damaging foreign keys in some circumstances, silently ignoring CHECK constraints, and using non-ACID data store unless asked otherwise, though the last one was fixed just few years back) and should have died long time ago.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- tyingq 10y ago