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 all 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
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[submodule "katana-services"]
path = katana-services
url = [email protected]:sdslabs/katana-services.git
[submodule "[email protected]:sdslabs/katana-services.git"]
branch = main

This file was deleted.

This file was deleted.

This file was deleted.

Binary file removed challenge_template/knock/knock.tar.gz
Binary file not shown.
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
1 change: 1 addition & 0 deletions challenges-sample/knock/flag-handler/flag-getter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flag getter
1 change: 1 addition & 0 deletions challenges-sample/knock/flag-handler/flag-setter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flag setter
13 changes: 13 additions & 0 deletions challenges-sample/knock/katana.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[challenge.author]
name = "paradox"
email = "[email protected]"

[challenge.metadata]
name = "knock"
flag = "dice{1_d00r_y0u_d00r_w3_a11_d00r_f0r_1_d00r}"
description = "Knock knock? Who's there? Another pastebin!!"
type = "web"
points = 100

[challenge.env]
port_mappings = ["3000:3000"]
15 changes: 15 additions & 0 deletions challenges-sample/the-varsity/challenge-checker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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

# this arg is not being passed need to fix that.------------------------
ARG chall_name
ENV CHALL_NAME=$chall_name

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

import chall_checker # import the module containing the script

app = Flask(__name__)


chall_name = os.environ.get("CHALL_NAME")
chall_name = chall_name[:-3]
chall_name = "the-varsity" # fix this later
# kissaki
svc_name = "kissaki-svc"
namespace = "katana"
team_count = 2 # ----------harcoded for now, need to fix this
# 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=svc_name, namespace=namespace)
# # cluster_ip = service.spec.cluster_ip
# ports = service.spec.ports
# port = ports[0].port

v1 = client.CoreV1Api()


def service_port(svc, ns):
service = v1.read_namespaced_service(name=svc, namespace=ns)
# cluster_ip = service.spec.cluster_ip
ports = service.spec.ports
port = ports[0].port
return port


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


@app.route("/test")
def test_challenge_checker():
res = (
"making request to "
+ "http://"
+ svc_name
+ "."
+ namespace
+ ".svc.cluster.local:"
+ str(service_port(svc_name, namespace))
+ "/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("/register")
def register_challenge_checker():
logging.info(
"making request to "
+ "http://"
+ svc_name
+ "."
+ namespace
+ ".svc.cluster.local:"
+ str(service_port(svc_name, namespace))
+ "/register "
)

# Register with kissaki
# keys in checker_info are harcoded if changed here then some change may be needed in katana-services/Kissaki/src/app.py
checker_info = {"ccName": chall_name + "-cc"}

response = requests.post(
"http://"
+ svc_name
+ "."
+ namespace
+ ".svc.cluster.local:"
+ str(service_port(svc_name, namespace))
+ "/register",
json=checker_info,
)
message = response.json().get("message")

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

return "challenge_checker registered in kissaki"


# {service_port(chall_svc, chall_ns)}
@app.route("/check")
def check_challenge():
i = 0
chall_svc = f"{chall_name}-svc-{i}"
chall_ns = f"katana-team-{i}-ns"
url = f"http://{chall_svc}.{chall_ns}.svc.cluster.local:80/"
return url
try:
status = chall_checker.check_challenge(url)
return status
except Exception as e:
logging.error(f"Error checking challenge: {str(e)}")
return str(e)


@app.route("/checker")
def check_route():
results = {"challengeName": chall_name, "data": []}
for i in range(team_count):
team_name = f"katana-team-{i}"
result = {"team-name": team_name}
chall_svc = f"{chall_name}-svc-{i}"
chall_ns = f"katana-team-{i}-ns"
url = f"http://{chall_svc}.{chall_ns}.svc.cluster.local:80/" # update this later ---port should not be hardcoded----
try:
status = chall_checker.check_challenge(url)
result["status"] = status
except Exception as e:
logging.error(f"Error checking challenge: {str(e)}")
result["error"] = str(e)
results["data"].append(result)
return jsonify(results)


if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
Loading
Loading