赞
踩
sql
注入:窃取数据库内容XSS
攻击:窃取前端的 cookie
内容server
端攻击方式非常多,预防手段也非常多nodejs
可以通过 web server
层面预防OP
支持,如 DDOS
sql
注入,如下所示:web2.0
就有了 sql
注入攻击sql
片段,最终拼接成一段攻击代码MySQL
的 escape
函数处理输入内容即可sql
注入在 nodeJS
项目中的应用,如下所示:db.js
,代码如下所示:const env = process.env.NODE_ENV // 环境参数 // 配置 let MYSQL_CONF let REDIS_CONF if (env === 'dev') { // mysql MYSQL_CONF = { host: 'localhost', user: 'root', password: '123456', port: '3306', database: 'nodeblog' } // redis REDIS_CONF = { port: 6379, host: '127.0.0.1' } } if (env === 'product') { // mysql MYSQL_CONF = { host: 'localhost', user: 'root', password: '123456', port: '3306', database: 'nodeblog' } // redis REDIS_CONF = { port: 6379, host: '127.0.0.1' } } module.exports = { MYSQL_CONF, REDIS_CONF }
mysql.js
,代码如下所示:const mysql = require('mysql') const { MYSQL_CONF } = require('../config/db') // 创建连接对象 const con = mysql.createConnection(MYSQL_CONF) // 开始连接 con.connect() // 统一执行的 sql 的函数 function exec (sql) { // 返回 promise 对象 const promise = new Promise((resolve, reject) => { con.query(sql, (err, result) => { if (err) { // console.error(err) reject(err) return } // console.log(result) resolve(result) }) }) return promise } module.exports = { exec, escape: mysql.escape }
user.js
,代码如下所示:const { exec, escape } = require('../db/mysql') const login = (username, password) => { username = escape(username) password = escape(password) const sql = `select username, realname from users where username=${username} and password=${password};` return exec(sql).then(rows => { return rows[0] || {} }) } module.exports = { login }
XSS
攻击,如下所示:js
代码,以获取网页信息js
的特殊字符XSS
攻击在 nodeJS
项目的应用,如下所示:npm i xss --save
命令下载 xss
模块blog.js
中,代码如下所示:const xss = require('xss')
const title = xss(blogData.title)
blog.js
,代码如下所示:// controller 只关心数据 const { exec } = require('../db/mysql') const xss = require('xss') const getList = (author, keyword) => { // 先返回假数据,格式是正确的 let sql = `select * from blogs where 1=1 ` if (author) { sql += `and author='${author}' ` } if (keyword) { sql += `and title like '%${keyword}%' ` } sql += `order by createtime desc;` // 返回 promise return exec(sql) } const getDetail = (id) => { // 先返回的数据 const sql = `select * from blogs where id='${id}'` return exec(sql).then(rows => { return rows[0] }) } const newBlog = (blogData = {}) => { // blogData 是一个博客对象,包含 title、content 属性 // console.log('newBlog blogData...', blogData) const title = xss(blogData.title) const content = xss(blogData.content) const author = blogData.author const createtime = Date.now() const sql = `insert into blogs (title, content, createtime, author) values ('${title}', '${content}', ${createtime}, '${author}');` return exec(sql).then(insertData => { console.log('insertData is ', insertData) return { id: insertData.insertId } }) } const updateBlog = (id, blogData = {}) => { // id 就是要更新博客的 id // blogData 是一个博客对象,包含 title、content 属性 const title = xss(blogData.title) const content = xss(blogData.content) const sql = `update blogs set title='${title}', content='${content}' where id=${id}` return exec(sql).then(updateData => { console.log('updateData is', updateData) if (updateData.affectedRows > 0) { return true } return false }) } const deleteBlog = (id, author) => { // id 就是要更新博客的 id const sql = `delete from blogs where id=${id} and author='${author}';` return exec(sql).then(delData => { if (delData.affectedRows > 0) { return true } return false }) } module.exports = { getList, getDetail, newBlog, updateBlog, deleteBlog }
nodeJS
项目中的应用,如下所示:cryp.js
,代码如下所示:const crypto = require('crypto') // 密钥 const SECRET_KEY = 'Wzs_sd274#' // md5 加密 function md5(content) { let md5 = crypto.createHash('md5') return md5.update(content).digest('hex') } // 加密函数 function genPassword(password) { const str = `password=${password}&key=${SECRET_KEY}` return md5(str) } // const result = genPassword('123') // console.log(result) module.exports = { genPassword }
user.js
,代码如下所示:const { exec, escape } = require('../db/mysql') const { genPassword } = require('../utils/cryp') const login = (username, password) => { // 先使用假数据 // if (username === 'zhangsan' && password === '123') { // return true // } // return false username = escape(username) // 生成加密密码 password = genPassword(password) password = escape(password) const sql = `select username, realname from users where username=${username} and password=${password};` return exec(sql).then(rows => { return rows[0] || {} }) } module.exports = { login }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。