Latest / CyberSecurity Summary / Windows 10 System Programming, Part 2
Transcript
- 0:00Speaker 1: Welcome to the deep dive. Today. We're really getting into
- 0:03Speaker 1: the weeds with Windows System Programming.
- 0:06Speaker 2: Yeah, this is part two, building on those fundamentals exactly.
- 0:09Speaker 1: We're thinking about our listener, the learner who's maybe bumped
- 0:12Speaker 1: into terms like DLLs, memory management, security, maybe even performance analysis.
- 0:17Speaker 2: Right and wants to get a clearer picture without drowning
- 0:22Speaker 2: in super technical details.
- 0:23Speaker 1: Our goal is to give you the learner, that clearer,
- 0:26Speaker 1: more insightful understanding.
- 0:28Speaker 2: Think of it like getting the essential toolkit for these
- 0:30Speaker 2: complex topics. Wow, we want those aha moments, not just
- 0:34Speaker 2: the list of functions for sure.
- 0:36Speaker 1: And we're drawing heavily from Windows ten System Programming Part two.
- 0:40Speaker 1: It's a great resource for digging into the advanced Windows
- 0:43Speaker 1: API stuff.
- 0:44Speaker 2: It really tries to bridge that gap for programmers working
- 0:46Speaker 2: with modern Windows, covering well everything from memory tricks to
- 0:50Speaker 2: security layers.
- 0:51Speaker 1: So our mission today extract the really crucial, the fascinating.
- 0:56Speaker 2: Bits exactly, give you those key insights that solid foundation
- 0:59Speaker 2: will hit advanced memory management, the whole world of DLLs.
- 1:03Speaker 1: The complexities of Window security, and.
- 1:07Speaker 2: The tools you need for debugging and diagnostics when things
- 1:11Speaker 2: go sideways.
- 1:12Speaker 1: Okay, let's kick things off with memory management. We're going
- 1:14Speaker 1: way beyond the basics here.
- 1:16Speaker 2: Definitely think raw control. First up is virtual memory allocation
- 1:21Speaker 2: virtual alec and uh virtual alex.
- 1:25Speaker 1: What's the core difference there, y x.
- 1:27Speaker 2: Well, virtual ALIC is for your own processes memory. Virtual
- 1:30Speaker 2: alec though, that lets you allocate memory inside another process,
- 1:34Speaker 2: assuming you have the permissions of course.
- 1:36Speaker 1: Ah. Okay, so that's key for things like debuggers, right
- 1:39Speaker 1: reaching into another program.
- 1:41Speaker 2: Space Precisely, it's fundamental for those kinds of tools. But
- 1:44Speaker 2: it also hints at more complex system interactions.
- 1:47Speaker 1: But that sounds potentially risky. How is that controlled?
- 1:50Speaker 2: Security is paramount? You need specific privileges to target another
- 1:54Speaker 2: process with virtual alex. We'll touch more in security later,
- 1:57Speaker 2: but it's tightly controlled. It makes it a powerful tool,
- 2:01Speaker 2: but not easily misused.
- 2:02Speaker 1: Okay, makes sense now the book mentioned something specific about
- 2:06Speaker 1: security with these functions. Committed pages get zamboed out.
- 2:09Speaker 2: Yes, that's crucial. When Windows commits memory using virtual alec
- 2:13Speaker 2: or x, it wipes it clean, fills it with zeros.
- 2:17Speaker 1: Why do that extra step?
- 2:19Speaker 2: It prevents information leakage. You don't want one process accidentally
- 2:23Speaker 2: seeing leftover data from whatever process. Use that physical.
- 2:27Speaker 1: Memory before think of it, like wiping a whiteboard.
- 2:29Speaker 2: Got it, unlike say, malik, which might just give you whatever.
- 2:33Speaker 1: Was there exactly a clean slate every time.
- 2:35Speaker 2: Now Reserving versus committing memory why the two stages.
- 2:39Speaker 1: It's like planning versus building. Reserving is like staking out
- 2:42Speaker 1: a claim in the virtual address space. You say this
- 2:44Speaker 1: range is mine, nobody else.
- 2:46Speaker 2: Touch it, okay, prevents conflicts, right.
- 2:48Speaker 1: Committing is when you actually tell the system, okay, I
- 2:50Speaker 1: need physical memory RAM or page filespace backing this part
- 2:54Speaker 1: of my reserve space. You make it usable so you.
- 2:56Speaker 2: Can reserve a big chunk, but only commit what you
- 2:59Speaker 2: actually need right now.
- 3:00Speaker 1: Precisely reserve the whole plot, build houses one by one,
- 3:03Speaker 1: very efficient.
- 3:04Speaker 2: What about these memory set and members Satendo flags. They
- 3:07Speaker 2: sound a bit niche, they are, Yeah. Member set basically
- 3:10Speaker 2: tells Windows, hey, I don't care about the contents of
- 3:13Speaker 2: this committed memory anymore. Feel free to take the physical
- 3:15Speaker 2: pages back if needed, but leave it allocated.
- 3:18Speaker 1: Sort of like marking it as discardible.
- 3:20Speaker 2: Kind of, and memorys Tottendo's signals you might need it again,
- 3:24Speaker 2: but there's no guarantee the old data is still there.
- 3:26Speaker 2: If the physical pages got reused, it's for specific optimation scenarios.
- 3:30Speaker 1: Okay, Now that micro excel to example in the book
- 3:33Speaker 1: using page based allocation and exception handling. That's the takeaway there.
- 3:37Speaker 2: It's clever. Virtual allock works on pages right, usually four
- 3:40Speaker 2: kb chunks, so microcell reserves a huge virtual address space
- 3:46Speaker 2: for its grid.
- 3:47Speaker 1: It doesn't actually commit most of it initially exactly.
- 3:50Speaker 2: Then if you try to access to sell in an
- 3:52Speaker 2: uncommitted part, it triggers an access violation and exception.
- 3:56Speaker 1: Ah.
- 3:57Speaker 2: The application catches that specific exception, commits the record fired
- 4:00Speaker 2: page using virtualolock inside the exception handler, and then tells
- 4:03Speaker 2: the CPU Okay, try that instruction again. Wow.
- 4:05Speaker 1: So it looks like a massive spreadsheet but only uses
- 4:08Speaker 1: RAM for the bits you're actually using Memory on demand.
- 4:11Speaker 2: BINGO super efficient for potentially huge data sets.
- 4:15Speaker 1: Brilliant. Okay. Let's shift gears slightly within memory management working sets.
- 4:20Speaker 1: What are those? Exactly?
- 4:21Speaker 2: A process is working set is basically the set of
- 4:25Speaker 2: its memory pages currently residing in physical RAM, stuff it
- 4:28Speaker 2: can access without a page fault.
- 4:30Speaker 1: So the stuff it needs readily available for good performance exactly.
- 4:34Speaker 2: The memory manager tries to keep the actively used pages
- 4:37Speaker 2: in the working set and juggles this across all running
- 4:40Speaker 2: processes to keep the system.
- 4:42Speaker 1: Responsive, and if a page isn't used for a while,
- 4:44Speaker 1: it might get kicked out of the working set. The
- 4:47Speaker 1: book mentions soft page faults though.
- 4:49Speaker 2: Right, If a page is removed from the working set
- 4:51Speaker 2: but it's still somewhere else in RAM, accessing it causes
- 4:55Speaker 2: a soft page fault. The system can quickly map it
- 4:58Speaker 2: back in minimal delay.
- 5:00Speaker 1: As opposed to a hard page fault.
- 5:01Speaker 2: Yeah, a hard fault happens when the page isn't in
- 5:04Speaker 2: RAM at all, it's been paged out to disc. That
- 5:06Speaker 2: requires reading it back from the disc, which is much
- 5:08Speaker 2: much slower.
- 5:09Speaker 1: Gotcha. Now, can you actually control the working set size?
- 5:12Speaker 1: The book mentions set process working set size and empty
- 5:15Speaker 1: working set.
- 5:16Speaker 2: You can influence it. Yeah, Set process working set size
- 5:19Speaker 2: lets you suggest minimum and maximum sizes useful if you
- 5:23Speaker 2: really need certain things to stay in RAM.
- 5:25Speaker 1: And empty working set.
- 5:26Speaker 2: That's more aggressive, especially with size at one. It tells
- 5:30Speaker 2: the system trim this process is working set way down.
- 5:33Speaker 2: Maybe if it's going idle for a long time. You
- 5:35Speaker 2: free UPRAM for others.
- 5:36Speaker 1: And tools like the working sets app let you see
- 5:39Speaker 1: the stuff they do.
- 5:39Speaker 2: Yeah, though you usually need admin rights to peek into
- 5:42Speaker 2: other processes working sets in detail.
- 5:44Speaker 1: Right makes sense? Okay, Next memory topic peeps beyond the
- 5:50Speaker 1: standard library malick free. What Windows specifics are there?
- 5:54Speaker 2: Well? Windows provides heap walk to look inside the default
- 5:57Speaker 2: heap of the current process, but just the current one.
- 6:00Speaker 1: What if you need to look at another processes heap,
- 6:02Speaker 1: like for debugging, Then.
- 6:03Speaker 2: You turn to the tool help library use functions like
- 6:06Speaker 2: create tool help thirty two snapshot with the right flags.
- 6:09Speaker 2: Then heap thirty two list first next to get the
- 6:11Speaker 2: heap list for the target process, and heap thirty two
- 6:14Speaker 2: first next to examine the blocks within each heap.
- 6:16Speaker 1: Okay, a bit more involved for external processes. The book
- 6:19Speaker 1: also talks about heap coalescing and heap compact.
- 6:22Speaker 2: Right coalescing is when the heat manager automatically merges adjacent
- 6:27Speaker 2: free blocks back together into bigger free blocks.
- 6:30Speaker 1: Helps fight fragmentation like tidying up the free space.
- 6:33Speaker 2: Exactly normally as automatic, but you can disable it with
- 6:36Speaker 2: a global flag maybe for some micro optimization. Heap compact
- 6:40Speaker 2: lets you force a coalescing operation if it's disabled, or
- 6:43Speaker 2: just generally try to compact the heap.
- 6:45Speaker 1: Interesting trade off there now a UMA non uniform memory
- 6:49Speaker 1: access sounds like it's mainly for big muzzy processor machines.
- 6:53Speaker 2: Absolutely on systems with multiple CPU sockets and dedicated memory banks.
- 6:57Speaker 2: For each access time can vary depending on whether a
- 7:00Speaker 2: CPU is accessing its local memory or memory attached to
- 7:03Speaker 2: another CPU. That's the non uniform part.
- 7:06Speaker 1: So how does Windows help with that?
- 7:08Speaker 2: There are functions like get new mode Processor mask X
- 7:11Speaker 2: to figure out the system's new MAY topology, which processors
- 7:14Speaker 2: belong to which node, and then critically, functions like virtual
- 7:18Speaker 2: alkix NUMA or the newer virtual loc too let you
- 7:21Speaker 2: specifically allocate memory on a preferred UMA node.
- 7:25Speaker 1: So you can try and keep memory close to the
- 7:26Speaker 1: CPU that's going to use.
- 7:27Speaker 2: It most precisely. For high performance computing or large database servers,
- 7:32Speaker 2: NUMA aware allocation can give a noticeable performance boost. It's
- 7:36Speaker 2: about minimizing that cross node latency.
- 7:39Speaker 1: Makes sense, okay. Last item in our memory deep dive
- 7:43Speaker 1: memory mapped files or MMFs. These sound quite powerful.
- 7:48Speaker 2: They really are. Mapewol file is the core function. You
- 7:51Speaker 2: take a file mapping object which might represent an actual
- 7:54Speaker 2: file on disc or not, and map it directly into
- 7:57Speaker 2: your process's virtual address.
- 7:58Speaker 1: Space, So accessing the file becomes like accessing RAM essentially.
- 8:02Speaker 2: Yes, The OS handles the magic of loading data from
- 8:05Speaker 2: the file into memory when needed and writing changes back.
- 8:08Speaker 2: It's very efficient for file io.
- 8:10Speaker 1: And great for inner process communication.
- 8:12Speaker 2: Right IPC Absolutely, It's a classic high performance IPC technique.
- 8:16Speaker 2: On a single machine, multiple processes map the same file
- 8:19Speaker 2: mapping object, and boom, they're sharing the same physical memory.
- 8:21Speaker 2: No need to copy data back and forth through pipes
- 8:24Speaker 2: or sockets.
- 8:24Speaker 1: Super efficient. The book mentions mapuof f alex NUMA. I
- 8:27Speaker 1: guessing that's the NUMA version you got.
- 8:29Speaker 2: It allows specifying a preferred UMMA node for the mapping,
- 8:33Speaker 2: just like with virtual alex NUMA.
- 8:34Speaker 1: Now this is cool. Anonymous MMFs you can share memory
- 8:39Speaker 1: without even having a file on disc. How does that work?
- 8:42Speaker 2: Right? Instead of being backed by a file, anonymous MMFs
- 8:45Speaker 2: are backed by the system's page file. The key is
- 8:48Speaker 2: you still get a handle to the mapping object. You
- 8:50Speaker 2: can pass this handle or often just the starting memory
- 8:53Speaker 2: address after mapping to another process.
- 8:55Speaker 1: How do you pass it?
- 8:56Speaker 2: Any standard IPC mechanism, window messages, named pipes, even just
- 9:00Speaker 2: a shared variable in a DLL. The both processes load
- 9:04Speaker 2: the second process then uses that handle or address to
- 9:07Speaker 2: map the same anonymous shared memory region into its own space.
- 9:10Speaker 1: That sounds incredibly simple for sharing data, just pass a pointer.
- 9:13Speaker 2: Essentially, it's a very elegant approach for certain kinds of IPC, and.
- 9:17Speaker 1: One less thing on mmf's MEMO large pages. What's the
- 9:20Speaker 1: benefit there?
- 9:22Speaker 2: Standard memory pages are small, typically for KB. Large pages
- 9:25Speaker 2: are much bigger, maybe two memb or even one GB
- 9:27Speaker 2: on some systems. Using large pages for memory mapping can
- 9:30Speaker 2: reduce TLB pressure. That's the translation look aside buffer cash
- 9:34Speaker 2: and potentially improve performance for applications accessing large amounts of
- 9:38Speaker 2: map data.
- 9:39Speaker 1: But there are requirements.
- 9:40Speaker 2: Yes, you need the Sellock memory privilege which is granted
- 9:43Speaker 2: by default, and the file mapping object itself must have
- 9:47Speaker 2: been created with the c clarch pages flag. So it's
- 9:49Speaker 2: an optimization for specific scenarios.
- 9:52Speaker 1: Whow okay, that covers a lot of ground on memory.
- 9:55Speaker 1: Let's switch gears now to dynamic link libraries DLLs. Everyone
- 9:59Speaker 1: knows they're about res usable code, but how does Windows
- 10:01Speaker 1: actually handle them?
- 10:03Speaker 2: Right? Dls are fundamental in Windows. The book gets into
- 10:06Speaker 2: how they're linked implicitly versus explicitly how they get loaded
- 10:10Speaker 2: is key.
- 10:10Speaker 1: Okay, implicit linking first, that's the common way where the
- 10:13Speaker 1: OS just loads the DLLs your program needs when it starts.
- 10:16Speaker 2: Pretty much when your ex loads, the Windows loader looks
- 10:19Speaker 2: at its import table to see which DLLs and functions
- 10:22Speaker 2: it needs. Then it has to find those ds.
- 10:25Speaker 1: Is there a specific search order? Where does it look
- 10:28Speaker 1: there is?
- 10:28Speaker 2: It's a defined path. First, it checks if the DLL
- 10:31Speaker 2: is in the known DS list. These are core system
- 10:33Speaker 2: ds always loaded from the system directory for security on
- 10:37Speaker 2: performance helps prevent DLL hijacking.
- 10:39Speaker 1: Okay, known ds first, Then.
- 10:42Speaker 2: Then the directory the ex itself is in. After that
- 10:46Speaker 2: the process is current working directory, then the system directory
- 10:50Speaker 2: System thirty two, the Windows directory, and finally all the
- 10:53Speaker 2: directory is listed in the path environment variable.
- 10:55Speaker 1: That order seems important for deployment. What if it can't
- 10:58Speaker 1: find a needed DLL, usually.
- 11:00Speaker 2: Get that dreaded error message box saying that DLL is
- 11:02Speaker 2: missing and the application just won't start. Process terminates. Figure
- 11:06Speaker 2: fifteen to seven in the book shows a typical example.
- 11:09Speaker 1: Not very helpful. Sometimes you mentioned known DLLs helping prevent hijacking.
- 11:13Speaker 2: Yeah, By forcing the load from the system directory to
- 11:15Speaker 2: find in the registry HKLM no ds, it stops malware
- 11:20Speaker 2: potentially placing a malicious version of a core DLL in
- 11:23Speaker 2: say the application directory to get loaded instead.
- 11:26Speaker 1: Smart Okay, so that's implicit. What about explicit linking.
- 11:29Speaker 2: Explicit linking is where you, the programmer, take control. You
- 11:32Speaker 2: use load library to load a DLL specifically when you
- 11:35Speaker 2: need it, and free library to unload it later.
- 11:38Speaker 1: When would you do that.
- 11:39Speaker 2: Perfect For things like plugins, you load the plug in
- 11:41Speaker 2: DLL only when the user activates that feature. Or maybe
- 11:45Speaker 2: if a feature depends on a DLL that might not
- 11:47Speaker 2: be installed, you can try to load library and if
- 11:50Speaker 2: it fails, you can handle it gracefully. Maybe disable the
- 11:53Speaker 2: feature instead of crashing.
- 11:54Speaker 1: Gives you more control and resilience. The book also mentions
- 11:58Speaker 1: set default deal directories and adult directory customizing the search
- 12:02Speaker 1: path exactly.
- 12:04Speaker 2: These let you modify how the loader searches for DLLs,
- 12:07Speaker 2: especially for explicit loads via load library. Set the fault
- 12:10Speaker 2: deal directories can restrict the search, for instance, only to
- 12:14Speaker 2: the system directory or directories added with adult directory.
- 12:17Speaker 1: And adult directory adds a specific path to the search
- 12:20Speaker 1: list right.
- 12:21Speaker 2: There are various flags you can use, like application diet,
- 12:23Speaker 2: user at I ers System thirty two, Table fifteen to
- 12:26Speaker 2: one lays them all out. It gives you finer control
- 12:29Speaker 2: over dependencies, which can be important for security and avoiding conflicts.
- 12:33Speaker 1: Useful for complex setups. Now, deal main. That's an entry
- 12:36Speaker 1: point for a DLL.
- 12:37Speaker 2: Correct, It's a function the loader calls inside the DLL
- 12:40Speaker 2: for various events when the dealers first loaded into a process,
- 12:44Speaker 2: DL processed, attack, when it's unloaded, delll process attached, and
- 12:48Speaker 2: also potentially when threads are created, DL thread attached or
- 12:51Speaker 2: exit deal thread attached within that process.
- 12:54Speaker 1: Why the thread notifications.
- 12:56Speaker 2: Some DLLs might need to do per thread initialization or cleanup,
- 13:00Speaker 2: but often they don't, so you can call disabled thread
- 13:02Speaker 2: library calls as an optimization to tell the loader, hey,
- 13:05Speaker 2: don't bother calling my dealmain for thread events saves a
- 13:08Speaker 2: bit of overhead.
- 13:09Speaker 1: Okay, but the book has a big warning about delmain
- 13:12Speaker 1: and something called the loader lock sounds serious.
- 13:15Speaker 2: It is very serious. The loader lock is an internal
- 13:17Speaker 2: lock the system uses while loading or unloading DLLs, including
- 13:21Speaker 2: when it calls delmain. The danger is deadlock if your
- 13:24Speaker 2: delmain code tries to do something that also requires the
- 13:27Speaker 2: loader lock, like calling load library itself or get module handle,
- 13:31Speaker 2: or maybe waiting on another synchronization object that some other
- 13:35Speaker 2: thread holds while it's waiting for the loader lock.
- 13:37Speaker 1: Everything grinds to a halt.
- 13:39Speaker 2: Exactly deadlock your process hangs. The strong advice is keep
- 13:44Speaker 2: dealmain simple, do minimal initialization, don't call functions that might
- 13:48Speaker 2: load other DLLs or wait on complex synchronization. It's a
- 13:52Speaker 2: notorious trap.
- 13:53Speaker 1: Definitely something to watch out for. Okay, let's talk about
- 13:55Speaker 1: DL injection and hooking. This sounds like it could be
- 13:58Speaker 1: used for interesting purposes.
- 14:02Speaker 2: Interesting is one word for it. Yes, it can be
- 14:04Speaker 2: used for legitimate tools like debuggers, monitoring utilities, accessibility aids,
- 14:09Speaker 2: but also for malware. DLL injection is basically forcing another
- 14:12Speaker 2: process to load a DLL it wasn't intending to load.
- 14:16Speaker 1: How is that typically done?
- 14:17Speaker 2: One common method uses create a remote thread. You need
- 14:20Speaker 2: privileges first like process vm right and process create thread.
- 14:23Speaker 2: Then you use virtual allox to allocate some memory in
- 14:26Speaker 2: the target.
- 14:27Speaker 1: Process, write the pass to your DLL into that memory.
- 14:29Speaker 2: Right using right process memory. Then you call create a
- 14:32Speaker 2: remote thread, telling it to start a new thread in
- 14:34Speaker 2: the target process, executing the load library function and passing
- 14:37Speaker 2: it the address of the DLL path you just wrote.
- 14:40Speaker 1: So you trick the other process into loading your code essentially.
- 14:43Speaker 2: Yes.
- 14:44Speaker 1: The book also mentions set Windows hook x. How does
- 14:47Speaker 1: that fit in can it cause injection?
- 14:49Speaker 2: It can? Yes. Set Windows sookx lets you install hooks
- 14:52Speaker 2: to intercept system events, keyboard input, mouse messages, window messages,
- 14:56Speaker 2: et cetera. If you install a global hook or one
- 14:59Speaker 2: the target's threads in another process.
- 15:01Speaker 1: The hook procedure itself needs to run in that other
- 15:03Speaker 1: process's context.
- 15:04Speaker 2: Exactly, and for that to happen, the hook procedure must
- 15:07Speaker 2: be located inside a DLL. When you install such a hook,
- 15:11Speaker 2: Windows automatically injects that DLL into all the relevant target processes,
- 15:15Speaker 2: so your hook code can run there.
- 15:17Speaker 1: Ah, So the hooking mechanism itself performs the injection correct.
- 15:21Speaker 2: The example in the book uses a whgt methage hook
- 15:24Speaker 2: to snoop on keyboard input in notepad, and the hook
- 15:27Speaker 2: code has to be in an injected dlll.
- 15:29Speaker 1: Okay, then there's itIt hooking Import address table hooking sounds
- 15:33Speaker 1: very low level.
- 15:34Speaker 2: It is super low level. Every module EXE DL has
- 15:38Speaker 2: an import address table that lists the functions that imports
- 15:40Speaker 2: from other ds, along with the memory addresses where those
- 15:43Speaker 2: functions actually live. IAT hooking involves finding the entry for
- 15:46Speaker 2: a specific function in.
- 15:48Speaker 1: That table and overwriting the address.
- 15:50Speaker 2: Yes, you overwrite the address with the address of your
- 15:53Speaker 2: own function, the hook function, so whenever the original code
- 15:56Speaker 2: tries to call the imported function, it gets redirected to
- 15:59Speaker 2: your code.
- 16:00Speaker 1: Instead, like intercepting the call. The book uses getsi's color
- 16:03Speaker 1: as an example.
- 16:04Speaker 2: Right replacing calls to the real getsy's color with calls
- 16:07Speaker 2: to a custom getsi's color hooked. You have to parse
- 16:10Speaker 2: the module's pe header structure to find the import descriptors,
- 16:14Speaker 2: locate a target DLL like user thirty two dot dll,
- 16:17Speaker 2: find the function pointer, and then patch it.
- 16:20Speaker 1: But the book flags this as tricky.
- 16:22Speaker 2: Very tricky. First, the exact way you patch the address
- 16:25Speaker 2: depends heavily on the CPU architecture. By eighty six by
- 16:28Speaker 2: sixty four arm are all different. Second, you must do
- 16:31Speaker 2: the patch atomically. If another thread tries to call the
- 16:34Speaker 2: function while you're halfway through changing the address, crash yaikes. Yeah.
- 16:39Speaker 2: It requires careful synchronization and knowledge of assembly or low
- 16:42Speaker 2: level intrinsics. It's powerful, but complex and often fragile. Higher
- 16:46Speaker 2: level hooking mechanisms are usually preferred if they can do
- 16:49Speaker 2: the job.
- 16:50Speaker 1: Good to know, okay, that wraps up DLLs. Let's move
- 16:53Speaker 1: into our third major area, Windows security.
- 16:57Speaker 2: This is huge, absolutely massive, and crucial. The book breaks
- 17:01Speaker 2: it down into key components, show nicely in figure sixteen
- 17:03Speaker 2: to one SIDS, tokens, access masks, privileges, security descriptors, UAC
- 17:10Speaker 2: integrity levels.
- 17:11Speaker 1: Let's start with sids security identifiers. These are the unique
- 17:15Speaker 1: ideas for users and.
- 17:16Speaker 2: Groups and computers services. Basically any security principle, every user account,
- 17:21Speaker 2: every group gets a unique SID. The book shows using
- 17:25Speaker 2: creat well known SID to get the sids for standard
- 17:28Speaker 2: built in accounts and groups like local system or.
- 17:30Speaker 1: Everyone, and you can convert them to strings.
- 17:32Speaker 2: YEP converts cit as strings in gives you that familiar
- 17:35Speaker 2: S one five format and look up accounts. It goes
- 17:37Speaker 2: the other way, finds the account name like administrators for
- 17:40Speaker 2: a given SID. It also tells you the SID type, user, group,
- 17:43Speaker 2: et cetera.
- 17:44Speaker 1: Okay, next, it up tokens. What's their role?
- 17:46Speaker 2: An access token is like a security passport for a
- 17:49Speaker 2: process or thread. When you log in, the system creates
- 17:51Speaker 2: a token holding your user SID, all the group sids
- 17:54Speaker 2: you belong to, analyst of your privileges.
- 17:55Speaker 1: Privileges like being able to debug other processes.
- 17:59Speaker 2: Exactly, or shut down the system or change the system time.
- 18:02Speaker 2: These are distinct from general user rights. Most privileges are
- 18:06Speaker 2: actually disabled by default in your token, even if you're
- 18:09Speaker 2: an admin. You have to explicitly enable them if you
- 18:12Speaker 2: need them. See Change Notified Privileges. One exception usually enabled.
- 18:16Speaker 2: Process Explorer shows this clearly.
- 18:18Speaker 1: How do you check or change privileges in a token?
- 18:22Speaker 2: Programmatically you use get token information to query the token's contents,
- 18:27Speaker 2: including privileges, and adjust token privileges to enable, disable, or
- 18:32Speaker 2: remove them. But you need the token adjust privileges right
- 18:35Speaker 2: on the token handle first.
- 18:36Speaker 1: And privileges have those weird names like c DE bug
- 18:39Speaker 1: privilege they do.
- 18:40Speaker 2: You use look of privilege value to get the internal
- 18:42Speaker 2: identifier the LOUE for a privileged name before you can
- 18:45Speaker 2: use adjust token privileges.
- 18:47Speaker 1: The book also talks about primary versus impersonation tokens.
- 18:50Speaker 2: Right, your main process token is the primary token, but
- 18:53Speaker 2: a thread, particularly in a server process, might temporarily impersonate
- 18:57Speaker 2: a client. Using an impersonation token means the thread acts
- 19:01Speaker 2: with the client security context. They're sids their privileges.
- 19:05Speaker 1: Why would it do that?
- 19:06Speaker 2: So the server can access resources on behalf of the
- 19:08Speaker 2: client using the client's permissions. If the client isn't allowed
- 19:11Speaker 2: to access a file, the server thread impersonating them won't
- 19:14Speaker 2: be either requires the same person at privilege. You use
- 19:18Speaker 2: functions like in person log on user and then revert
- 19:20Speaker 2: to self to stop impersonating. Figure sixteen ten illustrates this
- 19:24Speaker 2: client server.
- 19:25Speaker 1: Scenario, very important for secure server design. Okay, access masks,
- 19:30Speaker 1: these are the permissions themselves.
- 19:31Speaker 2: Yes, an access mask is just a bit mask, a
- 19:35Speaker 2: thirty two bit value, where each bit represents a specific
- 19:38Speaker 2: permission like read, write, to lead, execute, etc. Relevant to
- 19:42Speaker 2: a particular type of object.
- 19:43Speaker 1: Are they always specific? I've seen generic reed.
- 19:46Speaker 2: Good point. There are generic rights like generic creed, generic right,
- 19:49Speaker 2: generic xCT, generical. The system maps these generic rights to
- 19:53Speaker 2: a set of specific rights depending on the object type.
- 19:56Speaker 2: Generic creed on a file means something different than generic
- 19:58Speaker 2: creed on a registry key. Figure sixteen sixteen shows an
- 20:01Speaker 2: example mapping.
- 20:02Speaker 1: And these rights are controlled by security descriptors.
- 20:05Speaker 2: Correct Every securable object, file, registry, key, process, thread, mutex,
- 20:09Speaker 2: et cetera has a security descriptor attached. This structure holds
- 20:13Speaker 2: all the security info the owner SID, the primary group SID,
- 20:17Speaker 2: less use now the SACL system Access Control List for auditing,
- 20:22Speaker 2: and crucially the DACL Discretionary Access Control List.
- 20:26Speaker 1: The DACL is the important one for permissions.
- 20:29Speaker 2: It is the DACL contains a list of accs access
- 20:32Speaker 2: control entries. Each ACE specifies a SID, a user or group,
- 20:37Speaker 2: and an access mask stating whether that SID is allowed
- 20:40Speaker 2: or denied those specific rights.
- 20:41Speaker 1: Order matters in the DACL.
- 20:43Speaker 2: Critically, the system evaluates aces in order. Typically, deny aces
- 20:48Speaker 2: come first. The first ACE that matches the user group
- 20:50Speaker 2: in the requested permission determines the outcome, so an explicit
- 20:53Speaker 2: deny will override a later a LIE allowed for the
- 20:55Speaker 2: same group.
- 20:56Speaker 1: How do you work with these security descriptors encode?
- 20:59Speaker 2: For named objects like files or register keys, you use
- 21:01Speaker 2: get named security info and set name security info. The
- 21:04Speaker 2: book show is getting the owner of a file. Remember
- 21:06Speaker 2: not to free the return buffer yourself. For kernel objects
- 21:09Speaker 2: like Mutex's, it's get kernel object security and set kernel
- 21:12Speaker 2: object security.
- 21:13Speaker 1: What are the default permissions?
- 21:14Speaker 2: Usually unnamed kernel objects often get a NL DACL, meaning
- 21:18Speaker 2: everyone has full access. Named objects usually inherit a default
- 21:22Speaker 2: DACL based on the creator's token, and if you want
- 21:25Speaker 2: the standard window security UI there's the edit security API,
- 21:28Speaker 2: but it requires implementing a calm interface I Security information,
- 21:32Speaker 2: which is more involved.
- 21:33Speaker 1: Right. Okay, switching to UAC User account control still relevant today,
- 21:38Speaker 1: very much so.
- 21:39Speaker 2: Its goal was to get people out of the habit
- 21:41Speaker 2: of running as admin all the time, even if you
- 21:44Speaker 2: are an administrator. UAC means most of the things run
- 21:46Speaker 2: with a standard user token.
- 21:47Speaker 1: By default, which has fewer privileges than the full administrator token.
- 21:51Speaker 2: You also get exactly to use the full admin token.
- 21:54Speaker 2: A process needs to be.
- 21:55Speaker 1: Elevated, and the standard way to trigger elevation is.
- 21:58Speaker 2: Using shell execute or shell execute x with the runous
- 22:02Speaker 2: verb that tells the system this needs admin rights. Thep
- 22:06Speaker 2: info service then usually prompts the user with that consent dialogue,
- 22:09Speaker 2: consent dot ex. If approved, the process starts with the
- 22:12Speaker 2: full admin token. Figure sixteen twenty shows this flow.
- 22:16Speaker 1: What about UAC virtualization That sounds like a compatibility.
- 22:19Speaker 2: Thing it is. It's mainly for older thirty two bit
- 22:22Speaker 2: apps that weren't written with UAC in mind and try
- 22:25Speaker 2: to write two protected locations like program files or h
- 22:28Speaker 2: KLM in the registry.
- 22:29Speaker 1: What does virtualization do?
- 22:31Speaker 2: It transparently redirects those rights to per user locations under
- 22:35Speaker 2: users username, app beta local virtual store. The app thinks
- 22:38Speaker 2: it wrote to the protected location, but it actually wrote
- 22:41Speaker 2: to the user's private virtualized copy. Keeps the app working
- 22:44Speaker 2: without needing admin rights.
- 22:45Speaker 1: Usually, can you see if it's active?
- 22:47Speaker 2: Yeah, Task Manager and process explore can show the UAC
- 22:50Speaker 2: virtualization status for a process enabled, disabled, or not allowed.
- 22:55Speaker 2: Figures sixteen to eight and sixteen to nine illustrate.
- 22:57Speaker 1: This clever workaround. Okay, then integrity levels another layer on
- 23:01Speaker 1: top of standard permissions.
- 23:03Speaker 2: Kind of it's mandatory integrity control. Processes and objects have
- 23:07Speaker 2: an integrity level, typically low, medium, high, or system. Most
- 23:11Speaker 2: standard apps run at medium, Elevated apps run at high system,
- 23:15Speaker 2: services run at system sandbox. Things might run it low.
- 23:19Speaker 1: Viewable in Process Explorer too, yep.
- 23:21Speaker 2: Figure sixteen twenty two shows it. The key rule is
- 23:24Speaker 2: the no write up policy. By default, a lower integrity
- 23:27Speaker 2: process generally cannot modify a higher integrity object.
- 23:30Speaker 1: So a medium integrity web browser can't easily write to
- 23:34Speaker 1: files owned by a high integrity process.
- 23:36Speaker 2: Correct or even modify the high integrity process itself. In
- 23:39Speaker 2: memory access is restricted. By default. Objects get medium integrity
- 23:44Speaker 2: unless they have a specific mandatory label ace setting it. Otherwise,
- 23:48Speaker 2: you can lower a processes integrity, but raising it needs
- 23:51Speaker 2: a special privilege seria label privilege that users don't normally have.
- 23:55Speaker 1: Interesting. The book also mentioned specialized things like control flow
- 23:58Speaker 1: guard CFG.
- 24:00Speaker 2: Yeah. CFG is a compiler linker OS feature to mitigate
- 24:03Speaker 2: exploits that try to hijack the program's control flow. It
- 24:05Speaker 2: validates targets of indirect calls against a bitmap of known
- 24:08Speaker 2: valid function entry points. If the target isn't valid, the
- 24:11Speaker 2: process terminates dumb, and CFG can show if a binary.
- 24:15Speaker 1: Uses it and process mitigation policies.
- 24:17Speaker 2: These are extra security arding options you can enable system
- 24:20Speaker 2: wide or per process using tools like g flags X
- 24:23Speaker 2: or specific APIs, things like blocking nonsystem fonts, preventing child
- 24:28Speaker 2: process creation, or restricting system calls. Figure sixteen twenty nine
- 24:32Speaker 2: shows using g flags sx to disable win thirty two
- 24:35Speaker 2: K calls for Notepad, causing it to fail.
- 24:38Speaker 1: Wow. Lots of layers, Okay, Final technical section, Debugging and diagnostics.
- 24:42Speaker 1: How do we figure out what's really going on?
- 24:45Speaker 2: Crucial tools here? One simple one is output debugstring. Your
- 24:48Speaker 2: application sends text messages and a debugger can pick them up.
- 24:51Speaker 1: What if no debugger is attached.
- 24:53Speaker 2: Tools like c's internals debug you or the booksdwin viewer
- 24:57Speaker 2: can monitor these messages system wide. The use a shared
- 25:00Speaker 2: memory buffer and an event. The OS signals when new
- 25:02Speaker 2: messages arrive.
- 25:03Speaker 1: Okay, then performance counters getting metrics on CPU, memory, etc.
- 25:07Speaker 2: Right. The PDH Performance Data Helper API is the way
- 25:10Speaker 2: to go. Use pd open query than PDH ad counter
- 25:13Speaker 2: to specify which counters you want, like percent process or
- 25:16Speaker 2: time for a specific process.
- 25:17Speaker 1: Then collect the data yep.
- 25:19Speaker 2: PDH collect query data, samples the values, and PDHKT formatted
- 25:23Speaker 2: counter value gives you the result as a nice string
- 25:25Speaker 2: or number. The book shows getting CPU usage for all
- 25:29Speaker 2: processes using pdhkt formatted counter array. Sometimes you might want
- 25:33Speaker 2: pdhh crow counter value for raw data before calculations.
- 25:37Speaker 1: Useful for performance tuning. What about process snapshotting PSS?
- 25:41Speaker 2: PSS is cool. It lets you capture a consistent point
- 25:43Speaker 2: in time snapshot of a process is state, memory, threads, handles,
- 25:48Speaker 2: et cetera into a file or memory buffer.
- 25:50Speaker 1: Why would you do that?
- 25:51Speaker 2: Great for offline analysis of crashes or hangs. You grab
- 25:54Speaker 2: a snapshot when the problem occurs, then analyze it later
- 25:57Speaker 2: without holding up the live system. Scapture snapshot takes the
- 26:00Speaker 2: snapshot ps free snapshot cleans up. Table twenty to one
- 26:03Speaker 2: lists the flags for what data to.
- 26:04Speaker 1: Include okay now. ETW Event Tracing for Windows sounds very powerful.
- 26:09Speaker 2: Is the high performance tracing framework in Windows, the OS
- 26:12Speaker 2: and many applications. EMIT detailed events via ETW providers identified
- 26:16Speaker 2: by GEDs logman quaria providers shows you who's registered.
- 26:19Speaker 1: How do you capture these events?
- 26:21Speaker 2: You set up an ETW session using star trace. Then
- 26:24Speaker 2: you tell the session which providers to listen to using
- 26:26Speaker 2: enable trace x, often filtering by severity, level or keywords.
- 26:30Speaker 2: The events usually go to an ETL file.
- 26:32Speaker 1: And then you analyze the ETL file.
- 26:34Speaker 2: Right using tools like event Viewer for simple logs or
- 26:37Speaker 2: Windows Performance Analyzer WPA for really deep analysis. WPA is
- 26:41Speaker 2: incredibly powerful for correlating events across the system. Figures twenty
- 26:45Speaker 2: thirteen and twenty fourteen show these tools.
- 26:47Speaker 1: Can you process events in real time?
- 26:50Speaker 2: Yes? You use open trace with process racing meulial time
- 26:54Speaker 2: and invide a callback function. Your callback gets invoked for
- 26:56Speaker 2: each event as it happens.
- 26:58Speaker 1: How do you make sense of the event data?
- 27:00Speaker 2: The callback receives an event record. You use the Trace
- 27:03Speaker 2: Data Helper tdhapis like PDH get event information and TDH
- 27:08Speaker 2: format property to parse the event's metadata and format its
- 27:11Speaker 2: specific data fields based on the provider's manifest. The book
- 27:14Speaker 2: shows parsing and displaying event details.
- 27:16Speaker 1: It even mentions the kernel provider.
- 27:19Speaker 2: Yeah, the Windows kernel trace provider emits tons of low
- 27:22Speaker 2: level OS events about processes, threads, disco, networking, et cetera.
- 27:27Speaker 2: Requires admin rights. Obviously, you can even create your own
- 27:30Speaker 2: custom ETW providers for your applications.
- 27:33Speaker 1: Extremely versatile. Lastly, actual debuggers, the tools that let you
- 27:38Speaker 1: step through code right.
- 27:39Speaker 2: Windows provides APIs for one process to debug another. You
- 27:43Speaker 2: can attach to a running process using debug active process.
- 27:46Speaker 2: You just need its pig.
- 27:48Speaker 1: Or start a process under the debugger YEP.
- 27:50Speaker 2: Use create process with the debug process or debugul this
- 27:53Speaker 2: process flag. The debugger then gets notified of events in
- 27:56Speaker 2: the debuggy via the debug zet structure. What kind of
- 27:59Speaker 2: events exception, thread creation, exit process, exit DLL loads some
- 28:03Speaker 2: loads output from up with debug string Table twenty to
- 28:06Speaker 2: five lists them. The debugger calls wait for debug event
- 28:09Speaker 2: to get the next event, processes it, and then calls
- 28:11Speaker 2: continued debug event to let the debuggy run again.
- 28:14Speaker 1: The book has a simple debug example.
- 28:15Speaker 2: Yeah, shows the basic loop wait for event, check the
- 28:18Speaker 2: event type like output string er, DLL, load, print info continue.
- 28:21Speaker 2: It's the fundamental structure of a debugger.
- 28:23Speaker 1: Okay, we have covered an incredible amount of technical ground there.
- 28:26Speaker 2: We really have, from the depths of memory management and DLLs,
- 28:30Speaker 2: through the layers of security right up to debugging and
- 28:34Speaker 2: tracing with ETW. The full book also dies into CALM
- 28:37Speaker 2: and win RT, which are foundational.
- 28:39Speaker 1: To and hopefully for you the learner. This deep dive
- 28:43Speaker 1: has connected some dots, provided those aha moments, and maybe
- 28:48Speaker 1: demystified some of these complex Windows internals.
- 28:51Speaker 2: Yeah, hopefully gives you that solid footing. So maybe next
- 28:53Speaker 2: time you're using your computer and something interesting happens or
- 28:56Speaker 2: an application behaves in a certain.
- 28:58Speaker 1: Way, you can think about these mechanisms underneath. How is
- 29:01Speaker 1: memory being used, how are these components talking, what security checks.
- 29:04Speaker 2: Are happening exactly? What new questions does this raise for
- 29:07Speaker 2: you about the software you interact with every day. Something
- 29:11Speaker 2: to chew on.
- 29:12Speaker 1: A great thought to end on. Thanks for joining us
- 29:14Speaker 1: on the deep dive.