-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathshell.py
More file actions
206 lines (183 loc) · 8.34 KB
/
shell.py
File metadata and controls
206 lines (183 loc) · 8.34 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
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
import os
import re
import subprocess
from pathlib import Path
from typing import Any, Dict
from ms_agent.llm.utils import Tool
from ms_agent.tools.base import ToolBase
from ms_agent.utils.constants import DEFAULT_OUTPUT_DIR
class Shell(ToolBase):
def __init__(self, config):
super().__init__(config)
self.output_dir = getattr(self.config, 'output_dir',
DEFAULT_OUTPUT_DIR)
async def connect(self) -> None:
pass
async def _get_tools_inner(self) -> Dict[str, Any]:
tools = {
'shell': [
Tool(
tool_name='execute_single',
server_name='shell',
description='Execute a single shell command. '
'Use this tool to read/write/create file/dirs, '
'or start/stop processes or install required packages.'
'Note:\n '
'1. Do not execute dangerous commands which will affect the file system '
'or other processes\n '
'2. The work_dir arg should always base on the project you are working on',
parameters={
'type': 'object',
'properties': {
'command': {
'type': 'string',
'description': 'The shell command to execute.',
},
'work_dir': {
'type':
'string',
'description':
'The work dir of the command, this argument should always '
'be a relative sub folder of the project you are working on.',
}
},
'required': ['command', 'work_dir'],
'additionalProperties': False
}),
]
}
return tools
def check_safe(self, command, work_dir):
# 1. Check work_dir
output_dir_abs = Path(self.output_dir).resolve()
if work_dir.startswith('/') or work_dir.startswith('~'):
work_dir_abs = Path(work_dir).resolve()
else:
work_dir_abs = (output_dir_abs / work_dir).resolve()
if not str(work_dir_abs).startswith(str(output_dir_abs)):
raise ValueError(
f"Work directory '{work_dir}' is outside allowed directory '{self.output_dir}'"
)
# 2. Check dangerous commands
dangerous_commands = [
r'\brm\s+-rf\s+/', # rm -rf /
r'\bsudo\b', # sudo
r'\bsu\b', # su
r'\bchmod\b', # chmod
r'\bchown\b', # chown
r'\breboot\b', # reboot
r'\bshutdown\b', # shutdown
r'\bmkfs\b', # mkfs
r'\bdd\b', # dd
r'\bcurl\b.*\|\s*bash', # curl | bash
r'\bwget\b.*\|\s*bash', # wget | bash
r'\bcurl\b.*\|\s*sh\b', # curl | sh
r'\bwget\b.*\|\s*sh\b', # wget | sh
r'\b:\(\)\{.*\|.*&\s*\}', # fork bomb
r'\bmount\b', # mount
r'\bumount\b', # umount
r'\bfdisk\b', # fdisk
r'\bparted\b', # parted
]
for pattern in dangerous_commands:
if re.search(pattern, command, re.IGNORECASE):
raise ValueError(
f'Command contains dangerous operation: {pattern}')
# 3. Check path traversal
suspicious_patterns = [
r'(?:^|\s)/', # absolute path
r'\.\.', # parent directory
r'~', # HOME
r'\$HOME', # HOME env
r'\$\{HOME\}', # ${HOME}
]
for pattern in suspicious_patterns:
if re.search(pattern, command):
# 提取所有可能的路径
potential_paths = re.findall(r'(?:^|\s)([\w\./~${}]+)',
command)
for path_str in potential_paths:
if not path_str:
continue
try:
expanded_path = os.path.expandvars(
os.path.expanduser(path_str))
if not os.path.isabs(expanded_path):
full_path = (work_dir_abs
/ expanded_path).resolve()
else:
full_path = Path(expanded_path).resolve()
if not str(full_path).startswith(str(output_dir_abs)):
raise ValueError(
f"Command attempts to access path outside allowed directory: '{path_str}' "
f"resolves to '{full_path}', which is outside '{self.output_dir}'"
)
except Exception: # noqa
continue
# 4. Check dangerous redirections
redirect_patterns = [
r'>+\s*/(?!tmp/|var/tmp/|dev/null)', # redirect to root directory (except /tmp/, /var/tmp/, /dev/null)
r'<\s*/etc/', # read from /etc
r'>+\s*/dev/(?!null)', # redirect to device files (except /dev/null)
]
for pattern in redirect_patterns:
if re.search(pattern, command):
raise ValueError('Command contains dangerous redirection')
# 5. Check environment variable modifications
if re.search(r'\bexport\b|\benv\b.*=', command, re.IGNORECASE):
if re.search(r'\bPATH\s*=|LD_PRELOAD|LD_LIBRARY_PATH', command,
re.IGNORECASE):
raise ValueError(
'Command attempts to modify critical (PATH/LD_PRELOAD/LD_LIBRARY_PATH) '
'environment variables')
# 6. Check for command substitution and other shell injection risks
shell_injection_patterns = [
r'\$\(.*\)', # command substitution $(...)
r'`.*`', # command substitution `...`
]
for pattern in shell_injection_patterns:
if re.search(pattern, command):
substituted = re.findall(pattern, command)
for sub_cmd in substituted:
inner_cmd = re.sub(r'[\$\(\)`]', '', sub_cmd)
for dangerous in dangerous_commands:
if re.search(dangerous, inner_cmd, re.IGNORECASE):
raise ValueError(
f'Command substitution contains dangerous operation: {inner_cmd}'
)
async def execute_shell(self, command: str, work_dir: str):
try:
self.check_safe(command, work_dir)
if work_dir == '.' or work_dir == '.' + os.sep:
work_dir = ''
work_dir = os.path.join(self.output_dir, work_dir)
Path(work_dir).mkdir(parents=True, exist_ok=True)
ret = subprocess.run(
command,
shell=True,
cwd=work_dir,
capture_output=True,
text=True,
timeout=getattr(self.config.tools.shell, 'timeout', 5),
)
if ret.returncode == 0:
result = f'Command executed successfully. return_code=0, output: {ret.stdout.strip()}'
else:
result = f'Command executed failed. return_code={ret.returncode}, error message: {ret.stderr.strip()}'
except subprocess.TimeoutExpired:
result = f'Run timed out after {getattr(self.config.tools.shell, "timeout", 5)} seconds.'
except Exception as e:
result = f'Run failed with an exception: {e}.'
output = (
f'Shell command status:\n'
f'Command line: {command}\n'
f'Workdir: {work_dir}\n'
f'Result: {result or "The command does not give any responses."}')
return output
async def call_tool(self, server_name: str, *, tool_name: str,
tool_args: dict) -> str:
if tool_name == 'execute_single':
return await self.execute_shell(tool_args['command'],
tool_args['work_dir'])
else:
return f'Unknown tool type: {tool_name}'