LECTURE 22: SECURE CODING BASICS CSE 442 Software Engineering - - PowerPoint PPT Presentation

lecture 22 secure coding basics
SMART_READER_LITE
LIVE PREVIEW

LECTURE 22: SECURE CODING BASICS CSE 442 Software Engineering - - PowerPoint PPT Presentation

LECTURE 22: SECURE CODING BASICS CSE 442 Software Engineering Lessons Already Learned Hackers take advantage of any opening provided But most also lazy (talk to struggling 115/116 student) Any easy success in breaking code encourages


slide-1
SLIDE 1

LECTURE 22: SECURE CODING BASICS

CSE 442 – Software Engineering

slide-2
SLIDE 2

¨ Hackers take advantage of any opening provided

¤ But most also lazy (talk to struggling 115/116 student) ¤ Any easy success in breaking code encourages them

¨ Copy-and-pasting easy but who executes user input?

Lessons Already Learned

slide-3
SLIDE 3

Stupidity Never Stops

slide-4
SLIDE 4

Stupidity Never Stops

slide-5
SLIDE 5

Stupidity Never Stops

slide-6
SLIDE 6

Stupidity Never Stops

slide-7
SLIDE 7

Stupidity Never Stops

slide-8
SLIDE 8

Stupidity Never Stops

slide-9
SLIDE 9

Human Nature: 1 Humanity: 0

¨ Also need to remember some people are just jerks

slide-10
SLIDE 10

Hackers always win when it becomes a game

Prevention is key

Security Key Concept

slide-11
SLIDE 11

Web Applications

¨ Increasingly common software, even in industry ¨ Requires very different coding approach

¤ Should improve over time, but still new ¤ Distribution methods make security holes easier to find ¤ Over 70% of web apps had at least 1 security flaw

slide-12
SLIDE 12

Javascript

¨ Javascript can be force for good and evil ¨ Improves interactivity & enables good UI/UX ¨ Also means many security exploits opened ¨ Very open language with many ways to integrate

¤ Foreshadowing alert: creates big problem later

¨ Includes hooks to respond to lots of page events

slide-13
SLIDE 13

Hypertext Transfer Protocol

¨ Primary application layer protocol for web apps ¨ HTTP not encrypted; HTTPS is more secure version

¤ Browser encrypts/decrypts traffic with web server

¨ Both standards define stateless protocol

¤ Each request & response full interaction for protocol ¤ Connection info not retained by sender or receiver

¨ Stateless protocol good if only serving static page

¤ But this became problem once applications served ¤ Drove storing session data parallel to using protocol

slide-14
SLIDE 14

GET vs. POST

¨ Two ways to send data from page to server

¤ GET default approach; must specify when POST wanted

¨ GET uses URI with data embedded after link

¤ Data included as name-value pairs after ?

¨ POST sends data along page request

¤ Still readable, but harder since not included in URI

GET POST

slide-15
SLIDE 15

Cookies

¨ Small pieces of data that provide interaction state

¤ Each cookie stored as text on user's computer ¤ Set & retrieved when requested by server (or Javascript)

¨ Set of information defined by each cookie

¤ Name: unique string to identify it ¤ Value: stored data set by server ¤ Attributes: More name-value pairs

for holding other information (optional)

slide-16
SLIDE 16

Cookie Types

  • Short-term use;

Deleted when browser closes

Session cookie

  • Long-term use;

Eventually expires

  • Sent to creating

site each visit

Persistent cookie

  • Security use;
  • Only sent over

HTTPS

Secure cookie

  • Tracking use;
  • Set by

domain other than main site

Third-party cookie

  • Avoids XSS;
  • Limited use

by Javascript

HTTPOnly cookie

  • Avoids CSRF;
  • Not

supported in all browsers

SameSite cookie

slide-17
SLIDE 17

Cookie Scope

¨ Domain attribute sets which servers can use cookie

¤ Page on www-student.cse.buffalo.edu could specify

Domain=.buffalo.edu & then usable by all UB servers Domain=.cse.buffalo.edu for departmental servers Domain=www-student.cse.buffalo.edu for itself

