python代码安全性问题_这个python代码对注入安全吗?
我在Python中有一個(gè)服務(wù)器/客戶機(jī)套接字對(duì)。服務(wù)器接收特定的命令,然后準(zhǔn)備響應(yīng)并將其發(fā)送到客戶端。在
在這個(gè)問題中,我關(guān)心的只是代碼中的可能的注入:如果可以要求服務(wù)器對(duì)第二個(gè)參數(shù)做一些奇怪的事情——如果對(duì)命令內(nèi)容的控制不足以避免不希望發(fā)生的行為。在
編輯:根據(jù)收到的建議
在windows上調(diào)用check_output時(shí)添加了參數(shù)shell=True。不應(yīng)該是危險(xiǎn)的,因?yàn)槊钍且粋€(gè)普通的'dir'。在
一。在self.client, address = self.sock.accept()
...
cmd = bytes.decode(self.client.recv(4096))
ls:執(zhí)行系統(tǒng)命令,但只讀取目錄的內(nèi)容。在
^{pr2}$
cd:只調(diào)用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:將文件內(nèi)容發(fā)送到客戶端。在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)
總結(jié)
以上是生活随笔為你收集整理的python代码安全性问题_这个python代码对注入安全吗?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql如何植入到oracle_分享M
- 下一篇: python的六个类型_介绍Python