Starting a process from KernelMode

研究生课程笔记:软件包在流行病学中的应用(三)——STATA软件 关键词:STATA软件基本操作、STATA对于流行病学研究的实现、STATA对复杂抽样设计样本量计算的实现 一、STATA软件基本操作 这部分网上教程有很多,也可以直接根据stata自带的help功能进行自学,在此只记录课程中提及的功能项。 1. 变量编辑 1.1 赋值:label type(按精度区分) 1) 整型:int(32), short(16), long(32) 2) 实型:flo... 阅读详情

By Stan Alex

How to start a Win32 process from within a driver 

Sample Image - KernelExec.jpg

Introduction

After many unsuccessful attempts trying to find a way to start a working Win32 process from KernelMode, I finally stumbled upon a promising piece of code that was both original and innovative (Note: the ideea belongs to Valerino).

Unfortunately that code didn't seem to work properly on my machine, and it always ended up crashing it, or just a process (in the most fortunate cases). So I've decided to reimplement Mighty Valerino's ideea in my own way (although the structure of the code is basically the same).

So let's get started. First, you must know of...

APC's

APC, standing for Asynchronous Procedure Call, represents a kernel procedure that that is queued to a particular thread for execution. In other words it's a code that is forcibly executed in a thread's context (This method is mostly employed by the I/O Manager). That is the SIMPLEST explanation I can give you, and that's all you need to know for now. There are three types of APC's:

Kernel APC's - They can be queued to any kernel thread and they will execute only if the specified thread isn't already executing a Kernel APC.

Special Kernel APC's - Basically the same as above. They run at IRQL APC_LEVEL and cannot be blocked except by running at a raised IRQL. They can always preempt normal Kernel APC's.

User APC's - These are APC's that can be queued to UserMode threads, but there's a catch: the thread must have previously called a wait service like WaitForSingleObject with the Alertable field set to TRUE. The APC will be invoked next time the thread returns from KernelMode. This is the kind of APCs that we will be dealing with from now on.

Enough talking. Let's get to the fun part :)

Running that process

A brief description of the ideea behind starting a Win32 process is the following:

1. We loop through the list of running processes until we find Explorer.exe. Why explorer.exe? Because it's desktop-interactive service (I tried popping up a message box from WinLogon.exe and I could only hear the sound of it). It also has many waiting threads (both alertable and non-alertable), so it works best with this code.

2. Once we've found Explorer.exe we iterate through it's threads searching for an alertable thread. If no such thread is found, we simply save a pointer to a non-alertable one and set its ApcState.UserApcPending to TRUE, thus making it alertable (notice: in this case it usually takes a few seconds for the thread to return to KernelMode)

3. Now we have the PEPROCESS of Explorer.exe and one of its PETHREADs. Next we queue our APC object (which will contain the code to be executed in UserMode) and when it completes, we just release the memory that we have previously allocated for it. That's all.

The Implementation

The main procedure is RunProcess(LPSTR lpProcess) where lpProcess must be the FULL PATH to the application that is to be run ('c:/RawWrite.exe' in our example)

 

void RunProcess(LPSTR lpProcess)
{

  PEPROCESS pTargetProcess = NULL;//self explanatory
  PKTHREAD pTargetThread = NULL;  //thread that can be either
                                  //alertable or not
  PKTHREAD pNotAlertableThread = NULL;//non-alertable thread
  PEPROCESS pSystemProcess = NULL; //May not necessarily be the
                                   //'System' process

  PETHREAD pTempThread = NULL;
  PLIST_ENTRY  pNextEntry, pListHead, pThNextEntry; 

//...
}

We start off by retrieving a pointer to the 'System' process:

  pSystemProcess = PsGetCurrentProcess(); 
  //make sure you are running at IRQL PASSIVE_LEVEL
pSystemProcess->ActiveProcessLinks is a LIST_ENTRY field that contains links(pointers) to other processes ( PEPROCESS) running on the machine. Let's search for Explorer.exe and save a pointer to it and to one of its threads. (Note: you can queue an APC to ANY process, even CSRSS or SVCHOST, but the system will probably crash). Once we've got a pointer to Explorer.exe and one of its threads (i will not explain how to do that here) it's time to queue our APC to that thread:

 

