Cenzic 232 Patent
Paid Advertising
sla.ckers.org is
ha.ckers sla.cking
Sla.ckers.org
Whether this is about ha.ckers.org, sla.ckers.org or some other project you are interested in or want to talk about, throw it in here to get feedback. 
Go to Topic: PreviousNext
Go to: Forum ListMessage ListNew TopicSearchLog In
Token - session token php class.
Posted by: sydarex
Date: July 11, 2009 09:53AM

Token is a PHP class that implements a session token sistem to avoid CSRF attacks.

Cross Site Request Forgeries, also known as Session Riding Attacks, occurs when an attacker makes the victim’s browser to perform arbitrary actions on a trusted site. It is a dangerous attack that is very underestimated (while so much words are wasted for quite harmless attacks like most XSS) and a great number of sites (and CMS) does not protect their users from it.

Token uses a simple technique (inspired by Seride, a project by nex of PlayHack) based on session token. On every user’s request, is generated a random token that an attacker cannot guess. The request is considered invalid if the token isn’t the same generated by Token (and only the real user can, with his browser, send it).

Token is downloadable from here: http://sydarex.org/38/token-class.
Comments and feedback are welcome. =)

---
Francesco Ciracì - Sydarex
I'm not a English native speaker, sorry and report me my errors, thanks!

Options: ReplyQuote
Re: Token - session token php class.
Posted by: rvdh
Date: July 13, 2009 10:54AM

Only 8 chars in base16 [abcdef] [0-9] possible chars isn't really that much, not that's likely but I would lengthen the token to play safe.

I also would not trust this entropy since it's based upon microseconds and can be guessed pretty much instantly, it's far from being random:

sha1(uniqid(rand(), true));

uniqid isn't meant for security purposes, it's only useful in PHP to prevent multiple submissions into a database for example. So consider that obfuscation. If you insist, try to use mt_rand(). But try to avoid it at all in generating a relative secure token imho.

I would drop the use of: unset() by all means, it had/has issues and certain PHP versions are still vulnerable, just empty the session by making a new one, but empty, or: session_destroy();

What would be cool if you could randomly generate the input field name:

echo "<input type=\"hidden\" name=\"<?=$random_key;?>\" value=\"".$this->token."\" />";

Which you place in a session as well, this way it's very hard to regex it out when an XSS is available.

/end of critique ;-)

Options: ReplyQuote
Re: Token - session token php class.
Posted by: Spyware
Date: July 13, 2009 06:02PM

http://random.org/ This might be an interesting addition to your project, OP.

Options: ReplyQuote
Re: Token - session token php class.
Posted by: sydarex
Date: July 15, 2009 10:12AM

rvdh Wrote:
-------------------------------------------------------
> Only 8 chars in base16 [0-9] possible chars isn't
> really that much, not that's likely but I would
> lengthen the token to play safe.
>
MMh, I guess you're right. Maybe 32 chars would be better.
> I also would not trust this entropy since it's
> based upon microseconds and can be guessed pretty
> much instantly, it's far from being random:
>
>
> sha1(uniqid(rand(), true));
>
>
> uniqid isn't meant for security purposes, it's
> only useful in PHP to prevent multiple submissions
> into a database for example. So consider that
> obfuscation. If you insist, try to use mt_rand().
> But try to avoid it at all in generating a
> relative secure token imho.
>
How can I generate a better random hash?
> I would drop the use of: unset() by all means, it
> had/has issues and certain PHP versions are still
> vulnerable, just empty the session by making a new
> one, but empty, or: session_destroy();
>
Mmh, I'll think about.
> What would be cool if you could randomly generate
> the input field name:
>
> echo "\" value=\"".$this->token."\" />";
>
> Which you place in a session as well, this way
> it's very hard to regex it out when an XSS is
> available.
>
This is *really* a great idea ;)
> /end of critique ;-)
Thank you ;)


@Spyware: thank you ;)

---
Francesco Ciracì - Sydarex
I'm not a English native speaker, sorry and report me my errors, thanks!

Options: ReplyQuote
Re: Token - session token php class.
Posted by: rvdh
Date: July 16, 2009 05:53AM

You could use:

http://us3.php.net/manual/en/function.mcrypt-encrypt.php

So that you use a private key to generate a token based upon your chosen randomness. You can mix mt_rand and encrypt that, this way it's impossible retrieve the data without the key. And the token will not be in base16 anymore, but real random characters.

To generate a one time key, you can use a couple of hotbits from fourmilab: http://www.fourmilab.ch/hotbits/ which are generate from radioactive decay, impossible to guess, because it's true randomness (as far randomness is feasible of course)

But this might be a bit overwhelming I guess, only for the most paranoid.

But it's tricky, I'll have to admit. Cryptography isn't for the faint of heart, tons of pitfalls. As long you don't base your token upon something trivial like microseconds, or dates or time, or self written algorithms to generate randomness, then you will be fine I guess.

Options: ReplyQuote
Re: Token - session token php class.
Posted by: hookits
Date: September 14, 2009 08:20PM

@rvdh
You are a fucking genius :P

@sydarex
I found this site, wish to help you :P
http://blog.code-head.com/a-php-form-obfuscator-secure-and-spam-free-php-forms

Options: ReplyQuote


Sorry, only registered users may post in this forum.