I'd like to have a function that allows me to read the memory of another process.
I was thinking about something like this (pseudo code):
staticAddress = 0x026E0DC4
processId = GetProcessIdByName(processName)
processHandle = GetProcessHandle(processId)
processBaseAddress = GetBaseAddress(processHandle)
addressToRead = processBaseAddress+staticAddress
readValueAsInt = ReadMemoryInt(processHandle, addressToRead)
readValueAsFloat = ReadMemoryFloat(processHandle, addressToRead)
readValueAsString = ReadMemoryString(processHandle, addressToRead)
Would that even be possible?
Here is what I got so far:
#include <Windows.h>
#include <conio.h>
#include <tlhelp32.h>
#include <string>
#include <psapi.h>
#pragma comment( lib, "psapi" )
int GetProcessId(char* ProcName) {
PROCESSENTRY32 pe32;
HANDLE hSnapshot = NULL;
pe32.dwSize = sizeof( PROCESSENTRY32 );
hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( Process32First( hSnapshot, &pe32 ) ) {
do {
if( strcmp( pe32.szExeFile, ProcName ) == 0 )
break;
} while( Process32Next( hSnapshot, &pe32 ) );
}
if( hSnapshot != INVALID_HANDLE_VALUE )
CloseHandle( hSnapshot );
return pe32.th32ProcessID;
}
int GetModuleBase(HANDLE processHandle, string &sModuleName)
{
HMODULE *hModules;
char szBuf[50];
DWORD cModules;
DWORD dwBase = -1;
//------
EnumProcessModules(processHandle, hModules, 0, &cModules);
hModules = new HMODULE[cModules/sizeof(HMODULE)];
if(EnumProcessModules(processHandle, hModules, cModules/sizeof(HMODULE), &cModules)) {
for(int i = 0; i < cModules/sizeof(HMODULE); i++) {
if(GetModuleBaseName(processHandle, hModules[i], szBuf, sizeof(szBuf))) {
if(sModuleName.compare(szBuf) == 0) {
dwBase = (DWORD)hModules[i];
break;
}
}
}
}
delete[] hModules;
return dwBase;
}
int ReadMemoryInt(HANDLE processHandle, LPCVOID address) {
//LPVOID buffer = ??;
//SIZE_T size = ??;
SIZE_T NumberOfBytesToRead = 4; //??
ReadProcessMemory(processHandle, address, buffer, size, NumberOfBytesToRead)
return buffer; //??
}
int ReadMemoryFloat(HANDLE processHandle, LPCVOID address) {
//LPVOID buffer = ??;
//SIZE_T size = ??;
SIZE_T NumberOfBytesToRead = 8; //??
ReadProcessMemory(processHandle, address, buffer, size, NumberOfBytesToRead)
return buffer; //??
}
int ReadMemoryString(HANDLE processHandle, LPCVOID address) {
//LPVOID buffer = ??;
//SIZE_T size = ??;
SIZE_T NumberOfBytesToRead = 999; //??
ReadProcessMemory(processHandle, address, buffer, size, NumberOfBytesToRead)
return buffer; //??
}
int main()
{
//read an integer from "Program.exe"+0x05D8A3C4
int address = 0x05D8A3C4;
char* processName = "Program.exe";
int processId = GetProcessId(processName);
HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
int processBaseAddress = GetModuleBase(processHandle, (string)"Program.exe";
LPCVOID actualAddress = processBaseAddress+address;
int readValue = ReadMemory(processHandle, actualAddress);
std::cout << readValue << std::endl;
CloseHandle(processHandle);
return 0;
}
As you can see form the question marks in the code I'm really unsure about the "buffer" and "size" parameters of ReadProcessMemory. I'd really appreciate it if someone could help me figuring this out.
https://github.com/T-vK/Memory-Hacking-Class
Is a pretty simple class to do all that and even more.
Here is a list with all the methods it supports:
GetProcessId()
GetModuleBase()
SetPrivilege()
GetDebugPrivileges()
ReadInt()
GetPointerAddress()
ReadPointerInt()
ReadFloat()
ReadPointerFloat()
ReadText()
ReadPointerText()
Example usage:
#include "Memory.hpp"
using std::string;
int main() {
char* TARGET_PROCESS_NAME = "League of Legends.exe";
int GAME_VERSION_MODULE_OFFSET = 0x2A1D738; // [Base address of 'League of Legends.exe']+0x2A1D738 (address of a string containing a version number)
Memory Memory;
Memory.GetDebugPrivileges();
int processId = Memory.GetProcessId(TARGET_PROCESS_NAME);
HANDLE processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
int baseAddress = Memory.GetModuleBase(processHandle, (string)TARGET_PROCESS_NAME);
int gameVersionAddress = baseAddress + GAME_VERSION_MODULE_OFFSET;
string gameVersion = Memory.ReadText(processHandle, gameVersionAddress);
std::cout << "Game version: " << gameVersionAddress << std::endl;
cin.get();
return 0;
}
In case you were wondering, yes, I'm the author.
Here is an example for your ReadMemoryInt() function:
int ReadMemoryInt(HANDLE processHandle, LPCVOID address) {
int buffer = 0;
SIZE_T NumberOfBytesToRead = sizeof(buffer); //this is equal to 4
SIZE_T NumberOfBytesActuallyRead;
BOOL err = ReadProcessMemory(processHandle, address, &buffer, NumberOfBytesToRead, &NumberOfBytesActuallyRead);
if (err || NumberOfBytesActuallyRead != NumberOfBytesToRead)
/*an error occured*/ ;
return buffer;
}
The & mean that the address of the variable is passed instead its value.
And in ReadMemoryString() you cannot know the actual size you need to read, you could either read a big block (size 999) or read many little blocks till you get one containing \0.
And if you want to know if it works, you can start it in a debugger and look if the values you expect are returned.
In the following code the call to SetupDiEnumDeviceInfo() causes the subsequent CreateFile to return ERROR_SHARING_VIOLATION instead of opening the file. I was able to pinpoint the line by commenting out the other pieces of code until I hit one line that would cause the CreateFile to fail.
String SerialATDT::getComPortId()
{
#if 1
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
String comPort = "";
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_MODEM,
0, // Enumerator
0,
DIGCF_PRESENT );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return "";
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
int offset = 0;
while ( SetupDiEnumDeviceInfo(hDevInfo, offset++, &DeviceInfoData) )
{
DWORD DataT;
#if 1
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = (LPTSTR)LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
// Look for identifying info in the name
if ( mComPortIdentifier.size() > 0 ) {
const char *temp = strstr(buffer, mComPortIdentifier.c_str());
if ( temp == 0 ) {
continue;
}
}
// Now find out the port number
DWORD nSize=0 ;
TCHAR buf[MAX_PATH];
if ( SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, buf, MAX_PATH, &nSize) )
{
HKEY devKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ);
DWORD size = 0;
DWORD type;
RegQueryValueEx(devKey, TEXT("PortName"), NULL, NULL, NULL, & size);
BYTE* buff = new BYTE[size];
String result;
if( RegQueryValueEx(devKey, TEXT("PortName"), NULL, &type, buff, & size) == ERROR_SUCCESS ) {
comPort = (char*)buff;
if ( comPort.size() > 0 ) {
RegCloseKey(devKey);
break;
}
}
RegCloseKey(devKey);
delete [] buff;
}
#else
comPort = "COM44";
#endif
}
// Cleanup
SetupDiDestroyDeviceInfoList (hDevInfo);
if (buffer) {
LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS &&
GetLastError() != ERROR_INVALID_HANDLE )
{
TRACE_L("ATDT error after free %ld", GetLastError() );
// Insert error handling here.
return "";
}
return comPort;
#else
return "COM44";
#endif
}
bool SerialATDT::getComPort(HANDLE *hFile)
{
String comPort = getComPortId();
*hFile = INVALID_HANDLE_VALUE;
if ( comPort.size() > 0 ) {
String comPortStr;
comPortStr.Format("\\\\.\\%s", comPort.c_str());
*hFile = ::CreateFile( comPortStr.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL );
if ( *hFile == INVALID_HANDLE_VALUE ) {
TRACE_L("AT file open error %ld", GetLastError());
}
}
return *hFile != INVALID_HANDLE_VALUE;
}
I have been looking but have not found a reason why the DeviceInfoData needs to be cleared (nor have I found a method to do it). Has anybody run into this before?
I am trying to write an MFC dialog based application that reads the information from the serial COMM port of your PC and then write some information back to the serial COMM port. Any idea as to where do I start from?
Thanks in advance.
Does this help? :-)
SerialPort.h
/* /////////////////// Macros / Structs etc ////////////////////////// */
#ifndef __SERIALPORT_H__
#define __SERIALPORT_H__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vector>
/* ///////////////////////// Classes /////////////////////////////////////////// */
/* //// Serial port exception class //////////////////////////////////////////// */
void AfxThrowSerialException(DWORD dwError = 0);
class CSerialException : public CException
{
public:
/* Constructors / Destructors */
CSerialException (DWORD dwError);
~CSerialException ();
/* Methods */
#ifdef _DEBUG
virtual void Dump(CDumpContext & dc) const;
#endif
virtual BOOL GetErrorMessage(LPTSTR lpstrError, UINT nMaxError, PUINT pnHelpContext = NULL);
CString GetErrorMessage();
/* Data members */
DWORD m_dwError;
protected:
DECLARE_DYNAMIC(CSerialException)
};
/* // The actual serial port class ///////////////////////////////////////////// */
class CSerialPort : public CObject
{
public:
/* Enums */
enum FlowControl {
NoFlowControl,
CtsRtsFlowControl,
CtsDtrFlowControl,
DsrRtsFlowControl,
DsrDtrFlowControl,
XonXoffFlowControl
};
enum Parity {
EvenParity,
MarkParity,
NoParity,
OddParity,
SpaceParity
};
enum StopBits {
OneStopBit,
OnePointFiveStopBits,
TwoStopBits
};
/* Constructors / Destructors */
CSerialPort ();
~CSerialPort ();
/* General Methods */
static std::vector<CString> EnumSerialPorts( void );
void Open(int nPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE dataBits = 8,
StopBits stopBits = OneStopBit, FlowControl fc = NoFlowControl, BOOL bOverlapped = FALSE);
void Open(LPCTSTR szPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE dataBits = 8,
StopBits stopBits = OneStopBit, FlowControl fc = NoFlowControl, BOOL bOverlapped = FALSE);
void Close();
void Attach(HANDLE hComm);
HANDLE Detach();
operator HANDLE() const { return m_hComm; }
BOOL IsOpen() const { return m_hComm != INVALID_HANDLE_VALUE; }
#ifdef _DEBUG
void CSerialPort::Dump(CDumpContext & dc) const;
#endif
/* Reading / Writing Methods */
DWORD Read(void *lpBuf, DWORD dwCount);
BOOL Read(void *lpBuf, DWORD dwCount, OVERLAPPED &overlapped);
void ReadEx(void *lpBuf, DWORD dwCount);
DWORD Write(const void *lpBuf, DWORD dwCount);
BOOL Write(const void *lpBuf, DWORD dwCount, OVERLAPPED &overlapped);
void WriteEx(const void *lpBuf, DWORD dwCount);
void TransmitChar(char cChar);
void GetOverlappedResult(OVERLAPPED & overlapped,
DWORD & dwBytesTransferred,
BOOL bWait);
void CancelIo();
/* Configuration Methods */
void GetConfig(COMMCONFIG & config);
static void GetDefaultConfig(int nPort, COMMCONFIG & config);
void SetConfig(COMMCONFIG & Config);
static void SetDefaultConfig(int nPort, COMMCONFIG & config);
/* Misc RS232 Methods */
void ClearBreak();
void SetBreak();
void ClearError(DWORD & dwErrors);
void GetStatus(COMSTAT & stat);
void GetState(DCB & dcb);
void SetState(DCB & dcb, BOOL bClosePortOnErr = FALSE);
void Escape(DWORD dwFunc);
void ClearDTR();
void ClearRTS();
void SetDTR();
void SetRTS();
void SetXOFF();
void SetXON();
void GetProperties(COMMPROP & properties);
void GetModemStatus(DWORD & dwModemStatus);
/* Timeouts */
void SetTimeouts(const COMMTIMEOUTS& timeouts);
void GetTimeouts(COMMTIMEOUTS& timeouts);
void Set0Timeout();
void Set0WriteTimeout();
void Set0ReadTimeout();
/* Event Methods */
void SetMask(DWORD dwMask);
void GetMask(DWORD & dwMask);
void WaitEvent(DWORD & dwMask);
void WaitEvent(DWORD & dwMask, OVERLAPPED & overlapped);
/* Queue Methods */
void Flush();
void Purge(DWORD dwFlags);
void TerminateOutstandingWrites();
void TerminateOutstandingReads();
void ClearWriteBuffer();
void ClearReadBuffer();
void Setup(DWORD dwInQueue, DWORD dwOutQueue);
/* Overridables */
virtual void OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
protected:
HANDLE m_hComm; /* Handle to the comms port */
BOOL m_bOverlapped; /* Is the port open in overlapped IO */
static void WINAPI _OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped);
DECLARE_DYNAMIC(CSerialPort)
private:
void OpenComm(LPCTSTR szPort, DWORD dwBaud = 9600, Parity parity = NoParity, BYTE dataBits = 8,
StopBits stopBits = OneStopBit, FlowControl fc = NoFlowControl, BOOL bOverlapped = FALSE);
};
#endif /* __SERIALPORT_H__ */
SerialPort.cpp
/* /////////////////////////////// Includes ////////////////////////////////// */
#include "stdafx.h"
#include <winspool.h>
#include "serialport.h"
#include "winerror.h"
/* /////////////////////////////// defines ///////////////////////////////////// */
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/* ////////////////////////////// Implementation /////////////////////////////// */
/* Class which handles CancelIo function which must be constructed at run time
* since it is not imeplemented on NT 3.51 or Windows 95. To avoid the loader
* bringing up a message such as "Failed to load due to missing export...", the
* function is constructed using GetProcAddress. The CSerialPort::CancelIo
* function then checks to see if the function pointer is NULL and if it is it
* throws an exception using the error code ERROR_CALL_NOT_IMPLEMENTED which
* is what 95 would have done if it had implemented a stub for it in the first
* place !!
*/
class _SERIAL_PORT_DATA
{
public:
/* Constructors /Destructors */
_SERIAL_PORT_DATA ();
~_SERIAL_PORT_DATA ();
HINSTANCE m_hKernel32;
typedef BOOL ( CANCELIO )( HANDLE );
typedef CANCELIO *LPCANCELIO;
LPCANCELIO m_lpfnCancelIo;
};
_SERIAL_PORT_DATA::_SERIAL_PORT_DATA ()
{
m_hKernel32 = LoadLibrary( _T("KERNEL32.DLL") );
VERIFY(m_hKernel32 != NULL);
m_lpfnCancelIo = (LPCANCELIO)GetProcAddress(m_hKernel32, "CancelIo");
}
_SERIAL_PORT_DATA::~_SERIAL_PORT_DATA ()
{
FreeLibrary(m_hKernel32);
m_hKernel32 = NULL;
}
/* The local variable which handle the function pointers */
_SERIAL_PORT_DATA _SerialPortData;
/* //////// Exception handling code */
void AfxThrowSerialException(DWORD dwError /* = 0 */)
{
if(dwError == 0) {
dwError = ::GetLastError();
}
CSerialException *pException = new CSerialException(dwError);
TRACE( _T("Warning: throwing CSerialException for error %d\n"), dwError );
THROW( pException );
}
BOOL CSerialException::GetErrorMessage(LPTSTR pstrError, UINT nMaxError, PUINT pnHelpContext)
{
ASSERT( pstrError != NULL && AfxIsValidString(pstrError, nMaxError) );
if(pnHelpContext != NULL) {
*pnHelpContext = 0;
}
LPTSTR lpBuffer;
BOOL bRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, m_dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
(LPTSTR)&lpBuffer, 0, NULL);
if(bRet == FALSE) {
*pstrError = '\0';
} else {
lstrcpyn(pstrError, lpBuffer, nMaxError);
bRet = TRUE;
LocalFree(lpBuffer);
}
return bRet;
} /* GetErrorMessage */
CString CSerialException::GetErrorMessage()
{
CString rVal;
LPTSTR pstrError = rVal.GetBuffer(4096);
GetErrorMessage(pstrError, 4096, NULL);
rVal.ReleaseBuffer();
return rVal;
}
CSerialException::CSerialException (DWORD dwError)
{
m_dwError = dwError;
}
CSerialException::~CSerialException ()
{}
IMPLEMENT_DYNAMIC(CSerialException, CException)
#ifdef _DEBUG
void CSerialException::Dump(CDumpContext & dc) const
{
CObject::Dump(dc);
dc << "m_dwError = " << m_dwError;
}
#endif
/* //////// The actual serial port code */
CSerialPort::CSerialPort ()
{
m_hComm = INVALID_HANDLE_VALUE;
m_bOverlapped = FALSE;
}
CSerialPort::~CSerialPort ()
{
Close();
}
IMPLEMENT_DYNAMIC(CSerialPort, CObject)
#ifdef _DEBUG
void CSerialPort::Dump(CDumpContext & dc) const
{
CObject::Dump(dc);
dc << _T("m_hComm = ") << m_hComm << _T("\n");
dc << _T("m_bOverlapped = ") << m_bOverlapped;
}
#endif
std::vector<CString> CSerialPort::EnumSerialPorts(void)
{
/* Clear existing list of COMM ports */
std::vector<CString> commPortList;
/* COM ports can be numbered from 1 to 255, loop through all possibilities and add the ones we
* find.
*/
for( UINT i = 1; i < 256; i++ ) {
//Form the Raw device name
CString sPort;
sPort.Format( _T("COM%d"), i );
COMMCONFIG cc;
DWORD dwSize = sizeof(cc);
if( GetDefaultCommConfig(sPort, &cc, &dwSize) != 0 ) {
commPortList.push_back( (LPCTSTR)sPort );
}
}
return commPortList;
}
void CSerialPort::Open(int nPort, DWORD dwBaud, Parity parity, BYTE dataBits, StopBits stopBits,
FlowControl fc, BOOL bOverlapped)
{
/* Validate our parameters */
ASSERT(nPort > 0 && nPort <= 255);
/* Call CreateFile to open up the comms port */
CString sPort;
sPort.Format(_T("\\\\.\\COM%d"), nPort);
OpenComm(sPort, dwBaud, parity, dataBits, stopBits, fc, bOverlapped);
}
void CSerialPort::Open(LPCTSTR szPort, DWORD dwBaud, Parity parity, BYTE dataBits, StopBits stopBits,
FlowControl fc, BOOL bOverlapped)
{
OpenComm(szPort, dwBaud, parity, dataBits, stopBits, fc, bOverlapped);
}
void CSerialPort::OpenComm(LPCTSTR szPort, DWORD dwBaud, Parity parity, BYTE dataBits, StopBits stopbits,
FlowControl fc, BOOL bOverlapped)
{
m_hComm = CreateFile(szPort, (GENERIC_READ | GENERIC_WRITE), 0, NULL, OPEN_EXISTING,
bOverlapped ? FILE_FLAG_OVERLAPPED : 0, NULL);
if(m_hComm == INVALID_HANDLE_VALUE) {
TRACE( _T("Failed to open up the comms port\n") );
AfxThrowSerialException();
}
m_bOverlapped = bOverlapped;
/* Get the current state prior to changing it */
DCB dcb;
GetState(dcb);
/* Setup the baud rate */
dcb.BaudRate = dwBaud;
/* Setup the Parity */
switch(parity) {
case EvenParity:
dcb.Parity = EVENPARITY; break;
case MarkParity:
dcb.Parity = MARKPARITY; break;
case NoParity:
dcb.Parity = NOPARITY; break;
case OddParity:
dcb.Parity = ODDPARITY; break;
case SpaceParity:
dcb.Parity = SPACEPARITY; break;
default:
ASSERT(FALSE); break;
}
/* Setup the data bits */
dcb.ByteSize = dataBits;
/* Setup the stop bits */
switch(stopbits) {
case OneStopBit:
dcb.StopBits = ONESTOPBIT; break;
case OnePointFiveStopBits:
dcb.StopBits = ONE5STOPBITS; break;
case TwoStopBits:
dcb.StopBits = TWOSTOPBITS; break;
default:
ASSERT(FALSE); break;
}
/* Setup the flow control */
dcb.fDsrSensitivity = FALSE;
switch(fc) {
case NoFlowControl:
{
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
break;
}
case CtsRtsFlowControl:
{
dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = FALSE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
break;
}
case CtsDtrFlowControl:
{
dcb.fOutxCtsFlow = TRUE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
break;
}
case DsrRtsFlowControl:
{
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = TRUE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
break;
}
case DsrDtrFlowControl:
{
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = TRUE;
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
break;
}
case XonXoffFlowControl:
{
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutX = TRUE;
dcb.fInX = TRUE;
dcb.XonChar = 0x11;
dcb.XoffChar = 0x13;
dcb.XoffLim = 100;
dcb.XonLim = 100;
break;
}
default:
{
ASSERT(FALSE);
break;
}
}
/* Now that we have all the settings in place, make the changes */
SetState(dcb);
} /* Open */
void CSerialPort::Close()
{
if( IsOpen() ) {
BOOL bSuccess = CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
if(!bSuccess) {
TRACE( _T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError() );
}
m_bOverlapped = FALSE;
}
}
void CSerialPort::Attach(HANDLE hComm)
{
Close();
m_hComm = hComm;
}
HANDLE CSerialPort::Detach()
{
HANDLE hrVal = m_hComm;
m_hComm = INVALID_HANDLE_VALUE;
return hrVal;
}
DWORD CSerialPort::Read(void *lpBuf, DWORD dwCount)
{
ASSERT( IsOpen() );
ASSERT(!m_bOverlapped);
DWORD dwBytesRead = 0;
if( !ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, NULL) ) {
TRACE( _T("Failed in call to ReadFile\n") );
AfxThrowSerialException();
}
return dwBytesRead;
}
BOOL CSerialPort::Read(void *lpBuf, DWORD dwCount, OVERLAPPED & overlapped)
{
ASSERT( IsOpen() );
ASSERT(m_bOverlapped);
ASSERT(overlapped.hEvent);
DWORD dwBytesRead = 0;
BOOL bSuccess = ReadFile(m_hComm, lpBuf, dwCount, &dwBytesRead, &overlapped);
if(!bSuccess) {
if(GetLastError() != ERROR_IO_PENDING) {
TRACE( _T("Failed in call to ReadFile\n") );
AfxThrowSerialException();
}
}
return bSuccess;
}
DWORD CSerialPort::Write(const void *lpBuf, DWORD dwCount)
{
ASSERT( IsOpen() );
ASSERT(!m_bOverlapped);
DWORD dwBytesWritten = 0;
if( !WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, NULL) ) {
TRACE( _T("Failed in call to WriteFile\n") );
AfxThrowSerialException();
}
return dwBytesWritten;
}
BOOL CSerialPort::Write(const void *lpBuf, DWORD dwCount, OVERLAPPED & overlapped)
{
ASSERT( IsOpen() );
ASSERT(m_bOverlapped);
ASSERT(overlapped.hEvent);
DWORD dwBytesWritten = 0;
BOOL bSuccess = WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, &overlapped);
if(!bSuccess) {
if(GetLastError() != ERROR_IO_PENDING) {
TRACE( _T("Failed in call to WriteFile\n") );
AfxThrowSerialException();
}
}
return bSuccess;
}
void CSerialPort::GetOverlappedResult(OVERLAPPED & overlapped,
DWORD & dwBytesTransferred,
BOOL bWait)
{
ASSERT( IsOpen() );
ASSERT(m_bOverlapped);
ASSERT(overlapped.hEvent);
//DWORD dwBytesWritten = 0;
if( !::GetOverlappedResult(m_hComm, &overlapped, &dwBytesTransferred, bWait) ) {
if(GetLastError() != ERROR_IO_PENDING) {
TRACE( _T("Failed in call to GetOverlappedResult\n") );
AfxThrowSerialException();
}
}
}
void CSerialPort::_OnCompletion(DWORD dwErrorCode, DWORD dwCount, LPOVERLAPPED lpOverlapped)
{
/* Validate our parameters */
ASSERT(lpOverlapped);
/* Convert back to the C++ world */
CSerialPort *pSerialPort = (CSerialPort *)lpOverlapped->hEvent;
ASSERT( pSerialPort->IsKindOf( RUNTIME_CLASS(CSerialPort) ) );
/* Call the C++ function */
pSerialPort->OnCompletion(dwErrorCode, dwCount, lpOverlapped);
}
void CSerialPort::OnCompletion(DWORD /*dwErrorCode*/, DWORD /*dwCount*/, LPOVERLAPPED lpOverlapped)
{
/* Just free up the memory which was previously allocated for the OVERLAPPED structure */
delete lpOverlapped;
/* Your derived classes can do something useful in OnCompletion, but don't forget to
* call CSerialPort::OnCompletion to ensure the memory is freed up
*/
}
void CSerialPort::CancelIo()
{
ASSERT( IsOpen() );
if(_SerialPortData.m_lpfnCancelIo == NULL) {
TRACE( _T(
"CancelIo function is not supported on this OS. You need to be running at least NT 4 or Win 98 to use this function\n") );
AfxThrowSerialException(ERROR_CALL_NOT_IMPLEMENTED);
}
if( !::_SerialPortData.m_lpfnCancelIo(m_hComm) ) {
TRACE( _T("Failed in call to CancelIO\n") );
AfxThrowSerialException();
}
}
void CSerialPort::WriteEx(const void *lpBuf, DWORD dwCount)
{
ASSERT( IsOpen() );
OVERLAPPED *pOverlapped = new OVERLAPPED;
ZeroMemory( pOverlapped, sizeof(OVERLAPPED) );
pOverlapped->hEvent = (HANDLE) this;
if( !WriteFileEx(m_hComm, lpBuf, dwCount, pOverlapped, _OnCompletion) ) {
delete pOverlapped;
TRACE( _T("Failed in call to WriteFileEx\n") );
AfxThrowSerialException();
}
}
void CSerialPort::ReadEx(void *lpBuf, DWORD dwCount)
{
ASSERT( IsOpen() );
OVERLAPPED *pOverlapped = new OVERLAPPED;
ZeroMemory( pOverlapped, sizeof(OVERLAPPED) );
pOverlapped->hEvent = (HANDLE) this;
if( !ReadFileEx(m_hComm, lpBuf, dwCount, pOverlapped, _OnCompletion) ) {
delete pOverlapped;
TRACE( _T("Failed in call to ReadFileEx\n") );
AfxThrowSerialException();
}
}
void CSerialPort::TransmitChar(char cChar)
{
ASSERT( IsOpen() );
if( !TransmitCommChar(m_hComm, cChar) ) {
TRACE( _T("Failed in call to TransmitCommChar\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetConfig(COMMCONFIG & config)
{
ASSERT( IsOpen() );
DWORD dwSize = sizeof(COMMCONFIG);
if( !GetCommConfig(m_hComm, &config, &dwSize) ) {
TRACE( _T("Failed in call to GetCommConfig\n") );
AfxThrowSerialException();
}
}
void CSerialPort::SetConfig(COMMCONFIG & config)
{
ASSERT( IsOpen() );
DWORD dwSize = sizeof(COMMCONFIG);
if( !SetCommConfig(m_hComm, &config, dwSize) ) {
TRACE( _T("Failed in call to SetCommConfig\n") );
AfxThrowSerialException();
}
}
void CSerialPort::SetBreak()
{
ASSERT( IsOpen() );
if( !SetCommBreak(m_hComm) ) {
TRACE( _T("Failed in call to SetCommBreak\n") );
AfxThrowSerialException();
}
}
void CSerialPort::ClearBreak()
{
ASSERT( IsOpen() );
if( !ClearCommBreak(m_hComm) ) {
TRACE( _T("Failed in call to SetCommBreak\n") );
AfxThrowSerialException();
}
}
void CSerialPort::ClearError(DWORD & dwErrors)
{
ASSERT( IsOpen() );
if( !ClearCommError(m_hComm, &dwErrors, NULL) ) {
TRACE( _T("Failed in call to ClearCommError\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetDefaultConfig(int nPort, COMMCONFIG & config)
{
/* Validate our parameters */
ASSERT(nPort > 0 && nPort <= 255);
/* Create the device name as a string */
CString sPort;
sPort.Format(_T("COM%d"), nPort);
DWORD dwSize = sizeof(COMMCONFIG);
if( !GetDefaultCommConfig(sPort, &config, &dwSize) ) {
TRACE( _T("Failed in call to GetDefaultCommConfig\n") );
AfxThrowSerialException();
}
}
void CSerialPort::SetDefaultConfig(int nPort, COMMCONFIG & config)
{
/* Validate our parameters */
ASSERT(nPort > 0 && nPort <= 255);
/* Create the device name as a string */
CString sPort;
sPort.Format(_T("COM%d"), nPort);
DWORD dwSize = sizeof(COMMCONFIG);
if( !SetDefaultCommConfig(sPort, &config, dwSize) ) {
TRACE( _T("Failed in call to GetDefaultCommConfig\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetStatus(COMSTAT & stat)
{
ASSERT( IsOpen() );
DWORD dwErrors;
if( !ClearCommError(m_hComm, &dwErrors, &stat) ) {
TRACE( _T("Failed in call to ClearCommError\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetState(DCB& dcb)
{
ASSERT( IsOpen() );
if( !GetCommState(m_hComm, &dcb) ) {
TRACE( _T("Failed in call to GetCommState\n") );
AfxThrowSerialException();
}
}
void CSerialPort::SetState(DCB& dcb, BOOL bClosePortOnErr)
{
ASSERT( IsOpen() );
if( !SetCommState(m_hComm, &dcb) ) {
if( bClosePortOnErr == TRUE ) {
Close();
}
TRACE( _T("Failed in call to SetCommState\n") );
AfxThrowSerialException();
}
}
void CSerialPort::Escape(DWORD dwFunc)
{
ASSERT( IsOpen() );
if( !EscapeCommFunction(m_hComm, dwFunc) ) {
TRACE( _T("Failed in call to EscapeCommFunction\n") );
AfxThrowSerialException();
}
}
void CSerialPort::ClearDTR()
{
Escape(CLRDTR);
}
void CSerialPort::ClearRTS()
{
Escape(CLRRTS);
}
void CSerialPort::SetDTR()
{
Escape(SETDTR);
}
void CSerialPort::SetRTS()
{
Escape(SETRTS);
}
void CSerialPort::SetXOFF()
{
Escape(SETXOFF);
}
void CSerialPort::SetXON()
{
Escape(SETXON);
}
void CSerialPort::GetProperties(COMMPROP & properties)
{
ASSERT( IsOpen() );
if( !GetCommProperties(m_hComm, &properties) ) {
TRACE( _T("Failed in call to GetCommProperties\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetModemStatus(DWORD & dwModemStatus)
{
ASSERT( IsOpen() );
if( !GetCommModemStatus(m_hComm, &dwModemStatus) ) {
TRACE( _T("Failed in call to GetCommModemStatus\n") );
AfxThrowSerialException();
}
}
void CSerialPort::SetMask(DWORD dwMask)
{
ASSERT( IsOpen() );
if( !SetCommMask(m_hComm, dwMask) ) {
TRACE( _T("Failed in call to SetCommMask\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetMask(DWORD & dwMask)
{
ASSERT( IsOpen() );
if( !GetCommMask(m_hComm, &dwMask) ) {
TRACE( _T("Failed in call to GetCommMask\n") );
AfxThrowSerialException();
}
}
void CSerialPort::Flush()
{
ASSERT( IsOpen() );
if( !FlushFileBuffers(m_hComm) ) {
TRACE( _T("Failed in call to FlushFileBuffers\n") );
AfxThrowSerialException();
}
}
void CSerialPort::Purge(DWORD dwFlags)
{
ASSERT( IsOpen() );
if( !PurgeComm(m_hComm, dwFlags) ) {
TRACE( _T("Failed in call to PurgeComm\n") );
AfxThrowSerialException();
}
}
void CSerialPort::TerminateOutstandingWrites()
{
Purge(PURGE_TXABORT);
}
void CSerialPort::TerminateOutstandingReads()
{
Purge(PURGE_RXABORT);
}
void CSerialPort::ClearWriteBuffer()
{
Purge(PURGE_TXCLEAR);
}
void CSerialPort::ClearReadBuffer()
{
Purge(PURGE_RXCLEAR);
}
void CSerialPort::Setup(DWORD dwInQueue, DWORD dwOutQueue)
{
ASSERT( IsOpen() );
if( !SetupComm(m_hComm, dwInQueue, dwOutQueue) ) {
TRACE( _T("Failed in call to SetupComm\n") );
AfxThrowSerialException();
}
}
void CSerialPort::SetTimeouts(const COMMTIMEOUTS& timeouts)
{
ASSERT( IsOpen() );
if( !SetCommTimeouts(m_hComm, (LPCOMMTIMEOUTS)&timeouts) ) {
TRACE( _T("Failed in call to SetCommTimeouts\n") );
AfxThrowSerialException();
}
}
void CSerialPort::GetTimeouts(COMMTIMEOUTS & timeouts)
{
ASSERT( IsOpen() );
if( !GetCommTimeouts(m_hComm, &timeouts) ) {
TRACE( _T("Failed in call to GetCommTimeouts\n") );
AfxThrowSerialException();
}
}
void CSerialPort::Set0Timeout()
{
COMMTIMEOUTS Timeouts;
ZeroMemory( &Timeouts, sizeof(COMMTIMEOUTS) );
Timeouts.ReadIntervalTimeout = MAXDWORD;
Timeouts.ReadTotalTimeoutMultiplier = 0;
Timeouts.ReadTotalTimeoutConstant = 0;
Timeouts.WriteTotalTimeoutMultiplier = 0;
Timeouts.WriteTotalTimeoutConstant = 0;
SetTimeouts(Timeouts);
}
void CSerialPort::Set0WriteTimeout()
{
COMMTIMEOUTS Timeouts;
GetTimeouts(Timeouts);
Timeouts.WriteTotalTimeoutMultiplier = 0;
Timeouts.WriteTotalTimeoutConstant = 0;
SetTimeouts(Timeouts);
}
void CSerialPort::Set0ReadTimeout()
{
COMMTIMEOUTS Timeouts;
GetTimeouts(Timeouts);
Timeouts.ReadIntervalTimeout = MAXDWORD;
Timeouts.ReadTotalTimeoutMultiplier = 0;
Timeouts.ReadTotalTimeoutConstant = 0;
SetTimeouts(Timeouts);
}
void CSerialPort::WaitEvent(DWORD & dwMask)
{
ASSERT( IsOpen() );
ASSERT(!m_bOverlapped);
if( !WaitCommEvent(m_hComm, &dwMask, NULL) ) {
TRACE( _T("Failed in call to WaitCommEvent\n") );
AfxThrowSerialException();
}
}
void CSerialPort::WaitEvent(DWORD & dwMask, OVERLAPPED & overlapped)
{
ASSERT( IsOpen() );
ASSERT(m_bOverlapped);
ASSERT(overlapped.hEvent);
if( !WaitCommEvent(m_hComm, &dwMask, &overlapped) ) {
if(GetLastError() != ERROR_IO_PENDING) {
TRACE( _T("Failed in call to WaitCommEvent\n") );
AfxThrowSerialException();
}
}
}
Can I push a binary on a remote host windows using WINRM.
If not is there any other mechanism which allows me to push a binary on remote host.
I think I found a solution.
#include <windows.h>
#include <tchar.h>
#define SIZEOF_BUFFER 0x100
// Remote Parameters
LPCTSTR lpszMachine = NULL;
LPCTSTR lpszPassword = NULL;
LPCTSTR lpszUser = NULL;
LPCTSTR lpszDomain = NULL;
LPCTSTR lpszCommandExe = NULL;
LPCTSTR lpszLocalIP = _T("\\\\127.0.0.1");
char szThisMachine[SIZEOF_BUFFER] = "";
char szPassword[SIZEOF_BUFFER] = "";
LPCTSTR GetParamValue( LPCTSTR lpszParam )
{
DWORD dwParamLength = _tcslen( lpszParam );
for ( int i = 1; i < __argc; i++ )
if ( __targv[i][0] == _T('\\') || __targv[i][0] == _T('.'))
continue;
else
if ( __targv[i][0] == _T('/') || __targv[i][0] == _T('-') )
{
if ( _tcsnicmp( __targv[i] + 1, lpszParam, dwParamLength ) == 0 )
return __targv[i] + dwParamLength + 1;
}
else
return NULL;
return NULL;
}
LPCTSTR GetNthParameter( DWORD n, DWORD& argvIndex )
{
DWORD index = 0;
for( int i = 1; i < __argc; i++ )
{
if ( __targv[i][0] != _T('/') && __targv[i][0] != _T('-') )
index++;
if ( index == n )
{
argvIndex = i;
return __targv[i];
}
}
return NULL;
}
BOOL SetConnectionCredentials()
{
lpszPassword = GetParamValue( _T("pwd:") );
lpszUser = GetParamValue( _T("user:") );
return TRUE;
}
LPCTSTR GetRemoteMachineName()
{
DWORD dwIndex = 0;
LPCTSTR lpszMachine = GetNthParameter( 1, dwIndex );
if ( lpszMachine == NULL )
// return NULL;
return lpszLocalIP;
if ( _tcsnicmp( lpszMachine, _T(" "), 2 ) == 0 )
return lpszLocalIP;
if ( _tcsnicmp( lpszMachine, _T("\\\\"), 2 ) == 0 )
return lpszMachine;
// If a dot is entered we take it as localhost
if ( _tcsnicmp( lpszMachine, _T("."), 2 ) == 0 )
return lpszLocalIP;
return NULL;
}
// Establish Connection to Remote Machine
BOOL EstablishConnection( LPCTSTR lpszRemote, LPCTSTR lpszResource, BOOL bEstablish )
{
TCHAR szRemoteResource[_MAX_PATH];
DWORD rc;
_stprintf( szRemoteResource, _T("%s\\%s"), lpszRemote, lpszResource );
NETRESOURCE nr;
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = NULL;
nr.lpRemoteName = (LPTSTR)&szRemoteResource;
nr.lpProvider = NULL;
//Establish connection (using username/pwd)
rc = WNetAddConnection2( &nr, lpszPassword, lpszUser, FALSE );
if ( rc == NO_ERROR )
return TRUE; // indicate success
return FALSE;
}
BOOL CopyBinaryToRemoteSystem()
{
TCHAR drive[_MAX_DRIVE];
TCHAR dir[_MAX_DIR];
TCHAR fname[_MAX_FNAME];
TCHAR ext[_MAX_EXT];
TCHAR szRemoteResource[_MAX_PATH];
// Gets the file name and extension
_tsplitpath( lpszCommandExe, drive, dir, fname, ext );
_stprintf( szRemoteResource, _T("%s\\ADMIN$\\System32\\%s%s"), lpszMachine, fname, ext );
// Copy the Command's exe file to \\remote\ADMIN$\System32
return CopyFile( lpszCommandExe, szRemoteResource, FALSE );
}
int _tmain( DWORD, TCHAR**, TCHAR** )
{
int rc = 0;
DWORD dwIndex = 0;
lpszMachine = GetRemoteMachineName();
lpszCommandExe = GetNthParameter( 2, dwIndex );
SetConnectionCredentials();
if ( !EstablishConnection( lpszMachine, _T("ADMIN$"), TRUE ) )
{
rc = -2;
}
if ( !CopyBinaryToRemoteSystem())
{
}
return 0;
}