if(!pTargetThread)
{
  //No alertable thread was found, so let's hope
  //we've at least got a non-alertable one
  pTargetThread = pNotAlertableThread;
}

if(pTargetThread)
{
  DbgPrint("KernelExec -> Targeted thread: 0x%p",
           pTargetThread);
  //We have a thread, now install the APC
  InstallUserModeApc(lpProcess, 
                     pTargetThread,
                     pTargetProcess);
}
InstallUserModeApc has the following prototype:

 

NTSTATUS 
InstallUserModeApc(
                   IN LPSTR lpProcess, 
                   IN PKTHREAD pTargetThread, 
                   IN PEPROCESS pTargetProcess);
where pTargetProcess points to the PEPROCESS of Explorer.exe and pTargetThread is the PKTHREAD which the APC will be queued to. Let's now allocate some memory for that APC and for an MDL (Memory Descriptor List) to map our UserMode code:

 

PRKAPC pApc = NULL;
PMDL pMdl = NULL;
ULONG dwSize = 0; //Size of code to be executed in Explorer's address space

pApc = ExAllocatePool (NonPagedPool,sizeof (KAPC)); 

dwSize = (unsigned char*)ApcCreateProcessEnd-
         (unsigned char*)ApcCreateProcess;
pMdl = IoAllocateMdl (ApcCreateProcess, dwSize, FALSE,FALSE,NULL);

//Probe the pages for Write access and make them memory resident
MmProbeAndLockPages (pMdl,KernelMode,IoWriteAccess);
Our APC is now valid and pMdl is memory resident and maps our UserMode code ( ApcCreateProcess() that is. We'll get to that later). So what now? Should we deliver our APC to the thread and watch our Win32 process run? No no no.. not so fast! :)

How is Explorer.exe's thread supposed to invoke our APC routine if it doesn't have access to the Kernel memory? It cannot do that! Fine then, then let's map our APC code to UserMode memory:

 

KAPC_STATE ApcState;

//Attach to the Explorer's address space
KeStackAttachProcess(&(pTargetProcess->Pcb),&ApcState);

//Now map the physical pages (our code) described by pMdl
pMappedAddress = 
  MmMapLockedPagesSpecifyCache(pMdl,
                               UserMode,
                               MmCached,
                               NULL,FALSE,
                               NormalPagePriority);
To continue, first I must show you how ApcCreateProcess (the code mapped to UserMode memory, into Explorer's address space) works:

 

__declspec(naked)
void ApcCreateProcess(
  PVOID NormalContext,
  PVOID  SystemArgument1,
  PVOID SystemArgument2)
{
  __asm 
  {
    mov eax,0x7C86114D
    push 1
    nop
    push 0xabcd
    call eax
    jmp end       
    nop
    nop
    //...about 400 nop's here
end:
    nop
    ret 0x0c
  }
}

void ApcCreateProcessEnd(){} 
//Used only to calculate the size of the code above
We move the address of WinExec into eax (0x7C86114D is its address, on WinXP SP2), we push 1 on the stack (SW_SHOWNORMAL) and then we push... 0xabcd before calling WinExec. Why 0xabcd you might ask? Well, push 0xabcd is the first parameter of WinExec and it points to the path of the application to be executed. But that means 0xabcd cannot possibly point to the path all the times!

Why don't you simply push lpProcess from RunProcess(LPSTR lpProcess) then? Answer - Because WinExec will NOT be able to access it and it will throw an 'Access Violation' at you! You cannot access Kernel memory from UserMode, remember? Instead, right after we map our code to UserMode memory, we copy the path to the location right after the first nop instruction (that's why there are so many nop's in there) and we modify 0xabcd to point to it. Now here's the code:

 

ULONG *data_addr=0; //just a helper to change the address of the 'push' instruction
                    //in the ApcCreateProcess routine
ULONG dwMappedAddress = 0; //same as above

pMappedAddress = 
         MmMapLockedPagesSpecifyCache(pMdl,
                                      UserMode,
                                      MmCached,
                                      NULL,FALSE,
                                      NormalPagePriority);
dwMappedAddress = (ULONG)pMappedAddress;

//zero everything out except our assembler code
memset ((unsigned char*)pMappedAddress + 0x14, 0, 300);

//copy the path to the executable
memcpy ((unsigned char*)pMappedAddress + 0x14,
        lpProcess,
        strlen (lpProcess));

data_addr = (ULONG*)((char*)pMappedAddress+0x9);//address pushed on the stack
                                                //(originally 0xabcd)...
*data_addr = dwMappedAddress+0x14; //gets changed to point to our exe's path
    
//all done, detach now
KeUnstackDetachProcess (&ApcState);
What's left now is to initialize the APC and queue it to the thread. I will not explain how KeInitializeApc and KeInsertQueueApc works as Tim Deveaux as already done that here.

 

  //Initialize the APC...
  KeInitializeApc(pApc,
                  pTargetThread,
                  OriginalApcEnvironment,
                  &ApcKernelRoutine, //this will fire after 
                                     //the APC has returned
                  NULL,
                  pMappedAddress,
                  UserMode, 
                  NULL);
    
  //...and queue it
  KeInsertQueueApc(pApc,0,NULL,0);

  //is this a non-alertable thread?
  if(!pTargetThread->ApcState.UserApcPending)
  {
    //if yes then alert it
    pTargetThread->ApcState.UserApcPending = TRUE;
  }

  return STATUS_SUCCESS;
}

Compiling the code

This is simple - do a cd sys_path, where sys_path is the path to the driver project, then run build -ceZ. Or just press the F7 key in MS Visual Syudio 6 :)

