1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-15 07:43:50 +00:00

1. Fixed -resetnodeid for Windows Service

2. Updated PE Parser
This commit is contained in:
Bryan Roe
2022-06-03 01:19:47 -07:00
parent d5e0582dbc
commit 39150e1c27
5 changed files with 170 additions and 29 deletions

View File

@@ -4853,26 +4853,7 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char **
ILibRemoteLogging_printf(ILibChainGetLogger(agentHost->chain), ILibRemoteLogging_Modules_Microstack_Generic, ILibRemoteLogging_Flags_VerbosityLevel_1, "agentcore: argv[0] = %s", param[0]);
#if defined(_WINSERVICE)
// If running as a windows services, check the "ResetNodeId" key.
{
HKEY hKey;
DWORD len = 0;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\Open Source\\MeshAgent2"), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
if (RegQueryValueExA(hKey, TEXT("ResetNodeId"), NULL, NULL, NULL, &len) == ERROR_SUCCESS && len > 0)
{
if (RegDeleteValue(hKey, TEXT("ResetNodeId")) == ERROR_SUCCESS)
{
// Force certificate reset
ILIBLOGMESSAGEX("NodeID will reset, because ResetNodeID key was found in registry");
resetNodeId = 1;
}
}
RegCloseKey(hKey);
}
}
#else
#if !defined(_WINSERVICE)
// If running in console mode, check the --resetnodeid command switch
if (parseCommands != 0)
{
@@ -4960,6 +4941,22 @@ int MeshAgent_AgentMode(MeshAgentHostContainer *agentHost, int paramLen, char **
}
#endif
}
#if defined(_WINSERVICE)
duk_push_sprintf(tmpCtx, "require('_agentNodeId').checkResetNodeId('%s');", agentHost->meshServiceName);
if (duk_peval(tmpCtx) == 0)
{
if (duk_is_boolean(tmpCtx, -1) && duk_get_boolean(tmpCtx, -1) != 0)
{
resetNodeId = 1;
ILIBLOGMESSAGEX("NodeID will reset, because ResetNodeId was set in the registry");
}
}
else
{
char *tmp = (char*)duk_safe_to_string(tmpCtx, -1);
ILIBLOGMESSAGEX("Error checking ResetNodeId in registry: %s", tmp);
}
#endif
#endif
#if !defined(MICROSTACK_NOTLS)

View File

