Is there a python module to determine whether the command string will modify the linux system?

We have a system that allows you to execute commands from customer post, call ansible api to execute commands on remote systems,

should be limited to query commands, such as

ps -ef | grep java | rm somefile
netstat -anpt | grep 1234 | pkill someprocess

whether there is a python module or other method to determine whether a command string will modify the system

Apr.24,2021

checking the command string cannot prevent unknown risks. It is recommended to start with the user / group executing the command and be a safe sandboxie.


I changed my approach,

after each query command, such as ps/ss, query, generally do the grep operation

We limit the types of commands that users can execute. For example, when a ps/ss, user post, select the type of command to execute.
and can only add the Filter keyword in a format similar to

.
<cmd> key1 key2 key3

check key1., in turn limits that key can only be words or contain a limited number of special characters. Splicing will not affect the system.
this kind of check is relatively simple. It can be determined by a rule.

as follows

word_re = re.compile(r'([a-zA-Z0-9\.]+)')

greps = []
for key in keys:
    matches = word_re.findall(key)
    -sharp key 
    if len(matches) == 0 or len(matches) > 1 or matches[0] != key:
        return send_msg(from_user, f': {key}')
    greps.append(f'grep -i {key}')

cmd = f'ps -ef | {"|".join(greps)}'

after splicing commands, the final execution format is as follows

ps -ef | grep key1 | grep key2 | grep key3

this makes it possible that the verification command will only query after the user post keyword and will not affect the system

Menu