(smollerGraph)
  • Episode 19
  • 25 min

Active Directory attack paths in a graph database

A made-up company's Active Directory as a graph, and Cypher that follows several relationship types at once, so smollerGraph finds the paths from an account to Domain Admins, the way BloodHound does, in the browser too.

20 November 2026 · Watch on YouTube

Attack paths in a company's Active Directory are a shortest path question, and BloodHound made that well known, with Neo4j and Cypher. We make up a company's directory as facts, 275 accounts with their groups and computers, joined by five of the relationships BloodHound draws, MemberOf, AdminTo, HasSession, ForceChangePassword and GenericAll, each pointing the way an attacker would go. smollerGraph's graph, planner and parser learn to follow several relationship types at once, written with bars as Cypher writes them, and 105 of the accounts turn out to have a path to Domain Admins. Taking the backup account out of Domain Admins changes nothing, as it's still an admin on the domain controller, and taking away Engineering's full control of the database's account brings it down to 38. The explorer opens the company in your browser. It's a vanilla take on what BloodHound does, on a company that doesn't exist, built to see how it works.

Topics: Cypher, Graph algorithms, Visualization

The code

Each step on screen is a tag in the database's repository. The code at the end of the episode is ep019/end.

  1. 4:09ep019/step-1A made-up companyBrowseChanges
  2. 9:37ep019/step-2Several types at onceBrowseChanges
  3. 14:30ep019/step-3A bar in CypherBrowseChanges
  4. 16:58ep019/step-4In the explorerBrowseChanges

Transcript

Intro

0:00 In this made-up company, a hundred and five of its two hundred and seventy-five accounts have a path to Domain Admins, the group that runs the whole network. Our graph database finds a shortest path for each of them in about two milliseconds, in a browser tab, with a single Cypher query.

0:16 Welcome back to episode nineteen, where we're going to look at a company's network the way an attacker does, as a graph. To get there, our Cypher learns to follow several kinds of relationship in one pattern.

0:28 The series has a website of its own, with the background on the project and where it's heading. The code is on GitHub, and each step you see in these videos has a tag there, so you can build it as it was at that point. And there's a page on the site for each episode, where you can jump to a chapter or see which files it changed.

Last time

0:47 Last time, the explorer moved into a browser tab, without a server behind it. Emscripten compiled our library to WebAssembly, more or less unchanged, and the page builds its graph of mammals and birds from the Wikidata facts each time it opens. CI publishes it next to the API docs, on GitHub Pages, so anyone can try a query without installing anything. It has one graph to offer, though, with one relationship type in it, the parent taxon. Today it gets a second graph, and that one needs several types at once.

Attack paths

1:22 This is one of my favourite uses of a graph database, and it comes from security. A lot of companies run their Windows networks on Active Directory, Microsoft's directory of accounts, groups and computers, which decides who can log on where, and who can change what. At the top sits a group called Domain Admins, whose members can change any account and any computer in the domain. Each permission on its own looks harmless, a group here, an admin right on a computer there, and they're kept as long lists. Chained together, though, a few of them can lead from an ordinary account all the way to Domain Admins. John Lambert, at Microsoft, put it as defenders thinking in lists, while attackers think in graphs.

2:02 Here's the kind of chain I mean, four hops long. Amy works on the helpdesk, so her account is a member of the Helpdesk group. The helpdesk is an admin on each workstation, so that it can fix people's machines. Bob logged on to his workstation this morning, and while he's logged on, Windows keeps his credentials in the machine's memory, where an admin on that machine can often take them. So the arrow goes from the computer to Bob, as taking over the computer gets you Bob. Bob is also a member of Domain Admins, as he's one of the people who run the domain. So anyone who gets hold of Amy's account is four hops from running the whole network, and each of those hops comes from someone doing their job.

2:44 BloodHound, which SpecterOps develops, is the tool that made this well known as a graph question. Its collector reads a company's directory, and each permission becomes a relationship, pointing the way an attacker would move. It was built on Neo4j, which is why its queries are written in Cypher, and it's what we learn from today. We're going to use five of its relationship types, and MemberOf goes from an account or a group to the group it's in. AdminTo goes from an account or a group to a computer it's an admin on, and HasSession from a computer to someone logged on to it. ForceChangePassword means you can reset someone's password without knowing the old one, and GenericAll is full control of an object, which on an account means its password too. One of BloodHound's best-known questions is then a shortest path, from each account to Domain Admins, along any of these types.

