Podcast Lookup

Course 25 - API Python Hacking | Episode 6: Privilege Modification and User Impersonation

CyberCode Academy

  • February 27, 2026
  • 17 min
Episode page
About this episodeIn this lesson, you’ll learn about: Programmatic Privilege Modification How to use the AdjustTokenPrivileges API to enable or disable…
In this lesson, you’ll learn about:
  • Programmatic Privilege Modification
    • How to use the AdjustTokenPrivileges API to enable or disable specific privileges
    • Understanding the TOKEN_PRIVILEGES structure and how privilege attributes are modified
    • Enabling critical privileges like SeDebugPrivilege to allow advanced system access
  • Preparing for Token Manipulation
    • Identifying a target process or user through window handles or process IDs (PID)
    • Elevating your script’s permissions to allow interaction with protected system processes
    • Understanding why privilege elevation is required before duplicating tokens
  • Token Duplication Process
    • Using DuplicateTokenEx to create a new primary token from an existing process
    • Understanding how duplicated tokens inherit the identity and permissions of the original user
    • Preparing duplicated tokens for use in launching new processes
  • Launching Processes Under a Different Identity
    • Using CreateProcessWithToken to start applications (e.g., cmd.exe) under another user’s context
    • Understanding how impersonation allows execution with different privilege levels
    • Observing how processes can run with the security context of another active user or system account
  • Key Outcome
    • Understanding how Windows tokens can be modified, duplicated, and used for impersonation
    • Building the foundation for creating tools that perform privilege escalation, impersonation, and advanced system interaction


You can listen and download our episodes for free on more than 10 different platforms:
https://linktr.ee/cybercode_academy

Get the books of the CyberCode Academy podcast:
📚 The book series: 416 lessons, 12 books, hundreds of original diagrams: security, networking and programming.
https://cybercodeacademy.gumroad.com/

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Course 25 - API Python Hacking | Episode 6: Privilege Modification and User Impersonation

