how secure is your code?
DESCRIPTION
A brief look at PHP securityTRANSCRIPT
How secure is your code?Mikee Franklin
Who am I?
Developer @ coolpink
Working in the industry for 10 years
I never finish a personal proj...
I like to play with ALL languages. Use the right tool for the job. (except for javascript, javascript should be used for /everything/)
twitter: @mikeemooweb: www.mikeefranklin.co.uk
Why I find this interesting..
It's important to know for my work
I love finding things I shouldn't be able to find
I like to think I'm doing a 'good thing' if I find (and report) a security hole
I don't actually know much about it at all. I've barely scraped the surface of what's possible.
I don't “exploit” live websites.
The basics from an exploiters point of view
We want to get the server to run our own code
If we can run our own code, we can get shell access
If we can get shell access, we can find things we shouldn't be able to find, and we can potentially get root access.
If not, we can still extract a lot information. Passwords, account details.. etc.. those passwords will often be the same for other sites
It'll help to see the source
Having access to the source code makes it a lot easier to find the vulnerabilities.
Check for open /.svn/ folders
Have a poke around. Work out what plugins might be installed, check the source of them.
Check for known files that might give you the version number of the software. INSTALL, VERSION, LICENCE..etc.
File uploads
File uploading is obviously the easiest way to get your code up there.
Abuse bad extension checking
Some servers will execute .php.jpg as a php file – depends on configuration and version(?)
You can embed code in image metadata and PHP will still recognise it as a valid image, no matter what the extension.
So now we can execute our own code, what next?
Our PHP can execute a reverse socket shell which will attempt to connect back to our own machine and forward STDIN/STDOUT to a socket server running on our local machine.
We can run netcat locally and wait for the connection.
We now have shell access. But we're only running as the apache user... but we can now easily extract all of the data from the database, search the server for other files, and look to see what software is running that'll allow us to escalate permissions.
There's plenty of information out there with databases of exploits (for example, http://www.exploit-db.com)
Local file inclusion (LFI)
Get code onto the machine
Use local file inclusion to execute the code
good example:
require $_GET[“file”].”.php”;
But what about the .php? Surely that'll only open php files?
Using a null character strips off the end, for example:
index.php?file=../../../../../../../../../../etc/passwd%00
But.. we need to get our code onto the machine first...
Inject to Apache logs
We can inject code into apaches logs by causing an error message that contains our code.
This will throw an error:
index.php?file=<?php phpinfo(); ?>
Now we need to include the apache logs.
We can run through a list of obvious log paths
We can cycle through /proc/self/fd/[x] as one might be a symlink to our logs
Inject to FTP logs
We can just attempt to connect to the FTP using our code as the username
The handshake messages from the server will give us a clue to the location of the logs
Status: Resolving address of www.mikeefranklin.co.ukStatus: Connecting to 65.49.60.84:21...Status: Connection established, waiting for welcome message...Response: 220 (vsFTPd 2.2.2)Command: USER <?php phpinfo(); ?>
→ logs likely to be at /var/log/vsftpd.log
Write to the database
Another possibility is writing our code directly into a database. We can then attempt to include the database file to execute our code.
We can guess the location of the file
Knowing the database name will help us find the path to the database
..but we cant use LFI to read the database config, because the PHP get will executed..
… but we can use the php filter wrapper to help read it.
index.php?file=php://filter/convert.base64-encode/resource=config.php
This will output the file base64 encoded, which we can then decode.
If SQL injection is available, we can use it to retrieve the database path
Writing code using SQL injection
If the database permissions allow, we can write our code using SQL injection and “outfile”.
Select '<?php phpinfo(); ?>' into outfile '/tmp/myfile'
Now we can call..
index.php?file=../../../../../../../../tmp/myfile%00
SQL Injection
SQL Injection is common. Make sure all parameters are parsed
Can extract data we shouldn't be able to get to
Can potentially log in as different users
Maybe read files off the server
Maybe even execute our own code
Conclusions
Don't blindly trust open source. Even popular platforms can have vulnerabilities, and if they don't, their plugins/modules might.
Don't trust any user input. GET, POST, COOKIES.etc.
PDO prepare is your friend
Correctly check file extensions
Never give Apache or your MySQL user more permissions than they need
Keep an eye on news regarding new exploits