怎么制作自己的免費(fèi)網(wǎng)站濟(jì)南做網(wǎng)站建設(shè)的公司
時(shí)間軸:

演示案例:
環(huán)境搭建-NodeJS-解析安裝&庫安裝
功能實(shí)現(xiàn)-NodeJS-數(shù)據(jù)庫&文件&執(zhí)行
安全問題-NodeJS-注入&RCE&原型鏈
案例分析-NodeJS-CTF 題目&源碼審計(jì)
開發(fā)指南-NodeJS-安全 SecGuide 項(xiàng)目、
環(huán)境搭建-NodeJS-解析安裝&庫安裝
node.js是運(yùn)行在服務(wù)端的JavaScript。
作者安裝的是18.16.0.安裝好后重啟電腦,讓環(huán)境進(jìn)行配置。?(只有這樣才能使npm,node這些生效)
案例導(dǎo)入:
//express_demo.js 文件
var express = require('express');
var app = express();app.get('/', function (req, res) {res.send('Hello World');
})var server = app.listen(3000, function () {var host = server.address().addressvar port = server.address().portconsole.log("應(yīng)用實(shí)例,訪問地址為 http://%s:%s", host, port)})
使用node .\sql.js進(jìn)行運(yùn)行:
無法運(yùn)行時(shí)需要安裝express庫。
npm i express (會(huì)得到node modules)
運(yùn)行結(jié)果:(文字路徑不能太多)
網(wǎng)頁源代碼:
只顯示了Hello World,沒有顯示visual studio中的代碼
看到結(jié)果是運(yùn)行結(jié)果而不是源代碼
功能實(shí)現(xiàn)-NodeJS-數(shù)據(jù)庫&文件&執(zhí)行
登錄操作
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>后臺(tái)登錄</title><style>body {background-color: #f1f1f1;}.login {width: 400px;margin: 100px auto;background-color: #fff;border-radius: 5px;box-shadow: 0 0 10px rgba(0,0,0,0.3);padding: 30apx;}.login h2 {text-align: center;font-size: 2em;margin-bottom: 30px;}.login label {display: block;margin-bottom: 20px;font-size: 1.2em;}.login input[type="text"], .login input[type="password"] {width: 100%;padding: 10px;border: 1px solid #ccc;border-radius: 5px;font-size: 1.2em;margin-bottom: 20px;}.login input[type="submit"] {background-color: #2ecc71;color: #fff;border: none;padding: 10px 20px;border-radius: 5px;font-size: 1.2em;cursor: pointer;}.login input[type="submit"]:hover {background-color: #27ae60;}</style>
</head>
<body>
<div class="login" ><h2>后臺(tái)登錄</h2><form action="http://127.0.0.1:3000/login" method="POST"><label for="username">用戶名:</label><input type="text" name="username" id="username" class="user" ><label for="password">密碼:</label><input type="password" name="password" id="password" class="pass" ><button>登錄</button></form>
</div>
2.創(chuàng)建一個(gè)sql.js(覆蓋掉之前那一個(gè))
sql.js:
const express= require('express');
const app=express();//get路由
app.get('/login',function(req,res){ //req訪問,res結(jié)果res.send('<hr>登錄頁面</hr>');
})app.get('/',function(req,res){//res.send('<hr>首頁頁面</hr>');res.sendFile(__dirname+'/'+'sql.html'); //當(dāng)前運(yùn)行下的/sql.html(渲染為此頁面)
})const server = app.listen(3000,function(){console.log('web的3000端口已啟動(dòng)!');
})
運(yùn)行結(jié)果:
以get路由來傳參:
對(duì)sql.html進(jìn)行修改:
<div class="login" ><h2>后臺(tái)登錄</h2><form action="http://127.0.0.1:3000/login" method="GET"><label for="username">用戶名:</label><input type="text" name="username" id="username" class="user" ><label for="password">密碼:</label><input type="password" name="password" id="password" class="pass" ><button>登錄</button></form>
</div>
對(duì)sql.js進(jìn)行修改:
const express= require('express');
const app=express();//get路由
app.get('/login',function(req,res){const u = req.query.username //獲取sql.html中的usernameconst p = req.query.password //獲取sql.html中的passwordconsole.log(u);console.log(p)if(u == 'admin' && p == '123456'){res.send("歡迎進(jìn)入后臺(tái)管理頁面")}else{res.send('登錄用戶或密碼錯(cuò)誤!');}
})app.get('/',function(req,res){//res.send('<hr>首頁頁面</hr>');res.sendFile(__dirname+'/'+'sql.html');
})const server = app.listen(3000,function(){console.log('web的3000端口已啟動(dòng)!');
})
運(yùn)行結(jié)果:(使用console.log()來查看是否定義成功)
以post路由來傳參:
修改后的sql.js:
const express= require('express');
const bodyParser = require('body-parser');
const app=express();
// 創(chuàng)建 application/x-www-form-urlencoded 編碼解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })//get路由
app.get('/login',function(req,res){const u = req.query.username //query適用于getconst p = req.query.passwordconsole.log(u);console.log(p)if(u == 'admin' && p == '123456'){res.send("歡迎進(jìn)入后臺(tái)管理頁面")}else{res.send('登錄用戶或密碼錯(cuò)誤!');}
})//post路由
app.post('/login',urlencodedParser,function(req,res){const u = req.body.username; //body適用于postconst p = req.body.password;console.log(u);console.log(p);if(u == 'admin' && p == '123456'){res.send("歡迎進(jìn)入后臺(tái)管理頁面")}else{res.send('登錄用戶或密碼錯(cuò)誤!');}
})
app.get('/',function(req,res){//res.send('<hr>首頁頁面</hr>');res.sendFile(__dirname+'/'+'sql.html');
})const server = app.listen(3000,function(){console.log('web的3000端口已啟動(dòng)!');
})
在前面安裝一個(gè)const bodyParser = require('body-parser');
在終端使用npm i body-parser安裝庫
使用:
// 創(chuàng)建 application/x-www-form-urlencoded 編碼解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })是為了使得在network里看到的數(shù)據(jù)username =111&password =111有一個(gè)規(guī)范的格式。
數(shù)據(jù)庫管理:
數(shù)據(jù)庫連接:
mysql連接代碼:
var connection = mysql.createConnection({host : 'localhost',user : 'root',password : 'root',database : 'demo01'
});connection.connect();
const sql = 'select * from admin ';
console.log(sql);
connection.query(sql,function(error,data){if(error){console.log('數(shù)據(jù)庫連接失敗!');}console.log(data);
})
效果展示:
與數(shù)據(jù)庫相同
數(shù)據(jù)庫與登錄邏輯結(jié)合:
//post路由
app.post('/login',urlencodedParser,function(req,res){const u = req.body.username;const p = req.body.password;console.log(u);console.log(p);var connection = mysql.createConnection({host : 'localhost',user : 'root',password : 'root',database : 'demo01'});connection.connect();const sql = 'select * from admin where username="'+u+'" and password="'+p+'"'; console.log(sql);connection.query(sql,function(error,data){if(error){console.log('數(shù)據(jù)庫連接失敗!');}try{ //報(bào)錯(cuò)測(cè)試if(u==(data[0]['username']) && p==data[0]['password']){
//data[0]的意思是取第一行數(shù)據(jù)['username']是取里面的username值res.send('歡迎進(jìn)入后臺(tái)管理頁面');}}catch{res.send('錯(cuò)誤');};})})app.get('/',function(req,res){//res.send('<hr>首頁頁面</hr>');res.sendFile(__dirname+'/'+'sql.html');
})const server = app.listen(3000,function(){console.log('web的3000端口已啟動(dòng)!');
})
效果展示:
使用永“真”語句注入的話,無論前面是什么都會(huì)全部回顯。(or)
在頁面注入:
測(cè)試下來后沒有往后面運(yùn)行:
證明前面的代碼if(u==(data[0]['username']) && p==data[0]['password'])對(duì)其進(jìn)行了過濾:
u==(data[0]['username']是用來判斷鍵值是否相同,正常應(yīng)該是數(shù)據(jù)庫取出的行數(shù)進(jìn)行判斷,而不是data中取的值。
安全方法:
使用sql預(yù)編譯進(jìn)行寫比較安全(secguide-main)
文件管理功能:
創(chuàng)建file.js:(記得npm i fs)
const fs=require('fs');
const express = require('express');
const app = express();app.get('/file', function (req, res) {const dir=req.query.dir; //接受dirconsole.log(dir); //調(diào)試dirfilemanage(dir); //執(zhí)行dir
})var server = app.listen(3000, function () {console.log('web應(yīng)用3000端口已啟動(dòng)!')
})function filemanage(dir){fs.readdir(dir,function(error,files){console.log(files);
})};
運(yùn)行結(jié)果:使用node .\file.js(在網(wǎng)址后面加上/file?dir=)
命令執(zhí)行功能:
1、Express開發(fā)
2、實(shí)現(xiàn)自錄讀取
3、加入傳參接受
一命令執(zhí)行(RCE)
1、eval
2、exec & spawnSyn
創(chuàng)建一個(gè)rce.js:(npm i child_process)
const rce=require('child_process');//nodejs 調(diào)用系統(tǒng)命令執(zhí)行
//rce.exec('notepad');
//rce.spawnSync('calc');//nodejs 調(diào)用代碼命令執(zhí)行 把字符串當(dāng)做代碼解析
eval('require("child_process").exec("calc");');
node.js判斷:
安全問題-NodeJs-注入&RCE&原型鏈
1、SQL注入&文件操作
2、RCE執(zhí)行&原型鏈污染
2、NodeJS黑盒無代碼分析
實(shí)戰(zhàn)測(cè)試NodeJs安全
判斷:參考前期的信息收集
黑盒:通過對(duì)各種功能和參數(shù)進(jìn)行payload測(cè)試
白盒:通過對(duì)代碼中寫法安全進(jìn)行審計(jì)分析
原型鏈污染
如果攻擊者控制·并修改了一個(gè)對(duì)象的原型,(_proto_)那么將可以影響所有和這個(gè)對(duì)象來自同一個(gè)類、父祖類的對(duì)象。
// // foo是一個(gè)簡(jiǎn)單的JavaScript對(duì)象
// let foo = {bar: 1} //解釋:1=1 0 __proto__= x
// // 原型鏈污染
// // foo.bar 此時(shí)為1
// console.log(foo.bar)// // 修改foo的原型(即Object)
// foo.__proto__.bar = 'x'// // // 由于查找順序的原因,foo.bar仍然是1
// console.log(foo.bar)// // // 此時(shí)再用Object創(chuàng)建一個(gè)空的zoo對(duì)象
// let zoo = {}// // 查看zoo.bar,此時(shí)bar為2
// console.log(zoo.bar)let foo = {bar: 1};console.log(foo.bar);foo.__proto__.bar = 'require(\'child_process\').execSync(\'calc\');'console.log(foo.bar);let zoo = {};console.log(eval(zoo.bar));
運(yùn)行結(jié)果:
案例分析-NodeJS-CTF題目&源碼審計(jì)
1、CTFSHOW幾個(gè)題日
https://ctfshow/Web334-344
https://f1veseven.github.io/2022/04/03/ctf-nodejs-zhi-yi-xie-xiao-zhi-shi/
CTFSHOW-334:
1.打開zip發(fā)現(xiàn)是pk開頭:
證明是zip文件:使用winzip或者別的解壓軟件打開(Bandzip):
打開后由login.js和user.js:
(引用文章:Ctfshow web入門 nodejs篇 web334-web344-阿里云開發(fā)者社區(qū))
login.js:
//login.jsvar express = require('express'); //引入各個(gè)模塊
var router = express.Router();
var users = require('../modules/user').items; //引入用戶模塊(user.js)var findUser = function(name, password){ //定義函數(shù)return users.find(function(item){return name!=='CTFSHOW' && item.username === name.toUpperCase() && item.password === password;}); //如果name不等于CTFSHOW,并且將name都轉(zhuǎn)為大寫與item.name(CTFSHOW)相同,password=123456。則findUser返回true //toUpperCase()是javascript中將小寫轉(zhuǎn)換成大寫的函數(shù)。
};/* GET home page. */
router.post('/', function(req, res, next) { //POST請(qǐng)求的處理函數(shù)res.type('html'); //設(shè)置響應(yīng)(res)的內(nèi)容類型為htmlvar flag='flag_here';var sess = req.session;var user = findUser(req.body.username, req.body.password);if(user){req.session.regenerate(function(err) {if(err){return res.json({ret_code: 2, ret_msg: '登錄失敗'}); }req.session.loginUser = user.username;res.json({ret_code: 0, ret_msg: '登錄成功',ret_flag:flag}); //登錄成功返回flag});}else{res.json({ret_code: 1, ret_msg: '賬號(hào)或密碼錯(cuò)誤'});} });module.exports = router; //通過module.exports將該路由模塊導(dǎo)出,以便在其他文件中引入和使用
user.js:?
//user.jsmodule.exports = {items: [{username: 'CTFSHOW', password: '123456'}]
};//這段代碼是一個(gè)模塊文件,通過`module.exports`將一個(gè)對(duì)象導(dǎo)出。
//在這個(gè)模塊中,導(dǎo)出的對(duì)象是一個(gè)包含一個(gè)屬性`items`的對(duì)象。`items`屬性是一個(gè)數(shù)組,包含了一個(gè)用戶對(duì)象。這個(gè)用戶對(duì)象有兩個(gè)屬性:`username`表示用戶名為"CTFSHOW",`password`表示密碼為"123456"。//通過這種方式,其他文件可以引入該模塊并訪問`items`數(shù)組中的用戶對(duì)象,用于驗(yàn)證用戶的登錄信息。
突破點(diǎn):
1.2 nodejs語言的缺點(diǎn)
1.2.1 大小寫特性
toUpperCase()
toLowerCase()
對(duì)于toUpperCase(): 字符"?"
、"?"
?經(jīng)過toUpperCase處理后結(jié)果為?"I"
、"S"
對(duì)于toLowerCase(): 字符"K"
經(jīng)過toLowerCase處理后結(jié)果為"k"
(這個(gè)K不是K)
var findUser = function(name, password){ //定義函數(shù)return users.find(function(item){return name!=='CTFSHOW' && item.username === name.toUpperCase() && item.password === password;}); //如果name不等于CTFSHOW,并且將name都轉(zhuǎn)為大寫與item.name(CTFSHOW)相同,password=123456。則findUser返回true //toUpperCase()是javascript中將小寫轉(zhuǎn)換成大寫的函數(shù)。
};
payload:
name:ctfshow
password:123456
result:
2、YApi管理平臺(tái)漏洞
https://blog.csdn.net/weixin_42353842/article/details/127960229
開發(fā)指南-NodeJs-安全SecGuide項(xiàng)目:
https://github.com/Tencent/secguide
本文章由李豆豆喵和番薯小羊卷~共同完成。