Monday, August 20, 2007

Where Microsoft Knowledge Base come from ?

Add to Del.icio.us

Today I have realized that I have a special visitor on my blog. Why was that visitor so special ? Take a look here at the detailed statistics.

I was wondering what this guy was looking for. Maybe he is trying to configure a VMware machine to use WinDBG. But the question is why they don't use Virtual PC instead.
Or maybe they are trying to improve the compatibility between WinDBG and VMware ? Also I wonder from where exactly did they come from because there is no referral link. Anyway he didn't use msn.com search engine because MSN didn't indexed my blog.

BTW: My timezone is GMT+2 so the time is in the work hours there.

Sunday, March 11, 2007

Ring 0 Debuger

Add to Del.icio.us

One of the first useful things you will want to do when in the bowels of ring 0 is attack the thing from a debugger point of view. In my case I like using Windows Debugger [windbg] (hey its free, fully functional and does remote debugging really well), and found it neccessary to find a way to have it work with VMWare.
The trick to get it to work in VMWare is to get the host OS to believe it is able to connect to a serial port. Through VMWare, to accomplish this, you need to create a named pipe. The following steps will guide you to configuring VMWare

  1. Open the virtual machine
  2. Click "Edit virtual machine settings"
  3. On the hardware tab click "Add"
  4. Select a "Serial Port"
  5. Select "Output to named pipe"
  6. Set the name to "\\.pipe\com_1".
  7. Select "This endi is the server"
  8. Select "The other end is a application" and click next
  9. Activate the option "Yeld CPU on pool" This is an important step as the Kernel in the target virtual machine uses the port in polled mode, not interrupt mode.
  10. Check if everything is OK. It should look like this
  11. Start the virtual machine
  12. Once you have configured your VMWare session, you need to power it on. You will be able to confirm that the new virtual serial port is added by clicking on the Devices->serial0 menu item. You should note it is saying "Connecting". This means its now ready for a remote connection.

    Once the actual virtual machine is configured, you need to configure the target operating system installed in the virtual machine to support remote debugging. This is accomplished by editing the boot.ini found in the root of the C:\ drive. To do this, you need to add a

    /debugport=some_com_port /baudrate=some_baud_rate
    to the end of an [operating systems] line. I would not recommend doing it to the one that is there. It would be better to copy the line and paste it with the ammendments, and then use the OS's menu selection to determine which mode you would like to boot into. This is what my boot.ini looks like for XP Pro as the operating system in the virtual machine:


    [boot loader]
    timeout=30
    default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS
    [operating systems]
    multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Windows" /fastdetect /debug /debugport=COM1 /baudrate=115200
    multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /fastdetect


    You will notice that the second option sets the debugport to com1, and sets the baudrate to 115200. I am told you can tweak this out to get even more speed out.


  13. If you set up your boot.ini fine, save it and reboot. You should be prompted with something that looks like this:
  14. At this point you have completed setting up the VMWare side of things. Now you need to set up the host to connect to it. This is actually rather easy. You just need to tell windbg at the command like to connect to the pipe, like this:

    windbg -k com:port=\\.\pipe\com_1,pipe
    If you are using WinDbg 6.x or newer, a better alternative is to use:
    windbg -b -k com:pipe,port=\\.\pipe\com_1,resets=0
  15. Enjoy

Add to Del.icio.us

Terminology
One of the areas that takes some getting used to is the terms specific to the sub systems of Windows, and how that relates to driver development. Originally I thought I would just keep my books around me, but instead decided to post it here. If the original authors get upset with this (I have emailed them to see, but no responses yet) I will remove them. I doubt there will be a problem, but I don't find plagorism to be the most sincere form of flattery, and will abide by their wishes if they feel otherwise. Note: When we talk about "NT" in these explainations, it refers to all Microsoft Operating Systems that use NT as the core (NT, W2K, XP, Windows Server 2003 etc). Most of the definitions / descriptions come from books, web sites with slight modifications for clarity.

The HAL
The lowest layer within the operating system is the hardware abstraction layer (HAL). This layers deals directly with the hardware of the machine, and as its name implies, HIDES the complexities to layers above it. The HAL DLL contains the processor/machine specific details that would otherwise be exposed to the system. In this way, the operating system can be more portable across platforms through the use of a different HAL for each platform.

The Kernel
The kernel is the brain child of the system that provides all the essential services such as scheduling, interrupt dispatching, multi-processor synchronization etc. All other components of the NT core are preemptive. This allows for a more robust design and make NT somewhat unique as few operating systems have their core multithreaded. (ie: you can have more than one thread running in a priviledged mode)

