惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Vercel News
Vercel News
月光博客
月光博客
S
SegmentFault 最新的问题
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
小众软件
小众软件
WordPress大学
WordPress大学
G
Google Developers Blog
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 【当耐特】
I
InfoQ

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Weird Python "Features" That Might Catch You By Surprise Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime
Security and Cryptography Mistakes You Are Probably Doing...
Martin · 2019-12-13 · via Martin Heinz's Blog

There are a lot of myths and wrong assumptions about cryptography and security in general. It's common for people to misuse cryptographic primitives, assume that something is secure by default or use outdated technology/algorithms. So, let's look at some of those myths and common mistakes, so that you can avoid them in the future.

Stop Using /dev/random Already

Pretty much anybody would say that you should always use /dev/random for cryptographic purposes. Well, that's wrong. Both /dev/urandom and /dev/random are using the exact same CSPRNG. The difference between them is, that /dev/random blocks until it has enough entropy. Which is another issue - you don't want your applications to wait for entropy and design logic around possibility of /dev/random blocking, especially considering that you don't need that much entropy... All you need is about 256 bits of entropy, which you will get from /dev/urandom. If you want to delve deeper into this topic, then see article here.

Never Use AES ECB

As you surely know AES is a block cipher. It encrypts plain text in blocks of 128 bits. Alright, sounds good, so what is problem with using ECB (Electronic Code Book) mode? It uses unaltered key on every block, so if you encrypt more than 128 bits of data, your ciphertext will look something like this:

That's not much of an encryption, right? So, what should you use instead of ECB? Counter mode (CTR)! Why? Because encryption can be done in parallel and it uses IV (nonce) to get rid of the unaltered key problem, that ECB mode has. Just remember to never reuse the IV.

This is how the cipher text would look with CTR mode:

You can try it yourself with openssl:


~ $ identify gopher.png
gopher.png PNG 650x650 650x650+0+0 8-bit sRGB 38.9KB 0.000u 0:00.000  # Note the dimensions
~ $ convert -depth 32 gopher.png gopher.rgba
~ $ openssl enc -aes-128-ecb -e -in gopher.rgba -out gopher-ecb.rgba -k toomanysecrets  # Replace ecb with other modes too (e.g. ctr)
~ $ convert -size 650x650 -depth 32 gopher-ecb.rgba gopher-ecb.png

Never Use MD5 For Anything, Ever

While we are on the topic of never using stuff... Forget about MD5, like, right now. It was designed for use as a secure cryptographic hash algorithm, but there is nothing secure about it at this point. It has collision vulnerability, where you can create a pair of files that have same MD5 hash, meaning that you can fake a SSL or CA certificates.

So, what to use instead? It depends on use case - for password hashing use bcrypt or scrypt. Which one of those you use, is in my opinion personal preference - scrypt was designed to be "better" than bcrypt but it's newer so it faced less scrutiny. I - personally - still use bcrypt. As for the other use cases, use hash function from SHA-2 family, preferably longer digests like SHA-512.

Always Encrypt-then-MAC

The title of this section says "Encrypt-then-MAC", but let's backtrack a little bit first. A lot of people don't realize that they should always use encryption in conjunction with message authentication. I don't want to go into too much detail about why you should actually authenticate messages, all I'm gonna say is that if you don't authenticate messages, then you are opening your applications to quite a few possible attacks, like replay attack or active attack.

With that out of the way, why encrypt-then-MAC? Simply put, it's only way to get provable security, which isn't always necessary, but it means that it's essentially impossible to mess up cryptographically when using this construction.

If you want more in-depth explanation, then I recommend article here by Colin Percival.

Special Characters Don't Make Your Passwords Good

Not necessarily at least. Good password is one that you can remember, so throwing in characters like !@#$%^&* will make it more likely that you or users of your application will either forget it or write it down somewhere where someone else can see it.

What is reason to add special characters to password in the first place? It's entropy, so if we don't want to force users to include special characters in their passwords, then what is the alternative to keep the entropy high enough?

Make it loooong. As shown in popular xkcd Password Strength comic, it makes more sense to use password made by concatenating few random words, than trying to remember some gibberish. Creating these kinds of passwords satisfies both human and computer aspect of it, in other words it's easy to remember and reasonably hard to guess (high entropy, no way to brute force it).

Note: In ideal world everybody would use password manager and generate their random super high entropy passwords, but that's not something we can expect of average, non-tech savvy user.

Encryption Keys Rotation Misconception

Somebody probably already told you, that you should rotate your keys at some time interval and that's right, you absolutely should do that. A lot of people though, think that they should do it to decrease the probability of key being broken, which is not the case for encryption keys. You should rotate encryption keys to reduce the amount of data encrypted by the key, to lower the potential damage caused by single key being compromised. This misconception doesn't change the fact that you should rotate keys, but it's important to know why you are doing it, so that you can correctly assess what intervals should be used.

Never Store User Passwords

Back to the "never do stuff" topic once more. This one is little bit of rant, I've seen so many times, that website send me password by email after I registered... like... first thing you should do when you get password from user is to hash it and throw away the clear text version of password and if you are sending it to me by email, then you are clearly not doing that (end of rant)!

So, let's take a deep breath... how to store those passwords then?

As soon as you receive the password from user - hash it with slow hash function like bcrypt using work factor of at least 12 and erase the clear text password from memory. The work factor of 12 is valid now, at the time of posting and will not be enough a year from now. I would personally shoot for up to 400ms for validation of password, you can check on your machine which work factor would give you that.

If you're not gonna use bcrypt make sure to add salt to password to prevent precomputation attacks like rainbow table attack.

Containers Aren't Secure

A lot of people think that Docker containers are secure by default, but that's not the case. They were built to solve deployment problems not security ones.

There are even vulnerabilities that would allow attacker to get root access to host system and all that is needed is shell access to container. So, how to avoid these security problems and vulnerabilities?

  • Use minimal base images to reduce attack surface - Example of these images are alpine-based images or Red Hat universal base images.
  • Install only necessary libraries - The less you have in the container, the lower is chance, that there will be something to exploit.
  • Use trusted images - Don't just download any image from Docker Hub. Use ones that are reviewed, maintained by trustworthy teams and have lots of downloads (lots of eyes on them).
  • Don't use latest tags - By using fixed tags you make sure that no vulnerabilities will get introduced with your next build.
  • Use the least privileged user (Never run under root) - It is a good practice end your Dockerfiles with USER 1001 command to make sure that you use user with no privileges that could open your container to extra vulnerabilities.

There are much more things that you could do to secure your images, which you can google later, but I think the list above is bare minimum that everybody should have in mind when using Docker images/containers.

Conclusion

Every software developer/DevOps engineer is responsible for security of applications and system, at least to some extent (whether you like it or not). Therefore, I believe that all of us should take little bit of time to do the necessary research to make sure to avoid stupid mistakes and misconceptions like the ones above.

Don't just trust what people tell you (people make mistakes all the time). When it comes to security and cryptography look things up and check it yourself - make decisions based on facts.