Skip to content

Commit 9a652d7

Browse files
authored
Merge branch 'master' into rubyfu2portuguese
2 parents 60b97b5 + b636d01 commit 9a652d7

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

LANGS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
* [English](en/)
22
* [عربي](ar/)
33
* [français](fr/)
4-
* [Português](pt/)
4+
* [Português](pt/)

en/contributors/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
- Sven Vetsch | @disenchant - *PR proofreading*
2222
- Christian Fernandez | @b1naryFreed0m - *Code and PR*
2323
- Arron Crawford | @SquirrelsNabrrl - *Social media, background, advices and more*
24-
- Fernando Pinheiro | @n3k00n3 - *Translate to portuguese and more*
24+
2525
## Sponsors
2626
- [Arab Security Community (Security4arabs)][1]
2727

en/module_0x2__system_kung_fu/cryptography.md

+44-25
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Cryptography
22

3+
## Generating Hashes
34

4-
## Generating Hashes
5+
### MD5 hash
56

6-
### MD5 hash
77
```ruby
88
require 'digest'
99
puts Digest::MD5.hexdigest 'P@ssw0rd'
1010
```
11+
1112
### SHA1 hash
1213

1314
```ruby
@@ -16,9 +17,10 @@ puts Digest::SHA1.hexdigest 'P@ssw0rd'
1617
```
1718

1819
### SHA2 hash
19-
In SHA2 you have 2 ways to do it
2020

21-
**Way #1:** By creating a new SHA2 hash object with a given bit length.
21+
In SHA2 you have 2 ways to do it.
22+
23+
**Way \#1:** By creating a new SHA2 hash object with a given bit length.
2224

2325
```ruby
2426
require 'digest'
@@ -31,15 +33,26 @@ sha2_256.hexdigest 'P@ssw0rd'
3133
Digest::SHA2.new(bitlen = 256).hexdigest 'P@ssw0rd'
3234
```
3335

34-
**Way #2:** By Using the class directly
36+
**Way \#2:** By Using the class directly
37+
3538
```ruby
3639
require 'digest'
3740
puts Digest::SHA256.hexdigest 'P@ssw0rd'
3841
puts Digest::SHA384.hexdigest 'P@ssw0rd'
3942
puts Digest::SHA512.hexdigest 'P@ssw0rd'
4043
```
4144

