zoukankan      html  css  js  c++  java
  • (9)SQL的注入(致命的漏洞)

    用户登陆网站的时候进行账户验证输入特殊的格式和字符会触发一个漏洞,不需要密码直接登录成功

    import pymysql

    username = input('请输入账号: ')
    password = input('请输入密码: ')

    conn = pymysql.connect(host='localhost',user='root',password='',database='company',port=3306,charset='utf8',cursorclass=pymysql.cursors.DictCursor)

    cursor = conn.cursor()

    cursor.execute("select * from userinfo where name='%s'and password='%s'"%(username,password))
    res = cursor.fetchall()
    if res:
    print(res)
    else:
    print('账号密码错误')

    用户在输入账号的使用使用这种格式: 

    1、用户名+空格+ ' + 空格 + #, #知道用户名的情况下以这种格式进行登陆验证,密码随意填写,就会触发bug,用户的验证通过,并不需要密码

    2、任意字母 + 空格 + ' + or + 空格 + 1=1 + 空格 + #  #这种格式不需要账户和密码,能够直接通过数据库验证

    PS:注入的原理就是在用户名的后面加 #,这样值在传入指令的时候后面跟了一个 #,#后面所有的代码都被注释掉,这样值模仿判定代码就会生效

    解决注入漏洞的办法

    import pymysql

    username = input('请输入账号: ')
    password = input('请输入密码: ')

    conn = pymysql.connect(host='localhost',user='root',password='',database='company',port=3306,charset='utf8',cursorclass=pymysql.cursors.DictCursor)

    cursor = conn.cursor()

    '''避免注入,将指令放入变量中'''
    sql = "select * from userinfo where name=%sand password=%s"

    '''将执行sql语句的时候将值以元组的形式传入,python就会自动转义,去掉#号'''
    cursor.execute(sql,(username,password))

    res = cursor.fetchall()
    if res:
    print(res)
    else:
    print('账号密码错误')

    不单单是登陆验证,在插入数据的时候也不要用这种格式 

    "select * from userinfo where name='%s'and password='%s'"%(username,password)

    登陆和插入的时候都要以这种格式(安全格式)

    sql = "select * from userinfo where name=%sand password=%s"

    cursor.execute(sql,(username,password))
  • 相关阅读:
    pythonchallenge10
    线程同步
    查缺补漏
    查看QQ是否在线
    project euler10
    Toon Shading, step 2
    一种简易的卡通渲染方法(上)
    GLSL学习笔记 9.1 Transformation
    Gloss Mapping
    一种简易的卡通渲染方法(下)
  • 原文地址:https://www.cnblogs.com/shizhengquan/p/10309902.html
Copyright © 2011-2022 走看看