00:00—
  1. Speaker 1

    Welcome back to the deep dive. We have a stack of research today that is frankly, gonna make you look at your computer screen just a little different.

  2. Speaker 2

    Absolutely, we're going way past.

  3. Speaker 1

    The user interface today, right straight into the kernel. Because you know, we usually talk about security in this sort of abstract way, like don't click the link, use a password manager, that sort of basic hygiene stuff.

  4. Speaker 2

    Standard advice.

  5. Speaker 1

    Yeah, but today today we are really getting under the hood. We're talking about identity theft, but not the kind where someone steals your credit card number to buy a TV or whatever. Right, We're talking about digital identity theft at the operating system level, the kind where a program, you know, just a piece of code, decides it doesn't want to be itself anymore.

  6. Speaker 2

    It wants a promotion exactly.

  7. Speaker 1

    It wants to be the system administrator.

  8. Speaker 2

    It's a really fascinating distinction because in the physical world, if I want to impersonate you, I have to put on a disguise, maybe for just signature.

  9. Speaker 1

    It's messy, very messy.

  10. Speaker 2

    But in the Windows operating system, your identity isn't a physical thing. It's a data structure. Yeah, it's just a block of memory. And if you know how to find that block of memory and copy it. You don't just look like the administrator.

  11. Speaker 1

    You actually are the administrator.

  12. Speaker 2

    To the computer. Yes, you are the administrator, and that is.

  13. Speaker 1

    The core of what we're breaking down today. We're looking at this technical walkthrough on building a token and theft tool, and the best part is we aren't downloading some shady executable from a random forum.

  14. Speaker 2

    Definitely do not do that, right.

  15. Speaker 1

    We are actually going to deconstruct how you build this tool from scratch, line by line using Python.

  16. Speaker 2

    Which is what makes this specific deep dive so valuable for you listening, because when you use a pre made tool, you're just a user.

  17. Speaker 1

    You're just pushing a button exactly.

  18. Speaker 2

    But when you build it yourself using the Windows API, you actually learn how the operating system thinks. We're going to be talking about a standard Python library called modding types to talk directly to kernel three to two.

  19. Speaker 1

    Okay, so let's start with the absolute basics. If we're going to steal an identity, we need to know what an identity looks like to a computer. The sources keep using this term process token. What exactly is a process token?

  20. Speaker 2

    Think of the process token as a security badge Okay, like a literal ID card that you wear on a lanyard, Every single program running on your machine has one. If you open Notepad, the OS hands nopad a.

  21. Speaker 1

    Token, and if I open Chrome, Chrome.

  22. Speaker 2

    Gets a token. Yeah, and that token tells the system to fundamental things. Who are you and what are you allowed to do?

  23. Speaker 1

    So it's basically a permission slip exactly.

  24. Speaker 2

    It holds your security identifier or SID, which is your unique user ID. But honestly, more importantly for our purposes, it holds a list of privileges.

  25. Speaker 1

    Right, the specific rights.

  26. Speaker 2

    Yeah. Can this program change the system time? Can it load drivers? Can it debug other programs?

  27. Speaker 1

    And I assume most programs just run with standard privileges right like nope, pad can't do much damage.

  28. Speaker 2

    Correct, standard users, standard rights. But on processes, like the ones that run the operating system itself, they have tokens with almost every privilege enabled, the VIP badges, the VIP badges. You can get your hands on one of those tokens you own the machine.

  29. Speaker 1

    Okay, So the sources introduce a specific API call to mess with these tokens, and it's called adjust Token Privileges, which honestly sounds almost bureaucratic. It does like, excuse me, I'd like to adjust my privileges please.

  30. Speaker 2

    Well, Windows is incredibly bureaucratic. Yeah, oh yeah. You can't just flip a switch and say give me power. The source material highlights that to use this function you have to build a very specific structure.

  31. Speaker 1

    In memory, a token privileges structure.

  32. Speaker 2

    Yes, and this is where the coding gets the bit in the weeds because we're writing in Python, which is high level and friendly, but we have to speak.

  33. Speaker 1

    C because the Windows kernel is C.

  34. Speaker 2

    Exactly, it's low level and strict. So we have to use that C type's library to construct this C style structure. And inside that structure there is a really cical piece of data called the LUID.

  35. Speaker 1

    The LUD that stands for locally unique identifier.

  36. Speaker 2

    There's got it. See you and I know the privilege as say cedabug privilege.

  37. Speaker 1

    Right, it's a string of text, but.

  38. Speaker 2

    The kernel doesn't read English strings. It uses numbers. Interesting. Yeah, when your computer boots up, it assigns a random number to cedabug privilege. That number is the LUID, so.

  39. Speaker 1

    It changes every time you restart.

  40. Speaker 2

    Exactly, So your script can't just ask the system for debug rights. It first has to ask the system, hey, what number represents debug rights today?

  41. Speaker 1

    Ah, so it's like looking up a phone number in a directory before you can actually make the call.

  42. Speaker 2

    That's a perfect analogy. But you have that luid, you pack it into your structure, and you set an attribute to enabled, just a binary toggle on or off. Right, And the source code we're looking at actually has a really clever bit of logic to test this. It checks the current state of the privilege. Okay, if the privilege is currently on, the code turns it off. If it's off, it turns it on.

  43. Speaker 1

    If you have control over the state of the token.

  44. Speaker 2

    Exactly, if you can flip that bit back and forth, you have control.

  45. Speaker 1

    Okay, so we understand the badge and we know how to modify the badge. But here's the catch that really stood out to me and the reading. The expert warns that you can't just walk up to any process and steal its token.

  46. Speaker 2

    No, no, you can't.

  47. Speaker 1

    There's a prerequisite. You need a specific clearance level before you can even start stealing other people's clearances.

  48. Speaker 2

    It's the classic chicken and egg problem of privilege escalation. To steal a high level token, you need to be able to touch high level processes, but you can't touch them because you don't have a high level token yet.

  49. Speaker 1

    So how do we break that loop?

  50. Speaker 2

    We use a privilege called ceda bug privilege. We mentioned it just a second ago. Yeah, this is effectively god mode for programmers. It was originally designed so that developers could attach a debugger to any crashing program to fix it.

  51. Speaker 1

    Okay, But to debug a program you need total control over its memory exactly.

  52. Speaker 2

    You need to be able to pose it, read its memory, rewrite its memory.

  53. Speaker 1

    So if I have d bug privileges, the operating system basically assumes I'm a developer fixing a bug and just drops the security boundaries precisely.

  54. Speaker 2

    It lets you right into the building. So the very first thing our Python script does before it looks for target, before it tries to hack anything, is look in the mirror. It hacks itself, It elevates itself, It calls get current processed to find its own PID, then it opens its own process token okay, and then it uses that iluad lookup we just talked about to enable seedabug privilege on itself.

  55. Speaker 1

    So you're essentially pinning the god mode badge onto your own chest. Before you leave the house to go hunting, you have to.

  56. Speaker 2

    You try to open a system process like LSAs, dot ex or wind logan without seedabug privilege enadled, Windows will just instantly return access denied. You have to power up before you can attack.

  57. Speaker 1

    That makes total sense. Okay, so now we are powered up, we have the debug privilege enabled on our own script. Now we need a victim, yes we do. We need a token to steal, and the walkthrough describes this really specific four step process for the reconnaissance phase.

  58. Speaker 2

    Right, because we need to find a process that actually holds the token we want. In the example code, they target the task manager.

  59. Speaker 1

    Why task manager specifically just.

  60. Speaker 2

    A convenience target. It's almost always running and depending on how you launched it, it might have the privileges of the user you want to become. But honestly, for the script, the target doesn't actually matter. The mechanism is what counts.

  61. Speaker 1

    Here, right. The API calls yes.

  62. Speaker 2

    So step one is find window way.

  63. Speaker 1

    And this is just searching for the window title, right, like looking for a sign above a shop exactly.

  64. Speaker 2

    You give the API the string task manager and it scans the desktop if it finds it it gives you back a window handle.

  65. Speaker 1

    But a window handle isn't enough to steal the token.

  66. Speaker 2

    No, it's not. We need the process id the PID. So that brings us to step two, get window thread processing.

  67. Speaker 1

    Okay, so you feed the window handle into this function and it spits out the PID.

  68. Speaker 2

    Right, it gives you the unique number for that specific running instance of task manager.

  69. Speaker 1

    Now we have number.

  70. Speaker 2

    Now we have number. Step three is open process.

  71. Speaker 1

    That sounds a lot like opening a file.

  72. Speaker 2

    It's very similar conceptually. You're asking the OS for a handle to the process itself, and this is where that seat of bug privilege pays.

  73. Speaker 1

    Off because we enabled it earlier.

  74. Speaker 2

    Right, because we have it, The OS says, okay, you have permission to touch this process. Here's your handle.

  75. Speaker 1

    And finally, step four the ultimate goal, open process token.

  76. Speaker 2

    This is the grap you reach into that process handle and you extract the handle to its token. Wow. Now pause here for a second and think about where we are at this exact moment in the script. You are holding two things. You're running your own Python process, but you are holding a live connection to the identity of the task manager.

  77. Speaker 1

    It's literally like holding someone else's wallet. Ideally, yes, so my instinct, and I think the instinct of a lot of listeners right now would be okay, awesome, I have the wallet, let's go buy something, right, let's just use this token. But the guide explicitly says we shouldn't use it directly. We have to clone it first. Why can't we just use the stolen token?

  78. Speaker 2

    A few reasons. First, that token is already in use by the task manager. If you start messing with it, you might crash the other program, which is noisy, very noisy. But more importantly, we want to configure the identity for our specific needs. We want to create a brand new, perfect copy that we.

  79. Speaker 1

    Control, which leads us to the duplicate token x function. Yes, duplicate token. It sounds like something out of a sci fi movie, like the cloning machine.

  80. Speaker 2

    It essentially is the cloning machine. And this function takes a lot of parameters. Because Windows wants to know exactly what kind of clone you're trying to make, right, you have to specify the access rights. We usually use tokenal access because well, we want everything go big or go home exactly. But there are two settings here that are absolutely critical for you to understand if you're building this. First is the security attributes. The source code explicitly sets a flag called binherit handled to fall.

  81. Speaker 1

    Why false? What does that do?

  82. Speaker 2

    We don't want our current process, the Python script to accidentally inherit this new token. We are creating this token for a future process that we are about to launch.

  83. Speaker 1

    Oh, I see, you're.

  84. Speaker 2

    Going to want to keep those identities separate. It's just good security hygien okay.

  85. Speaker 1

    And what's the second critical setting the token type?

  86. Speaker 2

    This is the fundamental difference between an impersonation token and a primary token.

  87. Speaker 1

    Okay, break that down for us. What is the actual difference.

  88. Speaker 2

    An impersonation token is like a visitor badge. It less a service, check a file, or access a network share on behalf of someone else. But it's temporary, it's limited in scope. Okay. A primary token, on the other hand, is a permanent ID card. If you want to launch a new, independent application, like opening a brand new command prompt that stays open, you must have a primary token.

  89. Speaker 1

    So if we mess up this parameter and accidentally create an impersonation token, we can't launch our attack.

  90. Speaker 2

    Correct. The OS will just block the create process PI call if you feed it an impersonation token. It has to be primary, which is value one in the API.

  91. Speaker 1

    Got it, So we aren't just taking the wallet. We are printing a brand new valid ID card that works everywhere on the system.

  92. Speaker 2

    That's exactly what we're doing.

  93. Speaker 1

    Okay, so let's review. We've done the recon. We've elevated our own privileges to god mode. We found the target, we stole the token, and we created a primary clone. Now we're ready for the finale launch, right, and we use create process with TOKENW for this.

  94. Speaker 2

    Yes, this is the API call that puts all the pieces together.

  95. Speaker 1

    Walk us through what happens here. Because the source material causes a magic trick.

  96. Speaker 2

    It really is a magic trick. So picture this scenario. You are running your Python scribs. Okay, let's say you logged in a system the absolute highest authority on the box. Okay, you target a process running as a normal everyday user. Let's call him Brandon, Poor Brandon. Right, so you've duplicated Brandon's token. Now you call create process with TOKENW. You pass in that clone token. You tells you want to run cmd dot ex. The command prompt, A new black window pops up on your screen.

  97. Speaker 1

    And it just looks like a normal command prompt window.

  98. Speaker 2

    Visually it is completely identical, but inside that black box everything is changed. You type the command me and head enter, and the system replies.

  99. Speaker 1

    Brandon even those systems launched it.

  100. Speaker 2

    Even though system launched it, the operating system looks at the token attached to that process, sees Brandon, and applies all of Brandon's rules. If Brandon is denied access to a certain file, this new window is denied. If Brandon can access a secret file server, this window can access it. You have completely structurally swapped identities.

  101. Speaker 1

    That is wild. It really drives home the point that to a computer, you are nothing more than that token.

  102. Speaker 2

    That's the huge takeaway here. Identity is portable. If I can read your memory, I could become you man.

  103. Speaker 1

    Okay, I want to take a step back for a minute and talk about the actual code architecture, because the walkthrough spends a good amount of time on refactoring. It basically says, sure, you can write this as one giant Python script, but you really shouldn't.

  104. Speaker 2

    No, you really shouldn't. When you write this kind of code where you're constantly interfacing with C structures from Python, it gets incredibly verbose. Yeah, you have lines and lines of structure definitions, lued classes start to pinfow fields. If you put all of that in your main logic flow, it becomes completely unreadable.

  105. Speaker 1

    So the recommendation is to use classes to organize it.

  106. Speaker 2

    Yes, object oriented programming. Now create a privileged class, create a method inside it called enable. So it's cleaner, much cleaner. That way, in your main script, you just write something like privileged dot enable instead of writing out the twenty lines of C types code required to do it every single time. It separates the what you want to do from how.

  107. Speaker 1

    You do It makes sense, And there's also a big point made about portability here. Why are we doing this in Python and not say C plus plus or C shark.

  108. Speaker 2

    This is a massive advantage for security professionals. If I write this tool and C plus plus I I have to compile it into an executable file a dot ex. If I try to drop an unknown, custom compiled dot ex file onto a client server during a security audit, their antivirus is going to screen.

  109. Speaker 1

    It looks like malware.

  110. Speaker 2

    It looks highly suspicious. Yeah, but Python is different because it's just text exactly, It's just a text script. If the target machine already has Python installed, which a lot of servers do for basic automation tasks. I can just drop this text file and run it.

  111. Speaker 1

    So you aren't bringing in any external hacking tools.

  112. Speaker 2

    None. I'm literally just using the kernel threety two library that is already built into the computer.

  113. Speaker 1

    You're using the system against itself.

  114. Speaker 2

    It's a concept called living off the land. You aren't importing a weapon into the environment. You are building one for the parts you just found lying around in the garage.

  115. Speaker 1

    That is such a powerful concept. It really changes the way you think about what malware actually is. It doesn't have to be some complex virus. It can just be a Python script asking for privileges in the wrong order exactly.

  116. Speaker 2

    It completely demiss devise the magic. At the end of the day, it's just API calls.

  117. Speaker 1

    Now. We promise a challenge for the listeners who want to actually try this out and get their hands dirty. The script we discussed today targets a specific window name like task manager, But the expert in our notes suggests a way to make this tool a lot more powerful.

  118. Speaker 2

    Yes, because relying on find window a is brittle. If I simply rename the window, your entire hacking tool breaks not great, So the challenge is to rewrite the reconnaissance phase. Instead of looking for a window name, use a different API call called create tool held threety two snapshot.

  119. Speaker 1

    That sounds intense. What does a snapshot do?

  120. Speaker 2

    It does exactly what it sounds like. It takes a complete snapshot of every single process running on the system at that exact millisecond.

  121. Speaker 1

    So you get a list of everything.

  122. Speaker 2

    Everything, Chrome Outlook, background, system services, everything. So your challenge is to use that snapshot to build an interactive menu.

  123. Speaker 1

    Oh that's cool.

  124. Speaker 2

    Let the users scroll through the list of all running processes in the terminal and pick a PID to impersonate on the fly.

  125. Speaker 1

    So you're basically building a menu driven identity theft tool.

  126. Speaker 2

    Exactly. If you can build that, you aren't just copying code anymore. You genuinely understand how handles and process numeration work in Windows. And hey, put that on your GitHub portfolio. That is exactly the kind of project that gets you hired.

  127. Speaker 1

    I love that practical application of the theory. Now, before we sign off today, we have a quick review question to see if you were really paying attention.

  128. Speaker 2

    We talked about a chicken and egg problem earlier. There is a specific privilege that god mode for debugging that your script absolutely must enable on itself before it can successfully steal a token from another process.

  129. Speaker 1

    Without it, you just get access denied.

  130. Speaker 2

    Do you remember the exact name of that privilege?

  131. Speaker 1

    Think back to the developer fixing a bug.

  132. Speaker 2

    If you know the answer, you're ready to open up your code editor and start building. Just remember that privilege is incredibly powerful. Use it responsibly.

  133. Speaker 1

    Couldn't have set it better myself. This has been a really fascinating look into the gears of the Windows OS. Keep exploring, keep coding, and we will see you on the next deep dive.

  134. Speaker 2

    Happy coating everyone,