new docs, better executors, better expansion for languages

This commit is contained in:
Brian Seymour 2020-03-26 13:49:19 -05:00
parent 7bc9b5b63a
commit 3233db5c1f
24 changed files with 131 additions and 59 deletions

View File

@ -57,10 +57,8 @@ func Execute(res http.ResponseWriter, req *http.Request) {
"typescript", "ts", "typescript", "ts",
"php", "php",
"python", "python2", "python3", "python", "python2", "python3",
"r",
"ruby", "ruby",
"swift", "swift",
"brainfuck", "bf",
"rust", "rust",
"bash", "bash",
} }

View File

@ -66,9 +66,6 @@ case "$lang" in
"c#" | "csharp" | "cs") "c#" | "csharp" | "cs")
bin=csharp bin=csharp
;; ;;
"r")
bin=r
;;
"php") "php")
bin=php bin=php
;; ;;
@ -81,9 +78,6 @@ case "$lang" in
"swift") "swift")
bin=swift bin=swift
;; ;;
"brainfuck" | "bf")
bin=brainfuck
;;
"rust") "rust")
bin=rust bin=rust
;; ;;
@ -100,14 +94,12 @@ esac
# runner # runner
timeout -s KILL 10 \ timeout -s KILL 10 \
lxc-attach -n piston -- \ lxc-attach --clear-env -n piston -- \
/bin/bash -c "\ /bin/bash -l -c "bash /exec/$bin $newinc $epoch 2>&1 | head -c 65536"
PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin \
bash /exec/$bin $newinc $epoch 2>&1 | head -c 65536"
# process janitor # process janitor
lxc-attach -n piston -- \ lxc-attach --clear-env -n piston -- \
/bin/bash -c "\ /bin/bash -l -c "\
for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\ for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\
find /tmp -user runner$newinc -exec /bin/rm -rf {} \;\ find /tmp -user runner$newinc -exec /bin/rm -rf {} \;\
" > /dev/null 2>&1 & " > /dev/null 2>&1 &

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 bash code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 bash code.code"

View File

@ -1,2 +0,0 @@
cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 bf code.code"

View File

@ -1,3 +1,3 @@
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 10 gcc -std=c11 -o binary -x c code.code timeout -s KILL 10 gcc -std=c11 -o binary -x c code.code
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary"

View File

@ -1,3 +1,3 @@
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 10 g++ -std=c++17 -o binary -x c++ code.code timeout -s KILL 10 g++ -std=c++17 -o binary -x c++ code.code
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary"

View File

@ -1,3 +1,3 @@
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 10 mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary timeout -s KILL 10 mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary"

View File

@ -1,6 +1,6 @@
cd /tmp/$2 cd /tmp/$2
cp code.code interim.go cp code.code interim.go
file="interim.go" file="interim.go"
GOROOT=/usr/lib/go timeout -s KILL 10 go build $file timeout -s KILL 10 go build $file
file=${file%%.*} file=${file%%.*}
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file"

View File

@ -3,4 +3,4 @@ cp code.code interim.java
name=$(cat interim.java | grep -Eo 'public\s+class\s+([A-Za-z0-9]+)' | sed -n 's/ */ /gp' | cut -d' ' -f3) name=$(cat interim.java | grep -Eo 'public\s+class\s+([A-Za-z0-9]+)' | sed -n 's/ */ /gp' | cut -d' ' -f3)
mv interim.java $name.java mv interim.java $name.java
timeout -s KILL 10 javac $name.java timeout -s KILL 10 javac $name.java
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 java $name" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 java $name"

View File

@ -1,4 +1,4 @@
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 10 nasm -f elf64 -o binary.o code.code timeout -s KILL 10 nasm -f elf64 -o binary.o code.code
timeout -s KILL 10 ld binary.o -o binary timeout -s KILL 10 ld binary.o -o binary
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary"

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 nodejs code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code"

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 php code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 php code.code"

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python2 code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python code.code"

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python3.6 code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python3.6 code.code"

View File

@ -1,2 +0,0 @@
cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 Rscript code.code"

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ruby code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ruby code.code"

View File

@ -1,3 +1,3 @@
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 10 rustc -o binary code.code timeout -s KILL 10 rustc -o binary code.code
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary"

View File

@ -1,2 +1,2 @@
cd /tmp/$2 cd /tmp/$2
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 swift code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 swift code.code"

