r/PHPhelp • • Sep 23 '19

How do you prevent XSS and Code Injection using PHP?

8 Upvotes

29 comments sorted by

4

u/CyberJack77 Sep 23 '19

Some basic rules (when handling text data):

  1. Never ever trust (user) input.

  2. Prevent SQL injections by using tings like prepare statements and never try to place data inside a query manually.

  3. Like said before: Sanitization.
    But sanitize only in the correct places. Since you don't know what you will be doing with given data in the future, keep your stored data (in a database) the same as the original data (so don't sanitize). But when displaying the data, you always need to sanitize it.
    Note: A XSS script can do no harm inside a database (be sure to prevent sql injections though), but it can do harm inside the users browser.
    Note2: If you want to analyze a (potential) attack, the original source will help way more than a sanitized string.

When working with files, make sure they are stored outside the document root (which will prevent direct access) and always process uploaded images (which will remove any scripts hidden inside images).

There are a lot of other things that can cause problems (like path traversal), but most are explained at the already mentioned OWASP website.

1

u/PewPaw-Grams Sep 23 '19

If I don't place data in query manually, how am I going to query the data to database?

3

u/akas84 Sep 23 '19

Use pdo with binding params 👌

1

u/PewPaw-Grams Sep 23 '19

I'm sorry, I'm new to all these. What does it even mean? What is pdo and how can I do binding params?

2

u/akas84 Sep 23 '19

https://www.php.net/manual/en/book.pdo.php here you have all the documentation with examples.

1

u/PewPaw-Grams Sep 23 '19

Thank you very much. Appreciate it

2

u/freebytes Sep 23 '19

This is one of the most important links posted for your questions. Always use parameterized queries when interacting with the database.

1

u/PewPaw-Grams Sep 23 '19

Agree. I'll practice more and use parametrised queries

3

u/Fugaki Sep 23 '19

Prepared statements. Instead of

$q="select * from foo where bar='$baz'"

You use whatever prepared statement method is available, and write

$foo="select * from foo where bar = ?"

Then pass $baz as the parameter to the prepare method.

For example:

https://www.php.net/manual/en/pdostatement.execute.php

That's not the only way to do it but that should get you started.

3

u/Wiwwil Sep 23 '19

https://johnmorrisonline.com/prevent-xss-attacks-escape-strings-in-php/

echo htmlspecialchars($string, ENT_QUOTES, 'UTF-8');

// OR

// echo htmlentities($string, ENT_QUOTES, 'UTF-8');

Basically this

1

u/PewPaw-Grams Sep 23 '19

That easy? If that's the case, why is it that websites out there are so easy to hack and deface?

3

u/Wiwwil Sep 23 '19

It is easy to make a working website with PHP, even without knowledge. Easy to host as well. So beginners just write some code and boom that's on the web. It's fine in local and in the learning process, but they don't bother to learn any security related stuff. Old code most likely. Not using CMS or a framework. In the world there is PHP and PHP. Those easy to hack website are not called PHP for me.

5

u/PewPaw-Grams Sep 23 '19

What are they called then? Target practice?

2

u/Wiwwil Sep 23 '19

That's the spirit

2

u/[deleted] Sep 23 '19

Use frameworks, because they already have some preventions which you can use...

2

u/PewPaw-Grams Sep 23 '19

Which frameworks to use? The reason why I'm asking is to learn how to prevent it by coding it myself

4

u/Wiwwil Sep 23 '19

Symfony 4 or Laravel are the top frameworks as of now.

2

u/geosparks Sep 25 '19

Here is a video about XSS, what it does and how to prevent it if you are doing your own vanilla PHP.
https://youtu.be/CPeTGdRTYT4

5

u/ddrght12345 Sep 23 '19

Sanitization. Never trusting input. Ever.
htmlspecialchars or htmlentities will get you close, but ultimately, it's still too context specific.

Code injection is a pretty broad subject. SQL injection, directory traversal/injection, object injection, etc all have different prevention measures.

Your best bet for "how" is to just read up and learn about the different ways to "do". Check out OWASP. They have tons of examples.

1

u/crazedizzled Sep 23 '19

Twig

-1

u/PewPaw-Grams Sep 23 '19

WHAT!?!?!? Are you mocking me?

3

u/crazedizzled Sep 23 '19

No? Twig is a templating library which basically eliminates XSS from being a concern. On top of that, use a good Content-Security-Policy and you're set.

1

u/barvid Sep 24 '19

This was a great example of why explanations are needed instead of one-word answers.

1

u/[deleted] Sep 23 '19

Using a framework and escape everything

1

u/tanat0s Sep 23 '19

Yep...I started with twig recently

0

u/[deleted] Sep 23 '19

[deleted]

5

u/ddrght12345 Sep 23 '19

I'm sorry... NEVER, EVER tell someone to not sanitize server side!

Ever.

Client side is fine and all. UI/UX, usability, immediate feedback, and what not. But you should ALWAYS sanitize server side. I own everything I'm sending you, and I can personally guarantee, if I'm targeting your site, I won't be using a (traditional) browser. Your client side code won't be executed. Even if I was using a normal browser, it's stupid trivial to stop it.

Geezes, PLEASE learn more about how HTTP requests work before recommending advice like this.