import sqlite3 import json # The raw JSON data provided json_source = """ { "success": true, "data": [ {"id": 123, "type": "dynamic", "name": "drum machine", "value": 100, "solves": 160, "solved_by_me": true, "category": "rev", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 125, "type": "dynamic", "name": "Attack of the Clones", "value": 100, "solves": 173, "solved_by_me": true, "category": "crypto", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 126, "type": "dynamic", "name": "Revenge of the Sith", "value": 100, "solves": 224, "solved_by_me": false, "category": "crypto", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 127, "type": "dynamic", "name": "The Phantom Menace", "value": 100, "solves": 280, "solved_by_me": true, "category": "crypto", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 128, "type": "dynamic", "name": "zipbomb", "value": 100, "solves": 318, "solved_by_me": false, "category": "misc", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 129, "type": "dynamic", "name": "Wordler", "value": 100, "solves": 147, "solved_by_me": true, "category": "misc", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 131, "type": "dynamic", "name": "dilemma", "value": 100, "solves": 131, "solved_by_me": true, "category": "rev", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 133, "type": "dynamic", "name": "Quantum vernam", "value": 100, "solves": 202, "solved_by_me": false, "category": "crypto", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 135, "type": "dynamic", "name": "Another Android Applaketion", "value": 100, "solves": 106, "solved_by_me": true, "category": "rev", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 137, "type": "dynamic", "name": "Guess Flag", "value": 100, "solves": 284, "solved_by_me": true, "category": "crypto", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 138, "type": "dynamic", "name": "Ez Part", "value": 100, "solves": 96, "solved_by_me": true, "category": "crypto", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 140, "type": "dynamic", "name": "gamblecore", "value": 100, "solves": 142, "solved_by_me": true, "category": "web", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 142, "type": "dynamic", "name": "illegal downloading", "value": 100, "solves": 58, "solved_by_me": true, "category": "misc", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 143, "type": "dynamic", "name": "stackception-1", "value": 100, "solves": 123, "solved_by_me": false, "category": "pwn", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 147, "type": "dynamic", "name": "Le Canard du Lac", "value": 100, "solves": 289, "solved_by_me": true, "category": "web", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 130, "type": "dynamic", "name": "MagicAuth", "value": 101, "solves": 55, "solved_by_me": false, "category": "web", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 124, "type": "dynamic", "name": "New Mitigations", "value": 102, "solves": 48, "solved_by_me": false, "category": "pwn", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 144, "type": "dynamic", "name": "stackception-2", "value": 193, "solves": 15, "solved_by_me": false, "category": "pwn", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 132, "type": "dynamic", "name": "A real CTF", "value": 217, "solves": 13, "solved_by_me": false, "category": "rev", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 134, "type": "dynamic", "name": "Unlink THIS!", "value": 304, "solves": 8, "solved_by_me": false, "category": "pwn", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 141, "type": "dynamic", "name": "Pls respond", "value": 356, "solves": 6, "solved_by_me": false, "category": "misc", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"}, {"id": 139, "type": "dynamic", "name": "still-not-malloc", "value": 457, "solves": 3, "solved_by_me": false, "category": "pwn", "tags": [], "template": "/plugins/dynamic_challenges/assets/view.html", "script": "/plugins/dynamic_challenges/assets/view.js"} ] } """ DB_NAME = "ctf_challenges.db" def create_database(): try: # Load the JSON string into a Python dictionary data_dict = json.loads(json_source) challenges = data_dict['data'] # Connect to SQLite database (or create it if it doesn't exist) conn = sqlite3.connect(DB_NAME) cursor = conn.cursor() print(f"Connected to database: {DB_NAME}") # Create table # We store 'tags' as a TEXT field (JSON string) because SQLite doesn't have an ARRAY type. # We store 'solved_by_me' as INTEGER (0 or 1) because SQLite doesn't have a strict BOOLEAN type. create_table_query = """ CREATE TABLE IF NOT EXISTS challenges ( id INTEGER PRIMARY KEY, type TEXT, name TEXT, value INTEGER, solves INTEGER, solved_by_me INTEGER, category TEXT, tags TEXT, template TEXT, script TEXT ); """ cursor.execute(create_table_query) print("Table 'challenges' created successfully.") # Prepare data for insertion rows_to_insert = [] for challenge in challenges: row = ( challenge['id'], challenge['type'], challenge['name'], challenge['value'], challenge['solves'], 1 if challenge['solved_by_me'] else 0, # Convert bool to int challenge['category'], json.dumps(challenge['tags']), # Convert list to JSON string challenge['template'], challenge['script'] ) rows_to_insert.append(row) # Insert data insert_query = """ INSERT OR REPLACE INTO challenges (id, type, name, value, solves, solved_by_me, category, tags, template, script) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """ cursor.executemany(insert_query, rows_to_insert) conn.commit() print(f"Successfully inserted {cursor.rowcount} rows.") # Verification step: Read back first 3 rows print("\n--- Verifying Data (First 3 Rows) ---") cursor.execute("SELECT id, name, category, solved_by_me FROM challenges LIMIT 3") rows = cursor.fetchall() for row in rows: print(f"ID: {row[0]}, Name: {row[1]}, Category: {row[2]}, Solved: {bool(row[3])}") except sqlite3.Error as e: print(f"SQLite error: {e}") except json.JSONDecodeError as e: print(f"JSON parsing error: {e}") finally: if conn: conn.close() print("\nDatabase connection closed.") if __name__ == "__main__": create_database()