Implementation:0x4m4 Hexstrike ai Flask Tool Endpoint
| Knowledge Sources | |
|---|---|
| Domains | Web_API, Tool_Execution, Security |
| Last Updated | 2026-10-10 00:00 GMT |
Overview
Flask REST endpoint pattern that receives tool parameters from the MCP client, constructs shell commands, executes them with recovery, and returns JSON results.
Description
The HexStrike Flask server exposes 100+ tool endpoints under /api/tools/*. Each endpoint follows the same pattern: parse JSON from request.json, extract tool parameters, construct a shell command string, and delegate execution to execute_command_with_recovery(). The nmap endpoint at L10327 is representative of this pattern. Endpoints also exist under /api/intelligence/*, /api/bugbounty/*, /api/ctf/*, and /api/vuln-intel/* for higher-level workflows.
Usage
Import and run the Flask server to expose all security tool endpoints. The MCP client calls these endpoints via HTTP:
- Single tool execution: POST to /api/tools/{tool_name} with tool parameters
- Intelligent scanning: POST to /api/intelligence/smart-scan for AI-driven multi-tool scans
- Bug bounty workflows: POST to /api/bugbounty/comprehensive-assessment for full pipeline
- CTF solving: POST to /api/ctf/auto-solve-challenge for automated CTF solving
Code Reference
Source Location
- Repository: HexStrike AI
- File: hexstrike_server.py
- Lines: L10327-10375 (nmap endpoint, representative); 100+ endpoints across L10327-17230
Signature
@app.route("/api/tools/nmap", methods=["POST"])
def nmap():
"""
Execute an Nmap scan against a target.
Expects JSON body:
target: str - scan target (required)
scan_type: str - Nmap scan flags (default "-sV")
ports: str - comma-separated port list
additional_args: str - extra CLI args (default "-T4 -Pn")
use_recovery: bool - enable error recovery (default True)
Returns JSON response with stdout, stderr, success, and recovery_info.
"""
data = request.json
target = data.get("target", "")
scan_type = data.get("scan_type", "-sV")
ports = data.get("ports", "")
additional_args = data.get("additional_args", "-T4 -Pn")
use_recovery = data.get("use_recovery", True)
# Construct shell command
command = f"nmap {scan_type} {additional_args}"
if ports:
command += f" -p {ports}"
command += f" {target}"
# Execute with or without recovery
if use_recovery:
result = execute_command_with_recovery("nmap", command, data)
else:
result = execute_command(command)
return jsonify(result)
Import
# The Flask app is defined in hexstrike_server.py and run directly:
# python3 hexstrike_server.py
# The app listens on 0.0.0.0:5000 by default
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| target | str | Yes | IP address or hostname to scan |
| scan_type | str | No | Nmap scan flags (default "-sV") |
| ports | str | No | Comma-separated port list or ranges |
| additional_args | str | No | Extra Nmap CLI arguments (default "-T4 -Pn") |
| use_recovery | bool | No | Enable intelligent error recovery (default True) |
Outputs
| Name | Type | Description |
|---|---|---|
| success | bool | Whether the command executed successfully |
| stdout | str | Command standard output |
| stderr | str | Command standard error |
| recovery_info | Dict | Recovery attempts, actions, and history (when use_recovery=True) |
| human_escalation | Dict | Escalation data when recovery is exhausted (optional) |
Usage Examples
Execute Nmap via Flask Endpoint
import requests
# POST to the Flask server's nmap endpoint
response = requests.post("http://localhost:5000/api/tools/nmap", json={
"target": "192.168.1.1",
"scan_type": "-sV",
"ports": "1-1000",
"additional_args": "-T4 -Pn",
"use_recovery": True
})
result = response.json()
print(f"Success: {result['success']}")
print(f"Output: {result['stdout'][:500]}")
if result.get("recovery_info", {}).get("recovery_applied"):
print(f"Recovery applied: {result['recovery_info']['attempts_made']} attempts")
Execute Gobuster via Flask Endpoint
response = requests.post("http://localhost:5000/api/tools/gobuster", json={
"url": "http://target.example.com",
"mode": "dir",
"wordlist": "/usr/share/wordlists/dirb/common.txt",
"additional_args": ""
})
result = response.json()
print(f"Directories found: {result['stdout']}")
Related Pages
Implements Principle
- Principle:0x4m4_Hexstrike_ai_Client_Server_HTTP_Communication
- Principle:0x4m4_Hexstrike_ai_Resilient_Subprocess_Execution