commit 1da81aa782632940e892aafc585851022df32657
parent fdb2467913f6592b8f2f3d3030d7f8fd33a3d9c6
Author: mpizzzle <michael.770211@gmail.com>
Date: Sun, 24 Feb 2019 11:46:25 +0000
minor refactoring
Diffstat:
12 files changed, 48 insertions(+), 42 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+set3/mt19937.pyc
diff --git a/set1/aes_in_ecb_mode.py b/set1/aes_in_ecb_mode.py
@@ -1,7 +1,6 @@
from Crypto.Cipher import AES
-with open('files/7.txt') as f:
- file = f.read().decode("base64")
+file = open('files/7.txt').read().decode("base64")
key = "YELLOW SUBMARINE"
cipher = AES.new(key, AES.MODE_ECB)
diff --git a/set1/break_repeating_key_xor.py b/set1/break_repeating_key_xor.py
@@ -40,11 +40,8 @@ def get_key(file):
transposed_blocks = [''.join([block[x] for block in split_file[:len(split_file) - 1]]) for x in range(key_length)]
return ''.join([get_candidate_key_byte(block) for block in transposed_blocks])
-with open('files/6.txt') as f:
- file = f.read().decode("base64")
-
-with open('files/p059_cipher.txt') as f2:
- project_euler_59 = ''.join([chr(int(c)) for c in f2.read().split(',')])
+file = open('files/6.txt').read().decode("base64")
+project_euler_59 = ''.join([chr(int(c)) for c in open('files/p059_cipher.txt').read().split(',')])
key1 = get_key(file)
key2 = get_key(project_euler_59)
diff --git a/set1/detect_aes_in_ecb_mode.py b/set1/detect_aes_in_ecb_mode.py
@@ -1,7 +1,6 @@
from sets import Set
-with open('files/8.txt') as f:
- file = f.read().splitlines()
+file = open('files/8.txt').read().splitlines()
for ciphertext in file:
charlist = [0 for x in range(256)]
diff --git a/set1/detect_single_character_xor.py b/set1/detect_single_character_xor.py
@@ -1,5 +1,4 @@
-with open('files/4.txt') as f:
- hex_strings = f.read().splitlines()
+hex_strings = open('files/4.txt') .read().splitlines()
for hex in hex_strings:
for plaintext in [''.join([chr(x ^ ord(a)) for a in hex.decode("hex")]) for x in range(256)]:
diff --git a/set1/detect_single_character_xor_char_frequency.py b/set1/detect_single_character_xor_char_frequency.py
@@ -1,5 +1,4 @@
-with open('files/4.txt') as f:
- hex_strings = f.read().splitlines()
+hex_strings = open('files/4.txt').read().splitlines()
frequent_letters = "etaoi "
candidate = ""
diff --git a/set2/aes_in_cbc_mode.py b/set2/aes_in_cbc_mode.py
@@ -1,7 +1,6 @@
from Crypto.Cipher import AES
-with open('files/10.txt') as f:
- file = f.read().decode("base64")
+file = open('files/10.txt') .read().decode("base64")
split_file = [file[i:i + AES.block_size] for i in range(0, len(file), AES.block_size)]
key = "YELLOW SUBMARINE"
diff --git a/set2/ecb_cbc_detection_oracle.py b/set2/ecb_cbc_detection_oracle.py
@@ -27,8 +27,7 @@ def encryption_oracle(msg):
return ciphertext
-with open('files/10_decrypted.txt') as f:
- ciphertext = encryption_oracle(f.read())
+ciphertext = encryption_oracle(open('files/10_decrypted.txt').read())
blocks = [ciphertext[i:i + AES.block_size] for i in range(0, len(ciphertext), AES.block_size)]
diff --git a/set3/break_fixed_nonce_ctr_using_substitutions.py b/set3/break_fixed_nonce_ctr_using_substitutions.py
@@ -17,12 +17,10 @@ def drag_crib(crib, a_xor_b):
derp = []
for n in range(len(a_xor_b) - len(crib) + 1):
potential = xor(crib, a_xor_b[n : n + len(crib)])
-
- #if all(x.isalpha() or x.isspace() for x in potential):
- # if crib not in potential:
derp.append(potential)
+
print derp
for i in range(40):
- print i
+ #the string below was the result of half an hour of manual crib dragging
drag_crib("or polite meaningless ", xor(ciphertexts[i], ciphertexts[5]))
diff --git a/set3/clone_mt_rng_state.py b/set3/clone_mt_rng_state.py
@@ -1,3 +1,4 @@
+from random import randint
from mt19937 import MersenneTwister
def _int32(x):
@@ -38,7 +39,7 @@ def untemper(yy):
y = untemper_11(y)
return _int32(y)
-unknown_seed = 12668778
+unknown_seed = randint(0, 0xffffffff)
mt = MersenneTwister(unknown_seed)
cloned_mt_state = [0] * 624
diff --git a/set4/break_sha1_keyed_mac.go b/set4/break_sha1_keyed_mac.go
@@ -4,43 +4,58 @@ import (
"encoding/binary"
"fmt"
"math/rand"
- "io/ioutil"
+ //"io/ioutil"
"./sha_1"
- "strings"
+ //"strings"
"time"
)
-func padding(msg []byte) []byte {
- length := len(msg)
+func get_padding(msg []byte) []byte {
+ length := uint64(len(msg))
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
var tmp [64]byte
tmp[0] = 0x80
- if length % 64 < 56 {
- return tmp[0 : 56-length%64]
- } else {
- return tmp[0 : 64+56-length%64]
- }
+ //if length % 64 < 56 {
+ // return tmp[0 : 56-length%64]
+ // length = 56-length%64
+ //} else {
+ // return tmp[0 : 64+56-length%64]
+ // length = 64+56-length%64
+ //}
+
+ length <<= 3
+ sha_1.PutUint64(tmp[:], length)
+ return tmp[:]
+ //d.Write(tmp[0:8])
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
- file, _ := ioutil.ReadFile("/usr/share/dict/cracklib-small")
- lines := strings.Split(string(file), "\n")
- key := lines[rand.Intn(len(lines))]
+ //file, _ := ioutil.ReadFile("/usr/share/dict/cracklib-small")
+ //lines := strings.Split(string(file), "\n")
+ key := "a"//lines[rand.Intn(len(lines))]
msg := "comment1=cooking%20MCs;userdata=foo;comment2=%20like%20a%20pound%20of%20bacon"
- hash := sha_1.Sum([]byte(key + msg))
+ secret_prefix_hash := sha_1.Sum([]byte(key + msg))
var registers [5]uint32
for i := 0; i < 5; i++ {
- registers[i] = binary.BigEndian.Uint32(hash[i * 4 : (i * 4) + 4])
+ registers[i] = binary.BigEndian.Uint32(secret_prefix_hash[i * 4 : (i * 4) + 4])
}
injected := ";admin=true"
- forged := append(append(hash[:], []byte(injected)...), padding([]byte(msg + key))...)
-
- fmt.Printf("%x\n", sha_1.SumForged(forged, registers))
- fmt.Printf("%x\n", sha_1.Sum([]byte(key + msg + injected)))
+ pad := get_padding([]byte(key + msg))
+ forged := append(pad, []byte(injected)...)[1:]
+ //forged := []byte(injected)
+ forged_digest := sha_1.SumForged(forged, registers)
+
+ //fmt.Printf("%x\n", sha_1.SumForged(forged, registers))
+ fmt.Printf("%x\n", sha_1.Sum([]byte(key + msg + string(pad) + injected)))
+ fmt.Printf("%x\n", sha_1.Sum(append(forged, forged_digest[:]...)))
+ fmt.Printf("%x\n", sha_1.Sum(append(forged_digest[:], forged...)))
+ fmt.Printf("%x\n", sha_1.Sum(append([]byte(key), forged...)))
+ fmt.Printf("%x\n", secret_prefix_hash)
+ fmt.Printf("%x\n", sha_1.Sum([]byte(msg + key)))
}
diff --git a/set4/sha_1/sha1.go b/set4/sha_1/sha1.go
@@ -88,7 +88,7 @@ func (d *digest) checkSum() [Size]byte {
// Length in bits.
len <<= 3
- putUint64(tmp[:], len)
+ PutUint64(tmp[:], len)
d.Write(tmp[0:8])
if d.nx != 0 {
@@ -131,7 +131,7 @@ func SumForged(data []byte, registers [5]uint32) [Size]byte {
return d.checkSum()
}
-func putUint64(x []byte, s uint64) {
+func PutUint64(x []byte, s uint64) {
_ = x[7]
x[0] = byte(s >> 56)
x[1] = byte(s >> 48)