2018-09-21 20:45:09 +02:00
|
|
|
## Piston
|
2020-03-26 19:49:19 +01:00
|
|
|
Piston is the underlying engine for running untrusted and possibly malicious
|
|
|
|
code that originates from EMKC contests and challenges. It's also used in the
|
2020-12-31 22:22:44 +01:00
|
|
|
Engineer Man Discord server via [I Run Code](https://github.com/engineer-man/piston-bot) bot as well as 1000+ other servers.
|
|
|
|
To get it in your own server, go here: https://emkc.org/run.
|
2018-09-21 20:45:09 +02:00
|
|
|
|
2020-07-04 07:07:21 +02:00
|
|
|
#### Use Public API (new)
|
|
|
|
Requires no installation and you can use it immediately. Reference the API Usage section below to learn
|
|
|
|
about the request format but rather than using the local URLs, use the following URLs:
|
|
|
|
- `GET` `https://emkc.org/api/v1/piston/versions`
|
|
|
|
- `POST` `https://emkc.org/api/v1/piston/execute`
|
2020-07-04 07:08:17 +02:00
|
|
|
|
2021-01-04 20:35:01 +01:00
|
|
|
Important Note: The Piston API is rate limited to 5 requests per second
|
2020-07-04 07:07:21 +02:00
|
|
|
|
2018-09-21 20:45:09 +02:00
|
|
|
#### Installation
|
2021-01-12 22:11:14 +01:00
|
|
|
Updated installation instructions coming soon. See `var/install.txt` for how to do it from scratch.
|
2018-09-21 20:45:09 +02:00
|
|
|
|
2019-06-17 07:06:38 +02:00
|
|
|
#### CLI Usage
|
2021-01-13 08:36:10 +01:00
|
|
|
- `lxc/execute [language] [file path] [args]`
|
2018-09-21 20:45:09 +02:00
|
|
|
|
2019-06-17 07:06:38 +02:00
|
|
|
#### API Usage
|
2019-06-17 07:09:35 +02:00
|
|
|
To use the API, it must first be started. To start the API, run the following:
|
|
|
|
```
|
|
|
|
cd api
|
|
|
|
./start
|
|
|
|
```
|
2020-07-04 07:07:21 +02:00
|
|
|
|
|
|
|
#### Base URLs
|
|
|
|
For your own local installation, use:
|
|
|
|
```
|
|
|
|
http://127.0.0.1:2000
|
|
|
|
```
|
|
|
|
When using the public Piston API, use:
|
|
|
|
```
|
|
|
|
https://emkc.org/api/v1/piston
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Versions Endpoint
|
|
|
|
`GET /versions`
|
|
|
|
This endpoint takes no input and returns a JSON array of the currently installed languages.
|
|
|
|
|
|
|
|
Truncated response sample:
|
|
|
|
```json
|
2021-01-13 08:36:10 +01:00
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
2020-07-04 07:07:21 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"name": "awk",
|
|
|
|
"version": "1.3.3"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "bash",
|
|
|
|
"version": "4.4.20"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "c",
|
|
|
|
"version": "7.5.0"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Execution Endpoint
|
|
|
|
`POST /execute`
|
2019-06-17 07:06:38 +02:00
|
|
|
This endpoint takes the following JSON payload and expects at least the language and source. If
|
|
|
|
source is not provided, a blank file is passed as the source.
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"language": "js",
|
|
|
|
"source": "console.log(process.argv)",
|
|
|
|
"args": [
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
A typical response when everything succeeds will be similar to the following:
|
|
|
|
```json
|
2021-01-13 08:36:10 +01:00
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
2019-06-17 07:06:38 +02:00
|
|
|
{
|
|
|
|
"ran": true,
|
2020-03-29 21:40:34 +02:00
|
|
|
"language": "js",
|
|
|
|
"version": "12.13.0",
|
2019-06-17 07:06:38 +02:00
|
|
|
"output": "[ '/usr/bin/node',\n '/tmp/code.code',\n '1',\n '2',\n '3' ]"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
If an invalid language is supplied, a typical response will look like the following:
|
|
|
|
```json
|
2021-01-13 08:36:10 +01:00
|
|
|
HTTP/1.1 400 Bad Request
|
|
|
|
Content-Type: application/json
|
|
|
|
|
2019-06-17 07:06:38 +02:00
|
|
|
{
|
|
|
|
"code": "unsupported_language",
|
|
|
|
"message": "whatever is not supported by Piston"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-09-21 20:45:09 +02:00
|
|
|
#### Supported Languages
|
2020-06-09 04:24:15 +02:00
|
|
|
- awk
|
|
|
|
- bash
|
|
|
|
- c
|
|
|
|
- cpp
|
|
|
|
- csharp
|
2020-10-17 19:06:23 +02:00
|
|
|
- deno
|
2021-01-13 08:36:10 +01:00
|
|
|
- erlang
|
2020-06-09 04:24:15 +02:00
|
|
|
- elixir
|
|
|
|
- emacs
|
|
|
|
- go
|
2020-10-17 05:57:45 +02:00
|
|
|
- haskell
|
2020-06-09 04:24:15 +02:00
|
|
|
- java
|
2020-10-17 19:56:02 +02:00
|
|
|
- jelly
|
2020-06-09 04:24:15 +02:00
|
|
|
- julia
|
|
|
|
- kotlin
|
|
|
|
- nasm
|
|
|
|
- node
|
|
|
|
- perl
|
|
|
|
- php
|
|
|
|
- python2
|
|
|
|
- python3
|
2020-10-23 00:12:47 +02:00
|
|
|
- paradoc
|
2020-06-09 04:24:15 +02:00
|
|
|
- ruby
|
|
|
|
- rust
|
|
|
|
- swift
|
|
|
|
- typescript
|
2018-09-21 20:45:09 +02:00
|
|
|
|
2018-09-22 06:15:24 +02:00
|
|
|
#### Principle of Operation
|
2019-05-31 20:09:47 +02:00
|
|
|
Piston utilizes LXC as the primary mechanism for sandboxing. There is a small API written in Go which takes
|
|
|
|
in execution requests and executes them in the container. High level, the API writes
|
|
|
|
a temporary source and args file to `/tmp` and that gets mounted read-only along with the execution scripts into the container.
|
2018-09-22 18:52:19 +02:00
|
|
|
The source file is either ran or compiled and ran (in the case of languages like c, c++, c#, go, etc.).
|
2018-09-22 06:15:24 +02:00
|
|
|
|
|
|
|
#### Security
|
2019-05-31 20:09:47 +02:00
|
|
|
LXC provides a great deal of security out of the box in that it's separate from the system.
|
|
|
|
Piston takes additional steps to make it resistant to
|
2018-09-22 06:15:24 +02:00
|
|
|
various privilege escalation, denial-of-service, and resource saturation threats. These steps include:
|
|
|
|
- Disabling outgoing network interaction
|
2019-06-17 04:52:06 +02:00
|
|
|
- Capping max processes at 64 (resists `:(){ :|: &}:;`, `while True: os.fork()`, etc.)
|
|
|
|
- Capping max files at 2048 (resists various file based attacks)
|
2018-09-22 06:15:24 +02:00
|
|
|
- Mounting all resources read-only (resists `sudo rm -rf --no-preserve-root /`)
|
2021-01-13 08:36:10 +01:00
|
|
|
- Cleaning up all temp space after each execution (resists out of drive space attacks)
|
2019-06-17 04:52:06 +02:00
|
|
|
- Running as a variety of unprivileged users
|
2018-09-22 18:52:19 +02:00
|
|
|
- Capping runtime execution at 3 seconds
|
2018-09-22 06:15:24 +02:00
|
|
|
- Capping stdout to 65536 characters (resists yes/no bombs and runaway output)
|
|
|
|
- SIGKILLing misbehaving code
|
2018-09-21 20:45:09 +02:00
|
|
|
|
|
|
|
#### License
|
|
|
|
Piston is licensed under the MIT license.
|