This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org
Difference between revisions of "OWASP Backend Security Project PHP Security Programming"
From OWASP
| Line 1: | Line 1: | ||
= Overview = | = Overview = | ||
| − | |||
| − | == | + | == Example 1 == |
| − | '' | + | Here follows a tipical Login Forms to authenticate user. Such a credentials are stored on a backend Database Server whose connection parameters are stored in a ''.inc'' file. |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| + | '''auth.php'''<nowiki> | ||
| − | |||
| − | |||
| − | |||
| − | |||
<?php | <?php | ||
include('./db.inc'); | include('./db.inc'); | ||
| Line 45: | Line 34: | ||
if ($sUserName = sAuthenticateUser($_POST["username"], | if ($sUserName = sAuthenticateUser($_POST["username"], | ||
$_POST["password"])) { | $_POST["password"])) { | ||
| − | + | /* successfull authentication code goes here */ | |
| + | ... | ||
| + | ... | ||
} else { | } else { | ||
| − | + | /* unsuccessfull authentication code goes here */ | |
| + | ... | ||
| + | ... | ||
} | } | ||
| Line 53: | Line 46: | ||
</nowiki> | </nowiki> | ||
| − | '''db.inc | + | |
| − | + | '''db.inc'''<nowiki> | |
<?php | <?php | ||
| Line 76: | Line 69: | ||
?></nowiki> | ?></nowiki> | ||
| − | |||
| − | |||
| − | |||
| − | + | == Example 2 == | |
| + | The following sample code cames from a online book catalog. | ||
| + | '''getbook.php''' <nowiki> | ||
| − | |||
| − | |||
| − | |||
function aGetBookEntry($id) { | function aGetBookEntry($id) { | ||
$aBookEntry = NULL; | $aBookEntry = NULL; | ||
| Line 100: | Line 89: | ||
} | } | ||
} | } | ||
| − | |||
return $aBookEntry; | return $aBookEntry; | ||
| − | |||
} | } | ||
| − | + | .... | |
$id = $_GET['id']; | $id = $_GET['id']; | ||
$aBookEntry = aGetBookEntry($id); | $aBookEntry = aGetBookEntry($id); | ||
| − | + | /* Display retrieved book information */ | |
| + | ... | ||
| + | ... | ||
| + | |||
</nowiki> | </nowiki> | ||
| − | |||
| − | |||
| − | + | = Description = | |
| − | + | ||
| + | == PHP preventing SQL Injection == | ||
| + | |||
| + | === Escaping Quotes === | ||
| + | |||
| + | === Prepared Statements === | ||
| + | |||
| + | === Data Validation === | ||
| + | |||
| + | === Detecting Intrusions from WEBAPP === | ||
== PHP preventing LDAP Injection == | == PHP preventing LDAP Injection == | ||
| + | |||
| + | === Data Validation === | ||
| + | |||
| + | == Defeating Automated Tools == | ||
= References = | = References = | ||
Revision as of 22:21, 28 May 2008
Overview
Example 1
Here follows a tipical Login Forms to authenticate user. Such a credentials are stored on a backend Database Server whose connection parameters are stored in a .inc file.
auth.php
<?php
include('./db.inc');
function sAuthenticateUser($username, $password){
$authenticatedUserName="";
if ($link = iMysqlConnect()) {
$query = "SELECT username FROM users";
$query .= " WHERE username = '".$username."'";
$query .= " AND password = md5('".$password."')";
$result = mysql_query($query);
if ($result) {
if ($row = mysql_fetch_row($result)) {
$authenticatedUserName = $row[0];
}
}
}
return $authenticatedUserName;
}
if ($sUserName = sAuthenticateUser($_POST["username"],
$_POST["password"])) {
/* successfull authentication code goes here */
...
...
} else {
/* unsuccessfull authentication code goes here */
...
...
}
?>
db.inc
<?php
define('DB_HOST', "localhost");
define('DB_USERNAME', "user");
define('DB_PASSWORD', "password");
define('DB_DATABASE', "owasp");
function iMysqlConnect(){
$link = mysql_connect(DB_HOST,
DB_USERNAME,
DB_PASSWORD);
if ($link && mysql_select_db(DB_DATABASE))
return $link;
return FALSE;
}
?>
Example 2
The following sample code cames from a online book catalog.
getbook.php
function aGetBookEntry($id) {
$aBookEntry = NULL;
$link = iMysqlConnect();
$query = "SELECT * FROM books WHERE id = $id";
$result = mysql_query($query);
if ($result) {
if ($row = mysql_fetch_array($result)) {
$aBookEntry = $row;
}
}
return $aBookEntry;
}
....
$id = $_GET['id'];
$aBookEntry = aGetBookEntry($id);
/* Display retrieved book information */
...
...