NEKO

docker入门

2018/05/31

xman2018 pratice的web1的dockerfile写的很好。
方法貌似不能用了,参考这个:https://www.cnblogs.com/tianhei/p/7802064.html

推荐教程:docker从入门到实践


https://blog.csdn.net/wy_97/article/details/78984101

docker安装:

1
2
3
4
5
6
7
8
9
10
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

docker加速:
http://blog.csdn.net/bychahaha/article/details/52762675

docker命令:
https://www.cnblogs.com/go2bed/p/5703117.html

随机映射端口:

1
2
3
4
root@ubuntu:/var/run# docker run -d -p 0.0.0.0::80 c82
root@ubuntu:/var/run# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7c84f27a831 c82 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:32768->80/tcp musing_visvesvaraya

1
sudo docker run -itd -p 80:80 971

删除无用镜像:

1
sudo docker images prune

停止正在运行的容器

1
sudo docker stop $(sudo docker ps -q)

删除所有(已经停止的,正在运行的不能删除)容器

1
sudo docker rm $(sudo docker ps -a -q)

复制本地项目到容器:
docker cp 本地项目路径 容器名:容器地址

docker容器保存为镜像:
docker commit id name:注释

docker容器打包:
docker export id > neko.tar

打包文件恢复为镜像:
sudo cat neko.tar | sudo docker import - neko:neko

安装apache2+php+mysql:
https://www.cnblogs.com/impy/p/8040684.html
把apache2和php链接后要重启apache2生效.
或者直接去docker store pull一个lamp

swarm

https://www.cnblogs.com/xishuai/p/docker-swarm.html

图形化管理

portainer:https://portainer.io/install.html

1
2
3
4
5
6
7
8
9
10
$ docker volume create portainer_data
$ docker service create \
--name portainer \
--publish 9000:9000 \
--replicas=1 \
--constraint 'node.role == manager' \
--mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \
--mount type=volume,src=portainer_data,dst=/data \
portainer/portainer \
-H unix:///var/run/docker.sock

visualizer:

1
docker run -it -d -p 1234:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer

开启远程api

ubunut16.04
docker18.06.0-ce
百度真是不靠谱
Navigate to /lib/system/system in your terminal and open docker.service file
vi /lib/systemd/system/docker.service

Find the line which starts with ExecStart and adds -H=tcp://0.0.0.0:2375 to make it look like
ExecStart=/usr/bin/dockerd -H=fd:// -H=tcp://0.0.0.0:2375

Save the Modified File
Reload the docker daemon
systemctl daemon-reload
Restart the container
sudo service docker restart

Test if it is working by using this command, if everything is fine below command should return a JSON
curl http://localhost:2375/images/json

To test remotely, use the PC name or IP address of Docker Host

一些问题

基于ubuntu16.04构建的容器不能按装php5,ubuntu14.04可以
dockerfile里怎么安装mysql?因为要设置root密码.

docker-compose up -d后容器直接退出:https://blog.csdn.net/boling_cavalry/article/details/79050451
加上tty: true

1
2
3
4
5
6
7
8
version: "3"
services:
neko:
build: .
image: test
tty: true
ports:
- "9999:80"

不要用RUN 的方式启动服务,可以通过CMD或者ENTRYPOINT

比如运行apache2服务和mysql服务
可以写个run.sh:

1
2
3
4
#!/bin/sh

/etc/init.d/apache2 start && /etc/init.d/mysql start && echo "Servers running :)"
sleep infinity

然后在dockerfile中写入:

1
2
COPY src/run.sh /run.sh
CMD ["/run.sh"]

dockerfile中更换源:

1
RUN sed -i "s/http:\/\/archive.ubuntu.com/http:\/\/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.list

一个无奈的坑

git下来到windows后的文本文件都是dos格式的,这样开启docker会报错,需要在vim下

1
:set ff=unix

一些样本

1
2
3
RUN sed -i "s/http:\/\/archive.ubuntu.com/http:\/\/mirrors.aliyun.com/g" /etc/apt/sources.list

printf "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple\n" >> /root/.pip/pip.conf
1
2
3
FROM ubuntu:14.04
MAINTAINER n3k0 <894729083@qq.com>
ENV DEBIAN_FRONTEND noninteractive

一些好用的镜像

1
2
3
4
5
andreisamuilik/php5.5.9-apache2.4-mysql5.5   latest

php apache

php 5-apache

docker-compose布置实例

docker-compose.yml:

1
2
3
4
5
6
7
version: "3"
services:
view-source:
build: .
tty: true
ports:
- "0.0.0.0::80"

(随机端口映射)
dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FROM ubuntu:14.04

RUN apt-get update
RUN apt-get install -y apache2
RUN apt-get install -y php5
RUN apt-get install -y libapache2-mod-php5
RUN service apache2 start
RUN rm /var/www/html/index.html

COPY src/index.php /var/www/html
COPY src/run.sh /run.sh

RUN chmod +x /run.sh

EXPOSE 80

CMD ["/run.sh"]

run.sh:

1
2
3
4
#!/bin/sh

