-1

I am trying to develop a web application where the user enters data and retrieves it in a formatted manner in various sub applications within my site. Basically, it's a form heavy application. I use PHP, HTML and MySQL to develop it. I am hoping I can make it commercial one day. I am good enough to make the site function but As I develop it more, I want to think more about security. I also have an admin dashboard where I control and manage the main application.

So there are two applications, 2 MYSQL databases and information sharing between databases and tables. Programming language is not advanced at all (probably barely rookie level)

  1. For Forms, before I send entered data to mysql to store, I use following;

    $lname = stripslashes($_REQUEST['lname']);
    $lname = mysqli_real_escape_string($concfc, $lname);
    

    Before I develop more forms, should I implement something else to increase? What other methods in general I can follow to increase security?

  2. I use $_GET variables, pass variables through URL, like my question here.. There are a lot of talks about sanitizing it. I really dont know what that means, other than what I am doing for forms above.

  3. Eventually, app will host (will most likely use third party provider to host) user uploaded documents to be used (send via email, keep an order of the documents etc) within application.

  4. Is there anything else that I can start doing to make my application more secure? I hope that my questions are not so vague as I am trying to learn "best practices", at least for my level of programming knowledge. I am aware there is a lot of information on the web but I am really not sure which one is more suitable, necessary or applicable.

Thanks

Phil
  • 157,677
  • 23
  • 242
  • 245
eleven0
  • 263
  • 6
  • 13
  • Please see [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Phil Oct 21 '18 at 22:12
  • 1
    Obligatory link to [PHP the Right Way](https://www.phptherightway.com) which should answer many of your questions. – tadman Oct 21 '18 at 22:29
  • 1
    Note that `stripslashes` is garbage left over in the PHP API as a result of the wildly ill-considered "magic quotes" feature that was thankfully purged from PHP years ago. It has absolutely no use these days, so it should never show up in your code. It only mangles data. – tadman Oct 21 '18 at 22:29
  • If you're just getting started with PHP and want to build applications, I'd also strongly recommend looking at various [development frameworks](http://codegeekz.com/best-php-frameworks-for-developers/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and much stronger guidance on how to write your code and organize your files. – tadman Oct 21 '18 at 22:30
  • 1
    Hi @tadman. Thank you so much for PHP the Right Way link. That makes things easier. I explored options like Laravel before, but really could not understand coding in place to build more. I think i will enhance my overall understanding of programming before I move on to an environment such as Laravel. – eleven0 Oct 23 '18 at 07:48
  • The reason you *start* with a framework like Laravel is so that you learn from good examples and can be productive while learning. If you skip the framework you end up having to painfully re-invent multiple wheels, something that's not only pointless, but something that exposes you to a high level of security risk. A framework doesn't prevent you from learning, on the contrary it encourages it, as you can always dig deeper, open the source, read the core docs, to explore not only how PHP works, but how it can work. – tadman Oct 23 '18 at 15:12

1 Answers1

-2

Answering your questions with my knowledge of PHP and security programming for more than 10 years:

1) Your filter are basic, but effective to avoid SQL injection because you use mysqli_real_escape_string(). You need to keep in mind that you can't never trust the data received. At my systems I always sanitize all the data I need to process at top of code, for example:

<?php
$data = array ();
$data["name"] = trim ( ucfirst ( strtolower ( $_REQUEST["name"])));
if ( empty ( $data["name"]))
{
  die ( "User name is required.");
}
$data["age"] = (int) $_REQUEST["age"];
if ( $data["age"] == 0)
{
  die ( "User age is required.");
}
// and so on. If I receive an email, I validate it too. BTW, I always validate if a received value are completely valid, into a valid range, valid structure, etc.

After the sanitize, at the code I use from the $data[], which are the filtered and valid values received.

At the SQL query I use the mysqli_real_escape_string(). You could also use prepared statements.

2) When you use the GET method, your variables are clear to see at the URI. You also have a limitation of size.

The limit vary on server and client used (and if applicable, also the proxy the server or the client is using).

I.E. for example is limited to 2048 bytes (2KB), Opera in 4096 bytes (4KB) and Firefox in 8192 bytes (8KB), and most modern web servers have a limit of 8192 bytes (8KB).

Using POST should be preferred, when possible.

At PHP, you could access data through $_REQUEST, that mix $_GET and $_POST.

3) Remember to store it on disk. Database are created to store data, not documents. It's a common mistake.

4) Keep reading and looking for best practices. Store your user passwords using a secure hash (i recommend hash_pbkdf2()), use SSL, log every action made on your system to audit if necessary, keep all system backup up to date, control who can access your server, use firewall, check for login attempts, etc.

Hope this helps you, I know that this is far from a good compilation of measures to take, but it's a good start.

Ernani Azevedo
  • 461
  • 2
  • 6
  • 1
    Don't use PBKDF, use [`password_hash`](http://php.net/manual/en/function.password-hash.php) which defaults to Bcrypt, a password-specific hashing method that's extremely strong compared to SHA2. Don't use manual escaping, **always** use prepared statements with placeholder values whenever possible. – tadman Oct 21 '18 at 22:28
  • @tadman `PBKDF2` you can use `SHA256`, salt and iterations (I use 40000). This is FAR best from any other hash method. It's the latest security standard in password hashes. Take a read about it. – Ernani Azevedo Oct 21 '18 at 23:17
  • 1
    SHA2-256 has been optimized to ridiculous levels because of Bitcoin, so even 40,000 rounds is not really that challenging. Bcrypt is also designed to be GPU resistant, while SHA2-256 is ideal for GPUs. See [citations like this](https://www.openwall.com/presentations/PHDays2014-Yescrypt/mgp00004.html) where PBKDF2 gets a very bad review. You want something that's GPU resistant because of tools like [Hashcat](http://hashcat.net) which have extensive, and somewhat terrifying GPU options. – tadman Oct 22 '18 at 15:55