¨ Path attribute adds restrictions to paths on server

¤ Path=/team01 limits to pages in folders:

/team01, /team01/fac, /team01/fac/hertz, etc.

¤ Path=/team01/fac not usable if page just in /team01

slide-18
SLIDE 18

Viewing/Managing cookies

¨ Chrome: use URI chrome://settings/

content/cookies

¨ Firefox: Select menu Item

Tools/Web Developer/Storage Inspector

¨ MS Edge: Select menu item

Developer tools & then open Debugger tab

slide-19
SLIDE 19

Benefits of Cookies Authentication Session Management Personalization Tracking

slide-20
SLIDE 20

Benefits of Cookies Authentication Session Management Personalization Tracking

slide-21
SLIDE 21

Persistent Authentication

¨ PHP already includes support for browser cookies

¤ Stores client data that can be used to track a user ¤ May not be accepted; should include tests to be certain ¤ Cookies not secure; many attack vectors possible

¨ Cookies sent within HTTP[S] header during request

¤ setcookie() directs creation of cookie by PHP code ¤ Can also check if cookie already exists using isset() ¤ setcookie() also deletes cookie, but requires trick

slide-22
SLIDE 22

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

slide-23
SLIDE 23

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

isset - Returns true if cookie already exists $COOKIE[] - Site's cookies read via assoc array "SSID" – Specifies the name of cookie to read

retrieve_ssid_from_db - "Retrieves" "correct" SSID

slide-24
SLIDE 24

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

header - Built-in function issues browser redirect 'Location: - Required text for this function test.edu/login.php – target URI to load

slide-25
SLIDE 25

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <html>

setcookie - PHP's built-in function which creates, updates, & deletes cookie Must appear BEFORE <HTML> tag

slide-26
SLIDE 26

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

"SSID" "shoeSize" – Name of cookie is first argument "deleted"

slide-27
SLIDE 27

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

"Bob" "12" - Value of cookie is second argument; "" using "" theoretically deletes cookie

slide-28
SLIDE 28

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

".cse.buffalo.edu" - Sets domain attribute "/CSE442-542" - Path attribute also optional

slide-29
SLIDE 29

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

Third argument specifies time cookie will expire

  • If 0 (or skipped), lasts "forever" within client
  • All browsers delete cookie using expired time
slide-30
SLIDE 30

Persistent Cookie Example