/etc/init.d/apache2 start && echo "Servers running :)"
sleep infinity

sudo docker-compose up -d后:

常规pwn题模板

dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
FROM ubuntu:16.04

RUN sed -i "s/http:\/\/archive.ubuntu.com/http:\/\/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.list && \
apt-get update && apt-get -y dist-upgrade && \
apt-get install -y lib32z1 xinetd libssl1.0.0 libssl-dev

RUN dpkg --add-architecture i386 && apt-get update && apt-get install -y libssl1.0.0:i386 libc6:i386 libncurses5:i386 libstdc++6:i386 libc6-dev-i386 libc6-dbg libc6-dbg:i386 lib32ncurses5 lib32z1 zlib1g:i386

RUN useradd -m ctf

WORKDIR /home/ctf

RUN cp -R /lib* /home/ctf && \
cp -R /usr/lib* /home/ctf

RUN mkdir /home/ctf/dev && \
mknod /home/ctf/dev/null c 1 3 && \
mknod /home/ctf/dev/zero c 1 5 && \
mknod /home/ctf/dev/random c 1 8 && \
mknod /home/ctf/dev/urandom c 1 9 && \
chmod 666 /home/ctf/dev/*

RUN mkdir /home/ctf/bin && \
cp /bin/sh /home/ctf/bin && \
cp /bin/ls /home/ctf/bin && \
cp /bin/cat /home/ctf/bin

COPY ./ctf.xinetd /etc/xinetd.d/ctf
COPY ./start.sh /start.sh
RUN echo "Blocked by ctf_xinetd" > /etc/banner_fail

RUN chmod +x /start.sh

COPY ./bin/ /home/ctf/
RUN chown -R root:ctf /home/ctf && \
chmod -R 750 /home/ctf && \
chmod 740 /home/ctf/flag

CMD ["/start.sh"]

EXPOSE 9999

ctf.xinetd:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
service baby_stack
{
disable = no
socket_type = stream
protocol = tcp
wait = no
user = root
type = UNLISTED
port = 9999
bind = 0.0.0.0
server = /usr/sbin/chroot
# replace helloworld to your program
server_args = --userspec=1000:1000 /home/ctf ./baby_stack
banner_fail = /etc/banner_fail
# safety options
per_source = 10 # the maximum instances of this service per source IP address
rlimit_cpu = 20 # the maximum number of CPU seconds that the service may use
#rlimit_as = 1024M # the Address Space resource limit for the service
#access_times = 2:00-9:00 12:00-24:00
}

常规crypto题模板

和pwn差不多就dockerfile不太一样
dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
FROM ubuntu:xenial

RUN apt-get update && apt-get -y dist-upgrade && \
apt-get install -y xinetd python-pip

RUN pip install --upgrade pip
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pycrypto

RUN useradd -m ctf

COPY ./ctf.xinetd /etc/xinetd.d/ctf
COPY ./start.sh /start.sh

COPY ./bin/ /home/ctf/

RUN echo "Blocked" > /etc/banner_fail

RUN chmod +x /start.sh

RUN chown -R root:ctf /home/ctf && \
chmod -R 750 /home/ctf

CMD ["/start.sh"]

EXPOSE 12345

start.sh:

1
2
3
4
5
6
#!/bin/sh
# Add your startup script

# DO NOT DELETE
/etc/init.d/xinetd start;
sleep infinity;

ctf.xinetd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
service ctf
{
disable = no
socket_type = stream
protocol = tcp
wait = no
user = ctf
type = UNLISTED
port = 12345
bind = 0.0.0.0
server = /usr/bin/python
server_args = -u /home/ctf/cpushop.py
banner_fail = /etc/banner_fail
# safety options
per_source = 10 # the maximum instances of this service per source IP address
rlimit_cpu = 30 # the maximum number of CPU seconds that the service may use
}

server_args那里加一个-u 是为了解决python的缓冲区问题.

或者就一个Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
FROM ubuntu:18.04

RUN apt-get update && apt-get upgrade -y && apt-get install -y python3 python3-pip
RUN apt-get install -y socat
RUN pip3 install pycrypto

COPY /serv.py /

EXPOSE 8040

CMD ["socat", "-T10", "TCP-LISTEN:8040,reuseaddr,fork", "EXEC:'python3 /serv.py'"]

mysql启动fail

需要做一步空操作

1
2
3
4
RUN find /var/lib/mysql/mysql -exec touch -c -a {} + && \
service mysql start && \
mysql -uroot < /tmp/data.sql && \
rm -rf /tmp/data.sql

原文作者: n3k0

发表日期: May 31st 2018, 7:55:04

发出嘶吼: 没有魔夜2玩我要死了

CATALOG
  1. 1. swarm
  2. 2. 图形化管理
  3. 3. 开启远程api
  4. 4. 一些问题
    1. 4.1. 一个无奈的坑
  5. 5. 一些样本
    1. 5.1. 一些好用的镜像
    2. 5.2. docker-compose布置实例
    3. 5.3. 常规pwn题模板
    4. 5.4. 常规crypto题模板
  6. 6. mysql启动fail