Skip to content

Commit afc45d1

Browse files
committed
Initial Commit
0 parents  commit afc45d1

File tree

10 files changed

+1482
-0
lines changed

10 files changed

+1482
-0
lines changed

.gitignore

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
12+
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
18+
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
21+
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
25+
26+
# nyc test coverage
27+
.nyc_output
28+
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
31+
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
34+
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
40+
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
49+
*.tsbuildinfo
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*
131+

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## node.js + MySQL vulnerable boilerplate code PoC
2+
3+
Initially referenced from https://codeshack.io/basic-login-system-nodejs-express-mysql/

docker-compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '3.3'
2+
3+
services:
4+
service:
5+
build: ./service
6+
restart: always
7+
ports:
8+
- "3000:3000"
9+
links:
10+
- db
11+
12+
db:
13+
build: ./mysql
14+
restart: always
15+
environment:
16+
MYSQL_DATABASE: login
17+
MYSQL_USER: login
18+
MYSQL_PASSWORD: login
19+
MYSQL_ROOT_PASSWORD: root_password

mysql/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM mysql:5.7
2+
3+
ADD login.sql /docker-entrypoint-initdb.d

mysql/login.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
CREATE DATABASE IF NOT EXISTS `login` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
2+
USE `login`;
3+
4+
# CREATE USER 'login'@'%' IDENTIFIED BY 'login';
5+
# GRANT ALL PRIVILEGES ON login.* TO 'login'@'%';
6+
# FLUSH PRIVILEGES;
7+
8+
CREATE TABLE IF NOT EXISTS `accounts` (
9+
`id` int(11) NOT NULL,
10+
`username` varchar(50) NOT NULL,
11+
`password` varchar(255) NOT NULL,
12+
`email` varchar(100) NOT NULL
13+
) ENGINE = InnoDB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8;
14+
INSERT INTO `accounts` (
15+
`id`, `username`, `password`, `email`
16+
)
17+
VALUES
18+
(
19+
1,
20+
'admin',
21+
SHA2(
22+
CONCAT(
23+
RAND(),
24+
UUID(),
25+
RAND()
26+
),
27+
512
28+
),
29+
30+
);
31+
ALTER TABLE
32+
`accounts`
33+
ADD
34+
PRIMARY KEY (`id`);
35+
ALTER TABLE
36+
`accounts` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,
37+
AUTO_INCREMENT = 2;

service/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# syntax=docker/dockerfile:1
2+
FROM node:lts-buster
3+
WORKDIR /srv/
4+
RUN apt-get update && apt-get -y install ssh
5+
6+
# Add user..
7+
RUN useradd -d /home/stypr -s /bin/false stypr && \
8+
mkdir -p /home/stypr && \
9+
touch /home/stypr/.hushlogin
10+
RUN chmod 1733 /tmp /var/tmp /dev/shm /proc
11+
12+
# Install service
13+
COPY . /srv/
14+
RUN npm install && chmod 555 /srv/ && chown -R stypr:root /srv/
15+
16+
USER stypr
17+
EXPOSE 3000
18+
CMD ["node", "login.js"]

service/login.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Login Form Tutorial</title>
6+
<style>
7+
.login-form {
8+
width: 300px;
9+
margin: 0 auto;
10+
font-family: Tahoma, Geneva, sans-serif;
11+
}
12+
.login-form h1 {
13+
text-align: center;
14+
color: #4d4d4d;
15+
font-size: 24px;
16+
padding: 20px 0 20px 0;
17+
}
18+
.login-form input[type="password"],
19+
.login-form input[type="text"] {
20+
width: 100%;
21+
padding: 15px;
22+
border: 1px solid #dddddd;
23+
margin-bottom: 15px;
24+
box-sizing:border-box;
25+
}
26+
.login-form input[type="submit"] {
27+
width: 100%;
28+
padding: 15px;
29+
background-color: #535b63;
30+
border: 0;
31+
box-sizing: border-box;
32+
cursor: pointer;
33+
font-weight: bold;
34+
color: #ffffff;
35+
}
36+
</style>
37+
</head>
38+
<body>
39+
<div class="login-form">
40+
<h1>Login Form</h1>
41+
<form action="auth" method="POST">
42+
<input type="text" name="username" placeholder="Username" required>
43+
<input type="password" name="password" placeholder="Password" required>
44+
<input type="submit">
45+
</form>
46+
</div>
47+
</body>
48+
</html>

service/login.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
3+
Reference: https://codeshack.io/basic-login-system-nodejs-express-mysql/
4+
5+
*/
6+
7+
var mysql = require("mysql");
8+
var express = require("express");
9+
var session = require("express-session");
10+
var bodyParser = require("body-parser");
11+
var path = require("path");
12+
13+
var connection = mysql.createConnection({
14+
host: "db",
15+
user: "login",
16+
password: "login",
17+
database: "login",
18+
});
19+
20+
var app = express();
21+
app.use(
22+
session({
23+
secret: require("crypto").randomBytes(64).toString("hex"),
24+
resave: true,
25+
saveUninitialized: true,
26+
})
27+
);
28+
app.use(bodyParser.urlencoded({ extended: true }));
29+
app.use(bodyParser.json());
30+
31+
app.get("/", function (request, response) {
32+
response.sendFile(path.join(__dirname + "/login.html"));
33+
});
34+
35+
app.post("/auth", function (request, response) {
36+
var username = request.body.username;
37+
var password = request.body.password;
38+
if (username && password) {
39+
connection.query(
40+
"SELECT * FROM accounts WHERE username = ? AND password = ?",
41+
[username, password],
42+
function (error, results, fields) {
43+
if (results.length > 0) {
44+
request.session.loggedin = true;
45+
request.session.username = username;
46+
response.redirect("/home");
47+
} else {
48+
response.send("Incorrect Username and/or Password!");
49+
}
50+
response.end();
51+
}
52+
);
53+
} else {
54+
response.send("Please enter Username and Password!");
55+
response.end();
56+
}
57+
});
58+
59+
app.get("/home", function (request, response) {
60+
if (request.session.loggedin) {
61+
response.send("Welcome back, " + request.session.username + "!");
62+
} else {
63+
response.send("Please login to view this page!");
64+
}
65+
response.end();
66+
});
67+
68+
app.listen(3000);

0 commit comments

Comments
 (0)