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

Initial Python2 -> Python3 conversion #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
32 changes: 15 additions & 17 deletions PAC.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/local/bin/python2 -tt
#!/usr/bin/env python3

import struct
import collections
Expand Down Expand Up @@ -29,12 +29,12 @@ def TimeToBytes(time):
else:
td = time - datetime.datetime(1601,1,1)
#seconds = math.floor(td.total_seconds())
hundredsofnano = long(math.floor(td.total_seconds()) * 10000000)
hundredsofnano = int(math.floor(td.total_seconds()) * 10000000)

if hasattr(time, 'nanosecond'):
hundredsofnano += long(time.nanosecond / 100)
hundredsofnano += int(time.nanosecond / 100)
else:
hundredsofnano += long(time.microseconds * 10)
hundredsofnano += int(time.microseconds * 10)

return struct.pack('<Q', hundredsofnano)

Expand Down Expand Up @@ -97,9 +97,7 @@ def __str__(self):
return '%s.%09i' % (s.split('.')[0], self.nanosecond)
return s

class PacInfoStructure(object):
__metaclass__ = ABCMeta

class PacInfoStructure(object, metaclass=ABCMeta):
PrettyName = 'PacInfoStructure'

Type = None
Expand Down Expand Up @@ -505,11 +503,11 @@ def load(self, pac):
self.PacKdcChecksum = pis

if pis.Data != pis.encode():
print "NO MATCH!! %s" % pis.PrettyName
print("NO MATCH!! %s" % pis.PrettyName)

cmp(pis.Data, pis.encode(), verbose=True)
#print '%s\n%s' % (pis.Data.encode('hex'), pis.encode().encode('hex'))
print '----'
print('----')


def encode(self):
Expand Down Expand Up @@ -549,14 +547,14 @@ def cmp(s1, s2, comparelen=None, verbose=False):

if s1[:comparelen] == s2[:comparelen]:
if verbose:
print 'SAME %i %i' % (len(s1), len(s2))
print('SAME %i %i' % (len(s1), len(s2)))
return True
else:
if verbose:
print 'NOT SAME'
print s1[:comparelen].encode('hex')
print
print s2[:comparelen].encode('hex')
print('NOT SAME')
print(s1[:comparelen].encode('hex'))
print()
print(s2[:comparelen].encode('hex'))
return False


Expand All @@ -579,17 +577,17 @@ def main():
a = a.decode('hex')

p = PAC(a)
print p.PacLoginInfo.GroupRid
print(p.PacLoginInfo.GroupRid)
p.PacLoginInfo.GroupRid = 77
print p.PacLoginInfo.GroupRid
print(p.PacLoginInfo.GroupRid)
#print p


p2 = p.encode()

#cmp(a, p2, None, True)

print a == p2
print(a == p2)



Expand Down
18 changes: 9 additions & 9 deletions asreq-crack.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3

# Author
# ------
Expand Down Expand Up @@ -47,20 +47,20 @@ def crack(user_key, wordlist, plaintext_password):
sys.stderr.write(' Done!\n')
sys.stderr.write(' [+] Cracked with password(RC4) %s(%s)\n' % (plaintext_password,binascii.b2a_hex(user_key[1])))
except Exception as e:
print ' ERROR:', e
print(' ERROR:', e)

if __name__ == '__main__':
from getopt import getopt
from getpass import getpass

def usage_and_exit():
print >> sys.stderr, 'USAGE:'
print >> sys.stderr, '%s' % sys.argv[0]
print >> sys.stderr, ''
print >> sys.stderr, 'OPTIONS:'
print >> sys.stderr, ' -w <dictionary file>'
print >> sys.stderr, ' -p <clearPassword>'
print >> sys.stderr, ' --rc4 <ntlmHash>'
print('USAGE:', file=sys.stderr)
print('%s' % sys.argv[0], file=sys.stderr)
print('', file=sys.stderr)
print('OPTIONS:', file=sys.stderr)
print(' -w <dictionary file>', file=sys.stderr)
print(' -p <clearPassword>', file=sys.stderr)
print(' --rc4 <ntlmHash>', file=sys.stderr)
sys.exit(1)

opts, args = getopt(sys.argv[1:], 'w:p:', ['rc4='])
Expand Down
14 changes: 7 additions & 7 deletions extractasrepfrompcap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3

# Author
# ------
Expand Down Expand Up @@ -33,7 +33,7 @@ def findkerbpayloads(packets, verbose=False):
for p in packets:
# UDP
if p.haslayer(UDP) and p.sport == 88 and p[UDP].load[MESSAGETYPEOFFSETUDP] == AS_REP:
if verbose: print "found UDP payload of size %i" % len(p[UDP].load)
if verbose: print("found UDP payload of size %i" % len(p[UDP].load))
kploads.append(p[UDP].load)

