python代码安全性问题_这个python代码对注入安全吗?

我在Python中有一个服务器/客户机套接字对。服务器接收特定的命令,然后准备响应并将其发送到客户端。在

在这个问题中,我关心的只是代码中的可能的注入:如果可以要求服务器对第二个参数做一些奇怪的事情——如果对命令内容的控制不足以避免不希望发生的行为。在

编辑:根据收到的建议

在windows上调用check_output时添加了参数shell=True。不应该是危险的,因为命令是一个普通的'dir'。在

一。在self.client, address = self.sock.accept()

...

cmd = bytes.decode(self.client.recv(4096))

ls:执行系统命令,但只读取目录的内容。在

^{pr2}$

cd:只调用os.chdir。在elif cmd.startswith('cd '):

path = cmd.split(' ')[1].strip()

if not os.path.isdir(path):

self.client.send(b'is not path')

else:

os.chdir(path)

self.client.send( os.getcwd().encode() )

get:将文件内容发送到客户端。在elif cmd.startswith('get '):

file = cmd.split(' ')[1].strip()

if not os.path.isfile(file):

self.client.send(b'ERR: is not a file')

else:

try:

with open(file) as f: contents = f.read()

except IOError as er:

res = "ERR: " + er.strerror

self.client.send(res.encode())

continue

... (send the file contents)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值