-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
model.py
1537 lines (1299 loc) · 64.6 KB
/
model.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import peewee as p
import datetime
from app import apfell_db
import app.crypto as crypto
import json
from uuid import uuid4
def gen_uuid():
return str(uuid4())
class Operator(p.Model):
light_config = json.dumps({
"background-color": "hsl(263, 0%, 98%)",
"text-color": "black",
"hover": "grey",
"highlight": "grey",
"autocomplete": "#e6f3ff",
"highlight-text": "white",
"timestamp": "#24E0FF",
"operator": "#b366ff",
"display": "#FF4D4D",
"new-callback-color": "light",
"new-callback-hue": "",
"table-headers": "white",
"operation-color": "#b366ff",
"success_highlight": "#d5fdd5",
"failure_highlight": "#f68d8d",
"top-caret": "white",
"code-theme": "monokai",
"table-color": "",
"processing": "#fffade",
"processed": "#f0fff0",
"response-background": "hsl(225, 6%, 23%)",
"outline-buttons": "-",
"bg-header": "hsl(263, 15%, 25%)",
"bg-header-dark": " hsl(225, 0%, 45%)",
"bg-card-body": "white",
"bg-card-body-l1": "hsl(225, 0%, 58%)",
"bg-card-bddy-l2": "hsl(225, 0%, 85%)",
"bg-card-footer": "hsl(225, 0%, 54%)",
"bg-body": "hsl(225, 0%, 50%)",
"th": "hsl(255, 0%, 85%)"
})
dark_config = json.dumps({
"background-color": "hsl(225, 6%, 14%)",
"text-color": "#ECEDF0",
"hover": "hsl(225, 6%, 12%)",
"highlight": "hsl(225, 6%, 12%)",
"autocomplete": "#e6f3ff",
"highlight-text": "#b366ff",
"timestamp": "#24E0FF",
"operator": "#b366ff",
"display": "#FF4D4D",
"new-callback-color": "dark",
"new-callback-hue": "",
"table-headers": "#F1F1F1",
"operation-color": "#b366ff",
"success_highlight": "#340080",
"failure_highlight": "#f68d8d",
"top-caret": "white",
"code-theme": "monokai",
"table-color": 'table-dark',
"response-background": "hsl(225, 6%, 23%)",
"processing": "hsl(225, 6%, 14%)",
"processed": "hsl(225, 6%, 14%)",
"outline-buttons": "-outline-",
"bg-header": "hsl(225, 6%, 18%)",
"bg-header-dark": " hsl(225, 6%, 13%)",
"bg-card-body": "hsl(225, 6%, 22%)",
"bg-card-body-l1": "hsl(225, 6%, 23%)",
"bg-card-body-l2": "hsl(225, 6%, 27%)",
"bg-card-footer": "hsl(225, 6%, 23%)",
"bg-body": "hsl(225, 6%, 18%)",
"th": "hsl(225, 6%, 20%)"
})
username = p.TextField(unique=True, null=False)
password = p.TextField(null=False)
admin = p.BooleanField(null=True, default=False)
creation_time = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
last_login = p.DateTimeField(default=None, null=True)
# option to simply de-activate an account instead of delete it so you keep all your relational data intact
active = p.BooleanField(null=False, default=True)
current_operation = p.ForeignKeyField(p.DeferredRelation('Operation'), null=True)
ui_config = p.TextField(null=False, default=dark_config)
class Meta:
ordering = ['-id', ]
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'current_operation':
r[k] = getattr(self, k).name
elif k != 'password' and 'default' not in k:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['creation_time'] = r['creation_time'].strftime('%m/%d/%Y %H:%M:%S')
if 'last_login' in r and r['last_login'] is not None:
r['last_login'] = r['last_login'].strftime('%m/%d/%Y %H:%M:%S')
else:
r['last_login'] = "" # just indicate that account created, but they never logged in
return r
def __str__(self):
return str(self.to_json())
async def check_password(self, password):
temp_pass = await crypto.hash_SHA512(password)
return self.password.lower() == temp_pass.lower()
async def hash_password(self, password):
return await crypto.hash_SHA512(password)
# This is information about a class of payloads (like Apfell-jxa)
# This will have multiple Command class objects associated with it
# Users can create their own commands and payload types as well
class PayloadType(p.Model):
ptype = p.TextField(null=False, unique=True) # name of the payload type
operator = p.ForeignKeyField(Operator, null=False)
creation_time = p.DateTimeField(null=False, default=datetime.datetime.utcnow)
file_extension = p.CharField(null=True)
# if this type requires another payload to be already created
wrapper = p.BooleanField(default=False, null=False)
# which payload is this one wrapping
wrapped_payload_type = p.ForeignKeyField(p.DeferredRelation('PayloadType'), null=True)
# allow the ability to specify a template for people tha want to extend the payload type with more commands
command_template = p.TextField(null=False, default="")
supported_os = p.TextField(null=False, default="") # indicate which OS/versions this payload works for
execute_help = p.TextField(null=False, default="") # helpful options on how to run
# external indicates if this is an externally created payload
external = p.BooleanField(default=False, null=False)
# information about getting information to/from another container or machine for building/loading/transforming
last_heartbeat = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
container_running = p.BooleanField(null=False, default=False)
service = p.TextField(null=False, default="rabbitmq")
icon = p.TextField(null=True)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'operator':
r[k] = getattr(self, k).username
elif k == 'wrapped_payload_type':
r[k] = getattr(self, k).ptype
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['creation_time'] = r['creation_time'].strftime('%m/%d/%Y %H:%M:%S')
r['last_heartbeat'] = r['last_heartbeat'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class Transform(p.Model):
name = p.CharField() # what function in the TransformOperation class will be executed
parameter = p.TextField(null=False, default="") # optionally pass a parameter to a function
order = p.IntegerField() # which order in the list of transforms is this
payload_type = p.ForeignKeyField(PayloadType, null=False)
t_type = p.CharField() # is this for payload: creation, module load?
operator = p.ForeignKeyField(Operator) # who created this transform
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'payload_type':
r[k] = getattr(self, k).ptype
elif k == 'operator':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
# This has information about a specific command that can be executed by a PayloadType
# Custom commands can be created by users
# There will be a new Command instance for every cmd+payload_type combination
# (each payload_type needs its own 'shell' command because they might be implemented differently)
class Command(p.Model):
needs_admin = p.BooleanField(null=False, default=False)
# generates get-help info on the command
help_cmd = p.TextField(null=False, default="")
description = p.TextField(null=False)
cmd = p.CharField(null=False) # shell, for example, doesn't have to be a globally unique name
# this command applies to what payload types
payload_type = p.ForeignKeyField(PayloadType, null=False)
operator = p.ForeignKeyField(Operator, null=False)
creation_time = p.DateTimeField(null=False, default=datetime.datetime.utcnow)
version = p.IntegerField(null=False, default=1) # what version, so we can know if loaded commands are out of date
is_exit = p.BooleanField(null=False, default=False) # indicate if this command is the exit command for a payload type
class Meta:
indexes = ((('cmd', 'payload_type'), True),)
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'operator':
r[k] = getattr(self, k).username
elif k == 'payload_type':
r[k] = getattr(self, k).ptype
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['creation_time'] = r['creation_time'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
# these parameters are used to create an easily parsible JSON 'params' field for the agent to utilize
class CommandParameters(p.Model):
command = p.ForeignKeyField(Command)
name = p.TextField(null=False) # what is the name of the parameter (what is displayed in the UI and becomes dictionary key)
# String, Boolean, Number, Array, Choice, ChoiceMultiple, Credential, File
type = p.CharField(null=False, default="String")
hint = p.TextField(null=False, default="") # give a hint as to what the operator should input here, only used if isBool is false
choices = p.TextField(null=False, default="") # comma separated list of possible choices
required = p.BooleanField(null=False, default=False) # is this a required parameter
operator = p.ForeignKeyField(Operator, null=False)
class Meta:
indexes = ((('command', 'name'), True),)
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'command' and getattr(self, k) is not None and getattr(self, k) != "null":
r[k] = getattr(self, k).id
r['cmd'] = getattr(self, k).cmd
r['payload_type'] = getattr(self, k).payload_type.ptype
elif k == 'operator':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
# users will be associated with operations
# payload_types and commands are associated with all operations
# when creating a new operation, associate all the default c2profiles with it
class Operation(p.Model):
name = p.TextField(null=False, unique=True)
admin = p.ForeignKeyField(Operator, null=False) # who is an admin of this operation
complete = p.BooleanField(null=False, default=False)
# auto create an AES PSK key when the operation is created for things like PFS with EKE
AESPSK = p.TextField(null=False, unique=True)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'admin':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
class DisabledCommandsProfile(p.Model):
# A set of commands that are disabled for an operation due to OPSEC concerns
# only the lead of an operation will be able to set this for other operators on that operation
name = p.TextField(null=False) # name to group a bunch of disabled commands together for an operator
command = p.ForeignKeyField(Command, null=False)
class Meta:
indexes = ( (('command', 'name'), True), )
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'command':
r[k] = getattr(self, k).cmd
r['command_id'] = getattr(self, k).id
r['payload_type'] = getattr(self, k).payload_type.ptype
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
class DisabledCommands(p.Model):
command = p.ForeignKeyField(Command, null=False)
operator = p.ForeignKeyField(Operator, null=False)
operation = p.ForeignKeyField(Operation, null=False)
class Meta:
indexes = ( (('command', 'operator', 'operation'), True), )
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'command':
r[k] = getattr(self, k).cmd
r['command_id'] = getattr(self, k).id
r['payload_type'] = getattr(self, k).payload_type.ptype
elif k == "operator":
r[k] = getattr(self, k).username
elif k == "operation":
r[k] = getattr(self, k).name
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
# because operators and operations are a many-to-many relationship, we need a join table to facilitate
# this means operator class doesn't mention operation, and operation doesn't mention operator - odd, I know
class OperatorOperation(p.Model):
operator = p.ForeignKeyField(Operator)
operation = p.ForeignKeyField(Operation)
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
base_disabled_commands = p.ForeignKeyField(DisabledCommandsProfile, null=True)
class Meta:
indexes = ( (('operator', 'operation'), True), )
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'operator':
r[k] = getattr(self, k).username
elif k == 'operation':
r[k] = getattr(self, k).name
elif k == 'base_disabled_commands':
r[k] = getattr(self, k).name
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
# an instance of a c2profile
class C2Profile(p.Model):
name = p.TextField(null=False, unique=True) # registered unique name for this c2 profile
description = p.TextField(null=True, default="")
# list of payload types that are supported (i.e. have a corresponding module created for them on the client side
operator = p.ForeignKeyField(Operator, null=False) # keep track of who created/registered this profile
# This has information about supported payload types, but that information is in a separate join table
creation_time = p.DateTimeField(default=datetime.datetime.utcnow, null=False) # (indicates "when")
# indicates if the c2 profile is running
running = p.BooleanField(null=False, default=False)
last_heartbeat = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
# indicates if the c2 profile container is up and able to receive tasking
container_running = p.BooleanField(null=False, default=False)
# icon information
icon = p.TextField(null=True)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'operator':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['creation_time'] = r['creation_time'].strftime('%m/%d/%Y %H:%M:%S')
r['last_heartbeat'] = r['last_heartbeat'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
# this is a join table between the many to many relationship between payload_types and c2profiles
# ex: apfell-jxa PayloadType instance should be tied to default/twitter/etc c2profiles
# and default c2profile should be tied to apfell-jxa, apfell-swift, etc
class PayloadTypeC2Profile(p.Model):
payload_type = p.ForeignKeyField(PayloadType)
c2_profile = p.ForeignKeyField(C2Profile)
class Meta:
indexes = ( (('payload_type', 'c2_profile'), True), )
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'payload_type':
r[k] = getattr(self, k).ptype
r['payload_type_id'] = getattr(self, k).id
elif k == 'c2_profile':
r[k] = getattr(self, k).name
r['c2_profile_id'] = getattr(self, k).id
r['c2_profile_description'] = getattr(self, k).description
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
# this is an instance of a payload
class Payload(p.Model):
# this is actually a sha256 from other information about the payload
uuid = p.TextField(unique=True, null=False)
# tag a payload with information like spearphish, custom bypass, lat mov, etc (indicates "how")
tag = p.TextField(null=True)
# creator of the payload, cannot be null! must be attributed to somebody (indicates "who")
operator = p.ForeignKeyField(Operator, null=False)
creation_time = p.DateTimeField(default=datetime.datetime.utcnow, null=False) # (indicates "when")
# this is fine because this is an instance of a payload, so it's tied to one PayloadType
payload_type = p.ForeignKeyField(PayloadType, null=False)
# this will signify if a current callback made / spawned a new callback that's checking in
# this helps track how we're getting callbacks (which payloads/tags/parents/operators)
pcallback = p.ForeignKeyField(p.DeferredRelation('Callback'), null=True)
location = p.CharField(null=True) # location on disk of the payload
c2_profile = p.ForeignKeyField(C2Profile, null=False) # identify which C2 profile is being used
operation = p.ForeignKeyField(Operation, null=False)
wrapped_payload = p.ForeignKeyField(p.DeferredRelation('Payload'), null=True)
deleted = p.BooleanField(null=False, default=False)
# if the payload is in the build process: building, success, error
build_container = p.TextField(null=False)
build_phase = p.TextField(null=False, default="building")
build_message = p.TextField(null=False, default="") # capture error or any other info
hosted_path = p.TextField(null=False, default="") # current hosting path if we want a unique hosted path
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'operator':
r[k] = getattr(self, k).username
elif k == 'pcallback':
r[k] = getattr(self, k).id
elif k == 'c2_profile':
r[k] = getattr(self, k).name
elif k == 'payload_type':
r[k] = getattr(self, k).ptype
r["external"] = getattr(self, k).external
elif k == 'operation':
r[k] = getattr(self, k).name
elif k == 'wrapped_payload':
r[k] = getattr(self, k).uuid
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['creation_time'] = r['creation_time'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
# a specific payload instance has multiple commands associated with it, so we need to track that
# commands can be loaded/unloaded at run time, so we need to track creation_time
class PayloadCommand(p.Model):
payload = p.ForeignKeyField(Payload, null=False)
# this is how we can tell what commands are in a payload by default and if they might be out of date
command = p.ForeignKeyField(Command, null=False)
creation_time = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
# version of a command when the payload is created might differ from later in time, so save it off
version = p.IntegerField(null=False)
class Meta:
indexes = ((('payload', 'command'), True),)
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'payload':
r[k] = getattr(self, k).uuid
elif k == 'command':
r[k] = getattr(self, k).cmd
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['creation_time'] = r['creation_time'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
# C2 profiles will have various parameters that need to be stamped in at payload creation time
# this will specify the name and value to look for
class C2ProfileParameters(p.Model):
c2_profile = p.ForeignKeyField(C2Profile)
name = p.TextField(null=False) # what the parameter is called. ex: Callback address
key = p.TextField(null=False) # what the stamping should look for. ex: XXXXX
hint = p.TextField(null=False, default="") # Hint for the user when setting the parameters
class Meta:
indexes = ((('c2_profile', 'name'), True),)
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'c2_profile':
r[k] = getattr(self, k).name
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
# c2 profiles will have various parameters that need to be stamped in when the payload is created
# This is an opportunity to specify key-value pairs for specific C2 profiles
# There can be many of these per c2 profile or none
# This holds the specific values used in the C2ProfileParameters and which payload they're associated with
class C2ProfileParametersInstance(p.Model):
c2_profile_parameters = p.ForeignKeyField(C2ProfileParameters)
value = p.TextField(null=False) # this is what we will stamp in instead
payload = p.ForeignKeyField(Payload, null=True) # the specific payload instance these values apply to
instance_name = p.TextField(null=True) # name the group of parameter instances if we want to save off values for later
operation = p.ForeignKeyField(Operation, null=True) # tie this instance to an operation if there's no payload
class Meta:
indexes = ((('c2_profile_parameters', 'value', 'payload'), True), (('c2_profile_parameters','instance_name', 'operation'),True))
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'c2_profile_parameters' and getattr(self, k) is not None and getattr(self, k) != "null":
r['c2_profile'] = getattr(self, k).c2_profile.name
r['c2_profile_name'] = getattr(self, k).name
r['c2_profile_key'] = getattr(self, k).key
elif k == 'payload':
r[k] = getattr(self, k).uuid
elif k == 'operation' and getattr(self, k) is not None:
r[k] = getattr(self, k).name
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
class Callback(p.Model):
agent_callback_id = p.TextField(unique=True, null=False, default=gen_uuid)
init_callback = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
last_checkin = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
user = p.CharField(null=False)
host = p.CharField(null=False)
pid = p.IntegerField(null=False)
ip = p.CharField(max_length=100, null=False)
description = p.TextField(null=True)
operator = p.ForeignKeyField(Operator, null=False)
active = p.BooleanField(default=True, null=False)
# keep track of the parent callback from this one
pcallback = p.ForeignKeyField(p.DeferredRelation('Callback'), null=True)
registered_payload = p.ForeignKeyField(Payload, null=False) # what payload is associated with this callback
integrity_level = p.IntegerField(null=True, default=2)
# an operator can lock a callback to themselves so that other users cannot issue commands as well
locked = p.BooleanField(default=False)
locked_operator = p.ForeignKeyField(Operator, null=True, related_name="locked_operator")
operation = p.ForeignKeyField(Operation, null=False)
# the following information comes from the c2 profile if it wants to provide some form of encryption
encryption_type = p.CharField(null=True) # the kind of encryption on this callback (aes, xor, rc4, etc)
decryption_key = p.TextField(null=True) # base64 of the key to use to decrypt traffic
encryption_key = p.TextField(null=True) # base64 of the key to use to encrypt traffic
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'pcallback':
r[k] = getattr(self, k).id
elif k == 'operator':
r[k] = getattr(self, k).username
elif k == 'registered_payload' and getattr(self, k) is not None and getattr(self, k) != "null":
r[k] = getattr(self, k).uuid
r['payload_type'] = getattr(self, k).payload_type.ptype
r['c2_profile'] = getattr(self, k).c2_profile.name
r['payload_description'] = getattr(self, k).tag
elif k == 'operation':
r[k] = getattr(self, k).name
elif k == 'locked_operator' and getattr(self, k) is not None and getattr(self, k) != "null":
r[k] = getattr(self, k).username
elif k == 'encryption_key' or k == 'decryption_key' or k == 'encryption_type':
pass # we don't need to include these things all over the place, explicitly ask for them for more control
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['init_callback'] = r['init_callback'].strftime('%m/%d/%Y %H:%M:%S')
r['last_checkin'] = r['last_checkin'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class LoadedCommands(p.Model):
# this keeps track of which commands and versions are currently loaded in which callbacks
command = p.ForeignKeyField(Command, null=False)
callback = p.ForeignKeyField(Callback, null=False)
operator = p.ForeignKeyField(Operator, null=False) # so we know who loaded which commands
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False) # when it was loaded
version = p.IntegerField(null=False) # which version of the command is loaded, so we know if it's out of date
class Meta:
indexes = ((('command', 'callback'), True),) # can't have the same command loaded multiple times in a callback
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'command':
r[k] = getattr(self, k).cmd
r['version'] = getattr(self, k).version
elif k == 'callback':
r[k] = getattr(self, k).id
elif k == 'operator':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class Task(p.Model):
agent_task_id = p.TextField(unique=True, null=False, default=gen_uuid)
command = p.ForeignKeyField(Command, null=True) # could be added via task/clear or scripting by bot
params = p.TextField(null=True) # this will have the instance specific params (ex: id)
# make room for ATT&CK ID (T#) if one exists or enable setting this later
status_timestamp_preprocessing = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
status_timestamp_submitted = p.DateTimeField(null=True)
status_timestamp_processing = p.DateTimeField(null=True)
status_timestamp_processed = p.DateTimeField(null=True)
# this is the last timestamp that something happened
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
# every task is associated with a specific callback that executes the task
callback = p.ForeignKeyField(Callback, null=False)
# the operator to issue the command can be different from the one that spawned the callback
operator = p.ForeignKeyField(Operator, null=False)
status = p.CharField(null=False, default="preprocessing") # [preprocessing, submitted, processing, processed]
# save off the original params in the scenarios where we to transforms on it for logging and tracking purposes
original_params = p.TextField(null=True)
# people can add a comment to the task
comment = p.TextField(null=False, default="")
# the user that added the above comment
comment_operator = p.ForeignKeyField(Operator, related_name="comment_operator", null=True)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'callback':
r[k] = getattr(self, k).id
elif k == 'operator':
r[k] = getattr(self, k).username
elif k == 'command':
if getattr(self, k) and getattr(self, k) != "null":
r[k] = getattr(self, k).cmd
r['command_id'] = getattr(self, k).id
elif k == 'comment_operator':
if getattr(self, k) and getattr(self, k) != "null":
r[k] = getattr(self, k).username
else:
r[k] = "null"
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
if r['status_timestamp_preprocessing'] is not None:
r['status_timestamp_preprocessing'] = r['status_timestamp_preprocessing'].strftime('%m/%d/%Y %H:%M:%S')
if 'status_timestamp_submitted' in r and r['status_timestamp_submitted'] is not None:
r['status_timestamp_submitted'] = r['status_timestamp_submitted'].strftime('%m/%d/%Y %H:%M:%S')
if 'status_timestamp_processing' in r and r['status_timestamp_processing'] is not None:
r['status_timestamp_processing'] = r['status_timestamp_processing'].strftime('%m/%d/%Y %H:%M:%S')
if 'status_timestamp_processed' in r and r['status_timestamp_processed'] is not None:
r['status_timestamp_processed'] = r['status_timestamp_processed'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class Response(p.Model):
response = p.TextField(null=True)
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
task = p.ForeignKeyField(Task, null=False)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'task':
r[k] = (getattr(self, k)).to_json()
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class FileMeta(p.Model):
agent_file_id = p.TextField(unique=True, null=False, default=gen_uuid)
total_chunks = p.IntegerField(null=False) # how many total chunks will there be
chunks_received = p.IntegerField(null=False, default=0) # how many we've received so far
task = p.ForeignKeyField(Task, null=True) # what task caused this file to exist in the database
complete = p.BooleanField(null=False, default=False)
path = p.TextField(null=False) # where the file is located on local disk
operation = p.ForeignKeyField(Operation, null=False)
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
deleted = p.BooleanField(null=False, default=False)
operator = p.ForeignKeyField(Operator, null=True) # specify this in case it was a manual registration
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'task' and getattr(self, k) is not None and getattr(self, k) != "null":
r[k] = getattr(self, k).id
r['cmd'] = getattr(self, k).command.cmd
elif k == 'operation':
r[k] = getattr(self, k).name
elif k == 'operator':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class ATTACK(p.Model):
# store ATT&CK data
t_num = p.CharField(null=False, unique=True)
name = p.TextField(null=False)
os = p.TextField(null=False)
tactic = p.CharField(null=False)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
class ATTACKCommand(p.Model):
attack = p.ForeignKeyField(ATTACK, null=False)
command = p.ForeignKeyField(Command, null=False)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'attack':
r['t_num'] = getattr(self, k).t_num
r['attack_name'] = getattr(self, k).name
elif k == 'command':
r[k] = getattr(self, k).cmd
r['command_id'] = getattr(self, k).id
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
class ATTACKTask(p.Model):
attack = p.ForeignKeyField(ATTACK, null=False)
task = p.ForeignKeyField(Task, null=False)
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'attack':
r[k] = getattr(self, k).t_num
r['attack_name'] = getattr(self, k).name
elif k == 'task':
r[k] = getattr(self, k).id
r['task_command'] = getattr(self, k).command.cmd
r['task_params'] = getattr(self, k).params
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
return r
def __str__(self):
return str(self.to_json())
class Credential(p.Model):
type = p.CharField(null=False) # what kind of credential is it? [hash, password, certificate, etc]
# if you know the task, you know who, what, where, when, etc that caused this thing to exist
# task can be null though which means it was manually entered
task = p.ForeignKeyField(Task, null=True) # what task caused this credential to be here?
user = p.TextField(null=False) # whose credential is this
domain = p.TextField() # which domain does this credential apply?
operation = p.ForeignKeyField(Operation) # which operation does this credential belong to?
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False) # when did we get it?
credential = p.TextField(null=False) # the actual credential we captured
operator = p.ForeignKeyField(Operator, null=False) # who got us this credential? Especially needed if manual entry
class Meta:
indexes = ((('user', 'domain', 'credential', 'operation'), True),)
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'task':
if getattr(self, k) != "null" and getattr(self, k) is not None:
r[k] = getattr(self, k).id
r['task_command'] = getattr(self, k).command.cmd
elif k == 'operation':
r[k] = getattr(self, k).name
elif k == 'operator':
r[k] = getattr(self, k).username
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class Keylog(p.Model):
# if you know the task, you know who, where, when, etc
task = p.ForeignKeyField(Task) # what command caused this to exist
keystrokes = p.TextField(null=False) # what did you actually capture
window = p.TextField() # if possible, what's the window title for where these keystrokes happened
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False) # when did we get these keystrokes?
operation = p.ForeignKeyField(Operation, null=False)
user = p.TextField(null=False) # whose keystrokes are these? The agent needs to tell us
class Meta:
database = apfell_db
def to_json(self):
r = {}
for k in self._data.keys():
try:
if k == 'task' and getattr(self, k) is not None and getattr(self, k) != "null":
r[k] = getattr(self, k).id
r['callback'] = getattr(self, k).callback.id
elif k == 'operation':
r[k] = getattr(self, k).name
else:
r[k] = getattr(self, k)
except:
r[k] = json.dumps(getattr(self, k), default=lambda o: o.to_json())
r['timestamp'] = r['timestamp'].strftime('%m/%d/%Y %H:%M:%S')
return r
def __str__(self):
return str(self.to_json())
class CommandTransform(p.Model):
# these are transforms that apply to a command that can be toggled on/off for each task
command = p.ForeignKeyField(Command, null=False) # which command this applies to
name = p.TextField(null=False) # must be unique with the command
operator = p.ForeignKeyField(Operator) # who created this transform
timestamp = p.DateTimeField(default=datetime.datetime.utcnow, null=False)
# might as well allow the ability to take multiple transforms
order = p.IntegerField(null=False, default=1) # which order in the list of transforms is this
# ability to pass in a parameter to this transform
parameter = p.TextField(null=False, default="")
# make these transforms specific to the operation
operation = p.ForeignKeyField(Operation, null=False)
active = p.BooleanField(null=False, default=True) # indicate if this transform is globally active on the command
class Meta:
database = apfell_db
def to_json(self):
r = {}