Saturday, January 22, 2022

sql injection union command

SELECT *FROM sqlinjection.login
WHERE id=1
UNION SELECT * FROM sqlinjection.login;

data leaked by union command

sql injection with union command in search query to obtain table columns info

search found bob

sql injection with union command failed
//index.js

const express = require('express')
const app = express()
const port = 8080

var bodyParser = require('body-parser')
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
    extended: true
}));

var mysql = require('mysql')
var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'dvwa',
    password: 'p@ssw0rd',
    database: 'sqlinjection'
})

connection.connect()

//render html
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);

app.get('/', function (req, res) {
    res.render('index');
});

app.all('/search/', function (req, res) {
    if (!req.query.q) {
        res.render('index');
    }
    else {
        connection.query('SELECT * from login where name = ?', [req.query.q], function (err, rows, fields) {
            if (err) throw err

            res.send(rows);
        })
    }
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

reference:

prevention:
  1. Do not concatenate the SQL query strings. This is where the hackers will manipulate the SQL query to expose data. So never do the concatenate on SQL query.
  2. Always use the Prepared Statement approach( Question mark approach).
  3. The advantage of using the question mark is, it will match the data comparison with only the particular column. Even the hackers modify the input, the modified input value will be compared with only one column. So it does not expose the data.

Wednesday, January 19, 2022

limit repeated requests to public APIs

if requests are more than limits, server will block request


limt, limit remaining, and limit reset time are in response headers
if multiple users are online, current user limit won't be affected by others'
usage is tracked by ip address
//index.js
const express = require('express')
const rateLimit = require('express-rate-limit')
const app = express()

const PORT = 3000

// Create the rate limit rule
const apiRequestLimiter = rateLimit({
    windowMs: 1 * 60 * 1000, // 1 minute
    max: 3 // limit each IP to 3 requests per windowMs
})

// Use the limit rule as an application middleware
app.use(apiRequestLimiter)

app.get('/', function (req, res) {
  return res.send('Hello World')
})

app.listen(PORT, () => {
    console.log(`server started on port ${PORT}`)
})

reference:

Restoration Workshop