| 1 |
package pac |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
|
| 8 |
"gopkg.in/jcmturner/gokrb5.v5/crypto" |
| 9 |
"gopkg.in/jcmturner/gokrb5.v5/iana/keyusage" |
| 10 |
"gopkg.in/jcmturner/gokrb5.v5/mstypes" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v5/types" |
| 12 |
"gopkg.in/jcmturner/rpc.v0/ndr" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type CredentialsInfo struct { |
| 19 |
Version uint32 |
| 20 |
EType uint32 |
| 21 |
PACCredentialDataEncrypted []byte |
| 22 |
PACCredentialData CredentialData |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func (c *CredentialsInfo) Unmarshal(b []byte, k types.EncryptionKey) error { |
| 27 |
|
| 28 |
var p int |
| 29 |
var e binary.ByteOrder = binary.LittleEndian |
| 30 |
|
| 31 |
c.Version = ndr.ReadUint32(&b, &p, &e) |
| 32 |
if c.Version != 0 { |
| 33 |
return errors.New("credentials info version is not zero") |
| 34 |
} |
| 35 |
c.EType = ndr.ReadUint32(&b, &p, &e) |
| 36 |
c.PACCredentialDataEncrypted = ndr.ReadBytes(&b, &p, len(b)-p, &e) |
| 37 |
|
| 38 |
err := c.DecryptEncPart(k, &e) |
| 39 |
if err != nil { |
| 40 |
return fmt.Errorf("error decrypting PAC Credentials Data: %v", err) |
| 41 |
} |
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
func (c *CredentialsInfo) DecryptEncPart(k types.EncryptionKey, e *binary.ByteOrder) error { |
| 47 |
if k.KeyType != int32(c.EType) { |
| 48 |
return fmt.Errorf("key provided is not the correct type. Type needed: %d, type provided: %d", c.EType, k.KeyType) |
| 49 |
} |
| 50 |
pt, err := crypto.DecryptMessage(c.PACCredentialDataEncrypted, k, keyusage.KERB_NON_KERB_SALT) |
| 51 |
if err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
var p int |
| 55 |
c.PACCredentialData = ReadPACCredentialData(&pt, &p, e) |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
type CredentialData struct { |
| 61 |
CredentialCount uint32 |
| 62 |
Credentials []SECPKGSupplementalCred |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
func ReadPACCredentialData(b *[]byte, p *int, e *binary.ByteOrder) CredentialData { |
| 67 |
c := ndr.ReadUint32(b, p, e) |
| 68 |
cr := make([]SECPKGSupplementalCred, c, c) |
| 69 |
for i := range cr { |
| 70 |
cr[i] = ReadSECPKGSupplementalCred(b, p, e) |
| 71 |
} |
| 72 |
return CredentialData{ |
| 73 |
CredentialCount: c, |
| 74 |
Credentials: cr, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
type SECPKGSupplementalCred struct { |
| 80 |
PackageName mstypes.RPCUnicodeString |
| 81 |
CredentialSize uint32 |
| 82 |
Credentials []uint8 |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func ReadSECPKGSupplementalCred(b *[]byte, p *int, e *binary.ByteOrder) SECPKGSupplementalCred { |
| 87 |
n, _ := mstypes.ReadRPCUnicodeString(b, p, e) |
| 88 |
cs := ndr.ReadUint32(b, p, e) |
| 89 |
c := make([]uint8, cs, cs) |
| 90 |
for i := range c { |
| 91 |
c[i] = ndr.ReadUint8(b, p) |
| 92 |
} |
| 93 |
return SECPKGSupplementalCred{ |
| 94 |
PackageName: n, |
| 95 |
CredentialSize: cs, |
| 96 |
Credentials: c, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
type NTLMSupplementalCred struct { |
| 102 |
Version uint32 |
| 103 |
Flags uint32 |
| 104 |
LMPassword []byte |
| 105 |
NTPassword []byte |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func ReadNTLMSupplementalCred(b *[]byte, p *int, e *binary.ByteOrder) NTLMSupplementalCred { |
| 110 |
v := ndr.ReadUint32(b, p, e) |
| 111 |
f := ndr.ReadUint32(b, p, e) |
| 112 |
l := ndr.ReadBytes(b, p, 16, e) |
| 113 |
n := ndr.ReadBytes(b, p, 16, e) |
| 114 |
return NTLMSupplementalCred{ |
| 115 |
Version: v, |
| 116 |
Flags: f, |
| 117 |
LMPassword: l, |
| 118 |
NTPassword: n, |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
const ( |
| 123 |
|
| 124 |
NTLMSupCredLMOWF = 31 |
| 125 |
|
| 126 |
NTLMSupCredNTOWF = 30 |
| 127 |
) |