Skip to content

Commit e9da06a

Browse files
committed
change docker root exec
1 parent 6660b18 commit e9da06a

File tree

6 files changed

+46
-22
lines changed

6 files changed

+46
-22
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ docker-compose*.yml
55
docker/
66
!docker/msfconsole.rc
77
!docker/entrypoint.sh
8+
!docker/database.yml
9+
Dockerfile
810
README.md
911
.git/
1012
.github/

Dockerfile

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ FROM ruby:2.5.1-alpine3.7 AS builder
22
LABEL maintainer="Rapid7"
33

44
ARG BUNDLER_ARGS="--jobs=8 --without development test coverage"
5-
ENV APP_HOME /usr/src/metasploit-framework/
5+
ENV APP_HOME=/usr/src/metasploit-framework
66
ENV BUNDLE_IGNORE_MESSAGES="true"
77
WORKDIR $APP_HOME
88

9-
COPY Gemfile* metasploit-framework.gemspec Rakefile $APP_HOME
9+
COPY Gemfile* metasploit-framework.gemspec Rakefile $APP_HOME/
1010
COPY lib/metasploit/framework/version.rb $APP_HOME/lib/metasploit/framework/version.rb
1111
COPY lib/metasploit/framework/rails_version_constraint.rb $APP_HOME/lib/metasploit/framework/rails_version_constraint.rb
1212
COPY lib/msf/util/helper.rb $APP_HOME/lib/msf/util/helper.rb
@@ -40,23 +40,28 @@ RUN apk add --no-cache \
4040
FROM ruby:2.5.1-alpine3.7
4141
LABEL maintainer="Rapid7"
4242

43-
ENV APP_HOME /usr/src/metasploit-framework/
43+
ENV APP_HOME=/usr/src/metasploit-framework
4444
ENV NMAP_PRIVILEGED=""
45+
ENV METASPLOIT_GROUP=metasploit
4546

46-
COPY --from=builder /usr/local/bundle /usr/local/bundle
47-
COPY . $APP_HOME
47+
# used for the copy command
48+
RUN addgroup -S $METASPLOIT_GROUP
4849

4950
RUN apk add --no-cache bash sqlite-libs nmap nmap-scripts nmap-nselibs postgresql-libs python python3 ncurses libcap su-exec
5051

5152
RUN /usr/sbin/setcap cap_net_raw,cap_net_bind_service=+eip $(which ruby)
5253
RUN /usr/sbin/setcap cap_net_raw,cap_net_bind_service=+eip $(which nmap)
5354

55+
COPY --chown=root:metasploit --from=builder /usr/local/bundle /usr/local/bundle
56+
COPY --chown=root:metasploit . $APP_HOME/
57+
RUN cp -f $APP_HOME/docker/database.yml $APP_HOME/config/database.yml
58+
5459
WORKDIR $APP_HOME
60+
5561
# we need this entrypoint to dynamically create a user
5662
# matching the hosts UID and GID so we can mount something
5763
# from the users home directory. If the IDs don't match
58-
# it results in access denied errors. Once docker has
59-
# a solution for this we can revert it back to normal
64+
# it results in access denied errors.
6065
ENTRYPOINT ["docker/entrypoint.sh"]
6166

62-
CMD ["./msfconsole", "-r", "docker/msfconsole.rc"]
67+
CMD ["./msfconsole", "-r", "docker/msfconsole.rc", "-y", "$APP_HOME/config/database.yml"]

docker-compose.override.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ services:
99
BUNDLER_ARGS: --jobs=8
1010
image: metasploit:dev
1111
environment:
12-
DATABASE_URL: postgres://postgres@db:5432/msf_dev
12+
DATABASE_URL: postgres://postgres@db:5432/msf_dev?pool=200&timeout=5
1313
volumes:
1414
- .:/usr/src/metasploit-framework

docker-compose.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ services:
33
ms:
44
image: metasploitframework/metasploit-framework:latest
55
environment:
6-
DATABASE_URL: postgres://postgres@db:5432/msf
6+
DATABASE_URL: postgres://postgres@db:5432/msf?pool=200&timeout=5
77
links:
88
- db
99
ports:
1010
- 4444:4444
1111
volumes:
1212
- $HOME/.msf4:/home/msf/.msf4
13-
- /etc/localtime:/etc/localtime:ro
1413

1514
db:
1615
image: postgres:10-alpine

docker/database.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
development: &pgsql
2+
url: <%= ENV['DATABASE_URL'] %>
3+
4+
production: &production
5+
<<: *pgsql

docker/entrypoint.sh

+24-11
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@ MSF_GROUP=msf
55
TMP=${MSF_UID:=1000}
66
TMP=${MSF_GID:=1000}
77

8-
# don't recreate system users like root
9-
if [ "$MSF_UID" -lt "1000" ]; then
10-
MSF_UID=1000
11-
fi
8+
# if the user starts the container as root or another system user,
9+
# don't use a low privileged user as we mount the home directory
10+
if [ "$MSF_UID" -eq "0" ]; then
11+
"$@"
12+
else
13+
# if the users group already exists, create a random GID, otherwise
14+
# reuse it
15+
if ! grep ":$MSF_GID:" /etc/group > /dev/null; then
16+
echo "asdf"
17+
addgroup -g $MSF_GID $MSF_GROUP
18+
else
19+
addgroup $MSF_GROUP
20+
fi
1221

13-
if [ "$MSF_GID" -lt "1000" ]; then
14-
MSF_GID=1000
22+
# check if user id already exists
23+
if ! grep ":$MSF_UID:" /etc/passwd > /dev/null; then
24+
echo "cvbb"
25+
adduser -u $MSF_UID -D $MSF_USER -g $MSF_USER -G $MSF_GROUP $MSF_USER
26+
# add user to metasploit group so it can read the source
27+
addgroup $MSF_USER $METASPLOIT_GROUP
28+
su-exec $MSF_USER "$@"
29+
# fall back to root exec if the user id already exists
30+
else
31+
"$@"
32+
fi
1533
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)