Jump to content

How to attach to other Process's stdin?


Recommended Posts

Hi,

i currently try to write to a other's process standard input. I am using the OpenProcess Function and then the DuplicateHandle Function to get the Handle of the other's stdin.

Below is my non-working code:

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

int main( int argc, char* argv[] )
{
HANDLE hProcess, hMyProcess;
HANDLE hInput;
unsigned int nProcess;

if( argc != 2 ) {
 fprintf( stderr, "illegal arg count\n" );
 exit( -1 );
} else {
 nProcess = atoi( argv[1] );
}

fprintf( stdout, "Attaching pro Process %d ... ", nProcess );
if(( hProcess = OpenProcess( PROCESS_ALL_ACCESS, TRUE, ( DWORD ) nProcess )) != NULL ) {
 fprintf( stdout, "OK\n" );
} else {
 fprintf( stdout, "FAILED\n" );
 exit( -1 );
}

hMyProcess = GetCurrentProcess();

fprintf( stdout, "Trying to get STDIN Handle from Process ... " );
if( ! DuplicateHandle( hProcess, GetStdHandle( STD_INPUT_HANDLE ), hMyProcess, &hInput, 0, TRUE, DUPLICATE_SAME_ACCESS )) {
 fprintf( stdout, "FAILED\n" );
 fprintf( stdout, "%08X\n", GetLastError());
 exit( -1 );
} else {
 fprintf( stdout, "OK\n" );
}

CloseHandle( hInput );
return 0;
}

Does anyone see why this doesn't work? Do i have any logical errors?

Thanks, Egon

Link to comment
Share on other sites


Hi,

found out that it's impossible to connect to another's process stdin/stdout/stderr Handle when you are not the father of that process. It's a security thing.

Egon

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...