Odgadywanie hasła jedynie na podstawie czasu odpowiedzi serwera - atak czasowy
W najnowszym filmie na kanale mCoding, James Murphy wprowadza widzów w tajniki ataków czasowych na hasła. Pomimo obaw niektórych osób, nie jest to tutorial hakerski, lecz raczej analiza trudności w pisaniu bezpiecznego oprogramowania. Murphy omawia, jak pewne próby ataku, zwane atakami czasowymi, mogą być wykorzystane do złamania hasła, poprzez mierzenie czasu odpowiedzi serwera. W filmie demonstruje, jak zmierzyć długość hasła, a następnie, korzystając z tej informacji, stopniowo łamać każde znaki hasła wspierając się różnicą w czasie odpowiedzi serwera. Wszyscy programiści powinni być świadomi tych niebezpieczeństw, zwłaszcza przy projektowaniu aplikacji opartych na sieci.
Murphy zaznacza, że głównym celem jest znalezienie długości hasła. Prowadzi widzów przez proces zakupu różnych długości hasła i użycia funkcji do próby odgadnięcia tej długości, skrupulatnie mierząc czas odpowiedzi serwera na każde zapytanie. Używa metody 'timeit.repeat', aby przeprowadzić testy, co pozwala na dokonanie dokładnych pomiarów, nawet w przypadku lagów w serwerze. Gdy w końcu odkrywa, że hasło ma 20 znaków, Murphy zwraca uwagę, że przy bieżących testach ważne jest, aby nie mieć innych procesów uruchomionych na komputerze, które mogłyby zakłócić wyniki.
Zaraz po ustaleniu długości hasła, Murphy przystępuje do rzeczy o wiele bardziej złożonej, czyli samodzielnego odgadnięcia hasła. Zaczyna od losowego zgadywania hasła, a następnie stopniowo dostosowuje swoje zgadywania, pewnie wykorzystując subtelne różnice w czasie odpowiedzi serwera. Dzięki niemu, widzowie mogą zrozumieć, jak dzięki atakom czasowym udało się wyłonić złożone hasła. Murphy dzieli proces na etapy, w których na każdym etapie wprowadza jeden znak, wykorzystując podstawowe zasady porównywania łańcuchów w programowaniu.
W miarę przeprowadzania testów i zgadywania, Murphy na bieżąco dzieli się swoimi spostrzeżeniami i wynikami, a widzowie mogą z ciekawością oglądać, jak ostatecznie udaje mu się złamać 20-znakowe hasło. Każdy krok filmu utrzymany jest w przyjaznej dla widza formie, a kompleksowe wyjaśnienia i pokazy kodu sprawiają, że temat staje się przystępny nawet dla osób nieobeznanych z programowaniem.
Statystyki tego filmu na kanale mCoding są naprawdę imponujące, z ponad 178376 wyświetleń oraz 9407 łapkami w górę, co odzwierciedla zainteresowanie i docenienie wiedzy, jaką Murphy dzieli się ze swoimi widzami. Video2937790863C_R_SLIDE_29377902205SSS na koniec. Warto przyjrzeć się jego technikom programowania i zrozumieć, jak niezwykle istotne jest pisanie bezpiecznego kodu podczas rozwijania aplikacji webowych.
Toggle timeline summary
-
James Murphy wprowadza film o łamaniu haseł.
-
Przedstawienie sponsora filmu, Anvil, platformy rozwoju aplikacji internetowych.
-
Zgłasza, że film nie jest samouczkiem o hakowaniu.
-
Dyskusja na temat wyzwań związanych z pisaniem bezpiecznego oprogramowania.
-
Wprowadzenie do ataków czasowych, które wykorzystują czas wykonania algorytmów.
-
Wyjaśnienie systemu testowanego pod kątem łamania haseł.
-
Opisana początkowa trudność w łamaniu długich haseł.
-
Podkreślenie znaczenia czasowych odpowiedzi z serwera.
-
Demonstracja jak znaleźć długość hasła przy użyciu czasu.
-
Ilustracja wyniku przy testowaniu długości haseł.
-
Badanie, czy ataki czasowe mogą również zgadnąć rzeczywiste hasło.
-
Prezentacja podejścia kodowania do łamania hasła przy użyciu ataków czasowych.
-
Opis kolejnych kroków w łamaniu hasła.
-
Potwierdzenie, że hasło zostało pomyślnie złamane.
-
Ponowne przedstawienie demonstracji łamania długiego hasła.
-
Podkreślenie potrzeby bezpiecznego przetwarzania danych.
-
Zachęta dla widzów do nauki o Anvil w zakresie rozwoju aplikacji internetowych.
Transcription
Hello and welcome, I'm James Murphy, and in this video we're going to crack some passwords. Thanks to this video's sponsor, Anvil, a browser-based full-stack development platform allowing you to make web apps with nothing but Python. More about Anvil at the end of the video. Sorry to disappoint any wannabe hackers in the crowd, but this is not going to be a hacking tutorial. After watching this video you're not going to be able to just go out and hack your friends or your colleagues or government databases. What you are going to get from this video is a quick dip into the deep end. What are some of the things that make it so incredibly difficult to write secure software? In my opinion, the biggest difference between the way that code is modeled in the theoretical world and how it actually is in the real world is the amount of time that code takes to execute. Well, in the real world there's an entire class of attacks that exploit the physical amount of time that an algorithm took to execute. These are called timing attacks. That's what we're going to investigate in this video. We'll be able to crack a password that's 20 characters long in no time. Okay, so let's get started. What's the system that we're going to hack in this video? Well, I have a server which is really just a function and all this does is I pass in a user and my guess for the password and it's going to look up the password in the database which is actually just a dictionary up here and after it looks it up in the database all it does is tell you whether or not your guess was equal to the actual password. In this video we're going to be taking the attacker's perspective so we don't get to see the check password function. Check password is just a black box. We plug in the username and the guess and we either get you got it correct or you didn't. We don't get to see anything else. I know this is silly. Check password is just a function in the same file. This is totally ridiculous but think about check password as actually going out to the internet and trying to log into a server. This is a pretty bad server because it's going to allow us to make many many many guesses and it's not going to ban us for spamming or anything like that. Too many wrong attempts but I think it gets the point across. Okay let's get to cracking. At a first glance it might seem completely hopeless. Passwords can be extremely long, 30 characters or more, and all we're getting out of this is is the password correct or not. If we get even a single bit wrong then we basically gain no information right? Well not completely. The whole theme of this video is that we do get one extra piece of information that the server didn't tell us about. We can measure how much time it took to respond. If we suspected that the server was using a bad implementation like this, just equality checking the actual password against the guess, which by the way the server shouldn't even store your actual password, then we might be able to exploit something about how long it takes to compute the answer when the guess is closer to correct. In pretty much every programming language the first thing that happens when you compare two strings for equality is it checks to see if their lengths are the same. If the lengths are different then you can pretty quickly return false. So if we query the server with a guess, if the length of the password is wrong then the server will respond faster. Let's see if we can use that to crack the length of this password. Okay here's what we'll do. We'll take the user and the maximum possible length that we think the password could be. Then we'll just loop over the different lengths and use the timeit.repeat method to time how long each password attempt takes. The statement that we're timing is this check password of the user and a guess x, where here the user is inputted as whatever the argument was and x is a random string generated by this function. repeat returns a list and we just take the fastest time from that list to use as the server response time. We take the fastest time here because the server might have just lagged for some unknown reason and we don't want that to slow down our timing. We want to measure its performance assuming it didn't lag at all. To recap all I do is guess passwords of a bunch of different lengths and then return the length that takes the longest to respond. And here's what we see when we run it. Passwords that were randomly guessed of length 20 took a little bit longer than the rest. Notice that the second longest choice was 93 percent as fast. And what do you know the length of the actual password is 20. And this is not a fluke. If I change the length of the string so that now it's length 19 instead of 20 and run the program again now we see that the most likely length is 19. Of course if you have other things on your machine going on that are affecting the timing like recording video then it's going to make it a lot less likely to give you the right answer. When I ran it again I got that 25 is the most likely length which is not correct but 19 was still in the top three. So if you were a real hacker trying to do a real timing attack you would probably be doing this on a dedicated machine that doesn't have lots of random programs executing at the same time that might mess up the timing. Okay finding out the length of the password is one thing but guessing the actual password is something entirely different. So can the timing attack do that as well? Well if the implementation was one of these simple equality checks then yes because here's what an equality check for strings looks like under the hood. As we mentioned before first it does a length check and that's what we use to crack the length of the password. But after it does the length check it goes character by character and if any of them are not equal then it will return immediately because string comparison will return early if the password is incorrect at an earlier position. Again we find that the more correct the password is the longer the server will take to respond. This is exactly the same thing that we can use to exploit as with the length. So here's the code that I came up with in order to crack a password using a timing attack given that you've already cracked the length of the password. It's similar to cracking the length itself except we have to go position by position and get each character right in turn. We start with just a completely random guess of the correct length. We don't know how much of the password that we've guessed so far is correct so we're just going to repeatedly loop over the entire string try to get the first character correct then the second then the third and so on until the end and then start back at the beginning. We'll just keep repeating until we get the right password. For each position in the password we try every possible character that can be in the password and then we see what happens when we put that character in that position. We'll time how long it takes for our new guess, we'll time how long it takes for our old guess, and then whichever one took more time we keep as the old guess for the next iteration. Notice that by random chance we could have a more correct guess get replaced with a less correct guess. I'm sure that we'll see that happen when we actually run it but nevertheless the more correct guess is more likely to take longer and so eventually we will build up the correct password. Here we check to see if our new guess is the correct password and if it is then we're done we return. Otherwise what we do is we take the one that took longer and use that as the guess for the next iteration and that's it. All we have to do now is try it. So first we will crack the length of the password and then use that length pass it into the crack password function and see how long it takes to find the actual password. All right let's just see how well it does. So just on the first iteration it's gotten a lot of the characters right already. Let's see if it can finish off the last few in the next one. There we go it finished. Password cracked. Subscribe to mCoding. So there you have it. We just cracked a password that was 20 characters long using nothing but timing information. This was just one example of one timing attack and timing attacks themselves are just a single class of examples in a huge sea of things that you can mess up and if you mess up anything then you're going to get yourself hacked. Don't trust the security of your customer data or of your web app in general to something that's not extremely well tested and proven. And speaking of writing web apps if you're interested in that kind of thing be sure to stick around and hear about our sponsor Anvil. That's all I've got for now. See you in the next one. Anvil has everything you need to develop and deploy a web app written completely in Python, including the front end, so no JavaScript or HTML required. And it's free to try. Create your user interface by dragging and dropping components. Anvil's web-based IDE allows you to develop in your browser. No need to install anything. You can use Anvil's built-in database and let Anvil handle the tricky parts like user authentication. When you're ready, deploy your app to the cloud with a single click. Check out the link in the description to get started with Anvil today.