0

Following this post, I intent to run this command in my Qt 5.12.6 application on Windows 10 with admin privileges:

powershell -Command "agent.exe  -Verb runAs"

My agent.exe is next to my Qt application executable, i.e. its parent directory would be QCoreApplication::applicationDirPath().


Inspired by another post, my C++/Qt code looks like:

m_agent = new QProcess(this);

QString agentName = "/agent.exe";
// "agent.exe" executable is next to application executable
QString agentPath = QCoreApplication::applicationDirPath() + agentName;
QStringList args = QStringList();
// I'm not sure how to compose `args`
args << "-Command"; // ?
args << agentPath; // ?
args << "-Verb"; // ?
args << "runAs"; // ?
m_agent->start("powershell ", args);

My current args, composed above, is not starting the agent.exe.

My questions is: how should I compose args to be able to run my Windows PowerShell command with Qt?

Megidd
  • 7,089
  • 6
  • 65
  • 142
  • 1
    As a small note, you can chain calls to `<<`, like so: `auto args = QStringList() << "-Command" << agentPath << "-Verb" << "runAs";` or use the initializer_list constructor: `QStringList args = { "-Command", agentPath, "-Verb", "runAs" };` – Botje Dec 23 '19 at 09:35

1 Answers1

2

Some observations helped with fixing the problem.

Now the agent.exe is run as admin with this code:

QString agentName = "/agent.exe";
QString agentPath = QCoreApplication::applicationDirPath() + agentName;
QStringList args = QStringList();
args << "-Command";
args << "Start-Process";
args << agentPath;
args << "-Verb";
args << "runAs";
m_agent->start("powershell", args);

UPDATE

Alternatively, you can do this as suggested by @Botje

QString agentName = "/agent.exe";
QString agentPath = QCoreApplication::applicationDirPath() + agentName;
QStringList args = QStringList();
args = QStringList({"-Command", QString("Start-Process %1 -Verb runAs").arg(agentPath)});
m_agent->start("powershell", args);
Megidd
  • 7,089
  • 6
  • 65
  • 142
  • 1
    Note that the command-line invocation `powershell -Command "agent.exe -Verb runAs"` corresponds to `args={"-Command", QString("%1 -Verb runAs").arg(agentPath)}`. You can escape the quoting misery by passing a [base64-encoded command](https://mikefrobbins.com/2017/06/15/simple-obfuscation-with-powershell-using-base64-encoding/) instead. – Botje Dec 23 '19 at 09:39
  • @Botje Just thought you missed the `"Start-Process"` argument =) – Megidd Dec 23 '19 at 09:43