Click here to Skip to main content
15,907,906 members
Home / Discussions / Linux, Apache, MySQL, PHP
   

Linux, Apache, MySQL, PHP

 
Questionp0wned Pin
Christian Graus16-Jun-08 15:02
protectorChristian Graus16-Jun-08 15:02 
AnswerRe: p0wned Pin
Scott Dorman16-Jun-08 17:21
professionalScott Dorman16-Jun-08 17:21 
JokeRe: p0wned Pin
Leslie Sanford16-Jun-08 20:42
Leslie Sanford16-Jun-08 20:42 
QuestionFirst post !!! Pin
Christian Graus16-Jun-08 15:01
protectorChristian Graus16-Jun-08 15:01 
AnswerRe: First post !!! Pin
Scott Dorman16-Jun-08 17:20
professionalScott Dorman16-Jun-08 17:20 
AnswerRe: First post !!! Pin
Harvey Saayman17-Jun-08 1:19
Harvey Saayman17-Jun-08 1:19 
GeneralRe: First post !!! Pin
Paul Coldrey7-Oct-08 15:16
professionalPaul Coldrey7-Oct-08 15:16 
QuestionSQL Injection Prevention - How Good Are These Measures? Pin
nalorin11-Jun-08 7:55
nalorin11-Jun-08 7:55 
Some background: I have taken a few measures to prevent SQL injection on my PHP/MySQL setup (currently WAMP for development, but will be LAMP for production server):
- In my users table, I have entered a "bad" user, with all the fields equal to 0.
    - this is the first user in the table
    - if a hacker tries to enter ' or ''=' in the uname field:
        - (theoretically,) "bad" user will be first result, and
        - (theoretically,) # of results will also be greater than 1 (more than 1 user)
    - (theoretically,) the following code will prevent said hacker from gaining unauthorized access
- mysql_real_escape_string() function will be used to escape input when site is launched, but right now it is not in use to allow testing of common SQL injection methods.
    - I have read that mysql_real_escape_string() has some vulnerabilities.
    - I know mysql_real_escape_string() is more secure than addslashes().

<span style="color: green">/* // This block will be uncommented after development
$u = mysql_real_escape_string($_POST[uname]);
$p = mysql_real_escape_string($_POST[pass]); */

// These lines will be replaced by commented block above after development</span>
<span style="color: blue">$u</span> = <span style="color: blue">$_POST[uname]</span>;
<span style="color: blue">$p</span> = <span style="color: blue">$_POST[pass]</span>;

<span style="color: green">// I could add " order by uid asc" to the following query, to ensure 'bad' user listed first if SQL injection occurs, and to prevent hacker from using order by</span>
<span style="color: blue">$query</span> = <span style="color: red">"select uid,uname,fname,lname,email,phone,other,pass from ads.users where uname = '$u'"</span>;
<span style="color: blue">$result</span> = mysql_query(<span style="color: blue">$query</span>);
<span style="color: blue">$rows</span> = mysql_num_rows(<span style="color: blue">$result</span>);

<span style="color: green">// if more than 1 row, SQL injection attempted (uname has 'unique' flag in database, so only safe to return 1 row)</span>
if (<span style="color: blue">$rows</span> > 1) {
    <span style="color: green">/* <<Do Stuff - Security Measures (ban IP, etc)>> */</span>
    die (<span style="color: red">"Error[20]: You have entered potentially harmful input. Security measures have been put in place until this incident can be reviewed."</span>);
}

<span style="color: blue">$record</span> = mysql_fetch_assoc(<span style="color: blue">$result</span>);
<span style="color: blue">$passQuery</span> = <span style="color: red">"select password('$p') = '$result[pass]'"</span>;

