-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
273 lines (236 loc) · 7.87 KB
/
app.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
# -*- coding: utf-8 -*-
# app.py
import sys
import base64
from datetime import datetime
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms.fields import TimeField
from wtforms import Form, FieldList, FormField, IntegerField, SelectField, \
StringField, TextAreaField, SubmitField, DateField, BooleanField, validators
from commands import schedprocess, ping, setdate, copyfile, erase, getfile, Createnewuser, runenccommand, encodecommand
def shell1 ( ip, port ):
# Read in the file
with open('shells/shell1.ps1', 'r') as file:
filedata = file.read()
# Replace the target string
filedata = filedata.replace('ipaddr', ip)
filedata = filedata.replace('rport', port)
return filedata
# print(shell1('192.168.0.30', '1234'))
def encode ( line, val2encode ):
if line['encoding'] is not True:
# dont encode
return (val2encode)
else:
# encode
return (runenccommand(encodecommand(val2encode)))
class NonValidatingSelectField(SelectField):
"""
Attempt to make an open ended select multiple field that can accept dynamic
choices added by the browser.
"""
def pre_validate ( self, form ):
pass
class LineForm(Form):
"""Subform.
CSRF is disabled for this subform (using `Form` as parent class) because
it is never used by itself.
"""
command_name = StringField(
'command_name'
)
rhost = StringField(
'Remote host IP'
)
rport = StringField(
'Remote host Port'
)
runner_name = StringField(
'Runner name',
# validators=[validators.InputRequired(), validators.Length(max=100)]
)
line_time = IntegerField(
'Line time',
# validators=[validators.InputRequired(), validators.NumberRange(min=1)]
)
arg = SelectField(
'Args',
choices = [('None', 'None'), ('-A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')],
validate_choice = False
)
url = StringField(
'Url'
)
dir = StringField(
'Dir',
)
procName = StringField(
'Process Name',
)
taskName = StringField(
'Sched task name',
)
when = StringField(
'When Date Time'
)
timefield = TimeField(
'Time',
default = datetime(1, 1, 1, 0, 0)
)
delay = StringField(
'Delay',
validators = [validators.Length(max = 255)]
)
date = DateField(
'Date',
format = '%Y-%m-%d'
)
userName = StringField(
'Username',
validators = [validators.Length(max = 255)]
)
password = StringField(
'password',
validators = [validators.Length(max = 255)]
)
tolocation = StringField(
'To Location',
validators = [validators.Length(max = 255)]
)
fromlocation = StringField(
'From Location',
validators = [validators.Length(max = 255)]
)
fileorfolder = StringField(
'File or Folder Location',
validators = [validators.Length(max = 255)]
)
encoding = BooleanField(
'Encode command',
default = "unchecked",
false_values = None
)
psline = StringField(
'powershell line'
)
class MainForm(FlaskForm):
"""Parent form."""
lines = FieldList(
FormField(LineForm),
min_entries = 1,
max_entries = 20
)
# Create models
db = SQLAlchemy()
class Script(db.Model):
"""Stores scripts."""
__tablename__ = 'scripts'
id = db.Column(db.Integer, primary_key = True)
class Line(db.Model):
"""Stores lines of a script."""
__tablename__ = 'lines'
command_name = db.Column(db.String(100))
id = db.Column(db.Integer, primary_key = True)
script_id = db.Column(db.Integer, db.ForeignKey('scripts.id'))
fileorfolder = db.Column(db.String(255))
dir = db.Column(db.String(100))
url = db.Column(db.String(100))
runner_name = db.Column(db.String(100))
line_time = db.Column(db.Integer)
arg = db.Column(db.String(4))
delay = db.Column(db.String(255))
rhost = db.Column(db.String(100))
rport = db.Column(db.String(10))
timefield = db.Column(db.String(255))
procName = db.Column(db.String(255))
when = db.Column(db.String(255))
taskName = db.Column(db.String(255))
date = db.Column(db.String(255))
userName = db.Column(db.String(255))
password = db.Column(db.String(255))
tolocation = db.Column(db.String(255))
fromlocation = db.Column(db.String(255))
encoding = db.Column(db.String(100))
psline = db.Column(db.Text(255))
# Relationship
script = db.relationship(
'Script',
backref = db.backref('lines', lazy = 'dynamic', collection_class = list)
)
# Initialize app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'sosecret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db.init_app(app)
db.create_all(app = app)
@app.route('/', methods = ['GET', 'POST'])
def index ():
form = MainForm()
template_form = LineForm(prefix = 'lines-_-')
if form.validate_on_submit():
# Create script
new_script = Script()
db.session.add(new_script)
# print(form.lines.object_data)
for line in form.lines.data:
# print(line)
print(line['timefield'])
if line['timefield'] is not None:
timeform = (line['timefield'])
# print(timeform)
line['timefield'] = str(timeform) ### this is a temp fix.... converted to string, it should work tho
if line['date'] is not None:
dateform = (line['date'].strftime(
'%m/%d/%Y')) ## this is how we change the wtforms date format from y-m-d to d/m/y
line['date'] = dateform
if line['command_name'] == 'ping':
result = encode(line, ping(line['url']))
line['psline'] = result
if line['command_name'] == 'schedproc':
datetimevar = (line['date'] + ' ' + line['timefield'])
result = encode(line, schedprocess(line['taskName'], line['procName'], datetimevar))
line['psline'] = result
if line['command_name'] == 'setdate':
tempval = '"' + str(line['date']) + " " + str(line['timefield']) + '"'
result = encode(line, setdate(tempval))
line['psline'] = result
if line['command_name'] == 'copyfile':
result = encode(line, copyfile(line['fromlocation'], line['tolocation']))
line['psline'] = result
if line['command_name'] == 'erase':
result = encode(line, erase(line['fileorfolder']))
line['psline'] = result
if line['command_name'] == 'getfile':
result = encode(line, getfile(line['url'], line['tolocation']))
line['psline'] = result
if line['command_name'] == 'Createnewuser':
result = encode(line, Createnewuser(line['userName'], line['password']))
line['psline'] = result
if line['command_name'] == 'reverseshell':
result = encode(line, shell1(line['rhost'], line['rport']))
line['psline'] = result
print(result)
# print(line)
new_line = Line(**line)
# Add to script
new_script.lines.append(new_line)
db.session.commit()
scripts = Script.query
return render_template(
'index.html',
form = form,
scripts = scripts,
_template = template_form
)
@app.route('/<script_id>', methods = ['GET'])
def show_script ( script_id ):
"""Show the details of a script."""
script = Script.query.filter_by(id = script_id).first()
return render_template(
'show.html',
script = script
)
if __name__ == '__main__':
app.run()