1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Forms security

Discussion in 'Security' started by Vizuke, Sep 8, 2006.

  1. #1
    What are some tips you know about adding security to processing forms such as registration forms, generators, login forms, contact forms, etc? This is because I am thinking that a basic PHP form can be too unsecured from exploits such as spam submitting, spam registration, etc..
     
    Vizuke, Sep 8, 2006 IP
  2. wmburg

    wmburg Active Member

    Messages:
    300
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Never trust your users, Validate everything, Initialize your variables, Check user privileges on every page if you're using access control, Understand XSS, Understand SQL Injection, Do not display PHP errors to users, Turn off register_globals (defaulted to off in php 4.2.0+), CAPTCHA for spam.
     
    wmburg, Sep 8, 2006 IP
  3. Vizuke

    Vizuke Peon

    Messages:
    339
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I looked up XSS and SQL Injection and found some loopholes in my SQL forms and codings. But it is possible to do an SQL Injection through my forms.

    Here's my filtering function

    <?
    
    //login $_POST['username']
    //password $_POST['password']
    
    //I passed $_POST through smart_quotes first before sending to SQL query.
    
    //$_POST=mcheck($_POST)
    
    //after mcheck(), do the SQL queries...
    
    
    function mcheck($value) {
    	if(is_array($value)) {
    		if(get_magic_quotes_gpc()) {
    			$value=array_map("stripslashes",$value);
    			}
    		if(!array_map("is_numeric",$value)) {
    			$value=array_map("mysql_real_escape_string",$value);
    			}
    		}
    	else {
    		if(get_magic_quotes_gpc()) {
    			$value=stripslashes($value);
    			}
    		if(!is_numeric($value)) {
    			$value="'" . mysql_real_escape_string($value) . "'";
    			}
    		}
    	return $value;
    	}
    ?>
    
    PHP:
    I don't know if I am doing it right, can you explain what I have wrong?
     
    Vizuke, Sep 9, 2006 IP
  4. Vizuke

    Vizuke Peon

    Messages:
    339
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here's my SQL query

    
    $sql="SELECT * FROM members WHERE `username`='$username' AND `password`='$password";
    
    PHP:
     
    Vizuke, Sep 9, 2006 IP
  5. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #5
    anytime you use a variable to update a piece of a mysql_query, USE mysql_real_escape_string ON EVERY VARIABLE that the user has an opportunity to manipulate!

    IE
    mysql_query("SELECT * FROM books WHERE book_title = '".mysql_real_escape_string($_POST['book_title'])."'");
     
    drewbe121212, Sep 11, 2006 IP
  6. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Oh, also, never output and input variable to the output (like as an error). This opens it up for XSS
     
    drewbe121212, Sep 11, 2006 IP
  7. Vizuke

    Vizuke Peon

    Messages:
    339
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The variable is passed through my function shown above which calls the mysql_real_escape_string() but I can still do an SQL injection.
     
    Vizuke, Sep 11, 2006 IP
  8. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This libary gathers together various open source pieces into an app which can be included into the top level of a PHP app and which then tries to purify all input variables.

    I have not thoroughly tested it against MySql injection and would be interested to know if it also solves that problem. The link is http://www.stat-communications.com/security/
     
    clancey, Sep 11, 2006 IP
  9. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #9
    How is injection still available through that. I can understand XSS Flaw, definately, but Injection?!?!?!?
     
    drewbe121212, Sep 11, 2006 IP
  10. Vizuke

    Vizuke Peon

    Messages:
    339
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    With the mysql_real_escape_string() passed, I entered something like

    <script>alert("hello")</script>
    Code (markup):
    as a username which shows up a javascript error on the window and popups the alert box.
     
    Vizuke, Sep 12, 2006 IP
  11. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #11
    mysql_real_escape_string() is enough for queries but before you print that, use "htmlspecialchars();" or "htmlentities();".

    For example:

    echo htmlspecialchars($username);
     
    SoKickIt, Sep 12, 2006 IP
  12. bbqchips

    bbqchips Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Some general security tips:

    create two functions for untrusted data:

    one that puts it through mysql_real_escape_string() before using it in database queries
    one that puts it through htmlentities() before outputting it

    Always use them whenever you deal with untrusted information.

    Also, never code with register_globals on. If your server must have register_globals on, always have unique names for session variables (such as SESS_varname) and always declare a variable before using it (ex: $output = ''; $output .= 'hello!'; )
     
    bbqchips, Sep 12, 2006 IP
  13. Vizuke

    Vizuke Peon

    Messages:
    339
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Vizuke, Sep 12, 2006 IP
  14. bbqchips

    bbqchips Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    No, you can use $_GET['varname']; and $_POST['varname']; to access variables sent to your script with register globals off.

    More reading:

    http://ca.php.net/manual/en/reserved.variables.php#reserved.variables.get

    http://ca.php.net/register_globals
     
    bbqchips, Sep 13, 2006 IP
  15. Mrblogs

    Mrblogs Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Best to probably use strip_tags() as well as mysql_escape_string() if you do not wish them to enter Javascript or HTML characters.
     
    Mrblogs, Sep 13, 2006 IP
  16. explorer

    explorer Well-Known Member

    Messages:
    463
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    110
    #16
    Collect the IP addresses of submissions using:

    $ip = $_SERVER['REMOTE_ADDR'];
    Code (markup):
     
    explorer, Sep 29, 2006 IP
  17. Brian Kim

    Brian Kim Well-Known Member

    Messages:
    480
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #17
    if you have root access to your server, a good thing to do is install mod_security . It helps tons with spam via forms
     
    Brian Kim, Oct 5, 2006 IP
  18. shaz_again

    shaz_again Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    hmmmmmmmmm, that really helping stuff bcoz i m learning PHP & this will help me alot.
     
    shaz_again, Dec 11, 2007 IP
  19. SSANZ

    SSANZ Peon

    Messages:
    861
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Give me a PM when you have secured your box, I can get my team to scan it free of charge. ( just for a curiosity point for the members of the forums ) as we can test before and after effects of mod_security with default security rules.
     
    SSANZ, Dec 12, 2007 IP
  20. ven123

    ven123 Banned

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #20
    download a php validation class from phpclasses.org
     
    ven123, Dec 12, 2007 IP