auditing and securing php applications - frhack 2009

33
Auditing PHP applications Audit des applications en PHP 1 vendredi 13 novembre 2009

Upload: philippe-gamache

Post on 19-Jun-2015

845 views

Category:

Technology


0 download

DESCRIPTION

This presentation is biligual (french/english). In this laboratory, we will carry out a safety audit on the code of a web application. The technical objective is to provide a report and treat all phases of investigative work: source analysis, identifying vulnerabilities (XSS, injections, disclosure, etc.), recommendations for strengthening, and prioritization of tasks.

TRANSCRIPT

Page 2: Auditing and securing PHP applications - FRHACK 2009

Agenda Ordre du jourWorkshop presentation

Black box audit

Source code audit

Présentation de l’atelier

Audit boîte noire

Audit à code ouvert

2vendredi 13 novembre 2009

Page 3: Auditing and securing PHP applications - FRHACK 2009

Who speak? Qui parle?Philippe Gamache

Parler Haut, Interagir Librement : Web development, security audit, training

[email protected]

@SecureSymfony

Philippe Gamache

Parler Haut, Interagir Librement : Développement Web, audit de sécurité, formations

[email protected]

@SecureSymfony

3vendredi 13 novembre 2009

Page 4: Auditing and securing PHP applications - FRHACK 2009

Security bookLivre de sécurité

New 2009 editionComprehensive review of security system for MySQL, PHP, etc. Published in French

Planning translation

Nouvelle édition 2009Bilan complet de la sécurité : système, MySQL, PHP, etc.Édité chez EyrollesDédicaces sur demande

4vendredi 13 novembre 2009

Page 5: Auditing and securing PHP applications - FRHACK 2009

Yes, I take questionJe réponds aux questions

5vendredi 13 novembre 2009

Page 6: Auditing and securing PHP applications - FRHACK 2009

Why? Pourquoi?Confidence does not exclude control™

To validate external work

To validate internal work

Round-up

For an external perspective

As often as possible

La confiance n'exclut pas le contrôle™

Pour valider un travail externePour valider un travail internePour faire le point Pour avoir un regard externe Aussi souvent que possible

6vendredi 13 novembre 2009

Page 7: Auditing and securing PHP applications - FRHACK 2009

Full audit synopsis Synopsis d’un audit

Identification of the audit goals

Interview with the development teams

Black box testing

Open Code audit

Report

Identification des objectifs d'audit

Entretien avec les équipes de développement

Test boîte noire

Audit de code

Rapport

7vendredi 13 novembre 2009

Page 8: Auditing and securing PHP applications - FRHACK 2009

Identification of the audit goals Identification des objectifs d’audit

Audit scope

Security

Performance

Code quality

Étendue de l'audit

Sécurité

Performance

Qualité du code

8vendredi 13 novembre 2009

Page 9: Auditing and securing PHP applications - FRHACK 2009

Interview with the development teamsEntretien avec les équipes de développement

Check if development teams knows what to secure

Have the design explained first

Have them explains their approach

Check what they say

Check what they don’t say

Vérifiez si l’équipe de développement sait ce qu’il faut sécurisé

Demandez d’expliquer la conception

Demandez d’expliquer leur approche

Vérifiez ce qu'ils disent

Vérifiez ce qu'ils ne disent pas

9vendredi 13 novembre 2009

Page 10: Auditing and securing PHP applications - FRHACK 2009

The shy versionLa version timide

We know there are security problems

but we have no time to secure them

this app has been written years ago

we can’t keep up with the threats

Nous savons qu’il y a des problèmes de sécurité :

mais nous n'avons pas le temps de les sécuriser

ce logiciel a été écrit il y a quelques années

nous ne pouvons pas suivre les menaces

10vendredi 13 novembre 2009

Page 11: Auditing and securing PHP applications - FRHACK 2009

The strong versionLa version forte

We have secured the application

We use SSL and crypto

