Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better validation of keys and certificates #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions vcenter_saml_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,23 @@ def writekey(bytes, verbose):
return key


def check_key_valid(key, verbose=False):
lines = key.splitlines()
if lines[1].startswith('MI'):
def check_key_valid(key_bytes, verbose=False):
"""pkcs keys begin with the following hex structure
30 82 ?? ?? 02 01 00
"""
if key_bytes.startswith(b"0\x82") and key_bytes[4:7] == b"\x02\x01\x00":
return True
else:
if verbose:
print('[!] Key does not begin with magic bytes')
return False


def check_cert_valid(cert_bytes, verbose=False):
"""x509 certs begin with the following hex structure
30 82 ?? ?? 30 82
"""
if cert_bytes.startswith(b"0\x82") and cert_bytes[4:6] == b"0\x82":
return True
else:
if verbose:
Expand All @@ -127,9 +141,9 @@ def get_idp_cert(stream, verbose=False):
if any(not_it in cert_bytes for not_it in not_it_list):
continue

key = writekey(cert_bytes, verbose)
if not check_key_valid(key):
if not check_key_valid(cert_bytes):
continue
key = writekey(cert_bytes, verbose)

print('[*] Successfully extracted the IdP certificate')
return key
Expand Down Expand Up @@ -188,9 +202,9 @@ def get_trusted_cert1(stream, verbose=False):
print('[!] Cert does not contain ssoserverSign - keep looking')
continue

cert1 = writepem(cert1_bytes, verbose)
if not check_key_valid(cert1):
if not check_cert_valid(cert1_bytes):
continue
cert1 = writepem(cert1_bytes, verbose)

print('[*] Successfully extracted trusted certificate 1')
return cert1, domain
Expand Down Expand Up @@ -222,9 +236,9 @@ def get_trusted_cert2(stream, verbose=False):
if verbose:
print(f'Cert 2 Size: {cert2_size}')

cert2 = writepem(cert2_bytes, verbose)
if not check_key_valid(cert2):
if not check_cert_valid(cert2_bytes):
continue
cert2 = writepem(cert2_bytes, verbose)

print('[*] Successfully extracted trusted certificate 2')
return cert2
Expand Down