-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRestrictMyShell.py
More file actions
executable file
·46 lines (45 loc) · 1.36 KB
/
RestrictMyShell.py
File metadata and controls
executable file
·46 lines (45 loc) · 1.36 KB
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
#!/usr/bin/python
import os
import re
import readline
import sys
import subprocess
os.system('clear')
print 'WELCOME TO THE RESTRICTED SHELL'
if not os.path.exists('/etc/restrictmyshell'):
open('/etc/restrictmyshell','w')
print '\nEdit /etc/restrictmyshell with Whitelisted Commands'
sys.exit(1)
while True:
File = open('/etc/restrictmyshell', 'r')
allowedcommands = []
for word in File:
allowedcommands.append(word.rstrip('\n'))
try:
command = raw_input('%s@PyShell> ' %
subprocess.check_output(["whoami"]).rstrip('\n'))
command = command.lstrip(' ')
except (KeyboardInterrupt, EOFError):
print '\n'
continue
command = command.split(' ')
flag = False
newCommand = ''
for x in allowedcommands:
if command[0] == 'exit':
sys.exit(0)
try:
if command[0] == 'cd':
os.chdir(command[1])
except:
pass
if command[0] == x:
for f in command:
newCommand += re.sub(r'([\|\$\&\<\>\|\(\)\:\!\{\}\[\]\;\`].*)', r'', f) + ' '
if command[0] == 'ls':
newCommand += ' --color=always '
break
if newCommand == '':
print "You Are Not Allowed To Run This Command!"
else:
subprocess.call(newCommand, shell=True)