All content is validated and filtered

We don’t do any dynamical include

Our frameworks doesn’t allow this

Nous avons sécurisé l'application

Nous utilisons le protocole SSL et la cryptographie Tout le contenu est validé et filtré Nous ne faisons aucune inclusion dynamique Nos cadres d’applications sont sécurisées

11vendredi 13 novembre 2009

Page 12: Auditing and securing PHP applications - FRHACK 2009

Black box testingTest boîte noire

Easy to set up

Take into account the context of the application

Often spectacular

Generally shallow

Facile de mise en place

Prendre en compte le contexte de la demande

Souvent spectaculaires

Généralement peu profondes

12vendredi 13 novembre 2009

Page 13: Auditing and securing PHP applications - FRHACK 2009

Black box testingTest boîte noire

Finding informations

Look for vulnerabilities

Automatic scanners

By hand

Fuzzing

Scenarios

Strikes

Recherche d’informations

Rechercher les vulnérabilités

Scanneurs automatiques

À la main

Fuzzing

Scénarios

Attaquer

13vendredi 13 novembre 2009

Page 14: Auditing and securing PHP applications - FRHACK 2009

Black box testingTest boîte noire

Usual directories

includes, include, inc, com, classes, lib, library

admin, adm, administrator

tmp, TMP, ext, var

data, db, conf, config

uploads, install

Répertoires habituels

includes, include, inc, com, classes, lib, library

admin, adm, administrateur

tmp, TMP, ext, var

data, db, conf, config, configuration

uploads, install

14vendredi 13 novembre 2009

Page 15: Auditing and securing PHP applications - FRHACK 2009

Black box testingTest boîte noire

Typical files

.phps, .inc, .class

xml, ini, yaml, cfg

.tar, .gz, .zip, .rar, .bz2

Apache Alias : /icons/

robots.txt

Fichiers typiques

.phps, .inc, .class

xml, ini, yaml, cfg

.tar, .gz, .zip, .rar, .bz2

Apache Alias : /icons/

robots.txt

15vendredi 13 novembre 2009

Page 16: Auditing and securing PHP applications - FRHACK 2009

Open Code auditAudit de code

Look into the PHP code

Search for hidden problems

Usually less spectacular than black box

Easy to loose focus

Tempting to audit everything

Rechercher dans le code PHP

Rechercher les problèmes cachés

Habituellement, moins spectaculaire que les tests boîte noire

Facile de perdre sa concentration

Tentant de tout vérifier

16vendredi 13 novembre 2009

Page 17: Auditing and securing PHP applications - FRHACK 2009

Approch ApprocheWhat to search for?

What are the entry points?

What are the exit points?

Open-mindedness and discovery

How can they be exploited

Or protected ?

Que rechercher?

Quels sont les points d'entrée?

Quels sont les points de sortie?

Ouverture d’esprit et découverte

Comment peuvent-ils être exploités

Ou protégés?

17vendredi 13 novembre 2009

Page 18: Auditing and securing PHP applications - FRHACK 2009

Assessing the codeÉvaluer le code

One liners One line of code is sufficiently to be bad

Even thoughyou must follow the code

forward (for input) in reverse (for output)

En une ligneUne ligne de code est suffisamment pour être mauvais

Malgré tous vous devez suivre le code

vers l’avant (entrées)sens inverse (sorties)

<?php $action = $_POST['action']; $query_string = "action=$action"; $link = "index.php?$query_string"; ?> <a href="<?php echo $link; ?>"> Click Here </a>

18vendredi 13 novembre 2009

Page 19: Auditing and securing PHP applications - FRHACK 2009

What to search for?Que rechercher?

Injections

PHP

SQL

HTML

system

Injections

PHP

SQL

HTML

système

19vendredi 13 novembre 2009

Page 20: Auditing and securing PHP applications - FRHACK 2009

Tools OutilsYour eyes

Text editors

grep

