Latest / CyberCode Academy / Course 25 - API Python Hacking | Episode 3: From ctypes Basics to Building a Process Killer
Transcript
- 0:00Speaker 1: Welcome back to the deep dive. You know, usually when
- 0:04Speaker 1: we talk about Python, we're operating in this very safe,
- 0:07Speaker 1: kind of comfortable sandbox.
- 0:09Speaker 2: Yeah, we really are.
- 0:10Speaker 1: We're automating spreadsheets or maybe scraping a website or you know,
- 0:15Speaker 1: training a model. It's very high level, it's.
- 0:17Speaker 2: Clean, it is I mean, Python handles all that messy
- 0:20Speaker 2: memory management for you. It's sort of like driving a
- 0:23Speaker 2: modern car with lanessist and automatic braking. You don't really feel.
- 0:27Speaker 1: The road right exactly. But today we are turning off
- 0:30Speaker 1: the lanusist, We're popping the hood and we're going to
- 0:33Speaker 1: get grease on our hands. We are talking about the
- 0:35Speaker 1: Windows API.
- 0:36Speaker 2: Yes, and this is honestly one of my favorite topics
- 0:39Speaker 2: because it bridges that gap between being just a scriptor
- 0:43Speaker 2: and being a systems engineer. We aren't just using software today.
- 0:47Speaker 2: We're going to talk about how to control the operating
- 0:49Speaker 2: systems DNA directly. And the best part, we are installing anything.
- 0:52Speaker 1: Yeah that's the hook for me, right there, no PIP install,
- 0:55Speaker 1: no third party tools. We are doing this with standard
- 0:58Speaker 1: Python and just the operating system itself. So set the
- 1:02Speaker 1: stage for us. What's the mission today.
- 1:04Speaker 2: Well, the mission is to understand how to make Python
- 1:07Speaker 2: talk to Windows in its own language. We're gonna start
- 1:10Speaker 2: with something visual first, like making Windows pop up. Then
- 1:13Speaker 2: we'll dive into the invisible stuff, so error handling and
- 1:16Speaker 2: memory right. And finally we're going to build a tool
- 1:19Speaker 2: from scratch that can hunt down a specific program by
- 1:22Speaker 2: name and just kill it instantly.
- 1:23Speaker 1: A process killer. I like it. It sounds a little dangerous.
- 1:28Speaker 2: It is a little dangerous. Yeah, if you point it
- 1:29Speaker 2: at the wrong thing, you can completely blue screen your computer.
- 1:32Speaker 2: But honestly, that's how you learn.
- 1:34Speaker 1: Okay, Well, before we break anything, let's talk about the
- 1:36Speaker 1: tool you mentioned. We aren't installing anything. But Python doesn't
- 1:39Speaker 1: speak Windows natly. I mean, Python speaks Python, Windows speaks
- 1:44Speaker 1: C right, So how do we bridge that gap.
- 1:47Speaker 2: We use a library that comes built in with Python
- 1:49Speaker 2: called sea types.
- 1:50Speaker 1: Sea types sounds like a typo, honestly, it does.
- 1:53Speaker 2: Doesn't it, But it actually stands for sea types. Think
- 1:56Speaker 2: of C types as like a foreign diplomat or a translator.
- 2:01Speaker 2: Windows expects data in very specific, rigid C style formats.
- 2:06Speaker 2: It wants memory addresses, pointers, and specific integer sizes. Python
- 2:11Speaker 2: is dynamics, so it doesn't care about that stuff. Usually,
- 2:14Speaker 2: if Python just yelled at Windows, Windows wouldn't understand a
- 2:17Speaker 2: single word.
- 2:18Speaker 1: So C types of the middleman. It takes our Python variables,
- 2:21Speaker 1: puts them in a suit and tie, and presents them
- 2:23Speaker 1: to Windows in a way that the OS actually respects.
- 2:26Speaker 2: That is the perfect analogy. It allows us to load
- 2:29Speaker 2: what are called DLLs dynamic link libraries.
- 2:32Speaker 1: Right, the classic dot dll files exactly.
- 2:34Speaker 2: These are the files where Windows actually lives, and through
- 2:37Speaker 2: C types, we can call functions inside those DLLs just
- 2:41Speaker 2: like they were normal Python functions.
- 2:42Speaker 1: Okay, so let's meet our first foreign function. Then we
- 2:45Speaker 1: want to make a message box. Where does that live?
- 2:48Speaker 2: That lives in user thirty two dot DL.
- 2:50Speaker 1: User thirty two.
- 2:51Speaker 2: Right, Windows is split into a few main areas. You
- 2:54Speaker 2: can think of user thirty two dot dll as the artist.
- 2:57Speaker 2: It handles the user interface, so windows by and scroll bars,
- 3:01Speaker 2: mouse clicks. Basically, if you can see it and click it,
- 3:03Speaker 2: user thirty two is probably drawing it.
- 3:05Speaker 1: So if we want to call a function in user
- 3:06Speaker 1: thirty two, is it as simple as just typing print
- 3:09Speaker 1: Hello world.
- 3:10Speaker 2: I really wish it was in Python. Yeah, you just
- 3:12Speaker 2: give print a string. But the Windows API is needy, needy,
- 3:18Speaker 2: very We're going to use a function called message box
- 3:21Speaker 2: W and to make it work, we have to provide
- 3:24Speaker 2: four very specific parameters. If you miss one or even
- 3:27Speaker 2: get the type wrong, it just won't work.
- 3:29Speaker 1: Okay, let's unpack the four ingredients of a message box.
- 3:32Speaker 2: Then first you need to handle in the API. This
- 3:35Speaker 2: is usually called HWND. Like HWND exactly, it's basically asking
- 3:40Speaker 2: who acts as the parent of this window. Since we're
- 3:43Speaker 2: just a script running in the background, we don't actually
- 3:45Speaker 2: have a parent window, right, so we pass none. In
- 3:48Speaker 2: C terms, that's at anyl pointer. It basically tells Windows
- 3:52Speaker 2: to just throw this box on the desktop. Got it?
- 3:54Speaker 1: So handle is ingredient one, what's ingredient two?
- 3:57Speaker 2: The text? This is the body of message hello world
- 4:00Speaker 2: or system error or whatever you want to say to
- 4:02Speaker 2: these enough.
- 4:03Speaker 1: Ingredient three the caption.
- 4:05Speaker 2: This is the text that appears in the top title
- 4:07Speaker 2: bar of the box.
- 4:08Speaker 1: Okay, And the fourth one you said. This is where
- 4:10Speaker 1: it gets interesting.
- 4:10Speaker 2: The type right, This is where you really see the
- 4:13Speaker 2: difference between high level Python and low level C. You
- 4:16Speaker 2: don't pass a string saying I want an OK button,
- 4:19Speaker 2: You pass a number a hexadecimal code.
- 4:21Speaker 1: So we're speaking in math.
- 4:22Speaker 2: Now exactly. The type is a bit mask. If you
- 4:25Speaker 2: send a zero or zero x zero zero in hacks,
- 4:29Speaker 2: Windows interprets that as give me a message box with
- 4:32Speaker 2: just an OK button.
- 4:33Speaker 1: Okay.
- 4:33Speaker 2: But if you send a one so zero by zero one,
- 4:36Speaker 2: Windows interprets that as give me okay and cancel buttons.
- 4:40Speaker 1: And I assume there are codes for yes, no, or
- 4:43Speaker 1: abort retry.
- 4:44Speaker 2: There are codes for everything. There even codes for the icon.
- 4:47Speaker 2: Like if you want that scary red x error icon,
- 4:50Speaker 2: that's a specific number you add to them.
- 4:51Speaker 1: Oh, you add them together.
- 4:53Speaker 2: Yeah, so you might have zero by zero one for
- 4:54Speaker 2: the buttons plus zero by ten for the icon. You
- 4:57Speaker 2: literally do the math and add them together to tell
- 5:00Speaker 2: user thirty two exactly what to paint on the screen.
- 5:02Speaker 1: That's really cool. It's like building a custom order by
- 5:04Speaker 1: just adding up menu numbers. Now. I did see a
- 5:07Speaker 1: term in the documentation for the text part that which
- 5:09Speaker 1: absolutely terrifying. Lpcwstr.
- 5:12Speaker 2: Ah. Yes, lpcwstr. It looks like someone fill asleep on.
- 5:16Speaker 1: The keyboard, it really does. What on earth does that
- 5:19Speaker 1: actually mean?
- 5:19Speaker 2: So this is classic Microsoft Hungarian notation. It stands for
- 5:23Speaker 2: long pointer to a constant wide string.
- 5:26Speaker 1: Yeah, you're gonna have to translate that translation for me.
- 5:29Speaker 2: Let's break it down. Pointer just means we are pointing
- 5:31Speaker 2: to a place in memory. Constant means that text isn't
- 5:35Speaker 2: going to change while the box is open. But the
- 5:37Speaker 2: absolute most important part is the wide part W.
- 5:40Speaker 1: I noticed the function is called message box W exactly.
- 5:43Speaker 2: That W stands for wide, which means Unicode. Okay, and
- 5:46Speaker 2: the old days we used ANSI, which was really limited
- 5:49Speaker 2: to basic English characters. Modern Windows uses Unicode, which is
- 5:54Speaker 2: sixteen bit. It supports emojis, Chinese characters, everything right. When
- 5:58Speaker 2: we use C types, we have to be really careful.
- 6:00Speaker 2: If you try to feed a standard ASSE string to
- 6:02Speaker 2: a wide function, Windows will read it wrong. It puts
- 6:05Speaker 2: these padding zeros between every single.
- 6:07Speaker 1: Letter, so the word hello becomes hing e nothing.
- 6:11Speaker 2: Exactly, and it looks like absolute garbage on the screen.
- 6:14Speaker 2: So we have to ensure we are sending the right
- 6:15Speaker 2: data type. Seakyves does handle some of this for us,
- 6:18Speaker 2: but knowing the difference between the A functions for ANSI
- 6:21Speaker 2: and the W functions for wide is critical.
- 6:24Speaker 1: Okay, So we've got our data types right center parameters,
- 6:27Speaker 1: The box pops up I click okay, does the script end.
- 6:30Speaker 2: Not necessarily, the function actually returns the value. Remember this
- 6:34Speaker 2: is a conversation with the os If you click ok,
- 6:36Speaker 2: message box w returns the integer one. If you had
- 6:39Speaker 2: to cancel button and click that, it would return two.
- 6:42Speaker 1: So I can actually use this for logic in my script.
- 6:44Speaker 2: Absolutely, you could write a Python script that pauses, pops
- 6:47Speaker 2: up a native Windows box asking do you want to continue,
- 6:51Speaker 2: and then your script just waits. If the return value
- 6:53Speaker 2: is one, it proceeds. If it's two, it safely exits.
- 6:57Speaker 2: You're literally weaving the OSUI into your pythonline.
- 7:00Speaker 1: That is incredibly powerful. But let's be real for a second.
- 7:03Speaker 1: Code breaks, especially when we're messing with low level stuff
- 7:06Speaker 1: like this.
- 7:06Speaker 2: Oh yeah, in.
- 7:07Speaker 1: Python, we get a nice trace back. It tells us
- 7:09Speaker 1: index err on line five. What happens when a Windows
- 7:12Speaker 1: API call fails.
- 7:14Speaker 2: Silence, just silent, complete silence. That is the nightmare of
- 7:17Speaker 2: system programming. If a Windows API function fails, say you
- 7:21Speaker 2: passed it a bad handle, it usually just returns to zero.
- 7:25Speaker 2: It doesn't crash your script, it doesn't print scary red
- 7:28Speaker 2: text to the console. It just returns zero and keeps
- 7:31Speaker 2: right on going.
- 7:32Speaker 1: That sounds incredibly frustrating to debug. How do you actually
- 7:36Speaker 1: know why it failed?
- 7:37Speaker 2: You have to check the black box. And this brings
- 7:39Speaker 2: us to our second DLL for the day, Colonel thirty
- 7:41Speaker 2: two dot dll.
- 7:42Speaker 1: Okay, so user thirty two was the artist. Who is
- 7:46Speaker 1: kernel thirty two?
- 7:47Speaker 2: Pernel thirty two is the manager. It handles memory, processes, threads,
- 7:51Speaker 2: basically the core of the operating system. And it has
- 7:53Speaker 2: a very special function called get last.
- 7:55Speaker 1: Era, which does exactly what it says on the ten
- 7:57Speaker 1: it does.
- 7:57Speaker 2: But here is the catch. You have to call it immediately?
- 8:00Speaker 1: How immediately? Like next line of code?
- 8:02Speaker 2: The very next line of code, Windows has a global
- 8:05Speaker 2: variable where it stores the last error code. If your
- 8:08Speaker 2: function fails, it runs the error code there. But if
- 8:10Speaker 2: you run another function, even just a simple print statement,
- 8:13Speaker 2: sometimes that subsequent function might succeed and overwrite the error
- 8:17Speaker 2: code back to zero.
- 8:18Speaker 1: Oh wow, so the evidence is just destroyed.
- 8:21Speaker 2: Exactly, So your pattern should always be call the API
- 8:24Speaker 2: check if it returned to failure code like zero, and
- 8:28Speaker 2: if it did, immediately called get laster.
- 8:30Speaker 1: And what does get laster give us? Does it return
- 8:32Speaker 1: a string saying you messed up your handle?
- 8:35Speaker 2: No, of course not. It gives you a number. Computers
- 8:38Speaker 2: love numbers. You might get AIR five or ER six
- 8:41Speaker 2: or Era one twenty six, and.
- 8:42Speaker 1: We just have to memorize all of these.
- 8:44Speaker 2: No, you just google them or you look at the
- 8:46Speaker 2: official Microsoft documentation. For example, AIR five is probably the
- 8:50Speaker 2: most common one you'll see in security work. It means
- 8:53Speaker 2: access denied. AIR six is invalid handle access denied.
- 8:58Speaker 1: That is actually a perfect segue because for the next
- 9:00Speaker 1: part of our build we are going to run into
- 9:02Speaker 1: that ER five a lot. We're moving from making cute
- 9:05Speaker 1: little message boxes to touching actual running processes.
- 9:08Speaker 2: Right, we are done with user thirty two. We are
- 9:10Speaker 2: moving fully into kernel thirty two territory. Now we want
- 9:13Speaker 2: to write a script that can reach into the operating
- 9:15Speaker 2: system and grab a handle to a running program like
- 9:18Speaker 2: Notepad or paint or task manager, and.
- 9:20Speaker 1: The function for this is open process.
- 9:22Speaker 2: Correct. Open process is the gateway to interacting with other programs.
- 9:26Speaker 2: But unlike the message box, open process has security guards.
- 9:30Speaker 2: You can't just walk right in.
- 9:31Speaker 1: So what does it require at the door?
- 9:33Speaker 2: It needs three things? First, access rights, basically, what do
- 9:36Speaker 2: you want to do with this process? Read its memory,
- 9:39Speaker 2: terminate it. Second, an inheritance flag, which we usually just
- 9:43Speaker 2: set to falls, and third, the process ID or PID.
- 9:46Speaker 1: Let's talk about those access rights first. This goes back
- 9:49Speaker 1: to the.
- 9:49Speaker 2: Hex codes, right does there are specific X codes for
- 9:52Speaker 2: I want to read or I want to write. But
- 9:54Speaker 2: in our script, we are going to use the skeleton key.
- 9:56Speaker 2: It's a flag called processorle access.
- 9:58Speaker 1: That sounds pretty ominous, it really is.
- 10:00Speaker 2: It's the combination of every possible permission in hex It's
- 10:04Speaker 2: roughly zero x zero zero zero one zero fff. When
- 10:08Speaker 2: we send that number, we are explicitly telling Windows I
- 10:11Speaker 2: want full control over this process. I want to be
- 10:13Speaker 2: able to do anything to it.
- 10:14Speaker 1: Okay, so we have our skeleton key. Now we need
- 10:17Speaker 1: the target, the PID. If I open task Manager on
- 10:19Speaker 1: my computer, I see a list of processes and their IDs.
- 10:22Speaker 1: Let's say notepad is PID twenty one hundred, So I
- 10:25Speaker 1: just plug twenty one hundred into my script start right there.
- 10:28Speaker 2: That is the classic trap.
- 10:30Speaker 1: Why task manager literally says twenty one.
- 10:32Speaker 2: Hundred because task manager displays the ID in decimal base ten,
- 10:37Speaker 2: the normal numbers humans count with. But depending on how
- 10:41Speaker 2: you are viewing memory or using certain debugging tools, the
- 10:44Speaker 2: system might expect or display that in hexadecimal. Oh right, yeah,
- 10:50Speaker 2: if you hardcode a decimal number but the function expects
- 10:52Speaker 2: a X format, or if you just get confused between
- 10:54Speaker 2: the two while debugging, you're going to be pointing at
- 10:57Speaker 2: the completely wrong process or a process that doesn't even exist.
- 11:00Speaker 1: So you always have to verify your bases, convert the
- 11:03Speaker 1: decimal pid to hex if needed.
- 11:05Speaker 2: Always. It saves hours of debugging. But let's say you
- 11:08Speaker 2: get the pid right, you ask for processed access, You
- 11:12Speaker 2: run your script and boom, get last returns.
- 11:14Speaker 1: Live access denied. But wait, it's my computer. I'm logged
- 11:17Speaker 1: in as me. Why am I denied access to my
- 11:19Speaker 1: own task Manager process?
- 11:20Speaker 2: This is a really crucial concept in Windows internals. It's
- 11:23Speaker 2: called tokens.
- 11:24Speaker 1: Tokens like an arcade.
- 11:25Speaker 2: More like an ID badge at a corporate office. When
- 11:28Speaker 2: you log in, even if your account is an administrator,
- 11:31Speaker 2: your daily applications like your Python code editor or your
- 11:35Speaker 2: command prompt run with a standard user token. This is
- 11:39Speaker 2: a safety feature called User account control or UAC. Right,
- 11:43Speaker 2: it prevents a random script you download off the internet
- 11:45Speaker 2: from nuking your system silently in the background.
- 11:48Speaker 1: Okay, so my Python script is wearing a guest badge basically,
- 11:51Speaker 1: but I'm trying to access task manager, which is wearing
- 11:54Speaker 1: a system or admin badge.
- 11:56Speaker 2: Exactly. Windows looks at your guest badge, sees you asking
- 12:01Speaker 2: for process access and just laughs. It throws air five.
- 12:04Speaker 1: So how do we get the admin badge for our script.
- 12:08Speaker 2: You have to escalate the privileges of your Python process.
- 12:10Speaker 2: You know, when you write click an app and click
- 12:12Speaker 2: run as administrator.
- 12:13Speaker 1: Yeah, you get that little pop up asking if you're sure.
- 12:16Speaker 2: That is you asking Windows to issue a privileged token
- 12:19Speaker 2: for that specific session. If you run your terminal as
- 12:23Speaker 2: administrator and then run your Python script from inside that terminal,
- 12:27Speaker 2: your script inherits that privileged token. Now, when you ask
- 12:30Speaker 2: for processor access, Windows checks the badge, sees its admin
- 12:34Speaker 2: and says, right this way.
- 12:35Speaker 1: That makes perfect sense. It's not about you, the human user,
- 12:39Speaker 1: It's about the process and what token the process currently
- 12:42Speaker 1: holds precisely. Okay, so we've got the theory down. We
- 12:46Speaker 1: understand x types, the hex codes, error handling PIDs, and
- 12:49Speaker 1: the token system. I think it's time to put it
- 12:52Speaker 1: all together. The prop killer.
- 12:54Speaker 2: The prop killer. This is our capstone project for the session.
- 12:57Speaker 1: The goal here is I want to run a Python script,
- 13:00Speaker 1: give it the exact name of a window like untitled notepad,
- 13:03Speaker 1: and I want that process to vanish just puff gone.
- 13:06Speaker 2: To do this, we need to chain together four distinct steps.
- 13:09Speaker 2: We're going to be jumping back and forth between user
- 13:11Speaker 2: thirty two and kernel three two.
- 13:13Speaker 1: Walk us through step one.
- 13:14Speaker 2: Step one is finding the window. We have a string
- 13:17Speaker 2: name nopad, but Windows needs a handle, so you go
- 13:19Speaker 2: back to user threety two dot dl and use a
- 13:21Speaker 2: function called fine window A.
- 13:23Speaker 1: Wait, fine window A. We used message box W earlier.
- 13:26Speaker 1: Why the A this time?
- 13:27Speaker 2: Just to show the difference in how they work. A
- 13:29Speaker 2: stands for ANSI. Since we are just searching for a
- 13:32Speaker 2: simple standard English string like notepad, we can get away
- 13:35Speaker 2: with using the older ANSI function, right. But this means
- 13:38Speaker 2: in Python we actually need to encode our string. We
- 13:41Speaker 2: can't just pass the normal Python string object. We need
- 13:45Speaker 2: to use dot encode at f eight or converted to
- 13:47Speaker 2: a byte string so the bytes perfectly match what the
- 13:50Speaker 2: A function is expecting.
- 13:51Speaker 1: Okay, So fine win OA looks through all the open
- 13:54Speaker 1: windows on the desktop, finds the one matching notepad, and
- 13:57Speaker 1: returns a window handle in HWND.
- 14:00Speaker 2: But here is a very critical distinction. A window handle
- 14:04Speaker 2: is not a process ID.
- 14:05Speaker 1: Right, Because a process owns the window, they aren't the
- 14:07Speaker 1: same thing exactly.
- 14:09Speaker 2: We have the handle to the graphical display window, but
- 14:12Speaker 2: we need the idea of the actual process running it
- 14:14Speaker 2: in memory, and that leads us the step too, which.
- 14:17Speaker 1: Involves a function with probably the longest name ever, get
- 14:20Speaker 1: window thread process.
- 14:21Speaker 2: It is a massive mouthful, but this step introduces what
- 14:25Speaker 2: is probably the most difficult technical concept for Python programmers
- 14:29Speaker 2: to grasp, pass by reference.
- 14:31Speaker 1: Okay, let's definitely slow down here. Usually in Python, if
- 14:34Speaker 1: I call a function, I just say result equals function input.
- 14:38Speaker 1: The function calculates something inside itself and spits the answer
- 14:41Speaker 1: back out to me.
- 14:42Speaker 2: Right, that's the standard return value. But get window thread
- 14:45Speaker 2: process side is weird. It doesn't actually return the process ID.
- 14:49Speaker 2: It returns the thread ID, which we honestly don't care
- 14:52Speaker 2: about right now.
- 14:53Speaker 1: So where does the process ID go? How do we
- 14:55Speaker 1: get it?
- 14:56Speaker 2: You have to give the function a bucket. To put
- 14:58Speaker 2: the ID into a bucket. Yes, you create a special
- 15:01Speaker 2: variable using ag types, specifically a cou long which stands
- 15:04Speaker 2: for unsigned long integer. This variable is your empty bucket. Okay,
- 15:09Speaker 2: when you call the function, you don't pass the bucket's value,
- 15:12Speaker 2: you pass its address in memory. You're telling Windows here
- 15:15Speaker 2: is a specific memory address. Please write the process ID
- 15:18Speaker 2: right here.
- 15:19Speaker 1: And how do we actually do that?
- 15:20Speaker 2: In Python? We use a c types helper function called
- 15:23Speaker 2: buy ref byr EF. So the function call looks like
- 15:26Speaker 2: get window thread process hit it. Then you pass the
- 15:28Speaker 2: window handle and then buy ref my bucket.
- 15:31Speaker 1: That is very different from standard Python.
- 15:33Speaker 2: It really is. It's direct memory manipulation. We are literally
- 15:36Speaker 2: handing over a pointer. Windows follows that pointer, fills our
- 15:40Speaker 2: bucket with a PID, and then back in our normal
- 15:42Speaker 2: Python script, we can just look inside my bucket dot
- 15:45Speaker 2: value and boom, there's the process ID.
- 15:48Speaker 1: That's wild. So we use the window handle to get
- 15:51Speaker 1: the PID via a memory pointer. Now we have the PID,
- 15:54Speaker 1: we're ready for step three.
- 15:56Speaker 2: Step three is familiar territory. Now we go back to
- 16:00Speaker 2: ernal three to two dot DLL and we call open process.
- 16:03Speaker 1: We use our PID from the bucket, we ask for
- 16:05Speaker 1: processal access, and assuming we are running the script as.
- 16:09Speaker 2: Atman, we get back a process handle. This is the
- 16:11Speaker 2: master key. This isn't a handle to a graphical window anymore.
- 16:14Speaker 2: It's a handle to the application's very existence in the
- 16:17Speaker 2: system memory.
- 16:18Speaker 1: Which brings us to the end. Step four, the final
- 16:20Speaker 1: strike terminative process. It sounds so final it is.
- 16:23Speaker 2: This function takes two arguments, the process handle we just
- 16:26Speaker 2: got in an exit code. Usually we just send an
- 16:29Speaker 2: exit code of one to indicate to the system that
- 16:31Speaker 2: it was a forced termination.
- 16:32Speaker 1: And what happens when you run that line of code.
- 16:34Speaker 2: It's immediate. The application doesn't get a chance to ask
- 16:37Speaker 2: do you want to save your work? It doesn't run
- 16:40Speaker 2: its clean up routines. The operating system simply cuts the cord.
- 16:44Speaker 2: The window vanishes from the screen before your finger even
- 16:47Speaker 2: lifts off the enter key.
- 16:48Speaker 1: Wow, it's incredibly satisfying.
- 16:50Speaker 2: Isn't it? It is? But again, power and responsibility right.
- 16:54Speaker 1: Because if I accidentally put the PSOD for say NSX
- 16:58Speaker 1: dot XE, the clients sub untime process into that bucket.
- 17:02Speaker 2: Your screen immediately goes blue and your computer restarts. Yeah,
- 17:05Speaker 2: instant crash windows need certain processes to live. If you
- 17:09Speaker 2: kill them, you kill the entire OS session.
- 17:11Speaker 1: So double check your PIDs. Maybe don't write a loop
- 17:14Speaker 1: that just guesses random numbers and tries to kill them.
- 17:16Speaker 2: Definitely do not do that.
- 17:17Speaker 1: Good tip. Now, there was one more piece of advice
- 17:20Speaker 1: in the source material about writing this kind of code.
- 17:22Speaker 1: It heavily emphasized comments. Now, usually we say clean code
- 17:27Speaker 1: explains itself. Why is commenting so vital when we're doing
- 17:31Speaker 1: this because.
- 17:32Speaker 2: C TYPEES code is obscure by nature. If you look
- 17:35Speaker 2: at a line that says user thirty two dot message
- 17:37Speaker 2: box w none text caption zero x zero zero zero
- 17:42Speaker 2: zero zero zero zero one, you might remember today that
- 17:46Speaker 2: zero by one means okay, cancel. But will you remember
- 17:49Speaker 2: that in six months?
- 17:51Speaker 1: Probably not?
- 17:51Speaker 2: And will you remember why you use bi ref in
- 17:54Speaker 2: step two but not in step one? When you are
- 17:57Speaker 2: dealing with X codes, memory pointers, and foreign c fiunctions,
- 18:00Speaker 2: you have to leave breadcrumbs for your future self. You
- 18:03Speaker 2: need to document why you are using that specific flag.
- 18:06Speaker 1: That's great advice. So we've gone from Hello World to
- 18:09Speaker 1: Goodbye process. We've learned that Python can wear a C
- 18:11Speaker 1: language disguise using T types. We've learned about handles versus PIDs,
- 18:15Speaker 1: the importance of error handling, and the token system.
- 18:18Speaker 2: It really is a complete tour of the basement of Windows.
- 18:21Speaker 1: Now, for the listener who wants to cement this knowledge,
- 18:23Speaker 1: we have a challenge for you to try on your
- 18:24Speaker 1: own Yes.
- 18:26Speaker 2: In our description of the process killer, we assumed you
- 18:28Speaker 2: would search for a window name like notepad, right, But.
- 18:30Speaker 1: That's a bit rigid, right, So the challenges make it interactive.
- 18:34Speaker 2: Exactly. Write a Python script that asks the user for input,
- 18:38Speaker 2: have it prompt the terminal with please enter the pid
- 18:40Speaker 2: you want to terminate.
- 18:42Speaker 1: That sounds simple, but there's a catch. There isn't there.
- 18:44Speaker 2: The catch is data types. The standard Python input function
- 18:48Speaker 2: gives you a string, but the Windows API needs an integer,
- 18:52Speaker 2: and the user might type the PID in decimal. But
- 18:55Speaker 2: you might need to handle it differently depending on your
- 18:58Speaker 2: logic and the specific function.
- 18:59Speaker 1: The challenge is to accept a raw input, safely convert
- 19:03Speaker 1: it to the correct integer type that sees pipes expects,
- 19:07Speaker 1: and then pass it to open process and termin it process.
- 19:09Speaker 2: If you can do that, if you can take human
- 19:11Speaker 2: input and successfully translate it into a raw system call,
- 19:15Speaker 2: you have officially mastered the basics of the Windows API.
- 19:18Speaker 1: I love it. It moves you from just following a recipe
- 19:21Speaker 1: to actually cooking exactly well. This has been a truly
- 19:25Speaker 1: fascinating look under the hood. It turns out standard Python
- 19:28Speaker 1: is a lot more powerful than people give it credit for.
- 19:30Speaker 2: It is you don't always need a massive, fancy framework.
- 19:34Speaker 2: Sometimes you just need to know how to speak the
- 19:35Speaker 2: native language of the machine.
- 19:37Speaker 1: Absolutely, and here's a final thought for you to chew on.
- 19:41Speaker 1: We just spent all this time learning how to use
- 19:44Speaker 1: a process handle to terminate an application. But what if
- 19:48Speaker 1: you reverse that. If you have full access to a
- 19:51Speaker 1: process's memory, could you write into it instead of destroying it?
- 19:55Speaker 1: That is exactly how game trainers and certain types of
- 19:57Speaker 1: malware operate. It's the exact same access, just a different
- 20:01Speaker 1: function call.
- 20:02Speaker 2: That's a whole different rabbit hole.
- 20:03Speaker 1: It really is. Well, thanks for guiding us through the
- 20:06Speaker 1: matrix today, my pleasure, and to you listening. Go check
- 20:09Speaker 1: those air loogs, verify your handles, and we'll see you
- 20:11Speaker 1: in the next deep dive. Goodbye,