<?php if( (!isset($_COOKIE["SSID"])) || ($_COOKIE["SSID"] != retrieve_ssid_from_db($username) ) { header('Location: test.edu/login.php'); } else { setcookie("SSID", "Bob", time()+60, ".cse.buffalo.edu", "/CSE442-542", 1); setcookie("shoeSize", "12"); setcookie("deleted", "", time()-3600); } ?> <HTML>

1 in last argument limits use to HTTPS requests

slide-31
SLIDE 31

Script Kiddie Hacking

slide-32
SLIDE 32

Script Kiddie Hacking

slide-33
SLIDE 33

Misconfiguration

¨ System uses outdated server or 3rd party software ¨ Unpatched programming language (JSP, Java, PHP) ¨ Server not locked; leaves services, ports, APIs open ¨ Passwords unchanged or guessable for all to use

¤ Application has well known password for admin account ¤ Database exposed & allows users to subvert security

Not secured; Hack me!

slide-34
SLIDE 34

Script Kiddie Hacking

slide-35
SLIDE 35

Source Code Visibility

¨ Traditional app sent as machine-language

¤ No source code to hunt for weaknesses ¤ Most users lack skills to decompile

¨ Client-side code sent by web apps

¤ Client execution uses JS, HTML, & CSS ¤ Needs source code for greatest support ¤ Users will find & exploit weaknesses ¤ Even modify source with little effort ¤ Web app security must reside on server

slide-36
SLIDE 36

Script Kiddie Hacking

slide-37
SLIDE 37

Client Side – Javascript

¨ Cannot rely on Javascript checks for security either

function checkRole(role r) { if (r == admin){ return 1; } return 0; }

¨ Name changes insufficient; context all hacker needs ¨ Javascript code modified by client before running ¨ Using debugger, could change value of variables

slide-38
SLIDE 38

Client Side – Hidden fields

¨ Page relies on field having data, but field hidden

¤ Often done within form so sent as part of submission

<input type=“hidden” name=“role” value=“admin”>

¨ Feels secure since user cannot see or edit field

¤ But easy to see by client, since included in HTML code ¤ Using Web Developer mode, client can easily to modify ¤ Still creates game between developer and hacker

slide-39
SLIDE 39

Client Side – Cookies (1)

¨ Common flaw: using cookie to establish security role

¤ Web pages could include code to determine if cookie set

if (username == “Admin”) { document.cookie =”role=admin"; } else { document.cookie =”role=guest"; }

¤ Even without code, cookies still visible to users

¨ Only matter of time until someone finds admin role

¤ Hackers have more time; you will always lose game ¤ Even when role worthless, challenge drives hackers

slide-40
SLIDE 40

Client Side – Cookies (2)

¨ Common flaw: using cookie to establish security role

¤ Even if server executes code will still have weakness

<?php if(isset($_COOKIE["SSID"])) && $_COOKIE["ROLE"] == "%&d3dBD") { // Output admin view of page } else { // Output normal view of page } ?>

¨ Client machine stores cookies so users can modify

slide-41
SLIDE 41

Hackers always win when it becomes a game

Prevention is key

Security Key Concept

slide-42
SLIDE 42

Script Kiddie Hacking

slide-43
SLIDE 43

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

slide-44
SLIDE 44

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

Login: mhertz ****** Password:

slide-45
SLIDE 45

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

Login: mhertz ****** Password:

slide-46
SLIDE 46

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

slide-47
SLIDE 47

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

slide-48
SLIDE 48

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

String q = "SELECT * FROM useraccounts" + "WHERE account = ?"; PreparedStatement st=conn.prepareStatement(q,??); st.setString(1, uri.getFolder("1")); ResultSet results = st.executeQuery( );

slide-49
SLIDE 49

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

String q = "SELECT * FROM useraccounts" + "WHERE account = ?"; PreparedStatement st=conn.prepareStatement(q,??); st.setString(1, uri.getFolder("1")); ResultSet results = st.executeQuery( );

slide-50
SLIDE 50

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

slide-51
SLIDE 51

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

¤ GET request cannot be trusted without verification

String q = "SELECT * FROM useraccounts" + "WHERE account = ?"; PreparedStatement st=conn.prepareStatement(q,??); st.setString(1, uri.getParam("acct")); ResultSet results = st.executeQuery( ); http://example.edu/app/accountInfo?acct=admin

slide-52
SLIDE 52

Direct Object References

¨ Client’s authorization sets objects & info provided

¤ Easy edits on client’s machine cannot subvert security ¤ But anyone can access data using properly built request

¨ Client unreliable; server must keep client’s state

¤ GET request cannot be trusted without verification ¤ Also will be problem if POST or cookie only data source

slide-53
SLIDE 53

Script Kiddie Hacking

slide-54
SLIDE 54

Authentication Errors (1)

slide-55
SLIDE 55

Authentication Errors (2)

slide-56
SLIDE 56

Authentication Errors (3)

PHP to Require HTTPS Usage <?php if( (!isset($_SERVER['HTTPS'])|| ($_SERVER['HTTPS']!='on')) { header('Location: '. 'https://'. $_SERVER['SERVER_NAME']. $_SERVER['PHP_SELF']); } ?>

slide-57
SLIDE 57

Authentication Errors (3)

HTML supporting HTTP login <div id="login_form"> <!-- div avoids mistakenly submitting data --> User name: <input type="text" id="username"> Password: <input type="password" id="plaintext"> <input type="button" value="Login"

  • nclick="submit()">

</div>

slide-58
SLIDE 58

Authentication Errors (3)

JS supporting HTTP login // Using crypto-js & jQuery libraries var submit = function() { var name = $('#username').val(); var plaintext = $('#plaintext').val(); var cyphertext = CryptoJS.SHA3(plaintext); $.post("login.php", {username:name, passwd:cyphertext}, fnHandlingResult); };