Jump to content

Security of a guestbook

- - - - -

  • Please log in to reply
13 replies to this topic

#1
hoku_2000 _99

hoku_2000 _99

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
I've made a guestbook, right now it works fine, but the downside is that it can easily be hacked. How can I encode the special characters so that it doesnt get hacked?

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 856 posts
  • Location:Arkansas
What language and what database are you using for the backend? If it's PHP and MySQL, you can use mysqli_real_escape_string.
Doc: PHP: mysqli::real_escape_string - Manual
Or you could use prepared statements.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
hoku_2000 _99

hoku_2000 _99

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Yes, I am using PHP an MySQL. This is my first time using mysqli_real_escape_string, but I found an example.
PHP mysql_real_escape_string() Function Is the proper way to use mysqli_real_escape_string?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
If you are using the very old defunct mysql_* extension set then mysql_real_escape string is required. If you are using the newer mysqli_* (i for improved) extension set, then the suggested mysqli_real_escape_string (or its object-oriented counterpart, see linked manual) is suggested for consistency.

If you use bound statements with MySQLi, or alternatively using PDO instead, you will not even need to escape due to the fact it treats data separately from the query itself, if used properly.

For encoding characters, you may wish to disallow HTML with htmlentities() or similar, if you wish to filter and allow specific HTML (i.e. b, i, u tags) then certainly use a filtering software such as HTML Purifier that is dedicated to security.

Feel free to enquire about further security.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
hoku_2000 _99

hoku_2000 _99

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Now, I just had someone suggest to me that I shouldnt use mysql_real_escape_string because I dont have users logging in to my database. Is it better use mysql_real_escape_string for a guestbook like I have?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
If variables or substitutions are being used in your SQL queries, and they are not solid and hard coded strings, then use proper sanitisation.

You yourself can cause a messy database error, if you forget to manually escape an apostrophe.

The only time you should not escape yourself is when you are using bound parameters with a proper SQL library (mysqli, PDO, adodb)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
hoku_2000 _99

hoku_2000 _99

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
So, its ok to not use mysql_real_escape_string if I am just asking the user for a name, comment, and captcha code?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Assume ALL data submitted by a user is malicious, until proven otherwise.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
hoku_2000 _99

hoku_2000 _99

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Its wise to use mysql_real_escape_string then?

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
If you're sending data to the database, yes. Hackers aren't going to use a browser, they'll send raw data that contains whatever they want.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 856 posts
  • Location:Arkansas
I have a policy when I'm writing code: Nothing touches my database unless it's gone through mysqli_real_escape_string first.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#12
hoku_2000 _99

hoku_2000 _99

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Ok. Now my next question is, I am not sure, how to use mysql_real_escape_string. This is my first time using it. Could I have an example, please?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users