|
3 | 3 | ###ENG
|
4 | 4 | [PL](#pl-version)
|
5 | 5 |
|
| 6 | +We are given authentication logic running on the server: |
| 7 | + |
| 8 | +``` |
| 9 | +var express = require('express'); |
| 10 | +var app = express(); |
| 11 | +var port = process.env.PORT || 9898; |
| 12 | +var crypto = require('crypto'); |
| 13 | +var bodyParser = require('body-parser') |
| 14 | +var salt = 'somestring'; |
| 15 | +var iteration = /// some number here; |
| 16 | +var keylength = // some number here; |
| 17 | +
|
| 18 | +app.post('/login', function (req, res) { |
| 19 | + var username = req.body.username; |
| 20 | + var password = req.body.password; |
| 21 | + if (username !== 'chintu') { |
| 22 | + res.send('Username is wrong'); |
| 23 | + return; |
| 24 | + } |
| 25 | + if (crypto.pbkdf2Sync(password, salt, iteration, keylength).toString() === hashOfPassword) { |
| 26 | + if (password === 'complexPasswordWhichContainsManyCharactersWithRandomSuffixeghjrjg') { |
| 27 | + // some logic here and return something |
| 28 | + } else { |
| 29 | + // return flag here |
| 30 | + } |
| 31 | + } else { |
| 32 | + res.send('Password is wrong'); |
| 33 | + } |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +It seems straightforward: we need to login as user `chintu` and our password hash has to match the hash value for `complexPasswordWhichContainsManyCharactersWithRandomSuffixeghjrjg` while at the same time it has to be a different string. |
| 38 | + |
| 39 | +Initially we thought it will require time-consuming hash collision generation, almost impossible since we don't know the number of iterations and keylen. |
| 40 | + |
| 41 | +However, we found this: https://mathiasbynens.be/notes/pbkdf2-hmac which describes vulnerability of `pbkdf2Sync` function in case password is longer than the size of hash function used. |
| 42 | +In our case the default `sha1` hash is used and we notice that the user password is longer than 64 bytes. |
| 43 | +This means that `pbkdf2Sync` will actually use `sha1(password)` instead of actual passsword value, and therefore we can use `sha1(password)` as `password` itself -> `sha1('complexPasswordWhichContainsManyCharactersWithRandomSuffixeghjrjg') = e6~n22k81<[p"k5hhV6*` |
| 44 | + |
| 45 | +We therefore login using |
| 46 | +`username: chintu` |
| 47 | +`password: e6~n22k81<[p"k5hhV6*` |
| 48 | + |
| 49 | +And we get the flag. |
| 50 | + |
6 | 51 | ###PL version
|
| 52 | + |
| 53 | +Dostajemy kod autentykacji działającej na serwerze: |
| 54 | + |
| 55 | +``` |
| 56 | +var express = require('express'); |
| 57 | +var app = express(); |
| 58 | +var port = process.env.PORT || 9898; |
| 59 | +var crypto = require('crypto'); |
| 60 | +var bodyParser = require('body-parser') |
| 61 | +var salt = 'somestring'; |
| 62 | +var iteration = /// some number here; |
| 63 | +var keylength = // some number here; |
| 64 | +
|
| 65 | +app.post('/login', function (req, res) { |
| 66 | + var username = req.body.username; |
| 67 | + var password = req.body.password; |
| 68 | + if (username !== 'chintu') { |
| 69 | + res.send('Username is wrong'); |
| 70 | + return; |
| 71 | + } |
| 72 | + if (crypto.pbkdf2Sync(password, salt, iteration, keylength).toString() === hashOfPassword) { |
| 73 | + if (password === 'complexPasswordWhichContainsManyCharactersWithRandomSuffixeghjrjg') { |
| 74 | + // some logic here and return something |
| 75 | + } else { |
| 76 | + // return flag here |
| 77 | + } |
| 78 | + } else { |
| 79 | + res.send('Password is wrong'); |
| 80 | + } |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +Zadanie wydaje się dość proste koncepcyjnie: mamy zalogować się jako użytkownik `chintu` a hash podanego hasła musi zgodzić się z hashem dla hasła `complexPasswordWhichContainsManyCharactersWithRandomSuffixeghjrjg`, jednocześnie będąc innym stringiem. |
| 85 | + |
| 86 | +Początkowo myśleliśmy, że będzie wymagało to czasochłonnego liczenia kolizji hashy, praktycznie niemożliwego przy braku znajomości liczby iteracji oraz długości klucza. |
| 87 | + |
| 88 | +Niemniej jednak znaleźliśmy artykuł: https://mathiasbynens.be/notes/pbkdf2-hmac opisujący podatność funkcji `pbkdf2Sync` w sytuacji gdy podane hasło jest dłuższe niż rozmiar hasha używanej funkcji. |
| 89 | + |
| 90 | +W naszym przypadku używana jest domyślna funkcja `sha1` i widzimy że hasło ma wiecej niż 64 bajty. |
| 91 | +To oznacza, że `pbkdf2Sync` w praktyce użyje wartości `sha1(password)` zamiast wartości hasła a co za tym idzie możemy z powodzeniem użyć wartości `sha1(password)` jako `password` -> `sha1('complexPasswordWhichContainsManyCharactersWithRandomSuffixeghjrjg') = e6~n22k81<[p"k5hhV6*` |
| 92 | + |
| 93 | + |
| 94 | +Dzięki temu logujemy się za pomocą: |
| 95 | +`username: chintu` |
| 96 | +`password: e6~n22k81<[p"k5hhV6*` |
| 97 | + |
| 98 | +I dostajemy flagę. |
0 commit comments