【问题标题】:nodejs: timing attack on "=="nodejs:对“==”的定时攻击
【发布时间】:2021-12-04 11:57:12
【问题描述】:

最近我通过this 写了一篇关于hackerone 的CTF。在这篇文章中,完成挑战的部分是执行定时攻击。它激起了我的兴趣,我想创建一个容易受到时间攻击的网站。

为此,我决定使用 nodejs,因为这是我最熟悉的。但是,我无法复制它,所以我必须创建自己的 strcmp 函数并在该函数中引入时间差。现在代码看起来像这样

const { urlencoded } = require('body-parser');
const express = require('express')
const session = require('express-session')
const app = express()

app.use(express.static(__dirname+'/public'));
app.use(express.urlencoded({extended: false}))
app.set('view engine', 'ejs')

//im using this function with some bullcrap code in it to just extend the time it runs
const strcmp_test = (op1, op2) => {
    if(op1.length != op2.length) return false;
    for(let i = 0; i < op1.length; ++i) {
        if (op1[i] != op2[i]) return false;
        for(let bleh = 1; bleh < 10000000; bleh++){
            let test = 1000+bleh
            let test2 = test/155
        }
    }
    return true;
}

//MAIN SITE
app.get('/', (req,res) => {
    res.render('index')
})

app.get('/some/place', (req,res) => {
    res.render('a-view')
})


//LOGIN
app.post('/', (req,res) => {
    //I didnt care for setting up a db
    password_hash="c3e9fee675716951c547abe11e49e58190f9b1854924fa605b92d423be8716ab"
    username="admin"

    //if no body
    if(!req.body){
        console.log('NO BODY SENT')
        res.render('index')
    }
    //missing param
    if(!req.body.password || !req.body.login){
        console.log('MISSING PARAMETER')
        res.render('index')
    }
    //if password dont match. I know there is no check of username here, I guess that wont matter for the timing attack...?
    //before I used my own made function I used-->
    //if(req.body.login==username && req.body.password == password_hash) //
    if(strcmp_test(req.body.password,password_hash)){
        res.redirect('/some/place')
    } else {
        res.render('index')
    }
})



//IF NO ROUTE EXISTS
app.use(function(req, res, next) {
    res.status(404)
    res.render('error')
});

app.listen(3000)

为了测试我使用 pythons requests 库的时间。我的代码与文章中使用的代码非常相似,如下所示

def padding(h):
    r = h + ('f' * (64 - len(h)))
    return r
   
def send(payload):
    URL = 'http://127.0.0.1:8080/'
    r = requests.post(URL, data={'hash':payload})
    return r.elapsed.total_seconds()
    
if __name__ == '__main__':
    times = {}
    for x in range(0,0xff):
        times[format(x, 'x').zfill(2)] = send(padding(format(x, 'x').zfill(2)))
    print(times)

我的问题是:为什么仅使用== 来执行定时攻击在我的情况下不起作用?如果它应该起作用并且我想实施它,我需要在网站上做些什么不同的事情?

【问题讨论】:

    标签: node.js ctf timing-attack


    【解决方案1】:

    我想设置和处理 HTTP POST 请求所需的时间比比较字符串中的两个字符所需的时间多。

    尝试聚合具有相同值的多个调用所需的时间。或许你会看到不同:

    def send(payload):
        URL = 'http://127.0.0.1:8080/'
        t = 0.0
        # Might help to make one initial call to set up the HTTP pathway
        # requests.post(URL, data={'hash':payload})
        for _ in range(10000):
            t += requests.post(URL, data={'hash':payload}).elapsed.total_seconds()
        return t
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2016-10-06
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      相关资源
      最近更新 更多