This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgophishautomationstop.py
executable file
·161 lines (146 loc) · 4.94 KB
/
gophishautomationstop.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
#!/usr/bin/python3
import boto3
import requests
import os
from datetime import *
import urllib3
import json
import configparser
#Import from config
config = configparser.ConfigParser()
config.read('gophish.ini')
userpath = config['localsys']['userpath']
gophishapikey = config['gophish']['gophishapikey']
availabilityzone = config['aws']['availabilityzone']
securitygroup = config['aws']['securitygroup']
instanceid = config['aws']['instanceid']
campaigndays = config['gophish']['campaigndays']
gophishurl = config['gophish']['gophishurl']
elbprefix = config['aws']['elbprefix']
groupprefix = config['gophish']['groupprefix']
delaydays = config['gophish']['delaydays']
headers = {'Authorization': 'Bearer '+ gophishapikey,}
#Datime objects
class Time:
def __init__(self):
self.today = datetime.today().strftime('%Y-%m-%d')
self.year = date.today().strftime('%Y')
self.datenow = datetime.now()
#XYself.date_today = self.d.date()
self.today = datetime.today()
self.delay = timedelta(days=int(delaydays))
# This function returns an object of Time
def ret():
return Time()
time = ret()
urllib3.disable_warnings()
#Get Gophish Campagin ID
def get_campaginid():
id_list = []
response = requests.get(gophishurl + '/api/campaigns/', headers=headers, verify=False)
responsebody = response.content
responsedecode = json.loads(responsebody.decode('utf-8'))
for i in responsedecode:
id_list.append(i['id'])
return id_list
#Get GoPhish Campagin status
def get_campaginstatus(id_list):
datelist = []
dateform = []
campaginid = []
formatted_id_dict = {}
for campaign in id_list:
response = requests.get(gophishurl + '/api/campaigns/'+ str(campaign) + '/summary', headers=headers, verify=False)
responsebody = response.content
responsedecode = json.loads(responsebody.decode('utf-8'))
if responsedecode['status'] == 'In progress':
datelist.append(responsedecode['send_by_date'])
campaginid.append(responsedecode['id'])
else:
continue
for dateformat in datelist:
dateformat = dateformat[:10]
dateformatted = datetime.strptime(dateformat, "%Y-%m-%d")
dateform.append(dateformatted)
for key in campaginid:
for value in dateform:
formatted_id_dict[key] = value
break
return formatted_id_dict, dateform
#Check and compare dates
def date_check(formatted_id_dict, dateform):
end_id = []
end_campagin_id = []
for key, value in formatted_id_dict.items():
datenow = time.datenow
enddate = value + time.delay
if enddate <= datenow:
end_campagin_id.append(key)
else:
return False
if not end_campagin_id:
return False
else:
return end_campagin_id
#End Gophish campagin ID
def end_campagin(end_campagin_id):
for ending in end_campagin_id:
response = requests.get(gophishurl + '/api/campaigns/'+ str(ending)+'/complete', headers=headers, verify=False)
responsebody = response.content
return True
#Get AWS ELB
def get_elb():
elbnames = []
dnsnames = []
client = boto3.client('elb')
response = client.describe_load_balancers()
elbnames.append(response['LoadBalancerDescriptions'])
for names in elbnames:
for name in names:
for key,value in name.items():
if key == 'LoadBalancerName':
dnsnames.append(value)
return dnsnames
#Remove AWS ELB
def remove_elb(dnsnames):
if dnsnames == []:
pass
else:
client = boto3.client('elb')
for elbs in dnsnames:
response = client.delete_load_balancer(
LoadBalancerName=
elbs,
)
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
return True
#Remove Gophish usergroup
def remove_usergrp():
groupidlst = []
response = requests.get(config['gophish']['gophishurl'] + '/api/groups/', headers=headers, verify=False)
responsebody = response.content
responsedecode = json.loads(responsebody.decode('utf-8'))
for usergrp in responsedecode:
for key,value in usergrp.items():
if 'name' in key:
if groupprefix in value:
groupid = usergrp.get("id")
groupidlst.append(groupid)
else:
pass
for groups in groupidlst:
response = requests.delete(gophishurl + '/api/groups/' + str(groups), headers=headers, verify=False)
responsebody = response.content
def main():
get_campagin = get_campaginid()
formatted_id_dict, dateform = get_campaginstatus(get_campagin)
getelb = get_elb()
get_date = date_check(formatted_id_dict, dateform)
if get_date == False:
quit()
else:
end_camp = end_campagin(get_date)
rm_elb = remove_elb(getelb)
if rm_elb == True:
rm_grp = remove_usergrp()
main()