Fast, available, convenient

Tokenizer

Semantic, accurate

Vos yeux

Éditeurs de texte

grep

Rapide, disponible, pratique

Tokenizer

Sémantique, précis

20vendredi 13 novembre 2009

Page 21: Auditing and securing PHP applications - FRHACK 2009

Tokenizer [1] => Array ( [0] => 266 [1] => print [2] => 1 )

[2] => Array ( [0] => 370 [1] => [2] => 1 )

[3] => ( [4] => " [5] => Array ( [0] => 314 [1] => hello [2] => 1 )

[6] => Array ( [0] => 309 [1] => $world [2] => 1 )

[7] => Array ( [0] => 314 [1] => ! [2] => 1 )

[8] => " [9] => ) [10] => ;

[1] => Array ( [0] => PHP token [1] => PHP code [2] => Script line ) [2] => "

<?php print ("hello $world!"); ?>

21vendredi 13 novembre 2009

Page 22: Auditing and securing PHP applications - FRHACK 2009

PHP injectionsInjections PHP

dynamical inclusion

include, require and *_once

back ticks (`)

eval

Inclusions dynamiques d'inclusion

include, require and *_once

`

eval

22vendredi 13 novembre 2009

Page 23: Auditing and securing PHP applications - FRHACK 2009

Input EntréesHTML Forms:

form

input

$_GET

$_POST

$_REQUEST

Formulaire HTML:

form

input

$_GET

$_POST

$_REQUEST

23vendredi 13 novembre 2009

Page 24: Auditing and securing PHP applications - FRHACK 2009

Input EntréesDatabases:

mysql_query()

SELECT

HTTP Headers:

$_COOKIE

$_SERVER

Bases de données:

mysql_query()

SELECT

Entête HTTP:

$_COOKIE

$_SERVER

24vendredi 13 novembre 2009

Page 25: Auditing and securing PHP applications - FRHACK 2009

Input Entréesregister_globals strikes back

Foreach and $$

extract

import_request_var

$GLOBALS

parse_str

(ini_get(‘register_globals’))

register_globals contre-attaque

Foreach and $$

extract

import_request_var

$GLOBALS

parse_str

(ini_get(‘register_globals’))

25vendredi 13 novembre 2009

Page 26: Auditing and securing PHP applications - FRHACK 2009

Output SortiesClient:

echo

print

<?=

die

print_r

var_dump

Client:

echo

print

<?=

die

print_r

var_dump

26vendredi 13 novembre 2009

Page 27: Auditing and securing PHP applications - FRHACK 2009

Output SortiesDatabases:

mysql_query()

mysqli_multi_query

update, delete, insert

Headers

header()

Bases de données:

mysql_query()

mysqli_multi_query

update, delete, insert

En-têtes

header()

27vendredi 13 novembre 2009

Page 28: Auditing and securing PHP applications - FRHACK 2009

Output SortiesSystem:

exec()

passthru()

system()

Système:

exec()

passthru()

system()

28vendredi 13 novembre 2009

Page 29: Auditing and securing PHP applications - FRHACK 2009

Gotchas PiègesTrust of HTTP Headers:

Referer

Trust of $_SERVER:

$_SERVER['PHP_SELF']

Trust of Client-Side Restrictions:

maxlength

Confiance des en-têtes HTTP :

Referer

Confiance de $_SERVER :

$_SERVER['PHP_SELF']

Confiance des restrictions clients:

maxlength

29vendredi 13 novembre 2009

Page 30: Auditing and securing PHP applications - FRHACK 2009

Report Rapport

Vulnerability Critical Load

register_globals High High

Injections High Medium

SQL injection Medium High

headers Low Low

30vendredi 13 novembre 2009

Page 32: Auditing and securing PHP applications - FRHACK 2009

32vendredi 13 novembre 2009

Page 33: Auditing and securing PHP applications - FRHACK 2009

33vendredi 13 novembre 2009