Today

3:36 We're going to get there in four parts, starting from a company that doesn't exist. A small Python tool makes up a company and writes its directory as facts, as nothing real belongs in a public repository. Our Cypher follows a single relationship type in each part of a pattern, so the graph, the planner and the parser learn to follow several at once, with bars between them, as Neo4j writes it. The explorer gets the company as a second graph, so you can try these queries in your browser, and at the end, we take the defender's side, and work out which permissions to take away.

A made-up company

4:10 The company comes from a new tool, company, written in Python like the Wikidata one. Its docstring says it writes a made-up company's Active Directory as facts for sg load, with its people, their groups and their computers. Each relationship says who can take over what, pointing the way an attacker would go. The company and its people come from two lists of names and a seeded random generator, so each run writes an identical company. That matters here, as the facts go in the repository and a test counts on them. Underneath, it says how to run it, from the repository's root, with the facts file's path. It imports random, Python's module of random numbers, and sys, for the path we write to, and the seed is forty-two, like sg embed's. TEAMS has the five teams and how many people are in each, from eighty in sales and engineering down to twenty in HR, and then come the three that run IT. IT has those three groups, twelve people on the helpdesk, eight IT admins and three domain admins. SERVERS has the domain controller first, DC zero one, the server that holds the directory, then a file server, a database server and a backup server, the ones the IT admins look after. FIRST and LAST are the names, thirty first names and twenty-four last names, which make seven hundred and twenty pairs, plenty for two hundred and seventy-three people. They're made up, picked to look like a mixed office, and each person gets a first and a last name from them. The company function returns the nodes, each a name and a label, and the relationships, each a start, a type and an end, which is all a fact needs. The groups are Domain Users, which in Active Directory is everyone, then the teams and the IT groups, each a node labelled group, and the servers go in as computers. Sizes puts the teams and the IT groups together, and each person gets a made-up name, a workstation of their own, which they log on to, and their team, inside Domain Users. Everyone is each pair of a first and a last name, and sample picks as many of them as there are people, without repeats. Members keeps each team's people, and workstations each person's machine. The loop gives each person a name and a workstation, called W S and its number, in three digits, and adds both as nodes, the person labelled user and the workstation labelled computer. They're a member of Domain Users and of their team, and their workstation has their session, as they log on to it each day. Engineers are admins on their own workstations, to install their tools, and that's the first of the habits that make paths. The helpdesk is an admin on each workstation, resets the teams' passwords, and logs on to a few workstations to fix things. So Helpdesk gets AdminTo on each workstation, and ForceChangePassword on everyone in the five teams, though not on the people in IT. Each person on the helpdesk logs on to four workstations, picked at random, and the workstations go through sorted first, as sample needs a sequence, and a dictionary's values aren't one. The IT admins look after the servers, and each of them logs on to two. So IT Admins is an admin on the three servers after the domain controller, and each IT admin has a session on two of them, picked at random as well. Domain admins look after the domain controller, so their group is an admin on DC zero one. And in the loop, each domain admin has a session on it, from logging on to look after it. Last come two service accounts, the accounts that programs run as, and each of them got its rights for a reason. The backup's account runs on each server, and someone put it in Domain Admins, as it copies the domain controller too. The database's account runs SQL zero one, and Engineering has full control of it, so developers can restart the database when it hangs, without asking IT. So the loop makes the backup's account an admin on each server, with a session there. Then come its membership of Domain Admins, the database account's admin rights and session on SQL zero one, and Engineering's GenericAll on it. Main checks it has the facts file's path, and makes the company from the seed. It writes a comment at the top of the file, which says what the file is and which tool wrote it, so whoever finds the facts knows where they came from. Then it writes each node with its label, and each relationship as a fact, in the format sg load reads. I made all of this up, and I gave the company a few holes on purpose, for us to find later on.

