在python中使用子进程和optparse时如何防止劫持?

时间:2019-07-07 19:06:08

标签: python python-3.x

我看到了一些用于在Linux中更改MAC地址的python代码,出于好奇,我想看看我是否可以劫持输入参数。该代码使用optparse从用户和子进程获取输入以在python中运行linux命令。程序从这样的终端运行 sudo test.py -i eth0 -m 00:11:22:33:44:55我刚刚在Mac和我成功劫持后添加了ls,有人可以提供某种方法来防止这种攻击吗?

import subprocess
import optparse


def get_arguments():
     parser = optparse.OptionParser()
     parser.add_option("-i", "--interface", dest="interface", 
                     help="interface to change it's MAC address")
     parser.add_option("-m", "--mac", dest="new_MAC", help="New 
                       MAC address")
     options = parser.parse_args()
     if not options.interface:
         parser.error("[-] please enter interface, use --help for 
                      more info")
         elif not options.new_MAC:
         parser.error("[-] please enter new mac address, use -- 
                      help for more info")
         else:
              return options



def mac_changer(interface, new_mac):
      print("[+] change mac address for {0} to 
            {1}".format(interface,new_mac))
      subprocess.call(["ifconfig", interface, "down"])
      subprocess.call(["ifconfig", interface, "hw", "ether", 
                      new_mac])
      subprocess.call(["ifconfig", interface, "up"])


options = get_arguments()
mac_changer(options.interface, options.new_MAC)

0 个答案:

没有答案