@@ -635,6 +635,13 @@ int wmain(int argc, char* wargv[])
integratedJavaScript = ILibString_Copy(script, sizeof(script) - 1);
integragedJavaScriptLen = (int)sizeof(script) - 1;
}
if (argc == 2 && (strcasecmp(argv[1], "-resetnodeid") == 0))
{
// Set "resetnodeid" in registry
char script[] = "require('_agentNodeId').resetNodeId();process.exit();";
integratedJavaScript = ILibString_Copy(script, sizeof(script) - 1);
integragedJavaScriptLen = (int)sizeof(script) - 1;
}
CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (argc > 1 && strcasecmp(argv[1], "-updaterversion") == 0)
@@ -798,12 +805,6 @@ int wmain(int argc, char* wargv[])
if (len > 0) { printf_s(data); }
}
#endif
else if (argc == 2 && (strcasecmp(argv[1], "-resetnodeid") == 0))
{
// Set "resetnodeid" in registry
wmain_free(argv);
return 0;
}
else
{
int skip = 0;

File diff suppressed because one or more lines are too long

View File

@@ -203,6 +203,7 @@ function getVersionInfoData(fd, header)
const actualPtr = (verInfo.offsetToData - header.sections['.rsrc'].virtualAddr) + ptr;
var buffer = Buffer.alloc(verInfo.size);
require('fs').readSync(fd, buffer, 0, buffer.length, actualPtr);
header.resourcebuffer = buffer;
return (buffer);
}
}
@@ -272,6 +273,7 @@ function readStringTableStruct(buf, ptr)
r.szKey = require('_GenericMarshal').CreateVariable(buf.slice(ptr + 6, ptr + 6 + 16)).Wide2UTF8; // An 8-digit hexadecimal number stored as a Unicode string.
//console.log('readStringTableStruct', r.wLength, r.wValueLength, r.wType, r.szKey);
r.strings = readStringStructs(buf, ptr + 24 + r.wValueLength, r.wLength - 22);
console.info1('readStringTableStruct', JSON.stringify(r, null, 1));
return r;
}
@@ -315,10 +317,15 @@ function readVersionInfo(buf, ptr)
r.szKey = require('_GenericMarshal').CreateVariable(buf.slice(ptr + 6, ptr + 36)).Wide2UTF8;
if (r.szKey != 'VS_VERSION_INFO') return null;
////console.log('getVersionInfo', r.wLength, r.wValueLength, r.wType, r.szKey.toString());
if (r.wValueLength == 52) { r.fixedFileInfo = readFixedFileInfoStruct(buf, ptr + 40); }
if (r.wValueLength == 52)
{
r.fixedFileInfoBuffer = buf.slice(ptr + 40, ptr + 40 + 52);
r.fixedFileInfo = readFixedFileInfoStruct(buf, ptr + 40);
}
r.stringFiles = readStringFilesStruct(buf, ptr + 40 + r.wValueLength, r.wLength - 40 - r.wValueLength);
return r;
}
function getVersionInfo(fd, header, resources)
{
var r = {};
@@ -334,6 +341,107 @@ function getVersionInfo(fd, header, resources)
for (var i in strings) { r[strings[i].key] = strings[i].value; }
return r;
}
function encodeVersionInfo(info)
{
console.log(JSON.stringify(info, null, 1));
var i;
var tableLen = 0;
for (i = 0; i < info.stringFiles[0].stringTable.strings.length; ++i)
{
// Update wValueLength fields
info.stringFiles[0].stringTable.strings[i].wValueLength = info.stringFiles[0].stringTable.strings[i].value.length + 1;
// Calculate Padding:
var p = 6 + (2 * (info.stringFiles[0].stringTable.strings[i].key.length + 1));
p = (4 - (p % 4)) % 4;
// Update wLength fields
info.stringFiles[0].stringTable.strings[i].wLength = p + (2*info.stringFiles[0].stringTable.strings[i].wValueLength) + (2*(info.stringFiles[0].stringTable.strings[i].key.length + 1)) + 6;
tableLen += info.stringFiles[0].stringTable.strings[i].wLength;
console.log(info.stringFiles[0].stringTable.strings[i]);
}
// Update stringTable wLength
info.stringFiles[0].stringTable.wLength = 6 + (2 * (info.stringFiles[0].stringTable.szKey.length)) + 1;
console.log('X=>' + info.stringFiles[0].stringTable.wLength);
info.stringFiles[0].stringTable.wLength += ((4 - (info.stringFiles[0].stringTable.wLength % 4)) % 4);
info.stringFiles[0].stringTable.wLength += tableLen;
// Update Wlength
info.stringFiles[0].wLength = 6 + (2 * info.stringFiles[0].szKey.length + 1);
info.stringFiles[0].wLength += ((4 - (info.stringFiles[0].wLength % 4)) % 4);
info.stringFiles[0].wLength += info.stringFiles[0].stringTable.wLength;
console.log(JSON.stringify(info.stringFiles, null, 1));
// Calculate Table Lengths:
var tableLengths = 0;
for(i=0;i<info.stringFiles.length;++i)
{
tableLengths += (info.stringFiles[i].wLength + ((4 - (info.stringFiles[i].wLength % 4)) % 4));
}
console.log('tableLengths: ' + tableLengths);
console.log(info.szKey);
var bufLen = 0;
var loc1, loc2;
bufLen = (info.szKey.length + 1) * 2;
bufLen += 6;
bufLen += ((4 - (bufLen % 4)) % 4);
loc1 = bufLen;
bufLen += info.wValueLength;
bufLen += ((4 - (bufLen % 4)) % 4);
loc2 = bufLen;
bufLen += tableLengths;
console.log('bufLen: ' + bufLen);
console.log('location ', loc1, loc2);
var out = Buffer.alloc(bufLen);
out.writeUInt16LE(bufLen);
out.writeUInt16LE(info.wValueLength, 2);
out.writeUInt16LE(info.wType, 4);
require('_GenericMarshal').CreateVariable(info.szKey, { wide: true }).toBuffer().copy(out, 6);
info.fixedFileInfoBuffer.copy(out, loc1);
for (i = 0; i < info.stringFiles.length; ++i)
{
out.writeUInt16LE(info.stringFiles[i].wLength, loc2); loc2 += 4; // Yes, incremented by 4, becuase the next DWORD is 0, and is already 0
out.writeUInt16LE(info.stringFiles[i].wType, loc2); loc2 += 2;
loc2 += require('_GenericMarshal').CreateVariable(info.stringFiles[i].szKey, { wide: true }).toBuffer().copy(out, loc2);
loc2 += ((4 - (loc2 % 4)) % 4);
console.log('Writing: ' + info.stringFiles[i].szKey);
if (info.stringFiles[i].stringTable == null) { continue; }
// Write String Table
out.writeUInt16LE(info.stringFiles[i].stringTable.wLength, loc2); loc2 += 2;
out.writeUInt16LE(info.stringFiles[i].stringTable.wValueLength, loc2); loc2 += 2;
out.writeUInt16LE(info.stringFiles[i].stringTable.wType, loc2); loc2 += 2;
loc2 += require('_GenericMarshal').CreateVariable(info.stringFiles[i].stringTable.szKey, { wide: true }).toBuffer().copy(out, loc2);
loc2 += ((4 - (loc2 % 4)) % 4);
for(var j=0;j<info.stringFiles[i].stringTable.strings.length;++j)
{
out.writeUInt16LE(info.stringFiles[i].stringTable.strings[j].wLength, loc2); loc2 += 2;
out.writeUInt16LE(info.stringFiles[i].stringTable.strings[j].wValueLength, loc2); loc2 += 2;
out.writeUInt16LE(info.stringFiles[i].stringTable.strings[j].wType, loc2); loc2 += 2;
loc2 += require('_GenericMarshal').CreateVariable(info.stringFiles[i].stringTable.strings[j].key, { wide: true }).toBuffer().copy(out, loc2);
loc2 += ((4 - (loc2 % 4)) % 4);
loc2 += require('_GenericMarshal').CreateVariable(info.stringFiles[i].stringTable.strings[j].value, { wide: true }).toBuffer().copy(out, loc2);
loc2 += ((4 - (loc2 % 4)) % 4);
}
//console.log(info.stringFiles[i]);
}
var tst = readVersionInfo(out, 0);
console.log('TEST');
console.log(JSON.stringify(tst, null, 1));
}
module.exports = parse;
module.exports.readVersionInfo = readVersionInfo;
module.exports.encodeVersionInfo = encodeVersionInfo;

View File

@@ -114,6 +114,41 @@ function _meshName()
return (name);
}
function _resetNodeId()
{
var name = _meshName();
require('win-registry').WriteKey(require('win-registry').HKEY.LocalMachine, 'Software\\Open Source\\' + name, 'ResetNodeId', 1);
console.log('Resetting NodeID for: ' + name);
}
function _checkResetNodeId(name)
{
var status = false;
try
{
// Check if reset node id was set in the registry
status = require('win-registry').QueryKey(require('win-registry').HKEY.LocalMachine, 'Software\\Open Source\\' + name, 'ResetNodeId') == 1 ? true : false;
}
catch(x)
{
}
if (status)
{
try
{
// Delete the reset node id field in the registry
require('win-registry').DeleteKey(require('win-registry').HKEY.LocalMachine, 'Software\\Open Source\\' + name, 'ResetNodeId');
}
catch(y)
{
// If we can't delete it, we must pretend that it was never set, otherwise we risk getting in a loop where we constantly reset the node id
status = false;
}
}
return (status);
}
module.exports = _meshNodeId;
module.exports.serviceName = _meshName;
module.exports.resetNodeId = _resetNodeId;
module.exports.checkResetNodeId = _checkResetNodeId;