8:34 Running the tool writes the facts file, and git status prints nothing, so the company it made is the one in the repository, line for line. Grep dash o pulls out each relationship's type, and sort and uniq dash c count them. The five hundred and forty-seven memberships are the biggest share, and there's a single GenericAll, Engineering's, on the database's account. Then sg load reads it into a graph, with five hundred and sixty-one nodes and one thousand five hundred and five relationships, far smaller than the Wikidata graph.

Several types at once

9:07 Let's ask about one of the engineers, Dina Varga, and T holds the five types, with bars between them, so the query stays readable. The query asks for a shortest path from Dina to Domain Admins, along any of them, and our lexer stops at the first bar, as it's a character it doesn't know. With MemberOf on its own, the query runs, and there's no path at all. Dina's way in, if there is one, has to go through other kinds of relationship, so MemberOf on its own can't find it.

9:38 The change starts at the bottom, in the graph, where a node's relationships are followed. In the graph's header file, Hops takes a vector of types now, where it took a single type. The vector comes as a constant reference, so it isn't copied on each call. It returns the hops along relationships of any of the types, those that start at the node, those that end at it, or both, the ones out and then the ones in. Neighbours keeps its single type, as the walk and the embeddings ask for one, and it returns the nodes that the hops along that type lead to. Follow, the private function that reads a chain of relationships, takes a vector of tokens. It adds the hops whose type is one of the tokens, out or in, in the order they were added. In graph, Hops starts by turning each type into its token, the small number a relationship's record keeps in place of the type's name, and the vector of tokens starts empty. A type without a token isn't on any relationship, so it's left out, and when none of them has one, there's nothing to follow. The loop looks each type up in the store of types and keeps the ones it finds, and with none found, it returns straight away. Then Hops hands them on, to the chain of relationships out of the node and to the chain into it, depending on the direction, as before. Further down, Follow takes the vector of tokens now, and in it, the test on each relationship becomes std find, from the algorithm header, which looks for the record's token in the vector. A linear search sounds slow, but a pattern has a handful of types, so it's a few comparisons, and I think a hash set would cost more than it saves. Neighbours puts its type in braces, which makes a vector of one string, and asks Hops.

11:25 In the query header file, there are two functions to change, the shortest path from episode twelve and the cheapest path from episode thirteen. The shortest path now follows relationships of any of the types it's given, and it takes the vector of types as a constant reference, after the two nodes and before the direction. The cheapest path takes them in that spot as well, and a relationship without a weight that's a whole number of zero or more still isn't followed, as Dijkstra's algorithm needs. In query, the functions inside pass the types along, starting with Expand, which takes one side of the search from both ends a hop further, takes the types with its side and the other side, and asks Hops with them. Settle, which settles the cheapest node left in Dijkstra's algorithm and lowers its neighbours' costs, takes them before the weight's key, and asks Hops with them too. ShortestPath takes the types, and hands them to Expand, for whichever side has fewer nodes to go on from. CheapestPath takes them too, and hands them to Settle, for whichever side's next node is cheaper. Nothing else about the searches changes, and I like that, as they count hops, and a hop's type plays no part in that. In the plan's header file, the comments on the expand and the shortest path now say any of the step's types. A step's text gets empty braces as its default, and a new field, types, goes at the end, for the relationship types an expand or a path follows. The text needs a default, as the expand's initializer won't name it anymore, and Emscripten's newer Clang warns about a designated initializer that leaves out a field without one, as we saw last episode. And the new field goes at the end, as the steps written without names fill their fields in order. In plan, a small function called Types joins a step's types with bars, with Abseil's str join, and its comment gives MemberOf bar AdminTo as an example. An expand step gets the pattern's types, after its direction, and so does a path step, after its weight. Running an expand, the plan asks Hops for the node's hops along the types, in the step's direction, and puts each hop's node in its slot. The shortest and the cheapest path get the step's types, and EXPLAIN writes them with bars, with a star after them for a path. In the parser's header file, the type becomes a vector of types, and a relationship of any of them matches. The parser still reads one type, which it pushes on the vector, and the bar comes in the next step. In main, sg path puts its single type in braces. In the path tests, the helper and the calls put braces round their one type as well. Then a new test follows any of its types, from the red fox to the meadow, along EATS and LIVES_IN, following the arrows. The fox eats the rabbit, and the rabbit lives in the meadow, so a path that can take both types gets there, with a relationship of each. The test checks there's a path, and that it's the fox, the rabbit and the meadow. The cheapest path's test puts braces round KNOWS, like the others. In the parser's tests, the type they check becomes a vector, with EATS in it.

