// 正常
select * from users WHERE username="zhangsan" and password="524abb53cce35"
// sql注入 用戶名寫入:zhangsan'-- select * from users WHERE username='zhangsan'-- ' and password='5245'複製代碼
上面兩條sql語句都能查詢到用戶的存在,而且第二條語句中密碼校驗的語句已經被註銷了,那麼這個時候若是別人知道你的用戶名就能登陸你的帳號,這樣子豈不是很危險??????
前端
咱們來看下執行的sql語句,果真是 -- 後面被註釋了~~~
node
select id, username, realname from users where username='zhangsan '-- ' and password='696'複製代碼
const mysql = require('mysql')
const { MYSQL_CONF } = require('../conf/db')
// 建立連接對象
const con = mysql.createConnection(MYSQL_CONF)
// 開始連接
con.connect()
// 統一執行 sql 的函數
function exec(sql) {
const promise = new Promise((resolve, reject) => {
con.query(sql, (err, result) => {
if (err) {
reject(err)
return
}
resolve(result)
})
})
return promise
}
module.exports = {
exec,
escape: mysql.escape // 防止sql注入 編碼特殊字符
}複製代碼
const { exec, escape } = require('../db/mysql')
const { genPassword } = require('../utils/cryp')
const register = async (username, password) => {
...
}
const userNameFilter = async (username) => {
...
}
const login = async (username, password) => {
username = escape(username) // 格式化 預防sql注入
password = genPassword(password) // 生成加密密碼
password = escape(password) // 格式化 預防sql注入
const sql = `
select id, username, realname from users where username=${username} and password=${password}
`
// console.log('sql is', sql)
const rows = await exec(sql)
return rows[0] || ''
}
const userInfo = async (id) => {
...
}
module.exports = {
login,
register,
userNameFilter,
userInfo
}複製代碼
select id, username, realname from users where username='zhangsan \'-- ' and password='6996'複製代碼
npm install xss --save-dev複製代碼
const xss = require('xss') // 引入xss
const { exec } = require('../db/mysql')
const newBlog = async (blogData = {}) => {
// blogData 是一個博客對象,包含 title content author 屬性
const title = xss(blogData.title) // 防範xss攻擊
const content = xss(blogData.content) // 防範xss攻擊
const author = blogData.author
const createTime = Date.now()
const sql = `
insert into blogs (title, content, createtime, author)
values ('${title}', '${content}', ${createTime}, '${author}');
`
const insertData = await exec(sql)
return {
id: insertData.insertId
}
}
module.exports = {
getList,
getDetail,
newBlog
}複製代碼
<script>
、<a>
等標籤的< >
進行轉換,而後再保存到後臺數據庫。<script>
document.cookie </script>
, 就會被轉換爲下面的語句並存入數據庫:<script>
document.cookie </script>
,已達到沒法執行 <script>
的目的!!!!sql注入:竊取數據庫內容mysql
XSS攻擊:竊取前端的cookie等敏感信息web
密碼加密:保障用戶信息安全(重要)redis
DDOS攻擊:須要硬件和服務來支持(須要OP支持)sql
第五章:nodejs koa2 mysql redis 全棧開發--安全(sql注入,xss攻擊) :blog.csdn.net/u012878818/…npm