How to set other threads to the alertable state

If they wish to see it, they can go to phrack, which is I believe is from
where this strain originates.

http://www.phrack.org/issues.php?issue=62&id=6

That you can query this is not a good sign: *((unsigned char
*)TargetThread + 0x4a) = 1;

mm

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@osr.com
Sent: Thursday, March 31, 2011 5:45 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to set other threads to the alertable state

Does anyone other than me think this code example doesn’t belong on this
site?

I grant you, it’s a slippery slope once we start deleting posts from
people… I usually reserve it for only the most egregious violations. But
I don’t like this “inject some code from kernel mode and change the APC
state of the target thread” thing one bit.

If I’m the only one who thinks this example is nasty, I’ll leave it in the
thread. If enough people agree, I’ll remove the text (and leave the post)
using my magic powers.

Peter
OSR


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

As opposed to the really sound ways to use code that does stuff like this:

*((unsigned char *)TargetThread + 0x4a) = 1;

mm

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@hotmail.com
Sent: Thursday, March 31, 2011 6:01 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to set other threads to the alertable state

Hello,

remove the code if you want and think other people will use it in a
malicious way.

regards
michi


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

> Your service can open Explorer.exe via OpenProcess, create a chunk of code on the fly in

it using WriteProcessMemory, then SetThreadContext any thread to execute this code, and the code
will do LoadLibrary. A good old DLL injection stuff.

Actually, much easier way to do it is just to create a thread in the target process with LoadLibrary() as a function address. This trick had been described by J. Richter ages ago…

But, if your software will be blocked by antivirus/security tools, you should not complain :slight_smile:

Actually, you should…

Windows security model is based upon the notion of a user account ( I mean kernel-object one - then it comes to sending window messages the security boundary is desktop object). Any process that runs under a given user account is allowed to do whatever it wants with the processes that run under the same user account, including creation of threads, modification of their execution context, modification of the address spaces of other processes,etc.

In order to inject a DLL into the target process you need to open a handle with the appropriate rights to it, and the rest is handled by the means of well-documented Win32 calls. Therefore, any product that tries to block it is, in fact, blocking the functionality that any program running under Windows should be allowed to have. You should realize that there are no hacks involved here whatsoever.

What the OP tries to do here is basically different trick - it is based upon undocumented KeXXXApc() calls (btw, there is nothing particularly new about this trick - it had been described on rootkit.com ages ago), so don’t get surprised if you get “strange” results at some point…

Anton Bassov

Hello,

As opposed to the really sound ways to use code that does stuff like this:
The code “*((unsigned char *)TargetThread + 0x4a) = 1;” is the same as “TargetThread->ApcState.UserApcPending = TRUE;” but in a stupid hardcoded way.
I have never said that this code is OK!! The question in this tread was if it is possible to set the UserApcPending flag to true in a normal way (and not in a hacking or hardcoded way).

I also don’t understand why this sample code is so a big problem. If a programmer can create a windows driver then this programmer has also enough skills to create malicious software. We can do nothing to prevent this. If the programmer has not enough skills, then the programmer is also not able to compile this code. You know that it is not enough to copy and past the code.
One good thing for driver writer: on the new 64Bit operating system a driver normally contains no malicious code because it must be signed. Creating malicious code in windows driver is not interesting anymore.

I have searched only a solution to load a normal dll in the windows explorer and this in the kernel mode.
If there exist a clean way to set the UserApcPending flag, then the above code maybe is not so bad to load a dll.

regards
michi

IIRC an explorer shell extension can be loaded by limited users. This is
normal functionality and exactly how the fancy shell menus for .zip, .pdf,
etc. files are generated.

It seems to me that the important point you have missed is that explorer
itself will quite happily load almost ANY DLL that is registered as an
extension without you having to do anything at all. This will give you the
opportunity to execute in the user’s context and capture whatever kind of
events you need. These can be communicated back to your service thru an IPC
method of your choice or sent via your driver assuming an interface with an
appropriate SD (DACL). If I have missed that mark on what you are trying to
do, I hope you will correct me.

While it is possible to spawn a process / inject a DLL from KM, it is
extremely unwise a practice as it is contrary to the OS design. In Windows,
and classic OS theory, the kernel and any drivers exist only to serve
requests from UM processes and so should never instigate work in their own
right. This doesn’t preclude HW state driven changes of any kind, but
implies that the notifications exists to serve UM processes. I am not sure
what functionality your application is designed to provide, but you would be
well advised to keep these classic design principals in mind because then
your tasks will be easier

wrote in message news:xxxxx@ntdev…

Hello,

Thank you very much for all the information and possible solutions.

I will check for a solution in the user mode.

But there is a reason why I wanted to load the dll from the Kernel and not
from the user mode:
It should be also possible to install the dll if the service is not running
and the user is on a restricted user account. The driver runs all the time
but the service can but must not run. So only the driver has enough
privileges to load or unload the dll.

regards
michi

xxxxx@hotmail.com wrote:

I also don’t understand why this sample code is so a big problem. If a programmer can create a windows driver then this programmer has also enough skills to create malicious software.

