1stDevelop

Blog — Cyber

RAG for Penetration Testing: Keyword Search, Vectors or Graphs?

RAG for penetration testing explained simply: how keyword, vector and graph search differ, when each wins, and how to keep every client’s findings isolated

  • RAG
  • retrieval-augmented generation
  • penetration testing
  • pentest
  • graph RAG
  • vector search
  • keyword search
  • hybrid search

The problem: too much to remember, too little time

A penetration tester spends a surprising amount of the day looking things up. Which ports does this service use? Is there a known weakness in this software version? What did we find on this client’s network last year? Which tool command gets a clean result against this kind of system? The answers live in thousands of pages: vulnerability databases, tool manuals, past reports, team notes, attack technique catalogues.

An AI assistant can help, but only if it answers from your material and shows where each fact came from. Retrieval-Augmented Generation (RAG) does exactly that: the model looks up the relevant pages first, then writes an answer that cites them. The idea is simple. The hard part is the looking up, because there are three very different ways to do it, and a pentest knowledge base needs all three.

This article explains the three search styles in plain words, compares them on real pentest questions, and shows how to combine them in one system that keeps every client’s data separate from every other client’s.

Three ways to find the right page

Imagine the same question asked three ways. The question is: “How do attackers move from one Windows machine to another once they have a password?”

Keyword search (the index at the back of a book)

Keyword search matches the exact words you type. Type lateral movement windows, and it returns every page containing those words, ranked by how often and how prominently they appear. This is the classic search behind tools like Elasticsearch, grep, or a wiki search box.

It is fast, cheap, and exact. If you know the precise term, a CVE number like CVE-2021-34527, a tool flag, or an error string, keyword search finds it instantly and never “makes things up.” Its weakness is that it only knows words, not meaning. Search lateral movement and it will miss a page that says pass-the-hash or PsExec without ever using the phrase “lateral movement,” even though that page is exactly what you wanted.

Vector search (find pages that mean the same thing)

Vector search, the heart of ordinary RAG, matches by meaning instead of exact words. Every page is turned into a list of numbers (an embedding) that captures what it is about. Your question is turned into numbers the same way, and the system returns the pages whose numbers are closest.

Now lateral movement windows also finds the pass-the-hash and PsExec pages, because those ideas sit close together in meaning even though they share no words. This is what makes vector search feel smart. Its weakness is the mirror image of keyword search: it is fuzzy. It can miss an exact code or version number (numbers look similar to it even when they mean very different things), and it has no idea how facts connect to each other. It returns pages that look related; it cannot reason across them.

Graph search (follow the connections)

Graph search stores facts as a web of things and relationships: this host runs this service, this service has this vulnerability, this vulnerability is exploited by this technique, this technique was used in this past engagement. Instead of ranking pages, it follows links.

This is the only one of the three that can answer a connected question: “Starting from the web server we just cracked, what is the shortest path to the domain controller?” Keyword and vector search can hand you pages about each hop, but only a graph can walk the chain host by host and lay out the route. Its weakness is cost and upkeep: someone (or some pipeline) has to decide what the things and relationships are and keep the web accurate, which is far more work than dropping documents into a vector store.

The short version: keyword search knows words, vector search knows meaning, and graph search knows connections. None of them is best at all three.

Side by side

Keyword searchVector search (RAG)Graph search
Matches onExact wordsMeaningConnections between facts
Great atCVE IDs, tool flags, error strings, exact names“Find me things like this”, vague wording, synonymsAttack paths, “what connects to what”, blast radius
Weak atSynonyms, paraphrasesExact codes and version numbers; reasoning across pagesSetup and upkeep; needs clean, structured facts
Can it invent facts?NoIt retrieves real pages, but ranking can be fuzzyNo, but a wrong link misleads
Effort to buildLowLow to mediumHigh
Cost to runLowMediumMedium to high
Updates when a doc changesInstantRe-embed the changed pagesRe-extract facts and fix links

Here is how the three behave on everyday pentest questions:

  • “What is CVE-2021-34527?” Keyword search wins. The ID is exact; you want the precise record, not something that means roughly the same.
  • “Have we seen a misconfiguration like this before?” Vector search wins. You are describing a shape, not naming a thing, and past reports will word it differently.
  • “From this foothold, what can I reach?” Graph search wins. This is a path question, and only the graph walks the links.
  • “Summarise every finding touching the payment system across all our reports.” Vector search finds the candidate pages; a graph makes the list complete by following every link into the payment system. Together they beat either alone.

The lesson is not to pick one. It is to send each question to the search that answers it best, and to combine their results. That is what the architecture later in this article does.

What a pentest knowledge base holds

A pentester’s knowledge splits into two kinds, and they are stored differently. The first kind is shared reference: knowledge true for everyone, no matter the client. The second kind is engagement data: findings about one specific client, which must never mix with another client’s. The shared reference is the library; the engagement data is the case file.

Shared reference (one shelf per topic)

Like any library, build one collection per topic rather than one giant pile:

CollectionWhat goes inBest search for it
vuln_catalogueCVE records, advisories, exploit notesKeyword for the ID, vector for “like this”
techniquesAttack technique catalogue (for example MITRE ATT&CK), playbooksGraph for links, vector for wording
toolingTool manuals, command recipes, flagsKeyword for flags, vector for “how do I”
hardeningSecure-config guides, benchmarksVector, with keyword for exact setting names
methodologyTesting standards, checklists, report templatesVector

Most of these feed both a vector store (for meaning) and a keyword index (for exact terms) from the same documents. The techniques collection also feeds the graph, because techniques naturally link to the vulnerabilities and tools they use.

