Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved. 

# 

# This software is provided under under a slightly modified version 

# of the Apache Software License. See the accompanying LICENSE file 

# for more information. 

# 

# Config utilities 

# 

# Author: 

# Ronnie Flathers / @ropnop 

# 

# Description: 

# Helpful enum methods for discovering local admins through SAMR and LSAT 

 

from impacket.dcerpc.v5 import transport, lsat, samr, lsad 

from impacket.dcerpc.v5.dtypes import MAXIMUM_ALLOWED 

 

 

class EnumLocalAdmins: 

def __init__(self, smbConnection): 

self.__smbConnection = smbConnection 

self.__samrBinding = r'ncacn_np:445[\pipe\samr]' 

self.__lsaBinding = r'ncacn_np:445[\pipe\lsarpc]' 

 

def __getDceBinding(self, strBinding): 

rpc = transport.DCERPCTransportFactory(strBinding) 

rpc.set_smb_connection(self.__smbConnection) 

return rpc.get_dce_rpc() 

 

def getLocalAdmins(self): 

adminSids = self.__getLocalAdminSids() 

adminNames = self.__resolveSids(adminSids) 

return adminSids, adminNames 

 

def __getLocalAdminSids(self): 

dce = self.__getDceBinding(self.__samrBinding) 

dce.connect() 

dce.bind(samr.MSRPC_UUID_SAMR) 

resp = samr.hSamrConnect(dce) 

serverHandle = resp['ServerHandle'] 

 

resp = samr.hSamrLookupDomainInSamServer(dce, serverHandle, 'Builtin') 

resp = samr.hSamrOpenDomain(dce, serverHandle=serverHandle, domainId=resp['DomainId']) 

domainHandle = resp['DomainHandle'] 

resp = samr.hSamrOpenAlias(dce, domainHandle, desiredAccess=MAXIMUM_ALLOWED, aliasId=544) 

resp = samr.hSamrGetMembersInAlias(dce, resp['AliasHandle']) 

memberSids = [] 

for member in resp['Members']['Sids']: 

memberSids.append(member['SidPointer'].formatCanonical()) 

dce.disconnect() 

return memberSids 

 

def __resolveSids(self, sids): 

dce = self.__getDceBinding(self.__lsaBinding) 

dce.connect() 

dce.bind(lsat.MSRPC_UUID_LSAT) 

resp = lsad.hLsarOpenPolicy2(dce, MAXIMUM_ALLOWED | lsat.POLICY_LOOKUP_NAMES) 

policyHandle = resp['PolicyHandle'] 

resp = lsat.hLsarLookupSids(dce, policyHandle, sids, lsat.LSAP_LOOKUP_LEVEL.LsapLookupWksta) 

names = [] 

for n, item in enumerate(resp['TranslatedNames']['Names']): 

names.append("{}\\{}".format(resp['ReferencedDomains']['Domains'][item['DomainIndex']]['Name'].encode('utf-16-le'), item['Name'])) 

dce.disconnect() 

return names