if (<span style="color: blue">$rows</span> == 1 && mysql_num_rows(mysql_query(<span style="color: blue">$passQuery</span>))) {
    if (<span style="color: blue">$record[uid]</span> == 0) {
    <span style="color: green">// if they try to use the "bad" user account I setup as uid 0, or if they somehow manage to get past $rows > 1 statement on SQL injection attempt</span>
        die (<span style="color: red">"Error[25]: You have entered input that could be harmful to the site. Security measures have been put in place until this incident can be reviewed."</span>);
    }

    <span style="color: blue">$l</span> = 1; <span style="color: green">// user is now logged in</span>
    
    <span style="color: green">// the following session information is stored for previewing ads when users create them</span>
    <span style="color: blue">$_SESSION[uid]</span>   = <span style="color: blue">$record[uid]</span>;
    <span style="color: blue">$_SESSION[uname]</span> = <span style="color: blue">$record[uname]</span>;
    <span style="color: blue">$_SESSION[fname]</span> = <span style="color: blue">$record[fname]</span>;
    <span style="color: blue">$_SESSION[lname]</span> = <span style="color: blue">$record[lname]</span>;
    <span style="color: blue">$_SESSION[phone]</span> = <span style="color: blue">$record[phone]</span>;
    <span style="color: blue">$_SESSION[email]</span> = <span style="color: blue">$record[email]</span>;
} else <span style="color: blue">$loginError</span> .= <span style="color: red">"Error: Invalid username and/or password."</span>;


I dunno... maybe I'm just paranoid... I just want to make sure to CMA to prevent liability problems, since this will be a commercial site.

P.S. I hope the markup helps read my programming - I know my lines tend to be fairly long...

"Silently laughing at silly people is much more satisfying in the long run than rolling around with them in a dusty street, trying to knock out all their teeth. If nothing else, it's better on the clothes." - Belgarath (David Eddings)

AnswerRe: SQL Injection Prevention - How Good Are These Measures? Pin
Bradml11-Jun-08 21:36
Bradml11-Jun-08 21:36 
GeneralRe: SQL Injection Prevention - How Good Are These Measures? Pin
Mohammad Dayyan11-Jun-08 22:57
Mohammad Dayyan11-Jun-08 22:57 
GeneralRe: SQL Injection Prevention - How Good Are These Measures? Pin
Chris Maunder12-Jun-08 0:20
cofounderChris Maunder12-Jun-08 0:20 
GeneralRe: SQL Injection Prevention - How Good Are These Measures? Pin
Bradml12-Jun-08 0:58
Bradml12-Jun-08 0:58 
GeneralRe: SQL Injection Prevention - How Good Are These Measures? Pin
Hesham Amin13-Jun-08 0:09
Hesham Amin13-Jun-08 0:09 
AnswerRe: SQL Injection Prevention - How Good Are These Measures? Pin
alex.barylski1-Jul-08 10:29
alex.barylski1-Jul-08 10:29 
QuestionSmarty Question !!! Pin
Mohammad Dayyan11-Jun-08 1:40
Mohammad Dayyan11-Jun-08 1:40 
AnswerRe: Smarty Question !!! Pin
Bradml11-Jun-08 21:37
Bradml11-Jun-08 21:37 
GeneralRe: Smarty Question !!! Pin
Mohammad Dayyan11-Jun-08 22:44
Mohammad Dayyan11-Jun-08 22:44 
GeneralRe: Smarty Question !!! Pin
Bradml11-Jun-08 22:57
Bradml11-Jun-08 22:57 
GeneralRe: Smarty Question !!! Pin
Mohammad Dayyan11-Jun-08 23:03
Mohammad Dayyan11-Jun-08 23:03 
GeneralRe: Smarty Question !!! Pin
Bradml12-Jun-08 0:55
Bradml12-Jun-08 0:55 
GeneralRe: Smarty Question !!! Pin
Mohammad Dayyan12-Jun-08 2:27
Mohammad Dayyan12-Jun-08 2:27 
QuestionImage is not being generated... Pin
xelios4-Jun-08 20:42
xelios4-Jun-08 20:42 
AnswerRe: Image is not being generated... Pin
Bradml4-Jun-08 22:58
Bradml4-Jun-08 22:58 
AnswerRe: Image is not being generated... Pin
Mohammad Dayyan5-Jun-08 9:48
Mohammad Dayyan5-Jun-08 9:48 
QuestionAny simple example to implement CAPTCHA... Pin
xelios4-Jun-08 10:53
xelios4-Jun-08 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.