Engagement data (one case file per client)

This is what the team gathers during a test: discovered hosts and services, confirmed vulnerabilities, captured evidence, and the written findings. It becomes three things at once:

  • Vector pieces so you can ask “did we see anything like X” within this engagement.
  • Keyword records so an exact host name, IP or port is found instantly.
  • Graph nodes and links so you can trace attack paths: host runs service has vulnerability enables technique reaches next host.

The graph is where engagement data earns its keep. A list of findings is just a list; the same findings as a graph become a map of how an attacker moves through the client’s network, which is exactly what the client is paying to understand.

Every piece, in every store, carries labels: the source, the date, and, for engagement data, the engagement_id. That last label is the one the next section is built around.

One client, one wall

Client data is the most sensitive thing a pentest firm holds. A report describes exactly how to break into a company. Leaking one client’s findings into another client’s workspace, even inside a search result nobody reads, is a serious breach and probably a contract violation. So engagement data is isolated the same way patient data is in healthcare: the engagement is not a filter you remember to add, it is a wall the search runs inside.

Three ways to build the wall

ApproachHow it worksIsolationEffortGood for
Label filterOne store; every piece carries engagement_id and every query filters on itWeak: one missed filter leaks across clientsLowestPrototypes only
Namespace per engagementA separate index (namespace or partition) per engagement; a search opens inside oneStrong: the search never sees other engagementsModerateMost firms
Separate store per engagementA physically separate database or encrypted store per clientStrongest: separate keys and backupsHighestHigh-sensitivity clients, legal holds

The same choice applies to all three search styles. The vector store uses namespaces or per-engagement collections; the keyword index uses a separate index per engagement; the graph uses a separate graph (or a strictly partitioned one) per engagement. A query only ever touches the one engagement the user has opened.

Rules that make the wall hold

  • Scope first, search second. The app checks which engagement the user is working on and whether they are assigned to it, before any store opens. The engagement_id comes from that check, never from the user’s typed question.
  • No cross-engagement queries exist. There is no function that searches all clients at once. A firm-wide question (“how often do we see this across clients”) is answered from a separate, de-identified store with client details stripped out.
  • Shared reference stays separate. The CVE catalogue and technique library are not client data, so they live in the shared stores and are queried freely. Only engagement data is walled.
  • Encrypt per engagement and expire access. Separate keys per client make deletion at the end of a contract clean: destroy the key and the data is gone. Access tokens name the user, the engagement and a reason, and expire quickly.
  • Log every query. Who searched, which engagement, what came back. Clients and auditors can ask for this.
  • Test the wall. Keep automated tests that try to read engagement B while scoped to engagement A. They must fail every time, on every code change.

How the pieces fit together

The strong system is not one search; it is all three behind a router, with a scope gate in front and a merge step at the end.

A question first hits the scope check, which settles which engagement is open and which searches to run. A simple question may use one search; a hard one uses all three. Each search has two halves: a shared half (CVEs, techniques, tools) that anyone may query, and a client half that opens only the one engagement the user is scoped to. The results are merged and reranked, and the model writes an answer that cites each source, whether it came from the shared library or the client’s own case file.

Scope, safety and ethics

A pentest assistant is a power tool, and the same rules that govern the humans govern the system.

  • Stay inside the authorised scope. The engagement defines which systems may be tested. The assistant should reason only about those, and should refuse to help target anything outside the signed scope. Scope is data in the system, not a suggestion.
  • Cite, never invent. Every technical claim, a CVE, a default credential, a command, must point to a source in the knowledge base. An exploit detail with no citation is treated as unverified, because a confident wrong answer during a live test wastes time or breaks something.
  • Keep raw findings out of the shared stores. Client specifics, real hostnames, captured credentials, never flow into the shared reference or any cross-client store. Only de-identified, general lessons do.
  • Protect the knowledge base itself. It describes how to attack systems and holds client findings, so it is a high-value target. Encrypt it, lock down access, and log reads. A leak here is worse than a leak of any single report.
  • Keep a human in charge. The assistant drafts, suggests and recalls; a tester decides and acts. Nothing runs an exploit on its own.
  • Watch for poisoned sources. Advisories and write-ups pulled from the internet can be wrong or planted. Prefer trusted feeds, record where each document came from, and let testers flag a source as unreliable.

Where to start

Don’t build all three searches at once. Start with the one that pays off fastest and add the others as the need becomes real.

  • Start with keyword plus vector on shared reference. Index your CVE catalogue, technique library and tool docs. Most “what is this / how do I” questions are answered here, with no client data in play, so the risk is low while you learn chunking and testing.
  • Add engagement isolation before any client data goes in. Choose namespaces per engagement, wire scope to the user’s assignment, and put the cross-engagement leak test in your pipeline. Prove the wall holds before loading a single real finding.
  • Load engagement data as vector plus keyword first. “Did we see this before” and exact host lookups cover most daily use without the graph.
  • Add the graph when path questions start to matter. Begin with the technique library (it links naturally), then model engagement findings as a graph so you can trace attack paths and blast radius.
  • Keep a test set and a log from day one. Known questions with expected sources tell you when retrieval breaks; the query log is what clients and auditors will ask for.

The headline: keyword search knows words, vector search knows meaning, graph search knows connections, and a strong pentest assistant routes each question to the right one and keeps every client behind its own wall. The search styles are ordinary technology now. The discipline, scope, citations, isolation and a test that proves the wall holds, is what makes it safe to use on real engagements.

← Blog