-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautomation-ADGetComputer.yml
195 lines (182 loc) · 7.78 KB
/
automation-ADGetComputer.yml
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
args:
- description: Active Directory Distinguished Name for the desired computer
name: dn
- default: true
description: Name of the desired computer
name: name
- description: Include these AD attributes of the resulting objects in addition to
the default ones
name: attributes
- description: Search computer by this custom field type
name: customFieldType
- description: Search computer by this custom field data (relevant only if `customFieldType`
is provided)
name: customFieldData
- description: The columns headers to show by order
name: headers
- auto: PREDEFINED
description: ' Enter ''true'' to allow nested groups search as well'
name: nestedSearch
predefined:
- "true"
- "false"
comment: |-
Deprecated. Use the ad-get-computer command in the Active Directory Query v2 instead.
Use Active Directory to retrieve detailed information about a computer account. The computer can be specified by name, email, or as an Active Directory Distinguished Name (DN).
If no filters are provided, the result will show all computers.
commonfields:
id: ADGetComputer
version: -1
dependson:
must:
- ad-search
deprecated: true
name: ADGetComputer
outputs:
- contextPath: Endpoint
description: Active Directory Endpoint
- contextPath: Endpoint.Type
description: Type of the Endpoint entity
- contextPath: Endpoint.ID
description: The unique Endpoint DN (Distinguished Name)
- contextPath: Endpoint.Hostname
description: The Endpoint hostname
- contextPath: Endpoint.Groups
description: The groups the endpoint is part of
runonce: false
script: |-
def createEndpointEntities(t,attrs):
endpoints = []
for l in t:
endpoint = {}
endpoint['Type'] = 'AD'
endpoint['ID'] = demisto.get(l,'dn')
endpoint['Hostname'] = demisto.get(l,'name')
endpoint['Groups'] = demisto.get(l,'memberOf').split("<br>")
for attr in set(argToList(attrs)) - set(['dn','name','memberOf']):
endpoint[attr.title()] = demisto.get(l,attr)
endpoints.append(endpoint)
return endpoints
def prettifyDateTimeADFields(resAD):
try:
for m in resAD:
if isError(m):
continue
m['ContentsFormat'] = formats['table']
for f in [ 'lastlogon' , 'lastlogoff' , 'pwdLastSet' , 'badPasswordTime' , 'lastLogonTimestamp' ]:
if f not in m['Contents'][0]:
continue
if m['Contents'][0][f] == "0":
m['Contents'][0][f] = "N/A"
else:
try:
m['Contents'][0][f] = FormatADTimestamp( m['Contents'][0][f] )
except:
pass # Could not prettify timestamp - return as is
for f in [ 'whenChanged' , 'whenCreated' ]:
try:
m['Contents'][0][f] = PrettifyCompactedTimestamp( m['Contents'][0][f] )
except:
pass # Could not prettify timestamp - return as is
return resAD
except Exception as ex:
return { 'Type' : entryTypes['error'], 'ContentsFormat' : formats['text'], 'Contents' : 'Error occurred while parsing output from ad command. Exception info: ' + str(ex) + '\nInvalid output:\n' + str( resAD ) }
def listAll(attrs, using):
args = {}
args['filter'] = "(objectClass=Computer)"
args['attributes'] = attrs
if using:
args['using'] = using
resAD = demisto.executeCommand( 'ad-search', args )
if isError(resAD[0]) or isinstance(demisto.get(resAD[0],'Contents'), str) or isinstance(demisto.get(resAD[0],'Contents'), unicode):
return resAD
else:
resAD = prettifyDateTimeADFields(resAD)
return resAD
def queryBuilder(queryType,queryValue,attrs,nested,using):
args = {}
if using:
args['using'] = using
if not queryType == "distinguishedName":
filterstr = r"(&(objectClass=Computer)({0}={1}))".format(queryType,queryValue)
args['filter'] = filterstr
resp = demisto.executeCommand('ad-search', args)
if isError(resp) or isinstance(demisto.get(resp[0],'Contents'), str) or isinstance(demisto.get(resp[0],'Contents'), unicode):
return resp
else:
userDN = ""
try:
userDN = resp[0]['Contents'][0]['dn']
except Exception as ex:
return { 'Type' : entryTypes['error'], 'ContentsFormat' : formats['text'], '' : 'Error occurred while parsing output from ad command. Exception info: ' + str(ex) }
queryType = "distinguishedName"
queryValue = userDN
filterstr = r"(&(objectClass=Computer)(distinguishedName=" + queryValue + "))"
args['filter'] = filterstr
args['attributes'] = attrs
resAD = demisto.executeCommand( 'ad-search', args )
if nested:
group_attrs = 'name'
filterstr = r"(&(member{0}=".format(':1.2.840.113556.1.4.1941:') + queryValue + ")(objectcategory=group))"
group_resp = demisto.executeCommand( 'ad-search', { 'filter' : filterstr, 'attributes' : group_attrs } )
if isError(group_resp[0]):
return group_resp
else:
data = demisto.get(group_resp[0],'Contents')
data = data if isinstance(data, list) else [data]
memberOf = ",".join([demisto.get(k,'dn') for k in data])
resAD = prettifyDateTimeADFields(resAD)
try:
resAD[0]['Contents']['memberOf'] = memberOf
except:
pass # Could not get nested groups - return as is
return resAD
attrs = 'name,memberOf'
queryValue, queryType = "",""
headers = argToList(demisto.get(demisto.args(), 'headers'))
nestedSearch = True if demisto.get(demisto.args(), 'nestedSearch') == 'true' else False
if demisto.get(demisto.args(), 'attributes'):
attrs += "," + demisto.args()['attributes']
if demisto.get(demisto.args(), 'dn'):
queryValue = demisto.args()['dn']
queryType = "distinguishedName"
elif demisto.get(demisto.args(), 'name'):
queryValue = demisto.args()['name']
queryType = "name"
elif demisto.get(demisto.args(), 'customFieldType'):
if not demisto.get(demisto.args(), 'customFieldData'):
demisto.results({ 'Type' : entryTypes['error'], 'ContentsFormat' : formats['text'], '' : 'To do custom search both "customFieldType" and "customFieldData" should be provided' })
else:
queryValue = demisto.args()['customFieldData']
queryType = demisto.args()['customFieldType']
if queryValue and queryType:
resp = queryBuilder(queryType,queryValue,attrs,nestedSearch,demisto.get(demisto.args(),'using'))
else:
resp = listAll(attrs, demisto.get(demisto.args(),'using'))
if isError(resp):
demisto.results(resp)
else:
context = {}
data = demisto.get(resp[0],'Contents')
if isinstance(data, str) or isinstance(data, unicode) :
md = data
else:
data = data if isinstance(data, list) else [data]
md = tableToMarkdown("Active Directory Computer", data, headers)
context['Endpoint'] = createEndpointEntities(data,attrs)
for m in data:
if demisto.get(m,'name'):
context['DBotScore'] = {'Indicator': m['name'], 'Type': 'hostname', 'Vendor': 'AD', 'Score': 0, 'isTypedIndicator': True}
demisto.results({'Type' : entryTypes['note'],
'Contents': data,
'ContentsFormat' : formats['json'],
'HumanReadable': md,
'ReadableContentsFormat' : formats['markdown'],
'EntryContext' : context})
scripttarget: 0
system: true
tags:
- active directory
- enhancement
- hostname
type: python