Oh, that is SO not true. The samples in the WDK and the approachability
of KMDF have made it quite possible for unsophisticated users to create
and play with their own drivers.

Mature individuals know enough not pull any of this kind of crap.
Dedicated antisocial malware hackers will always find a way, but there
are relatively few of those in the world. The danger of including code
like that is that it encourages copycat script kiddies in their college
dorm rooms to put together code they don’t really understand, which
spreads havoc throughout the known universe.

–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

As an admin, to the extent to which you can control the proliferation of
this, I hope that you do

wrote in message news:xxxxx@ntdev…

Does anyone other than me think this code example doesn’t belong on this
site?

I grant you, it’s a slippery slope once we start deleting posts from
people… I usually reserve it for only the most egregious violations. But
I don’t like this “inject some code from kernel mode and change the APC
state of the target thread” thing one bit.

If I’m the only one who thinks this example is nasty, I’ll leave it in the
thread. If enough people agree, I’ll remove the text (and leave the post)
using my magic powers.

Peter
OSR

@NTDEV
explorer shell extension

I have created and tested already some explorer shell extensions.
The shell extensions like zip, .pdf, and so on get only loaded if the user opens a menu. You can test it by killing the explorer and then check with the process explorer the loaded modules.
They don?t get loaded if windows explorer restarts or starts. I have tested it on Windows 7 64 SP1.
Sorry but at the moment I have not found a way to load a shell extension without user interaction. If someone knows an example it would be nice.

@Source code trouble
Sorry for my post. Please delete the source. I can not delete it be myself. The admin must delete it.
It was not my intension to create trouble.

regards
michi

When you install a driver, the INF can also install and start (in Vista+, anyway) an usermode service. Depending on the installation time (if it happens very early in the boot for a newly detected device), the INF may not be able to modify HKLM\SOFTWARE hive, but I may be wrong. If your driver is purely software and installed when Windows is fully functional, the INF can create keys in HKLM\SOFTWARE tree.

If you want a service to perform thing in the user account context, you can either have a helper application started when an user logs in, or have a service obtain the logged on session token and start a process with that token. I believe you can also use new (Vista+) task scheduler capability to inject a helper process to the user session.

Use of a helper process is a standard approach that’s used by many other packages.

Now, I’m not even sure you need a driver.

Hello,
thank you every one for the help.

I will use the following simple code the load the DLL in the explorer.
(special thanks to @“anton bassov” and @“Maxim S. Shatskih” and every one else for the idea)

This code will also work on a no admin user account. I can call this from service or from a little helper app on the restricted user account.

regards
michi


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void usage(void);
void apierror(void);

int main(int argc, char *argv)
{
HANDLE hProcId;
HANDLE hlib;
LPVOID ipbase;
HMODULE kernel32;
FARPROC proc;

int libstrlen;
char *procname = “LoadLibraryA”;
char *modulename = “kernel32”;
char *lib = “E:\mylib.dll”;
if(argc > 1) {
if(argc == 2) {
if(!(hProcId = GetCurrentProcessId())) {
apierror();
return;
}
}
else {
hProcId = atoi(argv[2]);
}
if(!(hProcId = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE,NULL,hProcId))) {
apierror();
return;
}
lib = argv[1];
libstrlen = (lstrlen(lib) + 1);
if(!(ipbase = VirtualAllocEx(hProcId,NULL,libstrlen,MEM_COMMIT,PAGE_READWRITE))) {
apierror();
return;
}
if(!(WriteProcessMemory(hProcId,ipbase,lib,libstrlen,NULL))) {
apierror();

return;
}

kernel32 = GetModuleHandle(modulename);
if(!(proc = GetProcAddress(kernel32,procname))) {
apierror();
return;
}
if(!(CreateRemoteThread(hProcId,NULL,0,proc,ipbase,0,NULL))) {
apierror();
return;
}
return 0;
}
else {
printf(“Invalid number of arguments provided”);
usage();
}
}

void usage(void) {
printf(“\nUsage:\n Dllinector.exe "library pathname" "PID"\n”);
}

void apierror(void) {
DWORD errorId;
unsigned char errorMsg[512];
errorId = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM+FORMAT_MESSAGE_IGNORE_INSERTS,NULL,errorId,NULL,&errorMsg,512,NULL);
printf(“Error Occured: %s”,errorMsg);
}</windows.h></stdlib.h></stdio.h>

Docs for CreateRemoteThreadEx mention that CreateRemoteThread will fail if issued for a target process in a different session. As of Vista+, the services run in a session different than logged on users’.

My $0.02c … information, by it’s very definition, serves to “inform” people and hiding it only serves to negate that effect. Regardless of the personal opinions of others as to the value or efficiency of the code, by posting that code (which, as others have pointed out, can easily be found going all the way back to a Dr Dobbs article about a decade ago) and then following with a discussion of why that may *not* be the best idea we serve to “inform” future writers about this technique – the function of the list, yes?

By deleting/ hiding/ ignoring this code we do nothing to help future writers who may/ will/ likely have already discover this technique, find the Phrack/ Dr Dobbs/ [fill in other source here] discussion and without the counterbalance provided here simply believe that “if nobody said it was bad, then it *must* be good!” … which again is the function of the list, yes?

