Python2中input()函数漏洞

函数简介

input()函数是python中的内置函数,函数作用是从stdin中读取数据

input() 与 raw_input() 区别

python2 两个常见输入函数:input 和 raw_input 。

raw_input() 会将输入的内容转换为字符串:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a1 = raw_input("字符串:")
print type(a1)

a2 = raw_input("数字:")
print type(a2)

a3 = raw_input("变量名:")
print type(a3)

'''
$ python 1.py
字符串:skye
<type 'str'>
数字:2311
<type 'str'>
变量名:a3
<type 'str'>
'''

input() 能自动识别出输入的类型,将输入内容转换为对应类型(str、int、float)。这里我们先尝试输入正常无误的例子,注意字符串的输入方式:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
a1 = input("字符串:")
print type(a1)

a2 = input("数字:")
print type(a2)
'''
$ python 1.py
字符串:"skye"
<type 'str'>
数字:2311
<type 'int'>
'''

input() 产生漏洞原因

函数会将 stdin 输入的内容当做是 python2 代码去执行,看两个例子:

  1. a = raw_input()
    b = input()
    print "raw_input:"+a
    print "input:"+b
    '''
    $ python 1.py
    3+2
    3+2
    raw_input:%d 3+2
    input:%d 5
    '''
    

    input 输入的内容被当做是 python 代码去执行了。

  2. #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    ans = 10
    a = raw_input()
    if a == ans:
    	print "raw_input"
    b = input()
    if b == ans:
    	print "input"
    '''
    $ python 1.py
    ans
    ans
    input
    '''
    

    input 输入 ans 直接读取了 ans 的值到 b 当中,实际效果等同于b = ans。在这里也知道了为什么用 input() 输入字符串时,要加上引号,如果我们不加上,很可能被当做是变量名,结果就如同例子 2 。

利用方式

  • 如果 python 脚本本身就有引入 os 库,输入 payload 直接 getshell:
os.system('/bin/sh')
  • 如果 python 脚本没有引入 os 库,payload 如下:
__import__('os').system('/bin/sh')

修复方法

python2 中避免使用 input() 函数,使用 raw_input() 代替,如果需要 int 可以这样:int(raw_input())

python3 中 input() 输入默认转换为字符型,raw_input() 被去除。

参考文章

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值