Now copy KernelExec.sys to your C:/ directory, run Dbgview to see the output of the driver then double-click on Start_KE_Driver.exe to install and start the driver. Et voila! RawWrite.exe's window should be on your screen right now! :-)

P.S.: Make sure you first put an application called RawWrite.exe in your c:/ directory, because that is what the driver attempts to run.

Stan Alex

基于STM32环境温湿度监测系统设计 基于STM32环境温湿度监测系统设计1. 实验目的2. 任务和要求3. proteus仿真图4. 实验代码 1. 实验目的 (1) 掌握使用 KEIL MDK5 进行软件开发的具体流程; (2) 掌握硬件电路的设计方法; (3) 掌握 STM32 微控制器的程序设计及代码编写方法。 2. 任务和要求 (1)在 STM32F103 平台下,通过温度传感器 DS18B20 和湿度传感器 DHT11 实现环境温湿度信息的采集; (2)将温度传感器和湿度传感器所采集的数据在 LCD 液晶屏幕上显示出来,超 阅读详情

相关推荐

目标检测数据集 第059期-基于yolo标注格式的水果李子检测数据集(含免费分享)

李子作为中国三大水果之一,种植历史超 3000 年,在市场中占据重要地位。传统李子种植、施肥、采摘依赖大量人力,存在资源浪费、产量估算不准、采摘时机难把控等问题。此前,农业领域缺乏针对 “未成熟李子” 的专用数据集,现有水果检测数据集多聚焦于成熟果实(如苹果、柑橘),且未覆盖李子种植的复杂自然场景。在使用该数据集进行学术研究时,应遵守相关的学术规范,引用该数据集的来源,尊重数据集创作者的劳动成果。,用于训练改进型模型,解决李子精准检测的核心痛点。

深度学习算法工程师,专注于前沿算法研究与实战应用。在这里,我将分享深度学习领域的实战经验、优质数据集资源以及最新论文解读与精度分析,旨在与广大技术爱好者共同探索人工智能的奥秘,助力技术成长与创新。 775

A problem occurred starting process 'command 'node''

ReactNative的热更新,微软提供了CodePush的解决方案,在Android集成时遇到了如下问题: Error:Execution failed for task ':app:recordFilesBeforeBundleCommandDebug'. > A problem occurred starting process 'command 'node'' node命令...

100斤的博客 6633

NRF24L01学习操作教程(二)——NRF实现一对一,一对多通讯

以上内容详细介绍了,使用NRF24L01实现1对1单向通讯、1对1双向通讯、1对多双向通讯。

Joseph Wen的博客 7402

Android-Studio 运行报错

1、Execution failed for task ‘:react-native-update:compileDebugNdk’ 路径:android->app->build.gradlew android { sourceSets.main { jni.srcDirs = [] } } 路径:android->gradle.properti...

刘荣丹的日志 615

从零构建EtherCAT从站:SSC工具配置与TwinCAT3 PDO/SDO调试实战

