Skip to content

Commit

Permalink
Add MSRDPEX_SECURE_PIPE_NAME .rdp name pipe loading
Browse files Browse the repository at this point in the history
  • Loading branch information
awakecoding committed Mar 14, 2024
1 parent 0fa0967 commit 7beddce
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 109 deletions.
59 changes: 59 additions & 0 deletions dll/Environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,62 @@ void MsRdpEx_FreeEnvironmentVariables(int envc, char** envs)

free(envs);
}

char* MsRdpEx_ReadTextFromNamedPipe(const char* pipeName)
{
size_t size = 0;
size_t length = 0;
DWORD readBytes = 0;
char* text = NULL;
char* buffer = NULL;
char* tempBuffer = NULL;
HANDLE pipeHandle = NULL;
char filename[MSRDPEX_MAX_PATH];

if (!pipeName)
return NULL;

sprintf_s(filename, sizeof(filename) - 1, "\\\\.\\pipe\\%s", pipeName);

pipeHandle = CreateFileA(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

if (pipeHandle == INVALID_HANDLE_VALUE) {
goto exit;
}

size = 1024;
buffer = (char*) malloc(size);

if (!buffer)
goto exit;

length = 0;
while (1) {
if (!ReadFile(pipeHandle, buffer + length, size - length, &readBytes, NULL) || (readBytes == 0)) {
// If no bytes were read or an error occurred, break out of the loop.
break;
}

length += readBytes;
if ((size - length) <= 1) {
tempBuffer = (char*) realloc(buffer, size * 2);
if (!tempBuffer) {
goto exit;
}
buffer = tempBuffer;
size = size * 2;
}
}
buffer[length] = '\0';
text = _strdup(buffer);

exit:
if (buffer) {
SecureZeroMemory(buffer, size);
free(buffer);
}
if (pipeHandle && (pipeHandle != INVALID_HANDLE_VALUE)) {
CloseHandle(pipeHandle);
}
return text;
}
1 change: 1 addition & 0 deletions dll/MsRdpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ class CMsRdpClient : public IMsRdpClient10

CMsRdpExtendedSettings* pMsRdpExtendedSettings = m_pMsRdpExtendedSettings;
m_pMsRdpExtendedSettings->LoadRdpFile(NULL);
m_pMsRdpExtendedSettings->LoadRdpFileFromNamedPipe(NULL);
m_pMsRdpExtendedSettings->PrepareSspiSessionIdHack();
m_pMsRdpExtendedSettings->PrepareMouseJiggler();

Expand Down
110 changes: 109 additions & 1 deletion dll/RdpFile.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@

#include <MsRdpEx/RdpFile.h>

bool MsRdpEx_IsSensitivePropertyName(const char* name)
{
if (!name)
return false;

if (MsRdpEx_IStringEndsWith(name, "Password"))
return true;

return false;
}

MsRdpEx_RdpFileEntry* MsRdpEx_RdpFileEntry_New(char type, const char* name, const char* value)
{
MsRdpEx_RdpFileEntry* entry;
Expand Down Expand Up @@ -86,6 +97,10 @@ void MsRdpEx_RdpFileEntry_Free(MsRdpEx_RdpFileEntry* entry)
if (!entry)
return;

if (MsRdpEx_IsSensitivePropertyName(entry->name) && entry->value) {
SecureZeroMemory(entry->value, strlen(entry->value));
}

if (entry->name) {
free(entry->name);
entry->name = NULL;
Expand Down Expand Up @@ -251,7 +266,9 @@ bool MsRdpEx_RdpFile_Load(MsRdpEx_RdpFile* ctx, const char* filename)
}
else if (*type == 's') /* string type */
{
MsRdpEx_LogPrint(DEBUG, "RDP(s): %s = %s", name, value);
MsRdpEx_LogPrint(DEBUG, "RDP(s): %s = %s", name,
MsRdpEx_IsSensitivePropertyName(name) ? "*omitted*" : value);

entry = MsRdpEx_RdpFileEntry_New(*type, name, value);

if (entry) {
Expand All @@ -273,6 +290,97 @@ bool MsRdpEx_RdpFile_Load(MsRdpEx_RdpFile* ctx, const char* filename)
return true;
}

bool MsRdpEx_RdpFile_LoadText(MsRdpEx_RdpFile* ctx, const char* text)
{
size_t index;
size_t size;
size_t length;
char* line;
char* type;
char* d1;
char* d2;
char* beg;
char* name;
char* value;
char* tokctx = NULL;
char* buffer = NULL;
MsRdpEx_RdpFileEntry* entry;

if (!text)
return false;

size = strlen(text);
buffer = _strdup(text);

if (!buffer)
return false;

index = 0;
line = strtok_s(buffer, "\r\n", &tokctx);

while (line)
{
length = strnlen(line, size);

if (length > 1)
{
beg = line;

d1 = strchr(line, ':');

if (!d1)
goto next_line; /* no first delimiter */

type = &d1[1];
d2 = strchr(type, ':');

if (!d2)
goto next_line; /* no second delimiter */

if ((d2 - d1) != 2)
goto next_line; /* improper type length */

*d1 = 0;
*d2 = 0;
name = beg;
value = &d2[1];

if (*type == 'i') /* integer type */
{
MsRdpEx_LogPrint(DEBUG, "RDP(i): %s = %s", name, value);
entry = MsRdpEx_RdpFileEntry_New(*type, name, value);

if (entry) {
MsRdpEx_ArrayList_Add(ctx->entries, entry);
}
}
else if (*type == 's') /* string type */
{
MsRdpEx_LogPrint(DEBUG, "RDP(s): %s = %s", name,
MsRdpEx_IsSensitivePropertyName(name) ? "*omitted*" : value);

entry = MsRdpEx_RdpFileEntry_New(*type, name, value);

if (entry) {
MsRdpEx_ArrayList_Add(ctx->entries, entry);
}
}
else if (*type == 'b') /* binary type */
{

}
}

next_line:
line = strtok_s(NULL, "\r\n", &tokctx);
index++;
}

SecureZeroMemory(buffer, size);
free(buffer);
return true;
}

MsRdpEx_RdpFile* MsRdpEx_RdpFile_New()
{
MsRdpEx_RdpFile* ctx;
Expand Down
Loading

0 comments on commit 7beddce

Please sign in to comment.