Simulating keypress

This is a discussion on Simulating keypress within the Windows Programming forums, part of the Platform Specific Boards category; Process.cpp Code: #include "Process.h" LPCTSTR ProcessPrivileges::Tcb = SE_TCB_NAME; LPCTSTR ProcessPrivileges::Debug = SE_DEBUG_NAME; LPCTSTR ProcessPrivileges::Backup = SE_BACKUP_NAME; LPCTSTR ProcessPrivileges::IncreaseQuota = SE_INCREASE_QUOTA_NAME; ...

  1. #16
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Process.cpp
    Code:
    #include "Process.h"
    
    LPCTSTR ProcessPrivileges::Tcb           = SE_TCB_NAME;
    LPCTSTR ProcessPrivileges::Debug         = SE_DEBUG_NAME;
    LPCTSTR ProcessPrivileges::Backup        = SE_BACKUP_NAME;
    LPCTSTR ProcessPrivileges::IncreaseQuota = SE_INCREASE_QUOTA_NAME;
    
    Process::Process(const HANDLE& processHandle)
    {
    	this->handle       = processHandle;
    	this->handleOpened = true;
    }
    
    Process::Process(const PROCESSENTRY32& processEntry)
    {
    	this->processEntry = processEntry;
    	this->handleOpened = false;
    }
    
    Process::Process()
    {
    }
    
    Process::~Process()
    {
    	if (handleOpened) CloseHandle(handle);
    }
    
    HANDLE Process::getHandle()
    {
    	if (!handleOpened) 
    	{
    		handle       = OpenProcess(PROCESS_ALL_ACCESS, FALSE, getPid());
    		handleOpened = true;
    	}
    	
    	return handle;
    }
    
    DWORD Process::getPid()
    {
    	return processEntry.th32ProcessID;
    }
    
    DWORD Process::getParentPid()
    {
    	return processEntry.th32ParentProcessID;
    }
    
    DWORD Process::getThreadsCount()
    {
    	return processEntry.cntThreads;
    }
    
    DWORD Process::getModuleId()
    {
    	return processEntry.th32ModuleID;
    }
    
    char* Process::getExeFilename()
    {
    	return processEntry.szExeFile;
    }
    
    bool Process::setPrivilege(LPCTSTR privilege, bool enable)
    {
    	LUID luid;
    	HANDLE token;
    	TOKEN_PRIVILEGES tokenPrivileges;
    
    	if (!OpenProcessToken(getHandle(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) return false;
    
    	if (!LookupPrivilegeValue(NULL, privilege, &luid)) return false;
    
    	tokenPrivileges.PrivilegeCount = 1;
    	tokenPrivileges.Privileges[0].Luid = luid;
    
    	if (enable) tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    	else        tokenPrivileges.Privileges[0].Attributes = 0;
    
    	AdjustTokenPrivileges(token, FALSE, &tokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
    
    	DWORD error;
    	if ((error = GetLastError()) != ERROR_SUCCESS)
    	{
    		std::cout << error;
    		return false;
    	}
    
    	return true;
    }
    
    HANDLE Process::getCurrentProcessHandle()
    {
    	return OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
    }
    
    DWORD Process::getCurrentProcessId()
    {
    	return GetCurrentProcessId();
    }
    Main.cpp:
    Code:
    #include <iostream>
    
    #include "ProcessEnumerator.h"
    
    int main()
    {
    	ProcessEnumerator enumerator;
    	Process           actual;
    
    	Process myProcess(Process::getCurrentProcessHandle());
    
    	myProcess.setPrivilege(ProcessPrivileges::Debug, true);
    
    	enumerator.getFirst(actual);
    
    	do
    	{
    		std::cout << "Process Name: " << actual.getExeFilename() << '\n';
    		std::cout << "Process ID  : " << actual.getPid()         << '\n';
    	} while (enumerator.getNext(actual));
    
    	std::cin.get();
    
    	return 0;
    }
    Everything just fine, debug level has been seted, now whats the next step? (Do i have to close any handle that i havent or anything else in the actual code? )
    Last edited by Scarvenger; 01-30-2008 at 01:02 PM.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Yes, debug privilege require admin. This can be set in the manifest to require your program to run as admin. If you have Visual Studio 2008, you should be able to set this setting via project properties I think.
    The next step is a little hush-hush. The easiest way to inject code is via a dll (though your set-up routine needs to be an application). This is a little tricky. First you have to allocate a string in the target process's memory. There are several ways of doing it. I'm not an expert, but VirtualAllocEx might be able to do the trick.
    Inside this memory, copy the complete filename to your dll.
    Then use CreateRemoteThread to create a remote thread in the target process. As for the start routine, you should use LoadLibrary and the argument should be the remotely allocated memory that points to your dll.
    The result is that the target process will load your dll into memory!
    From there, you can use the dll's init code to start a chain reaction of executing code inside the target application!

    More than this... I don't know. Actually, I haven't tested the above, but it should work, because there are tutorials out there that guides you through how to do it exactly like that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    1) Ok, once i have done this what should i do? I mean, i have my code running inside the process, why have i done that? If it is to overwrite somre memory i could have done this by simply using WriteProcessMemory couldn't i? (Correct me if i am wrong).
    2) How could i make the class Process more usefull as Elysia said?

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Quote Originally Posted by Scarvenger View Post
    1) Ok, once i have done this what should i do? I mean, i have my code running inside the process, why have i done that? If it is to overwrite somre memory i could have done this by simply using WriteProcessMemory couldn't i? (Correct me if i am wrong).
    Because otherwise you need to inject code into the process which is way trickier than forcing it to load a dll from which inside you can execute your own code.
    You're actually going to have to re-direct a certain DirectInput function call to return your own device. So you need to overwrite the function address for the function. But the process can only execute code within its own virtual memory space.
    Injecting code a little about anywhere is extremely difficult and error prone. An easier way is just to redirect the call to a similar function within a dll within the project's virtual memory space.

    2) How could i make the class Process more usefull as Elysia said?
    For example, it could enumerate all processes, store them in a vector. It could expose member functions to return properties for a specific process (as opposed to creating a new class and passing arguments around).
    Essentially, make a class that is the enumeration itself. It can store a class which is the Process object itself. The process object is the ones you can manipulate, etc. The enumerator allows you to search and find processes easy.
    Last edited by Elysia; 01-31-2008 at 05:34 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    You're actually going to have to re-direct a certain DirectInput function call to return your own device. So you need to overwrite the function address for the function.
    Oh, i got it, and how can i know that function address?

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    It's stored somewhere in the executable header. Technical details I don't remember 100&#37;, but since Windows can randomly load dlls and such, for performance reasons, it stores all the real function addresses in a header and simply makes the code jump into that area when doing function calls.
    So you would have to walk the PE header and find the function you wanted to hook, then overwrite it with your own function address inside the dll. I don't remember how. But there are guides over at codeproject on how to do this. But be careful, this isn't exactly playing nice. Microsoft doesn't like it at all and that's typically why they made the KernelGuard in the first place.
    This is a common method to "take over" the system, like many security softwares does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    The simplest adn easiest way is to use PostMessage() to send the appropriate keypress information to the application. There are API methods for enumerating all the windows and getting the titlebar info from them. Even a directX program runnign in full screen mode has a titlebar, even if it isnt visible. Then just PostMessage() to that window and boom, you have yoru macro generator.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allegro input keypress
    By CaliJoe in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2009, 09:51 PM
  2. (MFC, Visual C++) Keypress Detection in a dialog...
    By guitarist809 in forum Windows Programming
    Replies: 4
    Last Post: 08-31-2008, 01:13 PM
  3. Replies: 6
    Last Post: 08-28-2008, 08:10 PM
  4. Detecting keypress
    By dandago in forum C Programming
    Replies: 4
    Last Post: 06-10-2007, 09:34 AM
  5. ahhh help....rapid keypress detection
    By technoXavage in forum Game Programming
    Replies: 1
    Last Post: 12-18-2003, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21