Google+

Some SQL injection in Android – how to use GROUP BY and CASE when you are not allowed to do so

9

8 February 2012 by ludovicianul

I suppose you’re all familiar with the “academic” examples of SQL injection when you put a AND 1=1 at the end of the SQL query and magically you get access to all kind of nasty things. I’ll show you how to use the same trick to do a SQL injection in Android, but for a good thing :)

I had the following situation: I’ve wanted to select group all the calls from the call log either by the caller name if the number was in the agenda or by number if the number was not in the agenda. To do this you need a content resolver, a URI and a projection to tell Android which columns you want to select. A typical call will look as follows:

getContentResolver().query(CallLog.Calls.CONTENT_URI, 
new String[] { projection}, selection, new String[] 
{selectionArgs}, sortOrder)

As you notice there is no way of telling android how to group the rows. But we can trick it:

getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[] 
{"_id", "name", "number",
 "duration","type", "numbertype", MAX(date) AS date",
"(CASE WHEN name=null THEN name ELSE number END) as logname"}, 
" 1=1 ) GROUP BY (logname",null,null);

You can notice the 1=1 ) GROUP by (logname String placed as the selection argument. This is why the magic happens. ;) Also notice the syntax for the CASE statement that conditionally selects the number or the name. You can further use it in any place you need conditional select of the columns.

Happy Android programming!

About these ads

9 thoughts on “Some SQL injection in Android – how to use GROUP BY and CASE when you are not allowed to do so

  1. [...] I suppose you’re all familiar with the “academic” examples of SQL injection when you put a AND 1=1 at the end of the SQL query and magically you get access to all kind of nasty things. I’ll show you how to use the same trick to do a SQL injection in Android, but for a good thing.    Database Read the original post on DZone… [...]

  2. Fadi E says:

    I wonder whether Android programmers nowadays really care about security when it comes to their code, especially SQL injection. Google should have an automated tool that will test apps for SQL injection or something….

  3. Homepage says:

    … [Trackback]…

    [...] Read More: insidecoding.wordpress.com/2012/02/08/some-sql-injection-in-android-how-to-use-group-by-and-case-when-you-are-not-allowed-to-do-so/ [...]…

  4. Eir says:

    Does not work on IceCreamSandwitch.

  5. arvchak says:

    Good one !!! kudos … Does it work for ICS btw?

  6. n3vr0s says:

    i have just fired this query :
    Uri uriSms = Uri.parse(“content://sms”);
    Cursor cursor = getContentResolver().query(uriSms, new String[] {“address”,”max(date)”}, ” 1=1 ) GROUP BY ( address”, null, null);
    cursor.moveToFirst();
    android.util.Log.i(“COLUMNS”, Arrays.toString(cursor.getColumnNames()));
    String [] columns= cursor.getColumnNames();
    on my s3 (ics) and it works great

  7. MWR Labs (Security Company that rocks, some of the developers of pieces contributed to the project worked at Samsung) has an awesome tool for doing just that :) http://labs.mwrinfosecurity.com/tools/2012/03/16/mercury/demo/

  8. Genia says:

    Hi there everyone, it’s my first visit at this website, and paragraph is genuinely fruitful for me, keep up posting such posts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: