Introduction
to
Web Security
Code Immersion Day
Fri 03 Aug 2018
A "bit" more about myself
* End at 2 mins
- Understand from your lecturer that you all will be learning web, PHP, C#, Swift
- (To quiz on time) Reason why I am here - PHP meetup here at ITE tonight at 7pm
- (To quiz on who opening keynote speaker is) PHPConf.Asia - opening keynote speaker is creator of PHP. Promo code at PHP meetup
The digital age
* End at 5 mins
- Realised something? I was referring to webpages all the way just now
- No Powerpoint slides, including this slide
- Everything going online, from school info, to journalism, to retail shops, and usually start with creation of website.
- The general rule of thumb would be the mantra, "Cheap, Fast, Good" - cost little to do up, get it up tomorrow, and showcase a world-class design.
- (To quiz) Iron Triangle: Speed, Quality, Good - can only have 2 out of 3
- Hokkien for this mantra
- Eg. Neighbour knock on door, ask you to do e-commerce site for $50 (Grin emoji), want it in 3 days (Downcast emoji), ppl see alr will have rainbow over heads (Anxious emoji)
- But is that all there is to it? How about the security of the website? Is it even important at all?
Being Paranoid
Validate input
Filter output
Robustness principle - Be conservative in what you send, be liberal in what you accept
* End at 7 mins
- Someone once told me, to be good at security, you must be paranoid
- (To quiz on slogan) Even if you do not understand or forget everything after this talk, just remember this security slogan - Never Trust The User
- (To quiz on rephrase) Robustness principle - lenient on input from others, strict on output from yourself
Filter output
* End at 10 mins
- This is what happens when you do not filter output. An attacker can slip in code to steal your browsing history via this.
- PHP Scripts Mall is a site where you can buy scripts when you are too lazy to code :P
- Use Microsoft Edge or Firefox. Chrome blocks XSS attack. Does not work on Safari.
- Go to https://www.phpscriptsmall.com/product/match-clone/, User Demo, Search (menu), click View Search By Id link
(http://74.124.215.220/~matridemo/multi-religion/searchbyid.php)
- Search normal text first
- Exploit by putting script alert('PHP Aug 2018')
Validate input
22 Dec 2016: CVE-2016-10033
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-10033
The mailSend function in the isMail transport in PHPMailer before 5.2.18 might allow remote attackers to pass
extra parameters to the mail command and consequently execute arbitrary code via a \" (backslash double quote)
in a crafted Sender property.
Demo
* End at 15 - 20 mins
- This is what happens when you do not validate input. Not all users are good users, or smart for that matter.
- PHPMailer is a popular PHP library used for sending mail
Validate input - vulnerability
setFrom
validateAddress
mailSend
$params = sprintf('-f%s', $this->Sender);
$params:
-f"evil\" -oQ/tmp/ -X/var/www/html/asg1/demo/footer.php sender"@vulnerable.com
// This will cause sendmail to execute with (other options omitted for brevity):
Arg no. 0 = [/usr/sbin/sendmail] // binary
Arg no. 1 = [-fevil\] // set sender name
Arg no. 2 = [-oQ/tmp/] // set queue group
Arg no. 3 = [-X/var/www/html/cs4239-asg1-patch/footer.php] // path of log file
Arg no. 4 = [-sender"@vulnerable.com] // invalid option
- Validation does not cover use case where email address has quotation marks. This allows additional parameters
to be injected in the address in the form: `"Attacker \" -Param2 -Param3"@test.com`
Validate input - patch
Compare v5.2.17 and v5.2.18
$params = sprintf('-f%s', escapeshellarg($this->Sender));
$params: // note the single quotes
-f'"evil\" -oQ/tmp/ -X/var/www/html/cs4239-asg1-patch/footer.php sender"@vulnerable.com'
// This will cause sendmail to execute with (other options omitted for brevity):
Arg no. 0 = [/usr/sbin/sendmail]
Arg no. 1 = [-f"evil\" -oQ/tmp/ -X/var/www/html/cs4239-asg1-patch/footer.php sender"@vulnerable.com] // valid email local-part within quotes
Demo
- `escapeshellarg` function applied on the sender email address in `PHPMailer::mailSend()`
Validate input - bypass
- Adding of `isShellSafe` method to prevent usage of single quotes to bypass v5.2.18 patch.
- Security is a cat and mouse game. Code is created by humans, hence humans can break code.
- You write code, your friend break it, another patch it and the cycle never ends :P
- (To quiz on the 2 demos) Validate input, filter output