View on GitHub

web-security-guide

🔐 The Hitchhiker's Guide to Web Security

image

Learn about the most important security concerns when developing websites, and what you can do to keep your servers, software, and data safe from harm. Instructor Kevin Skoglund explains what motivates hackers and their most common methods of attacks, and then details the techniques and mindset needed to craft solutions for these web security challenges. Learn the eight fundamental principles that underlie all security efforts, the importance of filtering input and controlling output, and smart strategies for encryption and user authentication. Kevin also covers special considerations when it comes to credit cards, regular expressions, source code managers, and databases.

This course is great for developers who want to secure their client’s websites, and for anyone else who wants to learn more about web security.

Topics include:

  • Why security matters
  • What is a hacker?
  • How to write a security policy
  • Cross-site scripting (XSS)
  • Cross-site request forgery (CSRF)
  • SQL injection
  • Session hijacking and fixation
  • Passwords and encryption
  • Secure credit card payments

Table of Content

Introduction

Introduction

1. Security Overview

What is security?

Why security matters

What is a hacker?

Total security is unachievable

The only secure computer is one that’s unplugged, locked in a safe, and buried 20 feet under the ground in a secret location… and I’m not even too sure about that one. —Dennis Hughes, FBI From: https://en.wikibooks.org/wiki/UNIX_Computing_Security/Introduction

Get in the security mind-set

Regular security reviews

Better developers

Write a security policy

Security policy

2. General Security Principles

Least privilege

class User {
-   public $valid_users = array('tom', 'mary', 'steve');
+   private $valid_users = array('tom', 'mary', 'steve');
-   public $password = 'secretpassword'
+   private $password = 'secretpassword'

    public function authenticate($user, $pwd) {
        return in_array($user, $valid_users) && password_matches($pwd);
    }

-   public function password_matches($pwd) {
+   private function password_matches($pwd) {
        return $password == $pwd;
    }
}

Least Privilege Benefits

Simple is more secure

Never trust users

Expect the unexpected

Defense in depth

Security through obscurity

Blacklisting and whitelisting

Map exposure points and data passageways

3. Filtering Input, Controlling Output

Regulating requests

Validating input

Sanitizing data

    // Hacker tries:
Input = "<script>alert ("Gotcha!");</script>"
// You remove <script> and </script> tags:
Input = "alert("Gotcha!")"
// Hacker tries:
Input = "<scr<script>ipt>alert ("Gotcha!");</scr</script>ipt>"
// You remove <script> and </script> tags:
input  = "<script>alert ("Gotcha!");</script>"

Labeling variables

$raw_email = $_POST['email'];
$safe_email = sanitize($raw_email);
// More often than not you would just do it as a one step process
$safe_email = sanitize($_POST['email']);
dirty = {}
clean = {}
dirty["first_name"] = params["user"]["first_name"]
clean["first_name"] = sanitize(dirty["first_name"])

clean_params = {}
clean_params["first_name"] = sanitize(params["user"]["first_name"])

Keeping code private

Keeping credentials private

Keeping error messages vague

Smart logging

4. The Most Common Attacks

Cross-site scripting (XSS)

// HTTP Request
GET /register.php?email=<script>alert("Gotcha!");</script>
// PHP
Email: <%= email %>
// Rendered HTML
Email: <script>alert("Gotcha!");</script>
// Reference: https://brakemanscanner.org/docs/warning_types/cross_site_scripting_to_json/
// Ruby on Rails
<script>
    var user_list = <%= @users.to_json %>;
</script>
// From database data
email = "[email protected]\"}; alert('Gotcha!'); //"
// Rendered
<script>
    var user_list = [
        {name: "jizu", email: "[email protected]"}; alert('Gotcha!'); //},     
        {name: "foo", email: "bar"}
    ]
</script>

Cross-site request forgery (CSRF)

SQL injection

SELECT * from users
WHERE username = '${username}' AND password = '${password}'

username = "jsmith' OR 1=1; -- "
password = "blank"

URL manipulation

Faked requests and forms

GET /somepath HTTP/1.1
Host: somesite.com
Referer: 
User-Agent: 
Accept:
Accept-Language:
Accept-Encoding:
Accept-Charset:
Cookie:

Session hijacking

Session fixation

GET /login HTTP/1.1
Host: yourbank.com
Cookie: SESSION_ID=3e3531fa4ba...

http://yourbank.com/login?SESSION_ID=a1b2c3d4e5...

Remote system execution

system
exec
shell
sh
shell_exec
open
popen
proc_open
call
subprocess
spawn
passthru
eval
%x
`

File-upload abuse

Denial of service

5. Encryption and User Authentication

Password encryption

Salting passwords

Password requirements

Brute-force attacks

Using SSL for login

Protecting cookies

Regulating access privileges

Handling forgotten passwords

Multi-factor authentication (MFA)

6. Other Areas of Concern

Credit card payments

Regular expression flaws

Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

https://en.wikiquote.org/wiki/Jamie_Zawinski

Conversions and transformations

Buffer overflows

Source code managers

Database security

Server security

Conclusion

Goodbye

image