Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kissaki #82

Merged
merged 12 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ vendor/*
teams/*
teamcreds.txt
lib/harbor/certs/*
sam/
work.txt
sam.yml
11 changes: 11 additions & 0 deletions challenges-sample/knock/challenge-checker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:20.04

RUN apt-get update
RUN apt-get install -y python3 python3-pip curl

WORKDIR /opt/kissaki/
COPY . .

RUN pip3 install -r requirements.txt

CMD ["/bin/bash", "-c", "python3 /opt/kissaki/app.py" ]
94 changes: 94 additions & 0 deletions challenges-sample/knock/challenge-checker/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import requests
import time
from flask import Flask, jsonify
import os
from kubernetes import client, config
import logging

# app = Flask(__name__)

# Set up logging
logging.basicConfig(level=logging.INFO)

try:
config.load_incluster_config()
except config.config_exception.ConfigException:
try:
config.load_kube_config()
except config.config_exception.ConfigException:
raise

v1 = client.CoreV1Api()
service = v1.read_namespaced_service(name="kissaki-svc", namespace="katana")
cluster_ip = service.spec.cluster_ip
ports = service.spec.ports
port = ports[0].port


# @app.route("/")
def hello():
return "Hello, world!"


# @app.route("/test")
def test_challenge_checker():
res = (
"making request to "
+ "http://"
+ str(cluster_ip)
+ ":"
+ str(port)
+ "/register "
)
return res


# @app.route("/register")
def register_challenge_checker():
logging.info(
"making request to "
+ "http://"
+ str(cluster_ip)
+ ":"
+ str(port)
+ "/register "
)

# Register with kissaki
checker_info = {
"name": "knock-challenge-checker",
"challenge": "knock",
} # Example info

response = requests.post(
"http://" + str(cluster_ip) + ":" + str(port) + "/register",
json=checker_info,
)
message = response.json().get("message")

logging.info(f"Received message from kissaki: {message}")

return "challenge_checker registered in kissaki"


# @app.route("/check")
def check_challenge():
for i in range(10):
# TODO: Implement challenge checking logic
challenge_status = {"status": "OK"} # Example status

# Send status to kissaki service
response = requests.post(
"http://" + str(cluster_ip) + ":" + str(port) + "/status",
json=challenge_status,
)
message = response.json().get("message")
logging.info(f"Received message from kissaki: {message}")

time.sleep(10) # Check every 10 seconds

return jsonify(challenge_status)


# if __name__ == "__main__":
# app.run(host="0.0.0.0", port=8080)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
challenge checker
3 changes: 3 additions & 0 deletions challenges-sample/knock/challenge-checker/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kubernetes
flask
requests
1 change: 1 addition & 0 deletions challenges-sample/knock/challenge-checker/scheduler.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contains informations about schedules
15 changes: 15 additions & 0 deletions challenges-sample/knock/challenge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:17.4.0-buster-slim

RUN mkdir -p /app

WORKDIR /app

COPY package.json .

RUN yarn

COPY . .

USER node

CMD ["node", "index.js"]
26 changes: 26 additions & 0 deletions challenges-sample/knock/challenge/challenge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: knock-knock
author: BrownieInMotion
description: |-
Knock knock? Who's there? Another pastebin!!

${link_main_0}

flag: dice{1_d00r_y0u_d00r_w3_a11_d00r_f0r_1_d00r}

provide:
- ./index.js
- ./Dockerfile

containers:
main:
build: .
ports:
- 3000
environment:
FLAG: "dice{1_d00r_y0u_d00r_w3_a11_d00r_f0r_1_d00r}"

expose:
main:
- target: 3000
http: knock-knock
healthContent: Create Paste
62 changes: 62 additions & 0 deletions challenges-sample/knock/challenge/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const crypto = require('crypto');

class Database {
constructor() {
this.notes = [];
this.secret = `secret-${crypto.randomUUID}`;
}

createNote({ data }) {
const id = this.notes.length;
this.notes.push(data);
return {
id,
token: this.generateToken(id),
};
}

getNote({ id, token }) {
if (token !== this.generateToken(id)) return { error: 'invalid token' };
if (id >= this.notes.length) return { error: 'note not found' };
return { data: this.notes[id] };
}

generateToken(id) {
return crypto
.createHmac('sha256', this.secret)
.update(id.toString())
.digest('hex');
}
}

const db = new Database();
db.createNote({ data: process.env.FLAG });

const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));

app.post('/create', (req, res) => {
const data = req.body.data ?? 'no data provided.';
const { id, token } = db.createNote({ data: data.toString() });
res.redirect(`/note?id=${id}&token=${token}`);
});

app.get('/note', (req, res) => {
const { id, token } = req.query;
const note = db.getNote({
id: parseInt(id ?? '-1'),
token: (token ?? '').toString(),
});
if (note.error) {
res.send(note.error);
} else {
res.send(note.data);
}
});

app.listen(3000, () => {
console.log('listening on port 3000');
});
9 changes: 9 additions & 0 deletions challenges-sample/knock/challenge/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "knock-knock",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.17.2"
}
}
45 changes: 45 additions & 0 deletions challenges-sample/knock/challenge/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<style>
* {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
'Helvetica Neue', sans-serif;
box-sizing: border-box;
}

html,
body {
margin: 0;
}

.container {
padding: 2rem;
width: 90%;
max-width: 900px;
margin: auto;
}

input:not([type='submit']) {
width: 100%;
padding: 8px;
margin: 8px 0;
}

textarea {
width: 100%;
padding: 8px;
margin: 8px 0;
resize: vertical;
font-family: monospace;
}

input[type='submit'] {
margin-bottom: 16px;
}
</style>

<div class="container">
<h1>Create Paste</h1>
<form method="POST" action="/create">
<textarea name="data"></textarea>
<input type="submit" value="Create" />
</form>
</div>
Loading