-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py.weird
executable file
·193 lines (143 loc) · 5.29 KB
/
app.py.weird
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
## The most Basic of modules you should always import ##
import sys, os, time, re, random, string
## We need to send mail ##
import smtplib
from email.mime.text import MIMEText
## Setup environment.... look in ./py_libs first ##
fqself = os.path.abspath(__file__)
my_libs = os.path.dirname(fqself) + '/py_libs/'
sys.path.insert(1, my_libs)
## Import dependencies ##
import web
import paramiko
import crypt, getpass, pwd
import xml.sax.saxutils
## Webpy Debug mode ##
web.config.debug = True
## Configure database connector ##
import MYconfig
db = web.database(dbn=MYconfig.options.get('dbn'),
user=MYconfig.options.get('user'),
pw=MYconfig.options.get('pw'),
db=MYconfig.options.get('db'))
## escape() and unescape() takes care of &, < and >.
html_escape_table = {
'"': """,
"'": "'"
}
html_unescape_table = dict(zip(html_escape_table.values(), html_escape_table.keys()))
## Some functions that all may need ##
def html_escape(text):
return xml.sax.saxutils.escape(text, html_escape_table)
def html_unescape(text):
return xml.sax.saxutils.unescape(text, html_unescape_table)
def make_text(string):
return string
def code_gen(size=6, chars=string.digits + string.ascii_letters):
return ''.join(random.choice(chars) for x in range(size))
def pass_gen(size=6, chars=string.digits + string.ascii_letters + string.punctuation):
return ''.join(random.choice(chars) for x in range(size))
def setpw(user, host, prikey, mypass, system):
f = open(my_libs + '/replace.hash.sh')
script = f.read()
f.close()
port = 22
trans = paramiko.Transport((host,port))
dsa_key = paramiko.DSSKey.from_private_key_file(prikey)
trans.connect(username=system, pkey=dsa_key)
session = trans.open_session()
session.get_pty()
## session.exec_command('''export PATH="${PATH}:/usr/local/bin" ; cat - | sudo /bin/bash -x 2> /tmp/out ''')
session.invoke_shell()
while not session.recv_ready():
time.sleep(1)
session.send('''export PATH="${PATH}:/usr/local/bin" ; sudo -s ''' + '\n')
## while not session.recv_ready():
## time.sleep(1)
cmd = '''USERID='%s'
SHA512HASH='%s'
CRYPTHASH='%s'
MD5HASH='%s'
%s
''' % (user, crypt.crypt(mypass), crypt.crypt(mypass , salt=crypt.METHOD_CRYPT), crypt.crypt(mypass , salt=crypt.METHOD_MD5), script)
session.send(cmd)
## time.sleep(4)
## session.send('\n')
## time.sleep(4)
## session.send('\n')
## time.sleep(4)
while not session.recv_ready():
time.sleep(1)
## session.send('exit $?\n')
session.shutdown_write()
session.close()
## time.sleep(4)
l = open(my_libs + '/replace.hash.sh.log', 'w')
l.write(cmd)
l.close()
stdout = session.makefile('rb', -1).readlines()
stderr = session.makefile_stderr('rb', -1).readlines()
status = session.recv_exit_status()
return {'stdout' : stdout,
'stderr' : stderr,
'status' : status}
## path where the all the webpy html templates go ##
render = web.template.render('/var/www/webpy-app/templates/')
## Setup our web form ##
my_form = web.form.Form(
web.form.Textbox('', class_='username', id='username', description='username:'),
web.form.Password('', class_='code', id='code', description='code:', autocomplete="off")
)
## Url/Class mapping ##
urls = ('/', 'tutorial',
'/index', 'index')
## Actual classes that spawn when mapped url is hit ##
class tutorial:
def GET(self):
form = my_form()
return render.tutorial(form, "Enter username.")
def POST(self):
form = my_form()
form.validates()
username = form.value['username']
code = form.value['code']
myvar1 = dict(username=username)
myvar2 = dict(code=code)
validuser = re.compile('^[.a-z0-9_-]+$').match(username)
validcode = re.compile('^[\S]{32}').match(code)
if validuser is None :
return make_text("invalid, you are wrong, wrong, wrong.")
msg = MIMEText(code_gen(32))
msg['Subject'] = 'Reset code'
msg['From'] = MYconfig.options.get('sender')
if validcode is not None:
results = db.select('users', myvar1, where="username = $username")
for record in results:
mylastupdate = int('0')
if record.timestamp is not None:
mylastupdate = int(time.mktime(record.timestamp.timetuple()))
currenttime = int(time.mktime(time.localtime()))
codeage = currenttime - mylastupdate
mycode = record.code
if mycode == code and codeage < 80:
mypass = pass_gen(32)
reset = setpw(username, MYconfig.options.get('testhost'), MYconfig.options.get('prikey'), mypass, MYconfig.options.get('system'))
if reset.get('status') is not 0:
return 'Reset Failed: ' + str(reset.get('status')) + str(reset.get('stdout'))
return '''%s [%s]''' % (html_escape(mypass), str(reset.get('status')))
results = db.select('users', myvar1, where="username = $username")
for record in results:
writecode = db.update('users', where="username = $username", code = msg._payload, vars=locals(), _test=True)
db.update('users', where="username = $username", code = msg._payload, vars=locals())
msg['To'] = record.email
send = smtplib.SMTP(MYconfig.options.get('mailrelay'))
send.sendmail(msg['From'], msg['To'], msg.as_string())
send.quit
time.sleep(4)
return make_text("You got mail.")
return make_text("not found")
## Uncomment to use internal webpy internal app engine(non production)
##app = web.application(urls, globals())
##if __name__ == '__main__':
## app.run()
application = web.application(urls, globals()).wsgifunc()