Xiaohui Li
Little Tips Go Far My homepage: http://www.comp.nus.edu.sg/~g0800981/
Thursday, August 4, 2011
Sunday, July 31, 2011
Beginning Django in Windows: MySqlDB and CSRF Failure
the line
How to use CSRF
- Add the middleware 'django.middleware.csrf.CsrfViewMiddleware' to your list of middleware classes,MIDDLEWARE_CLASSES. (It should come and before any view middleware that assume that CSRF attacks have been dealt with.)Alternatively, you can use the decorator csrf_protect() on particular views you want to protect (see below).
- In any template that uses a POST form, use the csrf_token tag inside the
- In the corresponding view functions, ensure that the 'django.core.context_processors.csrf' context processor is being used. Usually, this can be done in one of two ways:
- Use RequestContext, which always uses 'django.core.context_processors.csrf' (no matter what your TEMPLATE_CONTEXT_PROCESSORS setting). If you are using generic views or contrib apps, you are covered already, since these apps use RequestContext throughout.
- Manually import and use the processor to generate the CSRF token and add it to the template context. e.g.:
from django.core.context_processors import csrf from django.shortcuts import render_to_response def my_view(request): c = {} c.update(csrf(request)) # ... view code here return render_to_response("a_template.html", c)
You may want to write your own render_to_response() wrapper that takes care of this step for you.
Thursday, July 7, 2011
Kill Processes From The Command Prompt In Windows 7
Kill Processes From The Command Prompt In Windows 7
Monday, July 4, 2011
How To Safely Store A Password | codahale.com
Why Not {MD5
, SHA1
, SHA256
, SHA512
, SHA-3
, etc}?
These are all general purpose hash functions, designed to calculate a digest of huge amounts of data in as short a time as possible. This means that they are fantastic for ensuring the integrity of data and utterly rubbish for storing passwords.
A modern server can calculate the MD5 hash of about 330MB every second. If your users have passwords which are lowercase, alphanumeric, and 6 characters long, you can try every single possible password of that size in around 40 seconds.
And that's without investing anything.
If you're willing to spend about 2,000 USD and a week or two picking up CUDA, you can put together your own little supercomputer cluster which will let you try around 700,000,000 passwords a second. And that rate you'll be cracking those passwords at the rate of more than one per second.
Salts Will Not Help You
It's important to note that salts are useless for preventing dictionary attacks or brute force attacks. You can use huge salts or many salts or hand-harvested, shade-grown, organic Himalayan pink salt. It doesn't affect how fast an attacker can try a candidate password, given the hash and the salt from your database.
Salt or no, if you're using a general-purpose hash function designed for speed you're well and truly effed.
bcrypt
Solves These Problems
How? Basically, it's slow as hell. It uses a variant of the Blowfish encryption algorithm's keying schedule, and introduces a work factor, which allows you to determine how expensive the hash function will be. Because of this, bcrypt
can keep up with Moore's law. As computers get faster you can increase the work factor and the hash will get slower.
How much slower is bcrypt
than, say, MD5
? Depends on the work factor. Using a work factor of 12, bcrypt
hashes the password yaaa
in about 0.3 seconds on my laptop. MD5
, on the other hand, takes less than a microsecond.
So we're talking about 5 or so orders of magnitude. Instead of cracking a password every 40 seconds, I'd be cracking them every 12 years or so. Your passwords might not need that kind of security and you might need a faster comparison algorithm, but bcrypt
allows you to choose your balance of speed and security. Use it.
tl;dr
Use bcrypt
.
Updated February 24th, 2011
I've been getting pretty regular emails about this article for the past year, and I figured I'd address some of the concerns here rather than have the same conversations over and over again.
Isn't
bcrypt
just Blowfish? Where do you store the password?
Please read the Provos & Mazières paper. bcrypt
is an adaptive password hashing algorithm which uses the Blowfish keying schedule, not a symmetric encryption algorithm.
You said salts aren't helpful, but what about rainbow tables? Why would you suggest people not use salts?
As the Provos & Mazières paper describes, bcrypt
has salts built-in to prevent rainbow table attacks. So I'm not saying salts are without purpose, I'm saying that they don't prevent dictionary or brute force attacks (which they don't).
Rainbow tables, despite their recent popularity as a subject of blog posts, have not aged gracefully. CUDA/OpenCL implementations of password crackers can leverage the massive amount of parallelism available in GPUs, peaking at billions of candidate passwords a second. You can literally test all lowercase, alphabetic passwords which are ≤7 characters in less than 2 seconds. And you can now rent the hardware which makes this possible to the tune of less than $3/hour. For about $300/hour, you could crack around 500,000,000,000 candidate passwords a second.
Given this massive shift in the economics of cryptographic attacks, it simply doesn't make sense for anyone to waste terabytes of disk space in the hope that their victim didn't use a salt. It's a lot easier to just crack the passwords. Even a "good" hashing scheme ofSHA256(salt + password)
is still completely vulnerable these cheap and effective attacks, thus the importance of an adaptive hashing algorithm like bcrypt
.
Thursday, May 26, 2011
中国常见疾病死亡率排行榜
排位 | 疾病死亡原因 | 占死亡总λ数 (%) |
1 | 恶性肿瘤 | 22.17% |
2 | 脑血管病 | 22.63% |
3 | 心脏病 | 16.77% |
4 | 呼吸系统 | 14.09% |
5 | 损伤、中毒 | 6.18% |
6 | 消化系统疾病 | 3.10% |
7 | 内分泌 , 营养代谢系统 | 2.66% |
8 | 泌尿、生殖系病 | 1.49% |
9 | 精神病 | 1.16% |
10 | 神经病 | 0.97% |