Skip to content

Commit e48f538

Browse files
committed
Land rapid7#9568, handle mismatch uid/gids in docker images
2 parents eaca91c + 70ad419 commit e48f538

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

Dockerfile

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ LABEL maintainer="Rapid7"
33

44
ARG BUNDLER_ARGS="--jobs=8 --without development test coverage"
55
ENV APP_HOME /usr/src/metasploit-framework/
6-
ENV MSF_USER msf
76
ENV NMAP_PRIVILEGED=""
87
ENV BUNDLE_IGNORE_MESSAGES="true"
98
WORKDIR $APP_HOME
@@ -15,6 +14,7 @@ COPY lib/msf/util/helper.rb $APP_HOME/lib/msf/util/helper.rb
1514

1615
RUN apk update && \
1716
apk add \
17+
bash \
1818
sqlite-libs \
1919
nmap \
2020
nmap-scripts \
@@ -24,6 +24,7 @@ RUN apk update && \
2424
python3 \
2525
ncurses \
2626
libcap \
27+
su-exec \
2728
&& apk add --virtual .ruby-builddeps \
2829
autoconf \
2930
bison \
@@ -47,13 +48,16 @@ RUN apk update && \
4748
&& apk del .ruby-builddeps \
4849
&& rm -rf /var/cache/apk/*
4950

50-
RUN adduser -g msfconsole -D $MSF_USER
51-
5251
RUN /usr/sbin/setcap cap_net_raw,cap_net_bind_service=+eip $(which ruby)
5352
RUN /usr/sbin/setcap cap_net_raw,cap_net_bind_service=+eip $(which nmap)
5453

55-
USER $MSF_USER
56-
5754
ADD ./ $APP_HOME
5855

56+
# we need this entrypoint to dynamically create a user
57+
# matching the hosts UID and GID so we can mount something
58+
# from the users home directory. If the IDs don't match
59+
# it results in access denied errors. Once docker has
60+
# a solution for this we can revert it back to normal
61+
ENTRYPOINT ["docker/entrypoint.sh"]
62+
5963
CMD ["./msfconsole", "-r", "docker/msfconsole.rc"]

docker/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33

44
To run `msfconsole`
55
```bash
6-
docker-compose build
7-
docker-compose run --rm --service-ports ms
6+
./docker/bin/msfconsole
87
```
8+
99
or
10+
1011
```bash
11-
./docker/bin/msfconsole
12+
docker-compose build
13+
docker-compose run --rm --service-ports -e MSF_UID=$(id -u) -e MSF_GID=$(id -g) ms
1214
```
13-
1415
To run `msfvenom`
1516
```bash
16-
docker-compose build
17-
docker-compose run --rm --no-deps ms ./msfvenom
17+
./docker/bin/msfvenom
1818
```
19+
1920
or
21+
2022
```bash
21-
./docker/bin/msfvenom
23+
docker-compose build
24+
docker-compose run --rm --no-deps -e MSF_UID=$(id -u) -e MSF_GID=$(id -g) ms ./msfvenom
2225
```
2326

2427
You can pass any command line arguments to the binstubs or the docker-compose command and they will be passed to `msfconsole` or `msfvenom`. If you need to rebuild an image (for example when the Gemfile changes) you need to build the docker image using `docker-compose build` or supply the `--rebuild` parameter to the binstubs.

docker/bin/msfconsole

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ if [[ $PARAMS == *"--rebuild"* ]]; then
2727
exit $?
2828
fi
2929

30-
docker-compose run --rm --service-ports ms ./msfconsole -r docker/msfconsole.rc "$PARAMS"
30+
docker-compose run --rm --service-ports -e MSF_UID=$(id -u) -e MSF_GID=$(id -g) ms ./msfconsole -r docker/msfconsole.rc "$PARAMS"

docker/entrypoint.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
MSF_USER=msf
4+
MSF_GROUP=msf
5+
TMP=${MSF_UID:=1000}
6+
TMP=${MSF_GID:=1000}
7+
8+
# don't recreate system users like root
9+
if [ "$MSF_UID" -lt "1000" ]; then
10+
MSF_UID=1000
11+
fi
12+
13+
if [ "$MSF_GID" -lt "1000" ]; then
14+
MSF_GID=1000
15+
fi
16+
17+
addgroup -g $MSF_GID $MSF_GROUP
18+
adduser -u $MSF_UID -D $MSF_USER -g $MSF_USER -G $MSF_GROUP $MSF_USER
19+
20+
su-exec $MSF_USER "$@"

0 commit comments

Comments
 (0)