A bar in Cypher

14:30 Now for the bar, and in the lexer's header file, the comment on symbols gets the bar as its example. In the lexer, the bar joins the list of single characters that make a symbol, so a query with a bar in it gets past the lexer. In the parser, the grammar gives a relationship types, and types are a type followed by any number of bars, each with a type after it. The code reads them in a do while loop, like the one for the return items. It reads a type, and a bar without a type after it fails, saying a relationship type was expected, and where. A bar between two types, as in MemberOf bar AdminTo, matches a relationship of either of them. And the loop goes round again for as long as the next symbol is a bar. The parser's tests get a new one, reads several types, with a shortest path from Ben to Domain Admins along MemberOf and AdminTo. They check there's a single relationship, with both types in it, in order, and any number of hops. And a bar followed by a bracket fails at seventeen, where the type should be. In the plan's tests, a new test checks how EXPLAIN writes several types. On a shortest path from any user to Domain Admins, it writes a label scan for the users, a seek for Domain Admins, and the shortest path between them, with both types and their bar. And a new run test asks for what the rabbit eats and where it lives, along EATS and LIVES_IN at once, and gets the clover and the meadow.

The paths

16:02 We build it again, and with the new tests in, all seventy-one pass. Dina's query runs this time, with T holding the five types as before, and it prints her path. She's a member of Engineering, Engineering has full control of svc_sql, the database's account, which is an admin on SQL zero one, where svc_backup is logged on, and svc_backup is a member of Domain Admins. That's five hops, from an engineer to the group that runs the network. For all the accounts at once, Q holds the pattern, with u as any node labelled user, in place of Dina. The plan scans the label for the users, seeks Domain Admins in the index, checks its label, and looks for a shortest path from each user, with the five types and their bars. Running it returns a hundred and five rows, so a hundred and five of the two hundred and seventy-five accounts have a path, and it takes under a millisecond.

In the explorer

16:58 To try this without building anything, the company goes in the explorer, next to the mammals and birds. In the CMake file, the browser build's module now comes with whichever facts it builds its graph from, and a second preload file packs the company's facts into the module, beside Wikidata's. They're about sixty-eight kilobytes, next to over three megabytes of Wikidata, so the page loads about as fast as before. The api module gets the graphs the page can make, in an object called graphs. Wikidata's has its facts file and the type its embeddings follow, the parent taxon. The company's has its own facts, MemberOf for its embeddings, and a query to open on, the shortest paths from each user to Domain Admins, along the five types. The page picks one by its address, from the facts packed beside the module, so it's Wikidata's mammals and birds, or with question mark graph equals company, the made-up company's Active Directory. In the in-page function, a constant called asked reads graph from the address's search parameters, and a name the list doesn't have gets Wikidata, so a typo still opens a working page. Build makes that graph from its facts, and the object it returns says which graph it is, and the query it opens on. In app, at the bottom, before the first query runs, when api has a graph, the header's links show up. The header links to the graphs the page can make, with the one it made marked, and the company's graph opens on its query. D3 selects the nav, marks it shown, and marks as active the link whose data graph is the page's graph. Then the company's query goes in the box, when there's one. Under sg serve, api has no graph, so neither runs, as the server has a single graph to offer. The page's index gets the two links, in a nav called graphs, one to the page's own address, for mammals and birds, and one with the company in it. In the style sheet, the links are for the browser build, as sg serve can't make a second graph. The nav is hidden until it's marked shown, then it's a flex row, pushed to the right of the header. The links are muted, and the active one is chartreuse, the colour of the nodes. The module's test loads a second module, as a second page would, since each load starts a module with its own memory. It builds the company there, with MemberOf for its embeddings, as the page would. Then it runs the query from each user to Domain Admins, and checks that the answer, each user's name and the length of their path, counts a hundred and five accounts with a path. Ctest runs it, as it did the first. The README says a relationship can have a few types with bars between them, such as EATS bar LIVES_IN. A new section, a company's Active Directory, says the facts file is a made-up company's directory, written by the tool from two lists of names and a seeded random generator. It has two hundred and seventy-five accounts, two hundred and seventy-seven computers and nine groups, joined the way BloodHound draws them, each relationship pointing the way an attacker would go, with MemberOf, AdminTo, HasSession, ForceChangePassword and GenericAll. A shortest path along any of the five, from an account to Domain Admins, is an attack path. Then come the commands, to make the company with the tool, load it into a graph of its own, and ask for each user's path, with the query we ran a moment ago. And in the browser section, after the address to open, there's a second one, with question mark graph equals company, for the made-up company's directory, which opens on its attack paths. It's on GitHub Pages too, next to the explorer's own link.

