1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| const express = require('express'); const app = express(); const fs = require('fs'); const port = 3000; const bodyParser = require('body-parser');
app.set('view engine', 'ejs'); app.use(express.static('public')); app.use(bodyParser.urlencoded({ extended: true }));
let money = 1000; const initialMoney = 1000; let message = ''; const products = [ { name: '帽子', price: 10 }, { name: '棒球', price: 15 }, { name: 'iphone', price: 150 }, { name: 'flag', price: 1500 }, ];
app.get('/showflag', (req, res) => { res.render('readfile'); });
app.post('/readfile', (req, res) => { const fileName = req.body.fileName;
if (fileName.includes("fl")) { return res.status(200).send('你还真读flag啊'); } fs.readFile("/app/public/"+fileName, 'utf8', (err, data) => { if (err) { res.status(500).send('Error reading the file'); } else { res.send(data); } }); });
app.get('/', (req, res) => { res.render('index', { products, money, message }); });
app.get('/buy/:productIndex', (req, res) => { const productIndex = req.params.productIndex; let quantity = req.query.quantity || 1;
if (productIndex === '3') { quantity = Math.abs(quantity); if (products[productIndex] && money >= products[productIndex].price * quantity) { money -= products[productIndex].price * quantity; message = `购买flag成功啦!给你/showflag这个路由,听说那里面有flag`;
res.render('index', { products, money, message, showAlert: true }); } else { message = 'flag很贵的'; res.redirect('/'); } }else{ if (products[productIndex] && money >= products[productIndex].price * quantity) { money -= products[productIndex].price * quantity; message = `成功购买了 ${quantity} 件 "${products[productIndex].name}"!`;
res.render('index', { products, money, message, showAlert: true }); } else { message = '购买失败,钱不够啊老铁.'; res.redirect('/'); } } });
function copy(object1, object2) { if (typeof object1 !== 'object' || object1 === null || typeof object2 !== 'object' || object2 === null) { return; }
for (let key in object2) { if ( typeof object2[key] === 'object' && object2[key] !== null && typeof object1[key] === 'object' && object1[key] !== null ) { copy(object1[key], object2[key]); } else { object1[key] = object2[key]; } } }
app.post('/getflag', require('body-parser').json(), function (req, res, next) { res.type('html'); const flagFilePath = '/flag'; let flag = ''; fs.readFile(flagFilePath, 'utf8', (err, data) => { if (err) { console.error(`无法读取文件: ${flagFilePath}`); } else { flag = data; var secert = {}; var sess = req.session; let user = {}; copy(user, req.body); if (secert.testattack === 'admin') { res.end(flag);
} else { return res.send("no,no,no!"); } } }); });
app.get('/reset', (req, res) => { money = initialMoney; message = ''; res.redirect('/'); });
app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });
|