The NT Executive
The NT executive constitutes the majority of the Windows NT core. It sits on top of the kernel and provices a complex interface to the "outside world". The NT executive forms the part of the Windows NT core that is fully preemptive. Generally, the core components added by developers form a part of the NT executive or rather the I/O Manager. Hence, driver developers should always keep in mind that their code has to be fully preemptive.

The Object Manager
Windows NT is designed in an object-oriented fashion. Windows, devices, drivers, files, mutexes, processes, and threads have one thing in common: All of them are treated as objects. The main tasks of the Object Manager is to control:
  • Memory allocation/deallocation for objects
  • Object name space maintenance.
  • Handle maintenance. (The handle returned when an object is opened by a process)
  • Reference count maintenance.
  • Object security.

The I/O Manager
The I/O Manager controls everything related to input and output. It provides a framework that all the I/O-related modules (device drivers, file systems, Cache Manager, and network drivers) must adhere to.

The Security Reference Monitor
The Security Reference Monitor is responsible for validating a processs access permissions against the security descriptor of an object. The Object Manager uses the services of the Security Reference Monitor while validating a processs request to access any object.

The Virtual Memory Manager
The following two tasks are performed by the Virtual Memory Manager component of the operating system when it comes to the hardware memory:
  1. It provides a virtual machine, which is easy to program, on top of raw hardware, which is cumbersome to program. For example, an operating system provides services to access and manipulate files. Maintaining data in files is much easier than maintaining data on a raw hard disk.
  2. It allows the applications to share the hardware in a transparent way. For example, an operating system provides applications with a virtual view of the CPU, where the CPU is exclusively allotted to the application. In reality, the CPU is shared by various applications, and the operating system acts as an arbitrator.

The Process Manager
The Process Manager is responsible for creating processes and threads. Windows NT makes a very clear distinction between processes and threads. A process is composed of the memory space along with various objects (such as files, mutexes, and others) opened by the process and the threads running in the process. A thread is simply an execution context that is, the CPU state (especially the register contents). A process has one or more threads running in it.

The Local Procedure Call Facility
The local procedure call (LPC) facility is specially designed for the subsystem communication. LPC is based on remote procedure call (RPC), which is the de facto Unix standard for communication between processes running on two different machines.

The System Call Interface
The system call interface is a very thin layer whose only job is to direct the system call requests from the user mode processes to appropriate functions in the Windows NT core.

Understanding Ring 0
The operating system operates at several levels, or modes. The mode with the most privileges is Ring 0, which is also known as Kernel Mode. The name Ring 0 comes from the concept in which modes are visualized as a group of concentric circles around the CPU (picture those models of the solar system we all made in junior high school, especially the planets with rings of moons). The innermost circle is known as Ring 0, and any functions that are permitted to operate in Ring 0 are closer to the CPU and have more direct access to the CPU functions. These are what we call low-level services.

Software applications do not have access to Kernel Mode; instead, they run in User Mode. In User Mode, applications dont have direct access to any functions, and must request services from the operating system. This, of course, is slower than grabbing whatever you need without having to ask the operating system.

Depending on the operating system or how you want to describe the various levels of access to the CPUs features, you can define operating systems as having three Ring levels or two Ring levels. For Windows 95, Windows 98, and Windows NT, we usually discuss only two Ring levels, which are translated as Kernel Mode and User Mode. As another example, however, OS/2 has three clearly defined Rings; some operating system functions are at Ring 0, others are at Ring 1, and applications operate at Ring 3.

Saturday, January 13, 2007

The ring0

Add to Del.icio.us

In computer science, hierarchical protection domains, often called protection rings, are a mechanism to protect data and functionality from faults (fault tolerance) and malicious behaviour (computer security). This approach is diametrically opposite to that of capability-based security.



Computer operating systems provide different levels of access to resources. A protection ring is one of two or more hierarchical levels or layers of privilege within the architecture of a computer system. This is generally hardware-enforced by some CPU architectures, that provide different CPU modes at the firmware level. Rings are arranged in a hierarchy from most privileged (most trusted, usually numbered zero) to least privileged (least trusted, usually with the highest ring number). On most operating systems, Ring0 is the level with the most privileges and interacts most directly with the physical hardware such as the CPU and memory.



Special gates between rings are provided to allow an outer ring to access an inner ring's resources in a predefined manner, as opposed to allowing arbitrary usage. Correctly gating access between rings can improve security by preventing programs from one ring or privilege level from misusing resources intended for programs in another. For example, spyware running as a user program in Ring 1 should be prevented from turning on a web camera without informing the user, since hardware access should be a Ring0 function reserved for device drivers. Programs such as web browsers running in higher numbered rings must request access to the network, a resource restricted to a lower numbered ring.