-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontains_shell_syntax.py
More file actions
175 lines (159 loc) · 3.57 KB
/
contains_shell_syntax.py
File metadata and controls
175 lines (159 loc) · 3.57 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
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
"""Exports the contains_shell_syntax function"""
import regex as re
# Constants
dangerous_chars = [
"#",
"!",
'"',
"$",
"&",
"'",
"(",
")",
"*",
";",
"<",
"=",
">",
"?",
"[",
"\\",
"]",
"^",
"`",
"{",
"|",
"}",
" ",
"\n",
"\t",
"~",
"\r",
"\f",
]
commands = [
"sleep",
"shutdown",
"reboot",
"poweroff",
"halt",
"ifconfig",
"chmod",
"chown",
"ping",
"ssh",
"scp",
"curl",
"wget",
"telnet",
"kill",
"killall",
"rm",
"mv",
"cp",
"touch",
"echo",
"cat",
"head",
"tail",
"grep",
"find",
"awk",
"sed",
"sort",
"uniq",
"wc",
"ls",
"env",
"ps",
"who",
"whoami",
"id",
"w",
"df",
"du",
"pwd",
"uname",
"hostname",
"netstat",
"passwd",
"arch",
"printenv",
"logname",
"pstree",
"hostnamectl",
"set",
"lsattr",
"killall5",
"dmesg",
"history",
"free",
"uptime",
"finger",
"top",
"shopt",
":", # Colon is a null command
]
path_prefixes = [
"/bin/",
"/sbin/",
"/usr/bin/",
"/usr/sbin/",
"/usr/local/bin/",
"/usr/local/sbin/",
]
separators = [" ", "\t", "\n", ";", "&", "|", "(", ")", "<", ">", "\r", "\f"]
# Function to sort commands by length (longer commands first)
def by_length(a, b):
"""Sort by length desc"""
return len(b) - len(a)
# Create the regex pattern
commands_regex = re.compile(
r"([/.]*("
+ "|".join(map(re.escape, path_prefixes))
+ r")?("
+ "|".join(sorted(commands, key=len, reverse=True))
+ r"))",
re.I | re.M,
)
def contains_shell_syntax(command, user_input):
"""
Checks if the user input contains shell syntax given the command
"""
if user_input.isspace():
# The entire user input is just whitespace, ignore
return False
if any(c in user_input for c in dangerous_chars):
return True
# The command is the same as the user input
# Rare case, but it's possible
# e.g. command is `shutdown` and user input is `shutdown`
# (`shutdown -h now` will be caught by the dangerous chars as it contains a space)
if command == user_input:
match = commands_regex.match(command)
return match is not None and match.start() == 0 and match.end() == len(command)
# Check if the command contains a commonly used command
for match in commands_regex.finditer(command):
# We found a command like `rm` or `/sbin/shutdown` in the command
# Check if the command is the same as the user input
# If it's not the same, continue searching
if user_input != match[0]:
continue
# Otherwise, we'll check if the command is surrounded by separators
# These separators are used to separate commands and arguments
# e.g. `rm<space>-rf`
# e.g. `ls<newline>whoami`
# e.g. `echo<tab>hello`
char_before = command[match.start() - 1] if match.start() > 0 else None
char_after = command[match.end()] if match.end() < len(command) else None
# Check surrounding characters
if char_before in separators and char_after in separators:
# e.g. `<separator>rm<separator>`
return True
if char_before in separators and char_after is None:
# e.g. `<separator>rm`
return True
if char_before is None and char_after in separators:
# e.g. `rm<separator>`
return True
return False