# swift

> Arbitrarily execute swift code from the terminal.

The swift command is an interactive environment (REPL) for Swift.

- Page: https://loobins.io/binaries/swift/
- Author: 0v3rride (https://github.com/0v3rride)
- Created: 2023-12-23
- Tactics: Execution, Defense Evasion

## Paths

- `/usr/bin/swift`

## Example Use Cases

### Execute Swift code file

Executes the Swift code that is in a .swift file

```bash
swift mycode.swift
```

- Tactics: Execution
- Tags: swift, repl, bash, zsh

### Execute Swift one-liner before swift 5.8 / Xcode 14.3 Beta 1

Executes a Swift one-liner by piping an echoed string into the swift command

```bash
echo 'print("loobins")' | swift -
```

- Tactics: Execution, Defense Evasion
- Tags: swift, repl, bash, zsh

### Execute Swift one-liner with swift 5.8 / Xcode 14.3 Beta 1 or greater

Executes a Swift one-liner that executes the ls command to list the current directory using the -e option that was implemented in swift 5.8 / Xcode 14.3 Beta 1

```bash
swift -e 'import Foundation; let process = Process(); process.executableURL = URL(fileURLWithPath:"/bin/bash"); process.arguments = ["-c", "ls -alh"]; let stdout = Pipe(); let stderr = Pipe(); process.standardOutput = stdout; process.standardError = stderr; try process.run(); print(String(decoding: stdout.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)); print(String(decoding: stderr.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self));'
```

- Tactics: Execution, Defense Evasion
- Tags: swift, repl, bash, zsh

## Detections

- Process & Command Line Argument Detection (process contains swift)
- [Jamf Protect: Detect arbitrary code execution using a swift one-liner](https://github.com/jamf/jamfprotect/blob/main/custom_analytic_detections/swift_oneline_command_execution)

## Resources

- [Introduction to the Swift REPL](https://developer.apple.com/swift/blog/?id=18)
- [Scripting and Compiling Swift on the Command Line](https://jblevins.org/log/swift)
- [Scripting in Swift is Pretty Awesome](https://krakendev.io/blog/scripting-in-swift)
- [Swift -e runs code directly from the command line](https://blog.eidinger.info/swift-e-runs-code-directly-from-the-command-line)
- [Swift Programming From The Command Line](https://ed.com/command-line-swift/)