View File

@ -1,6 +1,6 @@
cd /tmp/$2 cd /tmp/$2
mv code.code interim.ts mv code.code interim.ts
timeout -s KILL 10 /usr/local/bin/tsc interim.ts timeout -s KILL 10 tsc interim.ts
rm interim.ts rm interim.ts
mv interim.js code.code mv interim.js code.code
runuser -l runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 nodejs code.code" runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code"

View File

@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
lxc-attach -n piston lxc-attach --clear-env -n piston

126
readme.md
View File

@ -1,6 +1,7 @@
## Piston ## Piston
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 Engineer Man Discord server via Piston is the underlying engine for running untrusted and possibly malicious
[felix bot](https://github.com/engineer-man/felix). code that originates from EMKC contests and challenges. It's also used in the
Engineer Man Discord server via [felix bot](https://github.com/engineer-man/felix).
#### Installation #### Installation
``` ```
@ -29,25 +30,111 @@ lxc-create -t download -n piston -- --dist ubuntu --release bionic --arch amd64
./shell ./shell
# install all necessary piston dependencies # install all necessary piston dependencies
echo 'source /opt/.profile' >> /opt/.bashrc
echo 'export HOME=/opt' >> /opt/.profile
echo 'export TERM=linux' >> /opt/.profile
export HOME=/opt export HOME=/opt
echo 'export PATH=/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin' >> /root/.bashrc export TERM=linux
echo 'export PATH=$PATH:/root/.cargo/bin' >> /root/.bashrc sed -i 's/\/root/\/opt/' /etc/passwd
sed -i \ sed -i \
's/http:\/\/archive.ubuntu.com\/ubuntu/http:\/\/mirror.math.princeton.edu\/pub\/ubuntu/' \ 's/http:\/\/archive.ubuntu.com\/ubuntu/http:\/\/mirror.math.princeton.edu\/pub\/ubuntu/' \
/etc/apt/sources.list /etc/apt/sources.list
apt-get update apt-get update
apt-get -y install git tzdata nano \ apt-get install -y \
dpkg-dev build-essential python python3 \ nano wget build-essential pkg-config libxml2-dev \
ruby nodejs npm golang php7.2 r-base mono-complete \ libsqlite3-dev mono-complete curl cmake libpython2.7-dev \
nasm openjdk-8-jdk ubuntu-make bf ruby
npm install -g typescript
umake swift # install python2
ln -s /opt/.local/share/umake/swift/swift-lang/usr/bin/swift /usr/bin/swift # final binary: /opt/python2/Python-2.7.17/python -V
curl https://sh.rustup.rs > rust.sh cd /opt && mkdir python2 && cd python2
sh rust.sh -y wget https://www.python.org/ftp/python/2.7.17/Python-2.7.17.tar.xz
ln -s /opt/.cargo/bin/rustc /usr/bin/rustc unxz Python-2.7.17.tar.xz
rm -rf /home/ubuntu tar -xf Python-2.7.17.tar
chmod 777 /tmp cd Python-2.7.17
./configure
# open Modules/Setup and uncomment zlib line
make -j16
echo 'export PATH=$PATH:/opt/python2/Python-2.7.17' >> /opt/.profile
source /opt/.profile
# install python3
# final binary: /opt/python3/Python-3.8.2/python -V
cd /opt && mkdir python3 && cd python3
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
unxz Python-3.8.2.tar.xz
tar -xf Python-3.8.2.tar
cd Python-3.8.2
./configure
make -j16
echo 'export PATH=$PATH:/opt/python3/Python-3.8.2' >> /opt/.profile
source /opt/.profile
# install node.js
# final binary: /opt/nodejs/node-v12.16.1-linux-x64/bin/node -v
cd /opt && mkdir nodejs && cd nodejs
wget https://nodejs.org/dist/v12.16.1/node-v12.16.1-linux-x64.tar.xz
unxz node-v12.16.1-linux-x64.tar.xz
tar -xf node-v12.16.1-linux-x64.tar
echo 'export PATH=$PATH:/opt/nodejs/node-v12.16.1-linux-x64/bin' >> /opt/.profile
source /opt/.profile
# install typescript
# final binary: /opt/nodejs/node-v12.16.1-linux-x64/bin/tsc -v
/opt/nodejs/node-v12.16.1-linux-x64/bin/npm i -g typescript
# install golang
# final binary: /opt/go/go/bin/go version
cd /opt && mkdir go && cd go
wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz
tar -xzf go1.14.1.linux-amd64.tar.gz
echo 'export PATH=$PATH:/opt/go/go/bin' >> /opt/.profile
echo 'export GOROOT=/opt/go/go' >> /opt/.profile
echo 'export GOCACHE=/tmp' >> /opt/.profile
source /opt/.profile
# install php
# final binary: /usr/local/bin/php -v
cd /opt && mkdir php && cd php
wget https://www.php.net/distributions/php-7.4.4.tar.gz
tar -xzf php-7.4.4.tar.gz
cd php-7.4.4
./configure
make -j16
make install
# install rust
# final binary: /opt/.cargo/bin/rustc --version
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
echo 'export PATH=$PATH:/opt/.cargo/bin' >> /opt/.profile
source /opt/.profile
# install swift
# final binary: /opt/swift/swift-5.1.5-RELEASE-ubuntu18.04/usr/bin/swift --version
cd /opt && mkdir swift && cd swift
wget https://swift.org/builds/swift-5.1.5-release/ubuntu1804/swift-5.1.5-RELEASE/swift-5.1.5-RELEASE-ubuntu18.04.tar.gz
tar -xzf swift-5.1.5-RELEASE-ubuntu18.04.tar.gz
echo 'export PATH=$PATH:/opt/swift/swift-5.1.5-RELEASE-ubuntu18.04/usr/bin' >> /opt/.profile
source /opt/.profile
# install nasm
# final binary: /opt/nasm/nasm-2.14.02/nasm -v
cd /opt && mkdir nasm && cd nasm
wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/nasm-2.14.02.tar.gz
tar -xzf nasm-2.14.02.tar.gz
cd nasm-2.14.02
./configure
make -j16
echo 'export PATH=$PATH:/opt/nasm/nasm-2.14.02' >> /opt/.profile
source /opt/.profile
# install java
# final binary: /opt/java/jdk-14/bin/java -version
cd /opt && mkdir java && cd java
wget https://download.java.net/java/GA/jdk14/076bab302c7b4508975440c56f6cc26a/36/GPL/openjdk-14_linux-x64_bin.tar.gz
tar -xzf openjdk-14_linux-x64_bin.tar.gz
echo 'export PATH=$PATH:/opt/java/jdk-14/bin' >> /opt/.profile
source /opt/.profile
# create runnable users and apply limits # create runnable users and apply limits
for i in {1..150}; do for i in {1..150}; do
@ -59,6 +146,10 @@ for i in {1..150}; do
echo "runner$i hard nofile 2048" >> /etc/security/limits.conf echo "runner$i hard nofile 2048" >> /etc/security/limits.conf
done done
# cleanup
rm -rf /home/ubuntu
chmod 777 /tmp
# leave container # leave container
exit exit
@ -106,7 +197,8 @@ If an invalid language is supplied, a typical response will look like the follow
``` ```
#### Supported Languages #### Supported Languages
Currently python2, python3, c, c++, go, node, ruby, r, c#, nasm, php, java, swift, brainfuck, rust, bash, and typescript is supported. Currently python2, python3, c, c++, go, node, ruby, r, c#, nasm, php, java,
swift, brainfuck, rust, bash, and typescript is supported.
#### Principle of Operation #### Principle of Operation
Piston utilizes LXC as the primary mechanism for sandboxing. There is a small API written in Go which takes Piston utilizes LXC as the primary mechanism for sandboxing. There is a small API written in Go which takes

View File

@ -1 +0,0 @@
+[----->+++<]>.++++++++..-----------.>++++++++++.

View File

@ -1 +0,0 @@
print('good')

View File

@ -20,12 +20,8 @@ echo 'testing python2'
../lxc/execute python2 test2.py ../lxc/execute python2 test2.py
echo 'testing python3' echo 'testing python3'
../lxc/execute python3 test3.py ../lxc/execute python3 test3.py
echo 'testing r'
../lxc/execute r test.r
echo 'testing ruby' echo 'testing ruby'
../lxc/execute ruby test.rb ../lxc/execute ruby test.rb
echo 'testing brainfuck'
../lxc/execute bf test.bf
echo 'testing rust' echo 'testing rust'
../lxc/execute rust test.rs ../lxc/execute rust test.rs
echo 'testing swift' echo 'testing swift'