45+
**Bonus: Generate Linux-like Shadow password**
46+
47+
```ruby
48+
require 'digest/sha2'
49+
password = 'P@ssw0rd'
50+
salt = rand(36**8).to_s(36)
51+
shadow_hash = password.crypt("$6$" + salt)
52+
```
53+
4254
### Windows LM Password hash
55+
4356
```ruby
4457
require 'openssl'
4558

@@ -49,11 +62,11 @@ end
4962

5063
def gen_keys(str)
5164
split7(str).map do |str7|
52-
65+
5366
bits = split7(str7.unpack("B*")[0]).inject('') do |ret, tkn|
5467
ret += tkn + (tkn.gsub('1', '').size % 2).to_s
5568
end
56-
69+
5770
[bits].pack("B*")
5871
end
5972
end
@@ -74,16 +87,19 @@ end
7487

7588
puts lm_hash "P@ssw0rd"
7689
```
77-
[Source | RubyNTLM][1]
90+
91+
[Source \| RubyNTLM](https://github.com/wimm/rubyntlm/blob/master/lib/net/ntlm.rb)
7892

7993
### Windows NTLMv1 Password hash
94+
8095
```ruby
8196
require 'openssl'
8297
ntlmv1 = OpenSSL::Digest::MD4.hexdigest "P@ssw0rd".encode('UTF-16LE')
8398
puts ntlmv1
8499
```
85100

86101
### Windows NTLMv2 Password hash
102+
87103
```ruby
88104
require 'openssl'
89105
ntlmv1 = OpenSSL::Digest::MD4.hexdigest "P@ssw0rd".encode('UTF-16LE')
@@ -93,37 +109,41 @@ puts ntlmv2
93109
```
94110

95111
### MySQL Password hash
112+
96113
```ruby
97114
puts "*" + Digest::SHA1.hexdigest(Digest::SHA1.digest('P@ssw0rd')).upcase
98115
```
99116

100117
### PostgreSQL Password hash
118+
101119
PostgreSQL hashes combined password and username then adds **md5** in front of the hash
120+
102121
```ruby
103122
require 'digest/md5'
104123
puts 'md5' + Digest::MD5.hexdigest('P@ssw0rd' + 'admin')
105124
```
106125

107-
## Symmetric Encryptions
126+
## Symmetric Encryptions
108127

109128
To list all supported algorithms
129+
110130
```ruby
111131
require 'openssl'
112132
puts OpenSSL::Cipher.ciphers
113133
```
114134

115-
To unserdatand the cipher naming (eg. `AES-128-CBC`), it devided to 3 parts seperated by hyphen `<Name>-<Key_length>-<Mode>`
135+
To unserdatand the cipher naming \(eg. `AES-128-CBC`\), it devided to 3 parts seperated by hyphen `<Name>-<Key_length>-<Mode>`
116136

117137
Symmetric encrption algorithms modes need 3 import data in order to work
118138

119-
1. Key (password)
120-
2. Initial Vector (iv)
121-
3. Data to encrypt (plain text)
139+
1. Key \(password\)
140+
2. Initial Vector \(iv\)
141+
3. Data to encrypt \(plain text\)
122142

123-
124-
### AES encryption
143+
### AES encryption
125144

126145
#### Encrypt
146+
127147
```ruby
128148
require "openssl"
129149

@@ -135,28 +155,28 @@ cipher.encrypt # Initializes the Cipher for enc
135155
key = cipher.random_key # If hard coded key, it must be 265-bits length
136156
iv = cipher.random_iv # Generate iv
137157
encrypted = cipher.update(data) + cipher.final # Finalize the encryption
138-
139158
```
140159

141160
#### Dencrypt
161+
142162
```ruby
143163
decipher = OpenSSL::Cipher::AES.new('256-CBC') # Or use: OpenSSL::Cipher::Cipher.new('AES-256-CBC')
144164
decipher.decrypt # Initializes the Cipher for dencryption. (Must be called before key, iv, random_key, random_iv)
145165
decipher.key = key # Or generate secure random key: cipher.random_key
146166
decipher.iv = iv # Generate iv
147167
plain = decipher.update(encrypted) + decipher.final # Finalize the dencryption
148-
149168
```
150169

151170
**Resources**
152-
- [OpenSSL::Cipher docs](https://ruby-doc.org/stdlib-2.3.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html)
153-
- [(Symmetric) Encryption With Ruby (and Rails)](http://stuff-things.net/2015/02/12/symmetric-encryption-with-ruby-and-rails/)
171+
172+
* [OpenSSL::Cipher docs](https://ruby-doc.org/stdlib-2.3.3/libdoc/openssl/rdoc/OpenSSL/Cipher.html)
173+
* [\(Symmetric\) Encryption With Ruby \(and Rails\)](http://stuff-things.net/2015/02/12/symmetric-encryption-with-ruby-and-rails/)
154174

155175
## Enigma script
156176

157177
| ![](../../images/module02/Cryptography__wiringdiagram.png) |
158-
|:---------------:|
159-
| **Figure 1.** Enigma machine diagram |
178+
| :---: |
179+
| **Figure 1.** Enigma machine diagram |
160180

161181
```ruby
162182
Plugboard = Hash[*('A'..'Z').to_a.shuffle.first(20)]
@@ -205,11 +225,10 @@ puts "Encrypted '#{plain_text}' to '#{encrypted = input(plain_text)}'"
205225
puts "Decrypted '#{encrypted}' to '#{decrypted = input(encrypted)}'"
206226
puts 'Success!' if plain_text == decrypted
207227
```
208-
[Source | Understanding the Enigma machine with 30 lines of Ruby][2]
209-
210228

229+
[Source \| Understanding the Enigma machine with 30 lines of Ruby](http://red-badger.com/blog/2015/02/23/understanding-the-enigma-machine-with-30-lines-of-ruby-star-of-the-2014-film-the-imitation-game)
211230

212231
---
213-
[1]: https://github.com/wimm/rubyntlm/blob/master/lib/net/ntlm.rb
214-
[2]: http://red-badger.com/blog/2015/02/23/understanding-the-enigma-machine-with-30-lines-of-ruby-star-of-the-2014-film-the-imitation-game
232+
233+
215234

0 commit comments

Comments
 (0)