Lec08 .8m
Lec08 .8m
Lec 09.
Server-side Security
Code Injection
2
05/05/2024
Command Injection
• Vulnerable code:
const filename = process.argv[2]
const stdout = childProcess.execSync(`cat
${filename}`)
console.log(stdout.toString())
• Input: file.txt
• Resulting command: cat file.txt
• Malicious input: file.txt; rm -rf /
• Resulting command: cat file.txt; rm -rf /
4
05/05/2024
• Unsafe
const filename = process.argv[2]
const stdout = childProcess.execSync(`cat
${filename}`)
• Safe
const filename = process.argv[2]
const { stdout } =
childProcess.spawnSync('cat', [filename])
SQL injection
6
05/05/2024
• Vulnerable code:
const { username, password } = req.body
const query = `SELECT * FROM users WHERE username =
"${username}" AND password = "${password}"`
db.get(query, (err, row) => {
if (err) {
console.error(err)
res.send('fail!’)
return
}
if (!row) {
res.send('fail!’)
return
}
/* Success */
})
• SQL template:
SELECT * FROM users WHERE username =
"${username}" AND password = "${password}"
• Input: { username: ‘alice’, password: ‘123456’ }
• Resulting query:
SELECT * FROM users WHERE username = “alice" AND
password = "123456"
• Malicious input: {username: ‘alice” OR 1=1 --’, password:
‘any’}
• Resulting query:
SELECT * FROM users WHERE username =
"alice" OR 1=1--“AND password = “any"
8
05/05/2024
10
05/05/2024
Parameterized SQL
• Vulnerable code:
const query = `SELECT * FROM users WHERE username =
"${username}"`
const results = db.all(query)
• Safe code:
const query = 'SELECT * FROM users WHERE username = ?’
const results = db.all(query, username)
• Will automatically handle escaping untrusted user input for
you
11
12
05/05/2024
13
14
05/05/2024
15
16
05/05/2024
ORM: Example
17
XXE
18
05/05/2024
Introduction
19
XXE: Example
app.post('/load_xml', upload.single('xml'), async
function (req, res) {
if (!req.file) {
res.sendStatus(500);
return;
}
try {
const xml = req.file.buffer;
const doc = libxmljs.parseXml(xml, {noent: true});
res.send(doc.text());
} catch (err) {
res.send(err.toString());
res.sendStatus(500);
}
});
20
05/05/2024
XXE Prevention
21
Read more
22
05/05/2024
23
24
05/05/2024
25
Example: Mitigation
26
05/05/2024
Example: Mitigation
27
Read more
28
05/05/2024
DoS
29
What is DoS?
30
05/05/2024
DoS mitigation
• Session
• Limit server side session time based on inactivity and
a final timeout
• Limit session bound information storage
• Input validation
• Limit file upload size and extensions
• Limit total request size
• Prevent input based resource allocation
• Prevent input based function and threading
interaction
31
Read more
32