Skip to content

Commit

Permalink
utils moved and hashfile only with cryptography mod
Browse files Browse the repository at this point in the history
  • Loading branch information
fportantier committed Apr 9, 2019
1 parent af7d1bb commit 8a58f64
Show file tree
Hide file tree
Showing 21 changed files with 152,259 additions and 0 deletions.
23 changes: 23 additions & 0 deletions utils/aes-decrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3

import sys

from binascii import unhexlify

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes

key = sys.argv[1].encode()
iv = unhexlify(sys.argv[2])
msg = unhexlify(sys.argv[3])

digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(key)
key_digest = digest.finalize()

cipher = Cipher(algorithms.AES(key_digest), modes.CFB(iv), backend=default_backend())
decryptor = cipher.decryptor()
plain = decryptor.update(msg) + decryptor.finalize()

print(plain.decode(errors='ignore'))
25 changes: 25 additions & 0 deletions utils/aes-encrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env python3

import os
import sys

from binascii import hexlify

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes

key = sys.argv[1].encode()
plain = sys.argv[2].encode()
iv = os.urandom(16)

digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(key)
key_digest = digest.finalize()


cipher = Cipher(algorithms.AES(key_digest), modes.CFB(iv), backend=default_backend())
encryptor = cipher.encryptor()
encrypted = encryptor.update(plain) + encryptor.finalize()

print(hexlify(iv).decode(), hexlify(encrypted).decode())
60 changes: 60 additions & 0 deletions utils/ca-create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3

import datetime

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes

private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)

public_key = private_key.public_key()

pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)

pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)

with open('/tmp/ca.key', 'wb') as out:
out.write(pem_private)

with open('/tmp/ca.pub', 'wb') as out:
out.write(pem_public)

print('Created files in /tmp/ca.key /tmp/ca.pub /tmp/ca.cert')

# Various details about who we are. For a self-signed certificate the
# subject and issuer are always the same.
subject = issuer = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, "AR"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "BA"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Buenos Aires"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Vulpy by Securetia"),
x509.NameAttribute(NameOID.COMMON_NAME, "www.securetia.com"),
])

cert = x509.CertificateBuilder().subject_name(subject)
cert = cert.issuer_name(issuer)
cert = cert.public_key(public_key)
cert = cert.serial_number(x509.random_serial_number())
cert = cert.not_valid_before(datetime.datetime.utcnow())
cert = cert.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=30))
cert = cert.sign(private_key, hashes.SHA256(), default_backend())

# Write our certificate out to disk.
with open('/tmp/ca.cert', 'wb') as out:
out.write(cert.public_bytes(serialization.Encoding.PEM))

39 changes: 39 additions & 0 deletions utils/ca-csr-create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python3

import datetime

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes

with open("/tmp/acme.key", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)

# Generate a CSR
csr = x509.CertificateSigningRequestBuilder()
csr = csr.subject_name(x509.Name([
# Provide various details about who we are.
x509.NameAttribute(NameOID.COUNTRY_NAME, "AR"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "BA"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Buenos Aires"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "ACME CORP"),
x509.NameAttribute(NameOID.COMMON_NAME, "acme.com"),
])
)

# Sign the CSR with our private key.
csr = csr.sign(private_key, hashes.SHA256(), default_backend())

# Write our CSR out to disk.
with open("/tmp/acme.csr", "wb") as out:
out.write(csr.public_bytes(serialization.Encoding.PEM))

print('Created /tmp/acme.csr')

38 changes: 38 additions & 0 deletions utils/ca-csr-load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3

import datetime

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes


with open("/tmp/ca.cert", "rb") as ca_cert_file:
ca_cert = x509.load_pem_x509_certificate(ca_cert_file.read(), default_backend())

with open("/tmp/acme.csr", "rb") as csr_file:
csr = x509.load_pem_x509_csr(csr_file.read(), default_backend())

with open("/tmp/ca.key", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)

cert = x509.CertificateBuilder().subject_name(csr.subject)
cert = cert.issuer_name(ca_cert.subject)
cert = cert.public_key(csr.public_key())
cert = cert.serial_number(x509.random_serial_number())
cert = cert.not_valid_before(datetime.datetime.utcnow())
cert = cert.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=30))
cert = cert.sign(private_key, hashes.SHA256(), default_backend())

# Write our certificate out to disk.
with open('/tmp/acme.cert', 'wb') as out:
out.write(cert.public_bytes(serialization.Encoding.PEM))

print('Created /tmp/acme.cert')
13 changes: 13 additions & 0 deletions utils/crack-cvv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
import hashlib

algorithm = sys.argv[1]
h = sys.argv[2]

for number in range(0, 1000):
cvv = "{:03}".format(number).encode()
result = hashlib.new(algorithm, cvv).hexdigest()
if h == result:
print('Cracked! CVV:', cvv.decode())
break

12 changes: 12 additions & 0 deletions utils/crack-hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python3

import hashlib
import sys

candidate = sys.argv[1]

for number in range(10000):
h = hashlib.sha512(str(number).encode()).hexdigest()
if h == candidate:
print('Cracked! Password:', number)

7 changes: 7 additions & 0 deletions utils/fernet-generate-key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3

from cryptography.fernet import Fernet

key = Fernet.generate_key()

print(key.decode())
17 changes: 17 additions & 0 deletions utils/generate-hmac.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3

import sys

from binascii import hexlify

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, hmac

key = sys.argv[1].encode()
msg = sys.argv[2].encode()

h = hmac.HMAC(key, hashes.SHA256(), backend=default_backend())
h.update(msg)

print(hexlify(h.finalize()).decode())

23 changes: 23 additions & 0 deletions utils/hashfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3

from binascii import hexlify

import click
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes


@click.command()
@click.argument('input_file', type=click.File('rb'), default='-')
def hashfile(input_file):

data = input_file.read()
digest = hashes.Hash(hashes.SHA512(), backend=default_backend())
digest.update(data)
hexdigest = hexlify(digest.finalize()).decode()

print('{:<12} {}'.format('sha512', hexdigest))


if __name__ == '__main__':
hashfile()
19 changes: 19 additions & 0 deletions utils/luncheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def luhnCheck(card_number):
""" checks to make sure that the card passes a luhn mod-10 checksum """

sum = 0
num_digits = len(card_number)
oddeven = num_digits & 1

for count in range(0, num_digits):
digit = int(card_number[count])

if not (( count & 1 ) ^ oddeven ):
digit = digit * 2
if digit > 9:
digit = digit - 9

sum = sum + digit

return ( (sum % 10) == 0 )

29 changes: 29 additions & 0 deletions utils/rsa-decrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

import sys

from binascii import unhexlify

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

ciphertext = unhexlify(sys.argv[1].encode())

with open("/tmp/acme.key", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)

msg = private_key.decrypt(
ciphertext,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)

print(msg.decode())
29 changes: 29 additions & 0 deletions utils/rsa-encrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

import sys

from binascii import hexlify

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

msg = sys.argv[1].encode()

with open("/tmp/acme.pub", "rb") as key_file:
public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)

ciphertext = public_key.encrypt(
msg,
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)

print(hexlify(ciphertext).decode())

33 changes: 33 additions & 0 deletions utils/rsa-keygen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization

private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)

public_key = private_key.public_key()

pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)

pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)

with open('/tmp/acme.key', 'wb') as out:
out.write(pem_private)

with open('/tmp/acme.pub', 'wb') as out:
out.write(pem_public)

print('Created files in /tmp/acme.key and /tmp/acme.pub')

Loading

0 comments on commit 8a58f64

Please sign in to comment.