本文详细介绍了从零构建EtherCAT从站的完整流程,重点讲解SSC工具配置与TwinCAT3 PDO/SDO调试实战。通过解析EtherCAT通信机制、PDO与SDO的核心概念,提供SSC工具配置技巧、XML文件生成与校验方法,以及TwinCAT3环境下的调试经验,帮助开发者高效完成EtherCAT从站开发与优化。

weixin_42520573的博客 349

I2C总线原理及总线信号的实现

I2C总线是一种多主机总线,连接IIC总线上的器件分为主机和从机,主机有权发起和结束一次通信,而从机只能被主机呼叫;当总线上有多个主机同时启用总线时,I2C也具备冲突检测和仲裁的功能来防止错误产生;每个连接到IIC总线上的器件都有一个唯一的地址(7bit),且每个器件都可以作为主机也可以作为从机(同一时刻只能有一个主机),总线上的器件增加和删除不影响其他器件正常工作;I2C总线在通信时总线上发送数据的器件为发送器,接收数据的器件为接收器;主要用于近距离(抗干扰能力稍弱),低速的芯片之间的通信(传输视频,带宽

YLG_lin的博客 3948

mac react native android 报错 A problem occurred starting process ‘command ‘node‘‘

一、通过错误信息找到文件的下载地址,复制到浏览器中下载(虽然下载速度依旧乌龟,但总能下载完)

u010537398的博客 430

RM3100应用笔记驱动程序SPI接口

PNI磁传感器套件RM3100磁传感器套件由2个Sen-XY-f(pn13104)地磁传感器,1个Sen-Z-f(pn13101)地磁传感器和MagI2C(pn13156)控制芯片组成,能够实现三维空间的磁场大小测量。

AIoT技术栈的博客 4927

React Native遇见的几个坑

1. 环境变量配置解决办法:配置ANDROID_HOME,和JAVA_HOME。安装Android SDK,并安装Build Tool 23.0.1,最好把所有版本装上,因为将来使用的插件可能依赖的版本是不一样的。2. 执行cd android && ./gradlew assembleRelease任务错误* What went wrong:Execution failed for task '

清箫的专栏 1万+

MVDR算法: 自适应波束形成在信号处理领域中扮演着重要的角色。本文将介绍MVDR(最小方差无失真响应)算法,并提供相应的Matlab源代码实现。

MVDR算法是一种用于抑制干扰信号并增强感兴趣信号的自适应波束形成算法。它的基本思想是通过对干扰信号进行空间滤波,使其在感兴趣信号方向上的输出功率最小化。这种方法可以提高信号的质量和可辨识度,特别是在存在多个干扰源的复杂环境中。本文将介绍MVDR(最小方差无失真响应)算法,并提供相应的Matlab源代码实现。使用MVDR算法,我们可以通过对观测信号进行波束形成来抑制干扰信号并增强感兴趣信号。以上是MVDR算法的Matlab实现。的每一列代表一个阵列元素的观测信号。表示期望的波束指向,即感兴趣信号的方向。

HackWhisper的博客 1248

【服务器电源架构与关键技术发展趋势】深度解析架构、方案、玩家与未来趋势

技术维度:台达 / 光宝在高功率密度(97%+ 效率)和垂直整合上领先,大陆厂商(麦格米特 / 欧陆通)在成本控制和快速响应上更优,国际厂商(英飞凌)聚焦核心器件突破。 客户维度:台达绑定英伟达核心供应链,麦格米特 / 欧陆通兼顾国内外客户,光宝 / 中恒电气侧重云端与互联网厂商。 趋势适配:所有厂商均加速布局 800V 高压架构、液冷技术和高功率 Power Shelf,以应对 AI 服务器单柜功率从 10kW→200kW 的升级需求。

昔时扬尘处 730

Android 开发 Gradle 相关问题

问题汇总

kiku 2148

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Starting a Process from KernelMode

IntroductionAfter many unsuccessful attempts trying to find a way to start a working Win32 process from KernelMode, I finally stumbled upon a promising piece of code that was both original and innov

zskof的专栏 1332

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

react native 错误总结(不定时更新)

错误截图: 原因:lineHeight只能设置成整数  可以使用parseInt(23.7)进行取整操作

吴蕴强的博客 2793

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

启动报错A problem occurred starting process ‘command ‘C:/Program Files/Java/jdk1.8.0_221/bin/java.exe‘‘

A problem occurred starting process ‘command ‘C:/Program Files/Java/jdk1.8.0_221/bin/java.exe’’ 可能原因:java启动命令行太长 Shorten command line :改为JAR manifest或其他可选项,进行调试; Shorten command line者一列可能不存在,需要点击Modify options选择; ...

qq_45389465的博客 9894

Execution failed for task ‘:app:bundleReleaseJsAndAssets‘

window电脑换Mac后,RN项目android端打包的时候出现如上报错!解决方案:打开app下的gradle文件:添加node路径。

weixin_41036707的博客 967

从核心模式启动一个进程----怎样从内部的驱动程序启动一个Win32进程

从核心模式启动一个进程----怎样从内部的驱动程序启动一个Win32进程[Romania]Stan Alex著   [China]rxxi 译 文章原址:http://www.codeproject.com/useritems/KernelExec.aspDownload source files - 55 Kb Download demo project - 27 Kb

C/C++ World 5319

Android Studio问题解决:A problem occurred starting process ‘command ‘,CreateProcess error=2, 系统找不到指定的文件。

一、遇到问题 二、解决问题 重新下载了22.0.7026061和22.1.7171670 ndk.dir中修改了路径 clean+Sync Project 哦了,大功告成

5211

A problem occurred starting process 'command '

androidStudio运行出现:  A problem occurred starting process 'command 'E:\sdk\ndk-bundle\toolchains\mips64el-linux-android-4.9\prebuilt\windows-x86_64\bin\mips64el-linux-android-strip''解决方案: 在local.propert...

json_yi的博客 1万+

A problem occurred starting process 'command 'G:\Android\sdk\ndk-bundle\toolchains\mips64el-linux-an

如下图: 我这边出现这个是因为项目里面加入了模拟器的so库导致的,网上找到了一个简单的解决办法 方法一: 打开 local.properties,将ndk目录的目录加上后缀 .cmd, 例如 ndk.dir=G\:\\Android\\sdk\\ndk-bundle.cmd,  重新build就可以正常编译了 如上图:     方法二: 版本回退,  ndk版本在r17版,...

xiayiye5的博客 3158

grails3 A problem occurred starting process 'command 'C:\Program Files\Java\jdk1.8.0_101\bin\java.ex

A problem occurred starting process 'command 'C:\Program Files\Java\jdk1.8.0_101\bin\java.exe''

lvbaolin123的博客 1万+

Thread VS Process From Stackoverflow

Preface:翻译水平渣,阅读需仔细。有错误欢迎指正。What is the difference between a thread and a process? 线程和进程之间有何区别?Processes vs Threads 进程VS线程A process is an executing instance of an application. What does that mean? We

莫利斯安的博客 1133

android studio提示A problem occurred starting process 'command d:\...ndk-bundle\ndk-build.cmd'

A problem occurred starting process 'command d:\...ndk-bundle\ndk-build.cmd' 根据提示分析,可能是Sdk配置路径错误,查看当前配置的sdk路径,在此路径下搜索这个命令,没有找到这个命令,说明可能ndk没有安装 在SDKmanager中安装NDK

潇湘淑女 1万+

用APC实现在内核模式运行用户程序

//=====================================================================================////Name: void RunUserModeProcess()                                                              ////        

2400

React Native 常见错误汇总

1、./gradlew assembleRelease 打Android包的时候出错 * What went wrong: Execution failed for task ':app:bundleReleaseJsAndAssets'. > A problem occurred starting process 'command 'node'' 解决:执行 ./gradle...

pursue的博客 1062

ReactNative学习笔记十六之Android打包Apk

这篇文章主要是记录一下我在Android打包的时候遇到的两个坑。项目完成后,肯定首要任务是进行打包,打包就需要签名,如果对签名有疑问的朋友,可以参考我之前的一篇文章:www.jianshu.com/p/4da94c575…这里我只记述我在执行gradle assembleRelease时遇到的两个问题。 Couldn't follow symbolic link 执行gradle assemble...

weixin_33991727的博客 251
上一篇: Writing to the Internet Explorer Cache
下一篇: Hooking a DirectX/COM Interface
iiprogram
博客等级 码龄25年 1068粉丝 1515原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址