Skip to content

Commit b499c35

Browse files
JorianWoltjergitbook-bot
authored andcommitted
GitBook: [#52] Make Grammarly corrections
1 parent f188416 commit b499c35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+320
-335
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ coverY: 140.1776798825257
88

99
# Home - Practical CTF
1010

11-
* Contains lots of easy copy-paste commands/scripts to quickly do things
12-
* My aim is to explain as much as possible how or why the attack works
13-
* Inspired by [HackTricks](https://book.hacktricks.xyz/welcome/readme) but in my own style, and including all the challenges I've done
11+
* Contains lots of easy copy-paste commands/scripts to do things quickly
12+
* I aim to explain as much as possible how or why the attack works
13+
* Inspired by [HackTricks](https://book.hacktricks.xyz/welcome/readme) but in my style, and including all the challenges I've done
1414

1515
{% hint style="warning" %}
16-
As you might see in the giant list of TODOs, this book is nowhere near done yet. So just keep in mind that it's still missing a lot
16+
As you might see in the giant list of TODOs, this book is nowhere near done. So just keep in mind that it's still missing a lot
1717
{% endhint %}
1818

1919
## Motivation
2020

21-
I make a lot of writeups on my blog where I explain how I solved a certain fun challenge. This is often to explain to others, but also partly to look back on if I remember _that_ I have done something, but not exactly _how_. 
21+
I make a lot of writeups on my blog where I explain how I solved a specific fun challenge. This is often to explain to others, but also partly to look back on if I remember _that_ I have done something, but not exactly _how_. 
2222

2323
{% embed url="https://jorianwoltjer.com/blog/" %}
2424
My blog where I post CTF writeups, and general Hacking-related things
2525
{% endembed %}
2626

27-
This book aims to be a big encyclopedia of everything I know about hacking. That way I can always look back at this book if I have done something before, without having the need for a challenge with a writeup. 
27+
This book aims to be a big encyclopedia of everything I know about hacking. That way I can always look back at this book if I have done something before, without needing a challenge with a writeup. 

SUMMARY.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555

5656
## 🐧 Linux
5757

58-
* [Hacking Linux Boxes](linux/hacking-linux-boxes.md)
59-
* [Networking](linux/networking.md)
58+
* [Shells](linux/shells.md)
6059
* [Linux Privilege Escalation](linux/linux-privilege-escalation/README.md)
6160
* [Enumeration](linux/linux-privilege-escalation/enumeration.md)
6261
* [Sudo](linux/linux-privilege-escalation/sudo.md)
@@ -71,9 +70,7 @@
7170
## ❔ Other
7271

7372
* [WSL Guide](other/wsl-guide.md)
74-
* [OSINT](other/osint/README.md)
75-
* [TODO: Googling](other/osint/todo-googling.md)
76-
* [TODO: WaybackMachine](other/osint/todo-waybackmachine.md)
73+
* [OSINT](other/osint.md)
7774

7875
## 🔨 TODO
7976

@@ -108,3 +105,6 @@
108105
* [Python](todo/python.md)
109106
* [Hacking Windows Boxes](todo/hacking-windows-boxes.md)
110107
* [Windows Privilege Escalation](todo/windows-privilege-escalation.md)
108+
* [Networking](todo/networking.md)
109+
* [Googling](todo/googling.md)
110+
* [WaybackMachine](todo/waybackmachine.md)

cryptography/aes.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ print(plaintext) # b'Hello, world! (CBC)'
4949

5050
![Diagram explaining AES ECB mode from Wikipedia](<../.gitbook/assets/image (7).png>)
5151

52-
With AES in ECB mode all plaintext data is split into blocks of 16 bytes, and then encrypted separately. This means that if two blocks of 16 bytes are the same anywhere in the plaintext, we would see two of the same ciphertext blocks as well. This is the main problem of ECB mode that allows for various attacks
52+
With AES in ECB mode, all plaintext data is split into blocks of 16 bytes and then encrypted separately. This means that if two blocks of 16 bytes are the same anywhere in the plaintext, we would see two of the same ciphertext blocks as well. This is the main problem of ECB mode that allows for various attacks
5353

5454
### Decrypt suffix (data after plaintext)
5555

56-
Sometimes secret data is appended after the plaintext before encrypting. In an ideal world you should not be able to extract this data from looking at the encrypted ciphertext without knowing the key. With AES ECB however, this is possible.&#x20;
56+
Sometimes secret data is appended after the plaintext before encrypting. In an ideal world, you should not be able to extract this data from looking at the encrypted ciphertext without knowing the key. With AES ECB however, this is possible.&#x20;
5757

58-
The only thing you need is a function that takes your plaintext, appends the secret data, encrypts it all and gives you back the ciphertext.&#x20;
58+
The only thing you need is a function that takes your plaintext, appends the secret data, encrypts it all, and gives you back the ciphertext.&#x20;
5959

6060
```python
6161
def oracle(plaintext):
@@ -75,7 +75,7 @@ AAAABBBBCCCCDDDc secret
7575
AAAABBBBCCCCDDDs secret # Match
7676
```
7777

78-
Then we know the first character is s, and we can repeat the same thing again by just making more space in our plaintext so the second character of the secret gets slotted into our block. Then during the brute-forcing we already know the first character and we only need to brute-force the second character again.&#x20;
78+
Then we know the first character is s, and we can repeat the same thing again by just making more space in our plaintext so the second character of the secret gets slotted into our block. Then during the brute-forcing, we already know the first character and we only need to brute-force the second character again.&#x20;
7979

8080
```python
8181
# Initial ("AAAABBBBCCCCDD")
@@ -98,7 +98,7 @@ Solution to a Cryptopals challenge with a script that exploits this AES ECB orac
9898

9999
![Diagram explaining AES CBC mode from Wikipedia](<../.gitbook/assets/image (18).png>)
100100

101-
With AES in CBC mode, all blocks depend on the previous block as seen in the diagram above. This means that the ECB attacks from earlier don't work here. But CBC still has some vulnerabilities when used in a wrong way. The main reason for this is the use of XOR from the previous block.
101+
With AES in CBC mode, all blocks depend on the previous block as seen in the diagram above. This means that the ECB attacks from earlier don't work here. But CBC still has some vulnerabilities when used in the wrong way. The main reason for this is the use of XOR from the previous block.
102102

103103
### Bit-flipping Attack
104104

@@ -121,7 +121,7 @@ After: 8f450a920a75227a19b22039deabf7b9 0f31008a84d45451d3114e9f162bee63
121121
[random garbage] admin=true
122122
```
123123

124-
If the application for example just check if a certain string is in the plaintext, without you being able to generate a ciphertext containing that string yourself, you can bypass it with this attack.&#x20;
124+
If the application for example just checks if a certain string is in the plaintext, without you being able to generate a ciphertext containing that string yourself, you can bypass it with this attack.&#x20;
125125

126126
To see an example of this attack see my solution script to the Cryptopals challenge:
127127

@@ -131,13 +131,13 @@ An implementation of the AES CBC bit-flipping attack in Python
131131

132132
### Padding Oracle
133133

134-
I'll start off by saying that this attack really takes some time to understand, but eventually it will all click. I found some good resources online that explain it well, and visually. Here is one of them:
134+
I'll start off by saying that this attack really takes some time to understand, but after enough time it will all click. I found some good resources online that explain it well, and visually. Here is one of them:
135135

136136
{% embed url="https://samsclass.info/141/proj/p14pad.htm" %}
137137
A site detailing the AES Padding Oracle attack with diagrams
138138
{% endembed %}
139139

140-
To understand the Padding Oracle you need understand the [bit-flipping attack](aes.md#bit-flipping-attack) first (see above). This attack builds upon it to eventually be able to decrypt any ciphertext you get.&#x20;
140+
To understand the Padding Oracle you first need to understand the [bit-flipping attack](aes.md#bit-flipping-attack) first (see above). This attack builds upon it to eventually be able to decrypt any ciphertext you get.&#x20;
141141

142142
All plaintext that gets encrypted by AES needs to be in chunks of 16 bytes. This means that if your input is not exactly a multiple of 16 bytes, you need to add **padding** until it is the correct length. The default is PKCS#7 padding which simply fills the needed space with bytes representing the length of the padding. So if there are 3 bytes missing, they will be filled with three `\x03` bytes:
143143

@@ -148,22 +148,22 @@ AAAABBBBCCCCDDDD EEEEFFFFGGGGH
148148
AAAABBBBCCCCDDDD EEEEFFFFGGGGH\x03\x03\x03
149149
```
150150

151-
When data needs to be unpadded to get back the original data, the code can just look at the last byte of the plaintext, which has the value of how many bytes of padding there are. In this example the code would see `\x03` and know the last 3 bytes are padding. The code could **validate** the padding by checking to see if all the bytes that should be padding have this `\x03` value. If any of the padding bytes do not have this same value, something must have gone wrong during the padding process at the start. An application can then choose to display some sort of error message saying the padding is invalid.&#x20;
151+
When data needs to be unpadded to get back the original data, the code can just look at the last byte of the plaintext, which has the value of how many bytes of padding there are. In this example, the code would see `\x03` and know the last 3 bytes are padding. The code could **validate** the padding by checking to see if all the bytes that should be padding have this `\x03` value. If any of the padding bytes do not have this same value, something must have gone wrong during the padding process at the start. An application can then choose to display some sort of error message saying the padding is invalid.&#x20;
152152

153153
When an application says the padding is invalid, it gives away a tiny bit of information about the ciphertext we put in it. Now we know if the ciphertext has valid padding when decrypted.&#x20;
154154

155155
But here we can abuse the bit-flipping attack to get more information. If you remember we can flip any bits in a block of the plaintext by flipping those bits in the previous block of the ciphertext. This means that we can also flip some padding bits to make them correct or incorrect.&#x20;
156156

157-
Lets say the unknown plaintext does have valid padding, which is often the case. Then you might think that changing the last byte of the padding would only be able to make it invalid. But if we think about how this padding works, a simple `\x01` would also pass as valid padding. This is because the validation just looks at the last byte, and check if that number of bytes are that same padding. In the case of 1, it would always be valid because it's the only byte of padding needed. So the application would return valid padding if we change the `\x03` at the end to a `\x01`.&#x20;
157+
Let's say the unknown plaintext does have valid padding, which is often the case. Then you might think that changing the last byte of the padding would only be able to make it invalid. But if we think about how this padding works, a simple `\x01` would also pass as valid padding. This is because the validation just looks at the last byte, and checks if that number of bytes is the same padding. In the case of 1, it would always be valid because it's the only byte of padding needed. So the application would return valid padding if we change the `\x03` at the end to a `\x01`.&#x20;
158158

159159
```
160160
Original: AAAABBBBCCCCDDDD EEEEFFFFGGGGH\x03\x03\x03
161161
Also valid: AAAABBBBCCCCDDDD EEEEFFFFGGGGH\x03\x03\x01
162162
```
163163

164-
If we just brute-force all 256 possible bytes in the last spot of the plaintext, there will be 2 situations where it has valid padding: When it is the same as the original ciphertext, meaning it's 3, but also when it becomes 1. In a real attack we wouldn't know the padding was `\x03` bytes, but when we know what two values cause the padding to be valid, we can get the difference between these two brute-forced bytes. This difference will be the same as the difference between their values, `\x03` and `\x01`, but we know the `\x01` would give valid padding beforehand so we can just remove that from the difference and we are left with `\x03`! This way we can learn the last padding byte is `\x03`. To really understand why this happens, look at the decryption diagram above and think about how XOR works there.&#x20;
164+
If we just brute-force all 256 possible bytes in the last spot of the plaintext, there will be 2 situations where it has valid padding: When it is the same as the original ciphertext, meaning it's 3, but also when it becomes 1. In a real attack, we wouldn't know the padding was `\x03` bytes, but when we know what two values cause the padding to be valid, we can get the difference between these two brute-forced bytes. This difference will be the same as the difference between their values, `\x03` and `\x01`, but we know the `\x01` would give valid padding beforehand so we can just remove that from the difference and we are left with `\x03`! This way we can learn the last padding byte is `\x03`. To really understand why this happens, look at the decryption diagram above and think about how XOR works there.&#x20;
165165

166-
Now to get more bytes we can repeat this idea. To get the 2nd-to-last byte of the plaintext we can again think about when the padding would be valid for this byte: In the original ciphertext, but also when its value is `\x02` and the last byte is also `\x02`. This would make the padding 2 long and both bytes would be `\x02`, so it would be valid. But in the original plaintext this last byte was `\x03`, as we just learned, so we would need to change this byte somehow.&#x20;
166+
Now to get more bytes we can repeat this idea. To get the 2nd-to-last byte of the plaintext we can again think about when the padding would be valid for this byte: In the original ciphertext, but also when its value is `\x02` and the last byte is also `\x02`. This would make the padding 2 long and both bytes would be `\x02`, so it would be valid. But in the original plaintext, this last byte was `\x03`, as we just learned, so we would need to change this byte somehow.&#x20;
167167

168168
Luckily we already have the necessary information to do this. We can also bit-flip the last byte to become `\x02` instead of `\x03`, just like in a normal bit-flipping attack. Then we brute-force the second byte until becomes `\x02` in the plaintext and gives a "valid padding" response. This will again be our signal that we're done and then we can again get the difference between the original ciphertext, and this new ciphertext. This difference is the same difference between the `\x03` and `\x02` in the plaintext, so we can again just remove the known `\x02` it should be from the difference to get another `\x03`, but this time in the 2nd-to-last byte.&#x20;
169169

@@ -179,7 +179,7 @@ Solution to a Cryptopals challenge with a script that exploits this AES CBC padd
179179

180180
This mode can turn AES which normally is a block cipher of 16 bytes at a time, into a stream cipher, meaning it can simply generate any amount of random bytes from a key as the seed. This keystream it generates can then be used to XOR with the actual plaintext to encrypt it. Decrypting goes exactly the same: generate the keystream, XOR it with the ciphertext and you get back the plaintext as XOR is symmetric in this way.&#x20;
181181

182-
It generates the keystream by **encrypting a CounTeR with the key**. This counter can simply start at 1, and goes up for every block. This way, you're encrypting some value to generate random looking output for the keystream.&#x20;
182+
It generates the keystream by **encrypting a CounTeR with the key**. This counter can simply start at 1, and goes up for every block. This way, you're encrypting some value to generate random-looking output for the keystream.&#x20;
183183

184184
```python
185185
cipher = AES.new(key, AES.MODE_ECB) # Uses AES-ECB with key
@@ -231,11 +231,11 @@ For more technical details, see the [pycryptodome docs](https://pycryptodome.rea
231231

232232
### Repeated Key Attack
233233

234-
If you have **multiple ciphertexts** encrypted with the same keystream, and you can score a plaintext ton how plausible it is, you can use a statistical approach to try bytes of the keystream until all ciphertext decrypt to something that looks English for example.&#x20;
234+
If you have **multiple ciphertexts** encrypted with the same keystream, and you can score a plaintext ton how plausible it is, you can use a statistical approach to try bytes of the keystream until all ciphertext decrypts to something that looks English for example.&#x20;
235235

236-
This is essentially the same attack as Repeating-key XOR, because if the CTR keystream that is generated is the same for all the plaintext, you have a bunch of plaintext with a repeated key. XOR also works on a byte-by-byte basis, so you can try all 256 possible keystream bytes, XOR them with all the ciphertext's first bytes, and see if they look plausible as a plaintext. You could check for example if all the characters are in the alphabet, including a few special characters like `" ,.!?"`.&#x20;
236+
This is essentially the same attack as Repeating-key XOR because if the CTR keystream that is generated is the same for all the plaintext, you have a bunch of plaintext with a repeated key. XOR also works on a byte-by-byte basis, so you can try all 256 possible keystream bytes, XOR them with all the ciphertext's first bytes, and see if they look plausible as plaintext. You could check for example if all the characters are in the alphabet, including a few special characters like `" ,.!?"`.&#x20;
237237

238-
Then just repeat this for how all the bytes you want. You can find an implementation of this attack on my Cryptopals solutions:
238+
Then just repeat this for all the bytes you want. You can find an implementation of this attack on my Cryptopals solutions:
239239

240240
{% embed url="https://github.com/JorianWoltjer/Cryptopals/blob/master/set3/19.py" %}
241241
Solution to a Cryptopals challenge with a script that exploits this AES CTR statistical attack

0 commit comments

Comments
 (0)