Skip to content

Commit

Permalink
test(signal): Add example script to send signal
Browse files Browse the repository at this point in the history
  • Loading branch information
julienloizelet committed Jan 26, 2024
1 parent 7080684 commit d51571d
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 7 deletions.
4 changes: 3 additions & 1 deletion examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

client = CAPIClient(
storage=SQLStorage(),
config=CAPIClientConfig(scenarios=["crowdsecurity/ssh-bf", "acme/http-bf"]),
config=CAPIClientConfig(
scenarios=["crowdsecurity/ssh-bf", "acme/http-bf"], prod=False
),
)

# Fetch signals from your data, and convert it into a list of signals accepted by CrowdSec
Expand Down
17 changes: 11 additions & 6 deletions examples/enroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __init__(self, prog, indent_increment=2, max_help_position=36, width=None):
try:
parser.add_argument("--prod", action="store_true", help="Use production mode")
parser.add_argument("--key", type=str, help="Enrollment key to use", required=True)
parser.add_argument("--machine_id", type=str, help="", required=True)
parser.add_argument(
"--machine_id", type=str, help="ID of the machine", required=True
)
parser.add_argument("--name", type=str, help="Name of the machine", default=None)
parser.add_argument("--overwrite", action="store_true", help="Force overwrite")
parser.add_argument(
Expand Down Expand Up @@ -56,18 +58,21 @@ def __init__(self, prog, indent_increment=2, max_help_position=36, width=None):
)
overwrite_message = "\033[1m(Force overwrite)\033[0m" if args.overwrite else ""
tags_message = f"\tTags:{args.tags}\n" if tags else ""
scenarios_message = f"Scenarios:{args.scenarios}" if scenarios else ""
env_message = "Env: production" if args.prod else "Env: development"
scenarios_message = f"\tScenarios:{args.scenarios}\n" if scenarios else ""
env_message = "\tEnv: production\n" if args.prod else "\tEnv: development\n"

database = "cscapi_examples.db" if args.prod else "cscapi_examples_dev.db"
database_message = f"Local storage database: {database}"
database_message = f"\tLocal storage database: {database}\n"

print(
f"\nEnrolling machine{name_message} with key '{args.key}' and id '{args.machine_id}' {overwrite_message}\n\n"
f"Details:\n"
f"{env_message}"
f"{scenarios_message}"
f"{tags_message}"
f"{user_agent_message}"
f"\t{env_message}\n\t{scenarios_message}\n\t{database_message}\n\n"
f"{database_message}"
f"\n\n"
)

confirmation = input("Do you want to proceed? (Y/n): ")
Expand All @@ -78,7 +83,7 @@ def __init__(self, prog, indent_increment=2, max_help_position=36, width=None):
client = CAPIClient(
storage=SQLStorage(connection_string=f"sqlite:///{database}"),
config=CAPIClientConfig(
scenarios=["crowdsecurity/ssh-bf", "acme/http-bf"],
scenarios=scenarios,
prod=args.prod,
user_agent_prefix=args.user_agent_prefix,
),
Expand Down
113 changes: 113 additions & 0 deletions examples/send_signal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
This script will send a simple signal.
"""

import argparse
import json
import sys
from cscapi.client import CAPIClient, CAPIClientConfig
from cscapi.sql_storage import SQLStorage
from cscapi.utils import create_signal


class CustomHelpFormatter(argparse.HelpFormatter):
def __init__(self, prog, indent_increment=2, max_help_position=36, width=None):
super().__init__(prog, indent_increment, max_help_position, width)


parser = argparse.ArgumentParser(
description="Script to send a simple signal.",
formatter_class=CustomHelpFormatter,
)

try:
parser.add_argument("--prod", action="store_true", help="Use production mode")
parser.add_argument(
"--machine_id", type=str, help="ID of the machine", required=True
)
parser.add_argument("--ip", type=str, help="Attacker IP", required=True)
parser.add_argument(
"--created_at",
type=str,
help="Signal's creation date. Example:'2024-01-26 10:20:46+0000'",
default="2024-01-26 10:20:46+0000",
)
parser.add_argument(
"--scenario",
type=str,
help="Signal's scenario. Example: 'crowdsecurity/ssh-bf'",
required=True,
)
parser.add_argument(
"--machine_scenarios",
type=str,
help='Json encoded list of scenarios. Example:\'["crowdsecurity/ssh-bf", "acme/http-bf"]\'',
default='["crowdsecurity/ssh-bf", "acme/http-bf"]',
)
parser.add_argument(
"--user_agent_prefix", type=str, help="User agent prefix", default=None
)
args = parser.parse_args()
except argparse.ArgumentError as e:
print(e)
parser.print_usage()
sys.exit(2)

ip_message = f"\tAttacker IP: '{args.ip}'\n"
created_at_message = f"\tCreated at: '{args.created_at}'\n"
scenario_message = f"\tScenario: '{args.scenario}'\n"
machine_scenarios = (
json.loads(args.machine_scenarios) if args.machine_scenarios else None
)
user_agent_message = (
f"\tUser agent prefix:'{args.user_agent_prefix}'\n"
if args.user_agent_prefix
else ""
)
machine_scenarios_message = (
f"\tMachine's scenarios:{args.machine_scenarios}\n" if machine_scenarios else ""
)
env_message = "\tEnv: production\n" if args.prod else "\tEnv: development\n"

database = "cscapi_examples.db" if args.prod else "cscapi_examples_dev.db"
database_message = f"\tLocal storage database: {database}\n"

print(
f"\nSending signal for machine '{args.machine_id}'\n\n"
f"Details:\n"
f"{env_message}"
f"{ip_message}"
f"{scenario_message}"
f"{created_at_message}"
f"{machine_scenarios_message}"
f"{database_message}"
f"{user_agent_message}"
f"\n\n"
)

confirmation = input("Do you want to proceed? (Y/n): ")
if confirmation.lower() == "n":
print("Operation cancelled by the user.")
sys.exit()

client = CAPIClient(
storage=SQLStorage(connection_string=f"sqlite:///{database}"),
config=CAPIClientConfig(
scenarios=machine_scenarios,
prod=args.prod,
user_agent_prefix=args.user_agent_prefix,
),
)

signals = [
create_signal(
attacker_ip=args.ip,
scenario=args.scenario,
created_at=args.created_at,
machine_id=args.machine_id,
)
]

client.add_signals(signals)

client.send_signals()

0 comments on commit d51571d

Please sign in to comment.