20:37 Built for the browser, and opened with the company in its address, the page makes the company's graph and runs its query straight away. The header has the two graphs, with the company's in chartreuse. The status line says a hundred and five rows, in about two milliseconds, in the browser. On the canvas, the engineers make the big cluster, and their lines run down through svc_sql and SQL zero one to svc_backup, and into Domain Admins. Helpdesk sits on the edge of that cluster, and its way down goes through WS two seven one and Ben Bose. Dina's path is easier to read on its own, so we type her query, the pattern with her name in it, and all five types spelled out, as the page has no T to hold them. It draws her five hops, with the relationships between those nodes. Searching for svc_backup opens its record, with the servers it's an admin on, its membership of Domain Admins, and its sessions, the arrows coming in from the servers.

Cutting the paths

21:35 Now the defender's side, where the question is which permissions to take away. Looking down the paths, the first ones all end with svc_backup's membership of Domain Admins. Grep dash c counts the lines with it, and ninety of the hundred and five paths go through that relationship. Our Cypher can't delete anything yet, so grep dash v writes the facts without that line, which is what taking the backup account out of Domain Admins would look like. We load them into a new graph, and ask again, and it's still a hundred and five. I expected that number to drop a lot, and it didn't move at all. The backup account's own path shows why, as it's an admin on the domain controller, where the domain admins log on. So it can take Ben Bose's credentials there, and it's three hops from Domain Admins without being in the group. Keep in mind that anything that's an admin on a domain controller is as good as a domain admin, whatever groups it's in.

22:31 The engineers came in through Engineering's full control of the database's account, all eighty of them, so the second fix takes that line away as well. In the new graph, the query for each user's path prints its count at the bottom, and thirty-eight accounts are left with a path, from a hundred and five. Dina is still one of them, though, six hops away now. She's an admin on her own workstation, and someone from the helpdesk logged on to it to fix something. The helpdesk is an admin on Ben Bose's workstation, where he logs on with his domain admin account each day. So the next fix would be the usual advice, a separate account for admin work, used on machines kept for admin work.

What's missing

23:11 Our version is a vanilla take on what BloodHound does with Neo4j, tried on five hundred and sixty-one nodes, where a real directory can have hundreds of thousands. We counted paths with grep, as our Cypher can't count, group or sort yet, and asking which relationship most paths go through needs all of those. Shortest path returns one path for each account, where Neo4j's all shortest paths returns each path of that length, so BloodHound can draw all the ways in at once. We took permissions away by editing the facts and loading them again, as our Cypher can't delete a relationship. And our company is made up, while a real directory's data comes from SharpHound, BloodHound's collector, run with your security team's say-so on a network you're responsible for.

Wrap-up

23:57 smollerGraph can look for attack paths now, on a company that a small Python tool made up, with Cypher that follows several relationship types at once. And the explorer opens that company in your browser, with its query ready to run. I enjoyed the defender's side the most, as it didn't go the way I expected. Taking the backup account out of Domain Admins changed nothing, while Engineering's one right over the database's account, which looked minor, took the count from a hundred and five to thirty-eight.

24:27 If you've run BloodHound at work, I'm curious what its paths turned up, and whether the fix that worked was the one you expected. Tell me in the comments, and subscribe to the channel if you'd like to follow the series. I hope you find this useful, and thanks for watching.