Latest / CyberCode Academy / Course 25 - API Python Hacking | Episode 4: Structures, Process Spawning, and Undocumented Calls
Transcript
- 0:00Speaker 1: You know, when I think of Python, I usually think
- 0:02Speaker 1: of it as the friendly language.
- 0:03Speaker 2: Oh. Absolutely, It's built to be friendly.
- 0:05Speaker 1: Right, It's like the batteries included language. You want to
- 0:09Speaker 1: open a file, it's one line. You want to request
- 0:11Speaker 1: a webpage, it's two lines. It just it feels safe.
- 0:14Speaker 2: It does feel safe, And that is the entire design
- 0:16Speaker 2: philosophy of Python right there.
- 0:18Speaker 1: Yeah, it holds your hand exactly.
- 0:20Speaker 2: It manages memory for you, it handles the data types
- 0:23Speaker 2: for you, and generally speaking, it tries to keep you
- 0:27Speaker 2: as far away from the messy gears of the machine.
- 0:29Speaker 1: As possible, which is great for everyday scripting it is,
- 0:33Speaker 1: but looking at the topic for today's deep dive, we
- 0:36Speaker 1: are effectively taking that safety manual and just throwing it
- 0:40Speaker 1: out the window.
- 0:40Speaker 2: We really are.
- 0:41Speaker 1: We aren't just writing scripts today. We are performing well.
- 0:45Speaker 1: It feels like brain surgery on the Windows operating system,
- 0:49Speaker 1: and we are using Python as the scalpel.
- 0:52Speaker 2: That is a fantastic way to put it. We are
- 0:54Speaker 2: moving from being a driver of the car to being
- 0:56Speaker 2: the mechanic under the hood while the engine is still running.
- 1:00Speaker 1: That sounds slightly terrifying.
- 1:02Speaker 2: It can be if you aren't careful but we are
- 1:04Speaker 2: going to use a standard library called dick types to
- 1:07Speaker 2: bypass all those safety rails and talk directly to the
- 1:10Speaker 2: Windows kernel.
- 1:11Speaker 1: And just to set the stage for everyone listening, this
- 1:14Speaker 1: isn't just a simple Hello World exercise, not at all.
- 1:17Speaker 1: We are going to build internal Windows memory structures from scratch.
- 1:21Speaker 1: We are going to spawn processes by manually allocating consoles.
- 1:25Speaker 1: And this is the part that honestly blew my mind
- 1:27Speaker 1: a little, the DNS part. Yes, we're going to use
- 1:30Speaker 1: undocumented API calls to dump the system's DNS cash directly
- 1:36Speaker 1: from memory.
- 1:36Speaker 2: It sounds intense, and it is intense. But the goal
- 1:39Speaker 2: here isn't just to show off some neat party tricks, right, What.
- 1:43Speaker 1: Is the actual mission here for the listener?
- 1:45Speaker 2: It's to bridge the gap. If you work in cybersecurity,
- 1:49Speaker 2: or red teaming, or even high level system administration, you
- 1:53Speaker 2: simply cannot rely on pre built tools forever.
- 1:55Speaker 1: Because eventually you hit a wall.
- 1:57Speaker 2: Exactly. Yeah, you need to understand how Windows actually think.
- 2:00Speaker 2: And the reality is Windows doesn't think in Python, it
- 2:03Speaker 2: thinks in C. It thinks in C. So our mission
- 2:06Speaker 2: today is to take you from a Python script or
- 2:08Speaker 2: to a system architect by the end of this deep dive,
- 2:12Speaker 2: you should be able to visualize how data sits in
- 2:15Speaker 2: memory without even looking at a monitor.
- 2:17Speaker 1: I love that. Let's get into it.
- 2:19Speaker 2: Let's do it.
- 2:19Speaker 1: Okay, so step one. If we want to talk to
- 2:22Speaker 1: the Windows API, we can't just shout Python commands at it.
- 2:26Speaker 1: We need a common language, right, And a big part
- 2:29Speaker 1: of this is the concept of internal structures.
- 2:32Speaker 2: Yeah. This is usually the biggest hurdle for Python developers
- 2:34Speaker 2: to get their heads around, because Python is dynamic, right.
- 2:38Speaker 1: Like if I create a variable and just type x equals.
- 2:40Speaker 2: Five exactly, Python finds a spot in memory. It puts
- 2:43Speaker 2: the five there, maybe add some metadata and handles all
- 2:47Speaker 2: the addressing. You don't care where it is and you
- 2:48Speaker 2: really don't care how.
- 2:49Speaker 1: Big it is, But the Windows kernel cares.
- 2:51Speaker 2: It cares immensely. The Windows API, being written in C
- 2:55Speaker 2: expects data to be laid out in a very rigid,
- 2:57Speaker 2: specific way, and these layouts are called Is there.
- 3:01Speaker 1: A good analogy for this, because structure sounds a bit abstract.
- 3:05Speaker 2: Think of a structure not like a variable, but like
- 3:07Speaker 2: a government form.
- 3:08Speaker 1: Oh man, I hate government forms.
- 3:10Speaker 2: Everyone does. But imagine a form where box A is
- 3:13Speaker 2: exactly four centimeters wide, Box B is eight centimeters wide,
- 3:18Speaker 2: and box C is a little checkbox.
- 3:20Speaker 1: Okay, I can picture that now.
- 3:21Speaker 2: If you scribble outside lines on that form, or if
- 3:25Speaker 2: you try to squeeze an entire paragraph into box A,
- 3:29Speaker 2: the clerk doesn't just ask you to fix it, They
- 3:31Speaker 2: reject it. They reject the application entirely. The operating system
- 3:34Speaker 2: behaves the exact same way. If your data isn't perfectly formatted,
- 3:38Speaker 2: your program crashes with an access violation.
- 3:40Speaker 1: So STIPES is basically our way of forcing Python to
- 3:44Speaker 1: fill out this bureaucratic form correctly precisely.
- 3:47Speaker 2: In Python, we represent a Windows structure by creating a class.
- 3:51Speaker 2: But it's a weird class. How so it has to
- 3:53Speaker 2: inherit from this typees DOT structure. That specific inheritance tells
- 3:58Speaker 2: the Python interpreter, do not optimize this class, do not
- 4:01Speaker 2: rearrange things behind the scenes.
- 4:03Speaker 1: It's like saying, lay this data out in the RAM
- 4:05Speaker 1: exactly how I tell you to.
- 4:07Speaker 2: Yes, keep your hands off my memory layout.
- 4:09Speaker 1: And the instructions for that layout are in that field's list, right,
- 4:12Speaker 1: that's the key.
- 4:13Speaker 2: Inside your class, you define a list called underscore fields.
- 4:16Speaker 2: Underscore it's a list of.
- 4:18Speaker 1: Tuples, and each tuple is a rule for a box.
- 4:21Speaker 2: On the form exactly, each tuple says, here's the name
- 4:24Speaker 2: of the field, and here's the exact C compatible data type.
- 4:27Speaker 2: You cannot use a standard Python integer here.
- 4:29Speaker 1: You have to use the C equivalents.
- 4:31Speaker 2: Right, you have to use a C long or a deward.
- 4:34Speaker 1: Let's ground this with a specific example. I want to
- 4:36Speaker 1: look at a classic structure called process information.
- 4:39Speaker 2: That is a perfect starting point.
- 4:41Speaker 1: If I'm looking at the Microsoft documentation. This structure is
- 4:44Speaker 1: required whenever you want to start a new program. What
- 4:46Speaker 1: does this form actually look like in memory?
- 4:49Speaker 2: It's fairly simple. Process information has exactly four fields. Imagine
- 4:54Speaker 2: four slots in memory sitting right next to each other. Okay,
- 4:57Speaker 2: Slot one is H process, Slot two is H thread,
- 5:01Speaker 2: Slot three is DW process, and slot four is DW threaded.
- 5:06Speaker 1: Okay, hold on, I recognize process ID. That's just the
- 5:08Speaker 1: four or five digit number I see in task Manager
- 5:11Speaker 1: when I open it up, right, that's the one.
- 5:12Speaker 2: It's the global identifier for the running program.
- 5:15Speaker 1: But what is an H process? What does the H
- 5:17Speaker 1: stand for?
- 5:18Speaker 2: The H stands for handle handle.
- 5:20Speaker 1: Okay, what is a handle?
- 5:21Speaker 2: Think of the handle like a claim check at a
- 5:23Speaker 2: coat check a code check. Yeah, you give the attendant
- 5:26Speaker 2: your coat, which in this case is the process you
- 5:29Speaker 2: just created, and they hand you a little paper ticket
- 5:32Speaker 2: the handle. Okay, that ticket isn't the coat itself. You
- 5:35Speaker 2: can't wear the ticket. It's just an opaque token that
- 5:38Speaker 2: lets you retrieve or modify the coat later. The h
- 5:41Speaker 2: process is your token to talk to that specific process.
- 5:45Speaker 1: So if I want to kill the process later on,
- 5:48Speaker 1: I don't give Windows the name notepad dot XE, I
- 5:52Speaker 1: give it the handle.
- 5:53Speaker 2: Usually Yes, it's much faster and much more specific, because
- 5:57Speaker 2: there could be fifty note pads running. The handle targets
- 5:59Speaker 2: the exact one you spawned.
- 6:01Speaker 1: And notice there is a handle for the process and
- 6:03Speaker 1: a separate handle for the thread.
- 6:05Speaker 2: Why both Because a process is just a container. It's
- 6:08Speaker 2: the house. The thread is the thing actually running inside
- 6:11Speaker 2: the house executing code.
- 6:13Speaker 1: Ah.
- 6:14Speaker 2: Sometimes you need to talk to the house itself, like
- 6:16Speaker 2: to tear it down. Sometimes you need to talk to
- 6:18Speaker 2: the resident inside. So Windows gives you a claim ticket
- 6:20Speaker 2: for both.
- 6:21Speaker 1: That makes total sense. So to build this in Python,
- 6:23Speaker 1: I'd write class process information and inherit from a types
- 6:26Speaker 1: dot structure. Then in my fieldsless i'd map those.
- 6:29Speaker 2: Four slots you're getting it.
- 6:30Speaker 1: H process becomes a handle type DW processed becomes a
- 6:34Speaker 1: d word.
- 6:34Speaker 2: Type spot on. And here is the real AHA moment
- 6:37Speaker 2: for why we do this. When you are about to
- 6:40Speaker 2: run a command, you create an instance of this class
- 6:45Speaker 2: in Python, right, but it's empty. It's a completely blank form,
- 6:49Speaker 2: just zeros to zeros. You pass that blank form to
- 6:51Speaker 2: the Windows API. The API goes off and does the
- 6:54Speaker 2: hard work of actually starting the program. Then, and this
- 6:57Speaker 2: is the magic part. The API fills in the blank
- 7:00Speaker 2: on your form.
- 7:00Speaker 1: Oh wow.
- 7:01Speaker 2: It writes the new process ID and the new handle
- 7:04Speaker 2: directly into that memory structure you created in Python.
- 7:07Speaker 1: That's a really important distinction. In normal Python, I feel
- 7:09Speaker 1: like I send data to a function and I get
- 7:11Speaker 1: a result back as a return value.
- 7:13Speaker 2: Right, like a standard assignment.
- 7:14Speaker 1: Yeah, but here I'm effectively sending an empty bucket to
- 7:18Speaker 1: the API and saying, please build this bucket up for me.
- 7:21Speaker 2: That is exactly how low level systems programming works. You
- 7:25Speaker 2: manage the memory allocation and the operating system populates it, and.
- 7:29Speaker 1: The beauty is reusability.
- 7:32Speaker 2: Absolutely. Yeah. Once you write that class, that boilerplate code
- 7:36Speaker 2: defining process information. It's done. It's a write once us
- 7:40Speaker 2: forever tool.
- 7:41Speaker 1: You just import it into whatever you're building next.
- 7:43Speaker 2: Exactly, any security tool or automation script you build in
- 7:46Speaker 2: the future, you just drop that class in.
- 7:49Speaker 1: So we've got our blueprints. We understand that we need
- 7:51Speaker 1: to be strict with memory. Now I want to see
- 7:53Speaker 1: something actually happen.
- 7:54Speaker 2: That's some damage.
- 7:55Speaker 1: I want to launch a program, let's say the command
- 7:57Speaker 1: prompt cmd dot XE. But I want to do it
- 8:00Speaker 1: the hard way.
- 8:01Speaker 2: The hard way is the best way to learn what
- 8:03Speaker 2: this system is really doing.
- 8:04Speaker 1: So we're using kernel three to two dot DLL and
- 8:06Speaker 1: a function called create process W.
- 8:08Speaker 2: Yes.
- 8:09Speaker 1: Now I've used subprocess dot run in Python a million times.
- 8:12Speaker 1: It's super easy. But create process w looks absolutely terrifying.
- 8:17Speaker 1: The documentation lists something like ten different parameters.
- 8:20Speaker 2: It is intimidating at first glance. Yeah, but you have
- 8:22Speaker 2: to remember subprocess is really just a wrapper around create
- 8:25Speaker 2: process W. It's just hiding the math, it's hiding all
- 8:28Speaker 2: the complexity. When you use the raw API, you have
- 8:31Speaker 2: total granular control. Let's break down those ten parameters. Honestly,
- 8:35Speaker 2: most of them are quite boring.
- 8:36Speaker 1: Okay, walk me through the highlights.
- 8:37Speaker 2: Then, well, First, you have application name that's just the
- 8:40Speaker 2: stringpath to your binary like see coll and backslash Windows
- 8:44Speaker 2: backslash System thirty two, backslash cmd dot XE.
- 8:49Speaker 1: Simple enough.
- 8:50Speaker 2: Then you have command line, which we can often just
- 8:51Speaker 2: leave as none or null if we aren't passing specific
- 8:56Speaker 2: arguments to the program.
- 8:57Speaker 1: And then there's a whole bunch of security.
- 8:58Speaker 2: Stuff, rightarameters like LP process attributes, LP thread attributes inhert handles.
- 9:04Speaker 1: Can we just ignore those for.
- 9:05Speaker 2: A basic script. Yes, you can just set these to
- 9:07Speaker 2: battle none. You're essentially telling Windows just use the default
- 9:11Speaker 2: security settings. I'm not doing anything fancy of permissions.
- 9:13Speaker 1: But then we get to the flags parameter, and this
- 9:16Speaker 1: is where things get interesting. There's a magic number here, right,
- 9:19Speaker 1: zero x one zero.
- 9:20Speaker 2: Ah, yes, zero x one zero, and the official API
- 9:24Speaker 2: documentation that x code corresponds to a flag called create
- 9:28Speaker 2: new console.
- 9:29Speaker 1: Why do we need that? If I run a program,
- 9:31Speaker 1: doesn't it automatically get a window?
- 9:33Speaker 2: Not necessarily?
- 9:34Speaker 1: Really?
- 9:34Speaker 2: Yeah? Yeah? If you run this script from an existing
- 9:37Speaker 2: command line, the new cmd dot xc might just hijack
- 9:42Speaker 2: your current window.
- 9:42Speaker 1: Oh I hate when that happens.
- 9:44Speaker 2: Or if you're running your Python script as a scheduled
- 9:46Speaker 2: background task. The new process might be completely invisible.
- 9:50Speaker 1: So the flag forces it to be visible.
- 9:53Speaker 2: By passing zero x one zero, you are explicitly ordering
- 9:57Speaker 2: the kernel allocade a brand new independence console window for
- 10:01Speaker 2: this process. That gives you that fresh pop up.
- 10:03Speaker 1: I have to admit using raw hex plants like zero
- 10:06Speaker 1: x one zero makes you feel like a bit of
- 10:07Speaker 1: a wizard. I cast spell zero x one zero.
- 10:11Speaker 2: It effectively is a spell. You are changing the fundamental
- 10:14Speaker 2: behavior of the process creation engine. But the trickiest part
- 10:17Speaker 2: of create process W isn't the flags, it's the startupin
- 10:21Speaker 2: info structure.
- 10:22Speaker 1: This is the second structure we have to build. Right,
- 10:24Speaker 1: we have process information for the output and start apinfo
- 10:27Speaker 1: for the input.
- 10:28Speaker 2: Yes, star two pinfo provides the detailed instructions. It controls
- 10:32Speaker 2: the windows starting position, its size, its title, and crucially
- 10:36Speaker 2: whether it is visible or hidden.
- 10:37Speaker 1: And there is a specific gatcha here that I know
- 10:40Speaker 1: trips up a lot of people.
- 10:41Speaker 2: The visibility flag.
- 10:43Speaker 1: Let's say I want the window to be explicitly visible.
- 10:46Speaker 1: I look at the structure and there's a field called
- 10:49Speaker 1: W show window. Right, I set wshow window to one,
- 10:53Speaker 1: which translates to show normal logic dictates that the window
- 10:57Speaker 1: should show up.
- 10:58Speaker 2: Logic would dictate that yes, yeah, but the Windows API
- 11:00Speaker 2: is highly bureaucratics. Uh oh, you can set W your
- 11:03Speaker 2: show window to whatever you want. You can set it
- 11:05Speaker 2: to one, ten, one hundred, but Windows will completely ignore
- 11:09Speaker 2: that field unless you also set a completely different field
- 11:12Speaker 2: called DW flags.
- 11:14Speaker 1: Wait, so I have to set the setting, and then
- 11:15Speaker 1: I have to set a flag that tells Windows please
- 11:18Speaker 1: look at the setting I just said exactly.
- 11:19Speaker 2: You have to set the DW flags field to start
- 11:22Speaker 2: use show window, which corresponds to the value zero x one.
- 11:26Speaker 1: That seems crazy.
- 11:28Speaker 2: This tells the API that data inside the W show
- 11:30Speaker 2: window field is actually valid, please process it. If you
- 11:34Speaker 2: don't raise that flag, the API assumes the field just
- 11:38Speaker 2: contains random garbage data in memory and skips it entirely.
- 11:41Speaker 1: That feels incredibly redundant. Why design an operating system that way?
- 11:45Speaker 2: It comes down to efficiency and backward compatibility from the
- 11:47Speaker 2: early days of Windows that start pin UFA structure is massive.
- 11:53Speaker 2: It has fields for window coordinates, buffer sizes, font fill attributes.
- 11:56Speaker 2: So it's huge, right if you only care about one
- 11:59Speaker 2: or two setting. Windows doesn't want to waste seep ucycles
- 12:02Speaker 2: parsing the other fifty fields that you left blank. It
- 12:05Speaker 2: only looks at the fields you explicitly flag as important.
- 12:08Speaker 1: Okay, I guess that makes sense from an optimization standpoint.
- 12:11Speaker 1: It is just really annoying for the programmer.
- 12:13Speaker 2: It is annoying. But once you know the golden rules,
- 12:15Speaker 2: set the value, then set the flag to validate the value,
- 12:19Speaker 2: you never forget it.
- 12:20Speaker 1: So we have our application name, our creation flags with
- 12:23Speaker 1: zero x one zero, and our newly built startup infrastructure.
- 12:27Speaker 1: Now we actually call the function.
- 12:29Speaker 2: Right, we load in other types dot window, dot kernel
- 12:31Speaker 2: three D two and call create process W.
- 12:33Speaker 1: But there's one last technical hurdle here. Yeah, the concept
- 12:36Speaker 1: of by ref pass by reference.
- 12:39Speaker 2: This goes all the way back to our government form analogy.
- 12:41Speaker 2: Remember process information, the empty bucket we want windows.
- 12:44Speaker 1: To fill, yep, the blank form.
- 12:46Speaker 2: We cannot pass the Python object itself directly into the
- 12:49Speaker 2: C function. Python objects are complex beasts under the hood.
- 12:53Speaker 2: C doesn't understand them.
- 12:55Speaker 1: So what do we send?
- 12:56Speaker 2: Instead, we send the address of the object, use the
- 13:00Speaker 2: function C types dot bi ref and pass our structure
- 13:03Speaker 2: into it.
- 13:03Speaker 1: What does that do?
- 13:05Speaker 2: This creates a pointer, essentially a raw memory address number.
- 13:09Speaker 2: We're sending that number to the API. We are telling
- 13:11Speaker 2: the colonel go to memory address zero x seven f
- 13:15Speaker 2: F whatever. You will find a blank structure sitting there.
- 13:18Speaker 2: Fill it out.
- 13:18Speaker 1: I see we aren't handing the clerk the paper. We
- 13:21Speaker 1: are giving them the GPS coordinates of where the paper
- 13:23Speaker 1: is sitting on the desk.
- 13:24Speaker 2: That is a perfect analogy. And if it all works, boom,
- 13:27Speaker 2: a new command prompt pops up on your screen, which
- 13:30Speaker 2: is awesome. You have successfully bypassed Python's standard library and
- 13:34Speaker 2: ask the kernel directly to do your bidding.
- 13:36Speaker 1: It is deeply satisfying, but honestly, it's still just opening
- 13:39Speaker 1: a window.
- 13:40Speaker 2: You want more.
- 13:40Speaker 1: I want to do something that feels a bit more
- 13:42Speaker 1: well forbidden.
- 13:43Speaker 2: I had a feeling you would say that which brings.
- 13:45Speaker 1: Us two undocumented API calls. Now undocumented sounds ominous, It
- 13:51Speaker 1: sounds illegal.
- 13:51Speaker 2: It definitely sounds shadier than it actually is. Undocumented just
- 13:56Speaker 2: means Microsoft hasn't officially promised that this function will exist forever.
- 14:01Speaker 1: So it's not in the official manual right.
- 14:03Speaker 2: They haven't written a nice public documentation page for it
- 14:06Speaker 2: on their developer network. These are the functions Windows uses
- 14:09Speaker 2: internally for its own.
- 14:10Speaker 1: Housekeeping but for cybersecurity professionals, these functions are gold mines, right.
- 14:15Speaker 2: Absolute gold mines. Because security tools like your anti virus
- 14:19Speaker 2: or your enterprise eder solutions, they often watch the front door.
- 14:23Speaker 1: They watch the documented stuff exactly.
- 14:25Speaker 2: They monitor known APIs like creat process. They might not
- 14:29Speaker 2: be watching the back door. Which are these internal, undocumented functions.
- 14:33Speaker 1: So what's our target for this forbidden technique?
- 14:35Speaker 2: We are going after the dns cash.
- 14:37Speaker 1: Explain what that is for the listener who might not
- 14:39Speaker 1: be deep into networking.
- 14:41Speaker 2: Every time you visit a website, whether it's Google or
- 14:43Speaker 2: a news site or even a malware command and control server,
- 14:46Speaker 2: Windows has to look up the IP.
- 14:48Speaker 1: Address for that name right through DNS.
- 14:50Speaker 2: Right and to save time on future requests, it saves
- 14:53Speaker 2: that result in memory. That is the dns cash. We
- 14:56Speaker 2: want to dump that memory. We want to see every
- 14:59Speaker 2: single site the computer has contacted recently.
- 15:01Speaker 1: And we can do this without just opening a terminal
- 15:04Speaker 1: and running a command like ipconfig. Slash displays yes.
- 15:08Speaker 2: Because you run it config, you spawn a new process.
- 15:10Speaker 2: A security tool might see that and flag it. It
- 15:13Speaker 2: might say, how is this random script checking the dns cash.
- 15:17Speaker 2: It's noisy, exactly, it's noisy. But if we just read
- 15:19Speaker 2: the memory directly using an internal API, it's completely silent.
- 15:24Speaker 1: Okay, so we need the right function. Sure, we are
- 15:26Speaker 1: looking at dns get cash data table inside you and
- 15:29Speaker 1: zapia dot dll.
- 15:31Speaker 2: Correct, And just like before, we need a structure to
- 15:33Speaker 2: hold the data we get back. This one is called
- 15:35Speaker 2: dns cash entry.
- 15:36Speaker 1: Okay.
- 15:37Speaker 2: This isn't a simple sequential form like the last one.
- 15:39Speaker 2: This structure is a link list.
- 15:41Speaker 1: Oh boy, computer science one zero one is coming back
- 15:44Speaker 1: to haunt us.
- 15:45Speaker 2: It's really not that bad, but it is fundamentally different.
- 15:47Speaker 2: Process information was just four numbers in a row and
- 15:50Speaker 2: a single block of memory. A link list is different
- 15:52Speaker 2: because the data is scattered. You have one entry sitting
- 15:55Speaker 2: over here, and inside that entry there is a map
- 15:58Speaker 2: to the next entry, which might be thousands of bytes
- 16:01Speaker 2: away somewhere else in memory.
- 16:02Speaker 1: Okay, so imagine a treasure hunt. You find the first note.
- 16:06Speaker 1: The note says Google dot com. But at the bottom
- 16:08Speaker 1: of the note it has the coordinates to the next
- 16:11Speaker 1: note exactly.
- 16:12Speaker 2: That coordinate is a pointer, and the structure was usually
- 16:14Speaker 2: defined as a field called p next.
- 16:16Speaker 1: But here's the problem. When Python reads that p next field,
- 16:20Speaker 1: it just sees a big number, right, it sees a
- 16:22Speaker 1: long integer like one, four, zero, seven, three, two whatever.
- 16:27Speaker 1: Python doesn't know that number is a coordinate. It just
- 16:29Speaker 1: thinks it's a piece of data.
- 16:31Speaker 2: Right. Python just sees an integer, But we know it's
- 16:34Speaker 2: an address. We have to essentially tell Python to put
- 16:36Speaker 2: on its structure glasses and look at that specific address again.
- 16:39Speaker 1: This is where casting comes in.
- 16:41Speaker 2: Yes, it tykes dot cast. This is arguably the most
- 16:45Speaker 2: powerful tool in the entire library.
- 16:47Speaker 1: How does it work.
- 16:48Speaker 2: We take that raw number at the address we found
- 16:50Speaker 2: in p next, and we tell bike tapes in it
- 16:52Speaker 2: point cast this raw address into a pointer to a
- 16:55Speaker 2: DNS cash entry structure.
- 16:57Speaker 1: It's like forcing the interpreter to reinterpret reality. You're telling
- 17:00Speaker 1: it this isn't just a number, it's a doorway to
- 17:03Speaker 1: another structure.
- 17:04Speaker 2: And once you cast it, suddenly you can access the
- 17:07Speaker 2: fields of that new structure. You can read the next
- 17:10Speaker 2: website name.
- 17:11Speaker 1: And then you grab the PNIX from that new.
- 17:13Speaker 2: One, past it again and you move down the chain.
- 17:15Speaker 1: So the logic loop is call the API, get the
- 17:19Speaker 1: first entry, read the website name, grab the next address,
- 17:23Speaker 1: cast it. Read the name, grab the next address, cast it.
- 17:26Speaker 1: You've got it until when, though, how does it end?
- 17:28Speaker 2: You loop until the pnx pointer equals nul or zero.
- 17:33Speaker 2: That signifies you've reached the very end of the linked list.
- 17:36Speaker 1: Now there is one little quirk to watch out for.
- 17:38Speaker 1: You mentioned skipping the first entry.
- 17:40Speaker 2: Yeah, that's a very specific behavior of these get catch
- 17:43Speaker 2: data table. The very first pointer it returns is often
- 17:47Speaker 2: just a header or internal metadata. It doesn't actually contain
- 17:50Speaker 2: a valid website name, so.
- 17:52Speaker 1: If you try to read the name field on that
- 17:54Speaker 1: first one, you.
- 17:54Speaker 2: Might just get garbage data or crash. So the best
- 17:57Speaker 2: practice here is call the API, take the px pointer,
- 18:00Speaker 2: immediately cast that, and then start your loop from the
- 18:03Speaker 2: second entry.
- 18:04Speaker 1: Got it. So the flow is load the DLL, define
- 18:08Speaker 1: the linkless structure, call the function skip the first century,
- 18:13Speaker 1: and then loop read, cast, repeat.
- 18:15Speaker 2: And suddenly you have a real time dump of the
- 18:17Speaker 2: system's browsing history sitting right there in your Python variables.
- 18:21Speaker 2: No external processes, spawns, no noise generated, just pure memory reading.
- 18:26Speaker 1: That is wild. It really illustrates the massive difference between
- 18:29Speaker 1: scripting and systems programming.
- 18:31Speaker 2: It does.
- 18:31Speaker 1: Scripting is asking another program to do a task for you.
- 18:34Speaker 1: Systems programming is reaching directly into the memory and just
- 18:37Speaker 1: doing it yourself.
- 18:38Speaker 2: And then it's exactly why we use c types. It
- 18:41Speaker 2: gives you the keys to the kingdom. You aren't limited
- 18:43Speaker 2: by what the Python Standard Library developers thought you might
- 18:45Speaker 2: need to do. You can access almost any part of
- 18:48Speaker 2: Windows memory.
- 18:48Speaker 1: We've covered a massive amount of ground here today. We
- 18:51Speaker 1: started with the concept of strict sea structures and filling
- 18:54Speaker 1: out government forms.
- 18:55Speaker 2: Yep.
- 18:55Speaker 1: We thought through the sheer bureaucracy of Create process W
- 18:59Speaker 1: and its flags, and we ended up traversing a linked
- 19:01Speaker 1: list in memory to stealthily steal DNS records.
- 19:05Speaker 2: If I had to boil this whole deep dive down,
- 19:08Speaker 2: i'd't say it centers on three core principles.
- 19:10Speaker 1: Let's hear them.
- 19:11Speaker 2: One, data isn't just a value, It has a physical shape.
- 19:15Speaker 2: That's the structure. Two you have to be wildly explicit.
- 19:19Speaker 2: If you want a window to show, you need a
- 19:20Speaker 2: flag to prove you actually mean it. And three three
- 19:24Speaker 2: memory is really just a chain of addresses. If you
- 19:27Speaker 2: know where the next link is, you can cast your
- 19:29Speaker 2: way through the entire system.
- 19:31Speaker 1: It completely changes how you look at the machine. It's
- 19:33Speaker 1: not a black box anymore. It's just a bunch of forms.
- 19:35Speaker 1: And addresses waiting to be filled out.
- 19:37Speaker 2: It empowers you, but I do want to add a
- 19:39Speaker 2: serious note of caution before we finish. Sure, we've been
- 19:42Speaker 2: talking about power a lot, but when you are writing
- 19:44Speaker 2: directly to memory addresses and using undocumented calls, you are
- 19:49Speaker 2: completely bypassing the safety rails that usually protect the system.
- 19:53Speaker 1: What's the worst that can actually happen?
- 19:55Speaker 2: Well, if you calculate a pointer or wrong, or you
- 19:57Speaker 2: cast something to the wrong structure size. Let's say you
- 20:00Speaker 2: tell Windows as structures one hundred bytes, but it's actually fifty.
- 20:04Speaker 2: You are going to overwrite memory that belongs to something else.
- 20:07Speaker 2: You won't just get a polite Python aer message. You
- 20:10Speaker 2: can instantly crash the application or even bluescreen the entire machine.
- 20:15Speaker 1: So with great power comes the ability to accidentally nuke
- 20:18Speaker 1: your own server.
- 20:19Speaker 2: Exactly. Yes, systems programming is incredibly unforgiving. Always test this
- 20:23Speaker 2: stuff in an isolated virtual machine first. Never run your
- 20:27Speaker 2: first draft on a production server.
- 20:29Speaker 1: Very good advice. Before we wrap up, I want to
- 20:32Speaker 1: throw a little review challenge at you, the listener, a
- 20:34Speaker 1: little pop quiz based on what we just discussed, to
- 20:37Speaker 1: see if it's stuck.
- 20:38Speaker 2: Let's hear the challenge.
- 20:39Speaker 1: We talked heavily about the star to Pinafo structure. Let's
- 20:42Speaker 1: say you are writing your code and you set bwushow
- 20:45Speaker 1: window equal to one because you want your new command
- 20:47Speaker 1: prompt to pop up on the screen naturally. But when
- 20:50Speaker 1: you run the script, nothing happens. The window remains completely hidden.
- 20:54Speaker 1: What one specific thing did you forget to do?
- 20:56Speaker 2: Think back to the bureaucracy.
- 20:58Speaker 1: The answer is you forgot the flag. You must set
- 21:01Speaker 1: DW flags to start you show show window. Without setting
- 21:04Speaker 1: that flag, Windows just ignores your visibility request entirely.
- 21:08Speaker 2: It is a classic mistake, and I promise you every
- 21:12Speaker 2: single systems programmer has spent at least one late night
- 21:16Speaker 2: staring at their monitor debugging that exact issue.
- 21:20Speaker 1: Thank you so much for guiding us through this. It's
- 21:22Speaker 1: been an absolute blast unpacking these structures.
- 21:24Speaker 2: It was a pleasure. It is always fun to peel
- 21:26Speaker 2: back the layers and peek behind the curtain.
- 21:28Speaker 1: And for everyone listening, your homework is simple. Don't just
- 21:31Speaker 1: listen to this deep dive. Open up your code editor,
- 21:34Speaker 1: try to write out that process information structure strictly from memory.
- 21:38Speaker 2: That's a great exercise.
- 21:39Speaker 1: See if you can remember the fields handle, handle, do word.
- 21:43Speaker 1: It's the best way to make sure you really own
- 21:45Speaker 1: this knowledge. Thanks for joining us, and we'll catch you
- 21:48Speaker 1: on the next deep dive.
- 21:49Speaker 2: Happy Coding,