Finally, there *are* valid places to use kernel level APC’s: the KeXXXApc functions are used throughout the MS codebase, aren’t KiXXXApc, are linkable with a simply header definition and, like a shared memory segment between userspace and kernelland, have a role in development. The trick is to know when and where to use that technique, something we have good opportunity to do here without bashing the OP burying all evidence of any discussion …

There are win32 apis to get you into the right session.

Mark Roddy

On Thu, Mar 31, 2011 at 10:33 PM, wrote:
> Docs for CreateRemoteThreadEx mention that CreateRemoteThread will fail if issued for a target process in a different session. As of Vista+, the services run in a session different than logged on users’.
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
>

>the installation time (if it happens very early in the boot for a newly detected device), the INF may not be able to

modify HKLM\SOFTWARE hive

INF processing never occurs this early.

–
Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

Sure. But (a) we don’t have to HELP them do so, and (b) we don’t have to post code that totally sucks that might lead people to believe what they’re reading is useful or recommended practice (that’s what CodeProject is for, after all).

Peter
OSR

>>the installation time (if it happens very early in the boot for a newly
detected device), the INF may not be able to

>modify HKLM\SOFTWARE hive

INF processing never occurs this early.

It may, if you happen to boot first time from different hardware path, for example AHCI instead of legacy IDE mode.

>>INF processing never occurs this early.

It may, if you happen to boot first time from different hardware path, for example AHCI

I think CriticalDeviceDatabase is used in this case instead of INF, and, if you boot this way, then later INF will still be processed (installing the boot HBA driver fully) and reboot will be requested.

–
Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

wrote in message news:xxxxx@ntdev…
…
>> But, if your software will be blocked by antivirus/security tools, you
>> should not complain :slight_smile:
>
> Actually, you should…
>
> Windows security model is based upon the notion of a user account ( I mean
> kernel-object one - then it comes to sending window messages the security
> boundary is desktop object). Any process that runs under a given user
> account is allowed to do whatever it wants with the processes that run
> under the same user account, including creation of threads, modification
> of their execution context, modification of the address spaces of other
> processes,etc.
>
>
> In order to inject a DLL into the target process you need to open a
> handle with the appropriate rights to it, and the rest is handled by the
> means of well-documented Win32 calls. Therefore, any product that tries to
> block it is, in fact, blocking the functionality that any program running
> under Windows should be allowed to have. You should realize that there are
> no hacks involved here whatsoever.

You should complain, complain in vain.
Windows secutity model was created in the past century. The very existence
of
all those antiviruses and “security tools” is a proof that it is not
relevant. At least for a client OS.
Few examples:

1. MS believed that a PC users should be able to access all their devices,
USB drives, CDs,
printers, bluetooth, wi-fi.
In reality: people beg to restrict USB devices, hide BT, SATA ports and so
on.

2. MS believed that user can run any executable on their machine.
In reality: they had to introduce “security zones” over the API, and
distinguish
normal files from internet cache, downloaded files etc. Then - integrity
levels.

3. … No, any app cannot open user’s address book and send mails on their
behalf.

4. … No, any app cannot create code on stack and execute it.

5. No, any driver cannot plug into a DRM’ed chain.

IT always wants more policy and locking down, so blocking or restricting
another API is not something new.
It may create another interop issue, so what. Users always get what their IT
wants.

–pa

Pavel:

An approach of open environment is biting MS in the behind. Any application sees the whole registry and filesystem, and can mess with other applications. Any process can mess with other processes of the same login session. Shared components are still causing DLL hell. A compromise of any application is a compromise of the whole logon session and of all the data (including total loss or theft) of the compromised user. If such an user has admin privileges (just like a default user up to and including Windows 7). Any installed application gets equal permissions.

Microsoft needs to follow iOS example by vetting and sandboxing the apps (at least never allow administrative privileges to code without MS signature or domain signature).

> -----Original Message-----

From: xxxxx@lists.osr.com [mailto:bounce-447796-
xxxxx@lists.osr.com] On Behalf Of xxxxx@broadcom.com
Sent: Saturday, April 02, 2011 6:09 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] How to set other threads to the alertable state

Microsoft needs to follow iOS example by vetting and sandboxing the apps
(at least never allow administrative privileges to code without MS signature
or domain signature).

Please don’t tell such things. It is enough they totally ruined WM and made it useless, do you want the same for ‘big’ Windows?

Michal

NOTE: The information in this message is intended for the personal and confidential use of the designated recipient(s) named above. To the extent the recipient(s) is/are bound by a non-disclosure agreement, or other agreement that contains an obligation of confidentiality, with AuthenTec, then this message and/or any attachments shall be considered confidential information and subject to the confidentiality terms of that agreement. If the reader of this message is not the intended recipient named above, you are notified that you have received this document in error, and any review, dissemination, distribution or copying of this message is strictly prohibited. If you have received this document in error, please delete the original message and notify the sender immediately.
Thank You!
AuthenTec, Inc. http://www.authentec.com/