接入層注入
關系型資料庫
- 存放結構化資料
- 可高效操作大量資料
- 方便處理資料之間的關聯關系
- 常見: access/sqlite(db 結尾,一般用于手機)/mysql/mssql server
sql 語言
select * from table where id = 1- 標準化
- 類似于自然語言的描述性語言
- 用于關系型資料庫
- 可完成增刪改查以及各種復雜資料庫操作
sql 注入
`select * from table where id = ${id};`
`1 or 1 = 1`
`select * from table where id =1 or 1 =1;`
select * from user where username = '${data.username}'
and password = '${data.password}'
1 'or '1'='1
select * from user where username = 'username'
and password = '1' or '1'='1'
SQL 的注入本質是將資料變成了具有邏輯的程式
一些神奇的 SQL 語法
select * from table where id="10" and 1=0
select * from table where id="10" and 1=1
select * from table where id="10" and mid(version(),1,1)=5--猜資料庫的版本
select 1,2,3 from table
select id,1,2,3 from table
select * from table union select 1,2,3 from table2--猜欄位個數
select * from table where min(username,1,1)="t"--猜用戶名
SQl 注入的危害
- 猜解密碼
- 獲取資料
- 刪庫操作
- 拖庫
SQL 注入防御
- 關閉錯誤輸出
- 通過將錯誤資訊模糊化
console.log("[/site/post] error:", e.message, e.stack);
ctx.body = {
status: -1,
body: "出錯了"
};
- 檢查資料型別
- 對資料型別進行轉義
let id = ctx.parmas.id;
id = parseInt(id, 10);
const post = await query{
`select * from post where id =${connecttion.escape(id)}`//escape進行轉義
// 有的時候支持下面這種操作
`select * from post where id = ?`, [id]
}
- 使用引數化查詢
- 選安裝第三方庫 運行
npm install mysql2 - mysql2,和 mysql 不一樣,不是同一個人寫的,但是 mysql2 向下兼容 mysql
- 選安裝第三方庫 運行
這個時候就要改一下引入的 mysql 庫,還有 query
const query = bluebird.promisify(
connection.execte.bind(connectionModel).getConnection()
);
//原來是
const query = bluebird.promisify(
connection.query.bind(connectionModel).getConnection()
);
- 使用 ORM(物件關系映射)
- 需要安裝第三方插件
- 運行
npm install sequelize --save
初始化 ORM 實體
var Sequelize = require("sequelize");
var sequelize = new Sequelize({
host: "localhost",
database: "safety",
username: "root",
define: {
freezeTableName: ture
}
});
module.exports = sequelize;
處理資料表
var sequelize = require("./sequelize");
var Sequelize = require("sequelize");
var Post = sequelize.define(
"post",
{
id: {
type: Sequelize.INTERGER,
primaryKey: ture
},
title: Sequelize.STRING(256),
imgUrl: Sequelize.STRING(256),
content: Sequelize.TEXT
},
{
tableName: "post"
}
);
module.export = Post;
查詢操作
let post = await Post.findById(id);
let comment = await Comment.findAll({
where: {
postId: post.id
}
});
nosql 注入和防御
看一段 nosql 代碼
var mongoose = require('mongoose');
login async function(ctx) {
var username = ctx.request.body.username;
var password = ctx.request.body.password;
mongoose.findOne({
username: username,
password: password
})
}
看似沒有什么問題,其實是有問題的,
比如:{"name":"user""password""{"$gt":0}}
這樣密碼當密碼大于 0 時就可以進行登錄,也就是任意密碼都行,當然用戶名也是可以這樣操作的
跟關系型一樣,從這幾方面入手
- 檢查資料型別
- 型別轉換
- 寫完整條件
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/3947.html
標籤:訊息安全
上一篇:前端傳輸安全
下一篇:前端-接入層上傳問題
