C# Equivalent of AutoHotKey NumPut and VarSetCapacity

0

I'm trying to convert this function from this repository, programmed with the AutoHotKey language. But I ended up stuck in one question: In C# what would the NumPut and VarSetCapacity functions present in AutoHotKey be equivalent to?

The function I want to convert calls certain functions in memory process by passing parameters that can be dynamic

AutoHotKey:

callWithParams(hProcess, dwFunc, aParams, bCleanupStack = true) {
    if(!hProcess) {
        ErrorLevel := ERROR_INVALID_HANDLE
        return false
    }
    validParams := 0
    
    i := aParams.MaxIndex()
    
    ;         i * PUSH + CALL + RETN
    dwLen := i * 5    + 5    + 1
    if(bCleanupStack)
        dwLen += 3
    VarSetCapacity(injectData, i * 5    + 5       + 3       + 1, 0) //This <------------
    
    i_ := 1
    while(i > 0) {
        if(aParams[i][1] != "") {
            dwMemAddress := 0x0
            if(aParams[i][1] == "p") {
                dwMemAddress := aParams[i][2]
            } else if(aParams[i][1] == "s") {
                if(i_>3)
                    return false
                dwMemAddress := pParam%i_%
                writeString(hProcess, dwMemAddress, aParams[i][2])
                if(ErrorLevel)
                    return false
                i_ += 1
            } else if(aParams[i][1] == "i") {
                dwMemAddress := aParams[i][2]
            } else {
                return false
            }
            NumPut(0x68, injectData, validParams * 5, "UChar") //And this <------------
            NumPut(dwMemAddress, injectData, validParams * 5 + 1, "UInt")
            validParams += 1
        }
        i -= 1
    }
    
    offset := dwFunc - ( pInjectFunc + validParams * 5 + 5 )
    NumPut(0xE8, injectData, validParams * 5, "UChar")
    NumPut(offset, injectData, validParams * 5 + 1, "Int")
    
    if(bCleanupStack) {
        NumPut(0xC483, injectData, validParams * 5 + 5, "UShort")
        NumPut(validParams*4, injectData, validParams * 5 + 7, "UChar")
        
        NumPut(0xC3, injectData, validParams * 5 + 8, "UChar")
    } else {
        NumPut(0xC3, injectData, validParams * 5 + 5, "UChar")
    }
    
    writeRaw(hGTA, pInjectFunc, &injectData, dwLen)
    if(ErrorLevel)
        return false
    
    hThread := createRemoteThread(hGTA, 0, 0, pInjectFunc, 0, 0, 0)
    if(ErrorLevel)
        return false
    
    waitForSingleObject(hThread, 0xFFFFFFFF)
    
    closeProcess(hThread)
    
    return true
}

C#:

public class ParamsObject
{
    public String type { get; set; }
    public dynamic value { get; set; }
}

public static bool callWithParams(IntPtr hProcess, int dwFunc, ParamsObject[] aParams, bool bCleanupStack = true)
        {
            if (hProcess == IntPtr.Zero) return false;
            int validParams = 0;

            int i = aParams.Length;
            int dwLen = i * 5 + 5 + 1;
            int i_ = 1;
            if (bCleanupStack) dwLen += 3;
            
            //this is VarSetCapacity (maybe?)
            byte[] injectData = new byte[i * 5 + 5 + 3 + 1];

            foreach(ParamsObject param in aParams)
            {
                if (!String.IsNullOrEmpty(param.type))
                {
                    dynamic dwMemAddress = 0x0;
                    if(param.type == "p")
                    {
                        dwMemAddress = param.value;
                    } else if (param.type == "s")
                    {
                        if (i_ > 3) return false;
                        if (i_ == 1) dwMemAddress = (int)pParam1;
                        if (i_ == 2) dwMemAddress = (int)pParam2;
                        if (i_ == 3) dwMemAddress = (int)pParam3;
                        if (!WriteString(hProcess, dwMemAddress, param.value)) return false;
                        i_ += 1;
                    } else if (param.type == "i")
                    {
                        dwMemAddress = param.value;
                    } else
                    {
                        return false;
                    }
                    //Here I need the NumPut <----------
                }
            }

            return true;
        }
c#
memory
memory-management
autohotkey
kernel32
asked on Stack Overflow Jan 25, 2021 by Wagner Rodrigues • edited Jan 26, 2021 by Wagner Rodrigues

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0