#TCP
Expand All @@ -48,8 +48,8 @@ def findkerbpayloads(packets, verbose=False):
else:
#print 'ERROR: Size is incorrect: %i vs %i' % (size, len(p[TCP].load))
unfinished[(p[IP].src, p[IP].dst, p[TCP].dport)] = (p[TCP].load[4:size+4], size)
if verbose: print "found TCP payload of size %i" % size
elif unfinished.has_key((p[IP].src, p[IP].dst, p[TCP].dport)):
if verbose: print("found TCP payload of size %i" % size)
elif (p[IP].src, p[IP].dst, p[TCP].dport) in unfinished:
ticketdata, size = unfinished.pop((p[IP].src, p[IP].dst, p[TCP].dport))
ticketdata += p[TCP].load
#print "cont: %i %i" % (len(ticketdata), size)
Expand All @@ -59,7 +59,7 @@ def findkerbpayloads(packets, verbose=False):
unfinished[(p[IP].src, p[IP].dst, p[TCP].dport)] = (ticketdata, size)
else:
# OH NO! Oversized!
print 'Too much data received! Source: %s Dest: %s DPort %i' % (p[IP].src, p[IP].dst, p[TCP].dport)
print('Too much data received! Source: %s Dest: %s DPort %i' % (p[IP].src, p[IP].dst, p[TCP].dport))


return kploads
Expand All @@ -85,9 +85,9 @@ def findkerbpayloads(packets, verbose=False):
packets = rdpcap(f)
kploads += findkerbpayloads(packets, args.verbose)
if len(kploads) == 0:
print 'no payloads found'
print('no payloads found')
else:
print 'writing %i hex encoded payloads to %s' % (len(kploads), args.outfile.name)
print('writing %i hex encoded payloads to %s' % (len(kploads), args.outfile.name))
for p in kploads:
args.outfile.write(p.encode('hex') + '\n')

Expand Down
18 changes: 11 additions & 7 deletions kek/ccache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@
# Contact : sylvain dot monne at solucom dot fr
# http://twitter.com/bidord

from collections import namedtuple
import struct
from collections import namedtuple
from struct import pack, unpack

from util import gt2epoch, bitstring2int
from krb5 import encode, Ticket, NT_PRINCIPAL
from .krb5 import encode, Ticket, NT_PRINCIPAL
from .util import gt2epoch, bitstring2int

CCacheCredential = namedtuple('CCacheCredential', 'client server key time is_skey tktflags addrs authdata ticket second_ticket')
CCacheCredential = namedtuple('CCacheCredential',
'client server key time is_skey tktflags addrs authdata ticket second_ticket')
CCacheKeyblock = namedtuple('CCacheKeyblock', 'keytype etype keyvalue')
CCacheTimes = namedtuple('CCacheTimes', 'authtime starttime endtime renew_till')
CCacheAddress = namedtuple('CCacheAddress', 'addrtype addrdata')
CCacheAuthdata = namedtuple('CCacheAuthdata', 'authtype authdata')
CCachePrincipal = namedtuple('CCachePrincipal', 'name_type realm components')

VERSION = 0x0504
DEFAULT_HEADER = '00010008ffffffff00000000'.decode('hex')
DEFAULT_HEADER = '00010008ffffffff00000000'


class CCache(object):
def __init__(self, primary_principal, credentials=[], header=DEFAULT_HEADER):
if not isinstance(primary_principal, CCachePrincipal):
if isinstance(primary_principal, basestring) and '@' in primary_principal:
if isinstance(primary_principal, str) and '@' in primary_principal:
realm, user_name = primary_principal.split('@', 1)
elif isinstance(primary_principal, tuple) and len(primary_principal) == 2:
realm, user_name = primary_principal
Expand Down Expand Up @@ -88,7 +90,7 @@ def read_principal(cls, fp):
realm = cls.read_string(fp)
components = [cls.read_string(fp) for i in range(num_components)]
return CCachePrincipal(name_type, realm, components)

@classmethod
def write_principal(cls, fp, p):
fp.write(pack('>II', p.name_type, len(p.components)))
Expand Down Expand Up @@ -157,12 +159,14 @@ def write_credential(cls, fp, c):
cls.write_string(fp, c.ticket)
cls.write_string(fp, c.second_ticket)


def get_tgt_cred(ccache):
for credential in ccache.credentials:
if credential.server.components[0] == 'krbtgt':
return credential
raise ValueError('No TGT in CCache!')


def kdc_rep2ccache(kdc_rep, kdc_rep_enc):
return CCacheCredential(
client=CCachePrincipal(
Expand Down
4 changes: 2 additions & 2 deletions kek/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from Crypto.Cipher import ARC4
from Crypto.Hash import HMAC, MD5, MD4
except ImportError:
from _crypto import ARC4, MD5, MD4
from ._crypto import ARC4, MD5, MD4
import hmac as HMAC

import binascii
Expand All @@ -26,7 +26,7 @@
HMAC_MD5 = 0xFFFFFF76

def random_bytes(n):
return ''.join(chr(c) for c in sample(xrange(256), n))
return ''.join(chr(c) for c in sample(range(256), n))

def decrypt(etype, key, msg_type, encrypted):
if etype != RC4_HMAC:
Expand Down
4 changes: 2 additions & 2 deletions kek/krb5.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from pyasn1.codec.der.encoder import encode
from pyasn1.codec.der.decoder import decode

from crypto import encrypt, decrypt, checksum, RC4_HMAC, RSA_MD5
from util import epoch2gt
from .crypto import encrypt, decrypt, checksum, RC4_HMAC, RSA_MD5
from .util import epoch2gt
from struct import pack, unpack

NT_UNKNOWN = 0
Expand Down
Loading