From 6e66b6bbe1c6614c24e04eef5f213d2fbf09b1d6 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Mon, 2 Nov 2020 18:35:50 +0800 Subject: [PATCH] add writeups for docker and unicode --- official/233 同学的 Docker/Dockerfile | 50 +++++++++++++++++ official/233 同学的 Docker/README.md | 56 ++++++++++++++++++++ official/233 同学的字符串工具/README.md | 49 +++++++++++++++++ official/233 同学的字符串工具/string_tool.py | 41 ++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 official/233 同学的 Docker/Dockerfile create mode 100644 official/233 同学的字符串工具/string_tool.py diff --git a/official/233 同学的 Docker/Dockerfile b/official/233 同学的 Docker/Dockerfile new file mode 100644 index 0000000..ec96360 --- /dev/null +++ b/official/233 同学的 Docker/Dockerfile @@ -0,0 +1,50 @@ +# Set the base image to use to centos 7 +FROM centos:7 + +# Set the file maintainer +MAINTAINER Software_Engineering_Project + +# Install necessary tools +RUN yum -y install wget make yum-utils + +# Install python dependencies +RUN yum-builddep python -y + +# Install tools needed +RUN yum -y install gcc +RUN yum -y install vim +RUN yum -y install mariadb-devel + +# Download the python3.7.3 +RUN wget -O /tmp/Python-3.7.3.tgz https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz + +# Build and install python3.7.3 +RUN tar -zxvf /tmp/Python-3.7.3.tgz -C /tmp/ +RUN /tmp/Python-3.7.3/configure +RUN make && make install + +# Create symbolic link +RUN rm -f /usr/bin/python +RUN ln -s /usr/local/bin/python3 /usr/bin/python +RUN ln -s /usr/local/bin/pip3 /usr/bin/pip + +# Upgrade the pip +RUN pip install --upgrade pip + +# Fix the yum +RUN sed -i 's/python/python2/' /usr/bin/yum + +# Clean +RUN rm -rf /tmp/Python-3.7.3* +RUN yum clean all + +RUN pip3 install ipython +RUN pip3 install bpython +RUN pip3 install pipenv + +ENV PYTHONUNBUFFERED 1 +RUN mkdir /code +WORKDIR /code +COPY . /code/ +RUN rm /code/flag.txt +ENTRYPOINT python /code/app.py diff --git a/official/233 同学的 Docker/README.md b/official/233 同学的 Docker/README.md index 8e0d501..0e5e227 100644 --- a/official/233 同学的 Docker/README.md +++ b/official/233 同学的 Docker/README.md @@ -1 +1,57 @@ # 233 同学的 Docker + +[这道题的 Dockerfile](./Dockerfile) + +## 命题背景与思路 + +Docker 可能是目前最流行的容器管理器,但很多人(包括一些培训机构)并没有理解 `RUN` 的真正功能,而是把 Dockerfile 当 shell 脚本去写,最后了生成巨大的镜像。这道题提示大家,垃圾文件没删事小,泄漏隐私事大。一定要合理使用 `RUN`! + +## 解法 + +对于没有了解过 Docker 的同学来说,此时从头学起也不晚。如果你看的是质量过硬的资料,它应该会告诉你,每条 `RUN` 命令将产生一个新的 layer,旧 layer 将被保留下来。此时题设中的 + +> 于是写了一行命令删掉这个文件 + +应该会提示到你:233 同学用的应该正是诸如 `RUN rm flag.txt` 这样的命令。结合上面所说的 “旧 layer 被保留”,应该是有办法把某个 layer 中的文件取出来的。于是,接下来的思路就是:找到这个 layer,找到这个文件。 + +既然有思路了,就可以着手解题了。第一步当然是把镜像拉下来。 + +``` +docker pull 8b8d3c8324c7/stringtool +``` + +这里的 Dockerfile 因为是从真实项目中复制的,所以大小也非常真实,请耐心等待。 + +### Docker 镜像分析 + +这里有诸多手段,可以供大家参考。 + +1. 直接在 Docker Hub 的 [Image Layers](https://hub.docker.com/layers/8b8d3c8324c7/stringtool/latest/images/sha256-aef87a00ad7a4e240e4b475ea265d3818c694034c26ec227d8d4f445f3d93152?context=repo) 页面上看。 +2. 使用 [`dive`](https://github.com/wagoodman/dive)。 +3. 使用 `docker inspect` 之类的命令。 + +最终可以确认到 flag 应该在 `/code/flag.txt` 中。 + +### 提取文件 + +这里也有诸多手段,一种简单的方法是使用 `docker save`。当然,还有不少别的方法,可以自行搜索探索。 + +``` +docker save 8b8d3c8324c7/stringtool > img.tar +mkdir img +tar xvf img.tar -C img +``` + +至于之后是怎么确定文件在哪里,就各凭本事了。比如 grep,比如暴力展开所有 tar。实际上可以直接去看 manifest.json,倒数第二个就是倒数第二层(文件到底在哪一层可以通过上一步分析得出)。 + +``` +cd c319bce601a5672aa9ff8297cfde8f65479a58857c1da43f6cd764df62116d9d +tar xvf layer.tar +cat code/flag.txt +``` + +这时就会输出我们想要的 flag: + +``` +flag{Docker_Layers!=PS_Layers_hhh} +``` diff --git a/official/233 同学的字符串工具/README.md b/official/233 同学的字符串工具/README.md index 3489a88..d615efc 100644 --- a/official/233 同学的字符串工具/README.md +++ b/official/233 同学的字符串工具/README.md @@ -1 +1,50 @@ # 233 同学的字符串工具 + +[这道题的源代码 string_tool.py](./string_tool.py) + +## 命题背景与思路 + +这两道题主要目的是科普与 Unicode 相关的一些知识。目前 Unicode 字符集及其相关的 UTF-8 等编码在互联网上得到了广泛应用,但很多开发者仍然对 Unicode 及其编码的一些「坑点」缺乏认知,如认为 UTF-16 是定长的,认为 UCS-4 中一个码位就是一个字符,没有充分考虑到正则化和 Unicode 的国际化特征等等。 + +本题两问均有实际背景。 + +## 「字符串大写工具」题解 + +这道题的背景见 [Hacking Github with Unicode](https://eng.getwisdom.io/hacking-github-with-unicode-dotless-i/)。 + +代码的意思是:如果我们输入一个字面上不是 "flag" 但转换为大写后会变成 "FLAG" 的字符串,就可以得到 flag。 + +我们可以以 "unicode uppercase collision" 为关键字搜索,不难找到一个连字(ligature) + +``` +fl (0xFB02) +``` + +这个“字符”将在转换为大写时变成 `FL` 两个字符!因此,只需输入 `flag` 即可得到 flag。 + +``` +flag{badunic0debadbad} +``` + +## 「UTF-7 转换工具」题解 + +这道题的背景见 [remove UTF-7 from browser encoding menus](https://bugzilla.mozilla.org/show_bug.cgi?id=441876)。 + +代码的意思是:如果我们输入一个字面上不是 "flag" 但从 UTF-7 转换为 UTF-8 后会变成 "flag" 的字符串,就可以得到 flag。 + +不妨查阅 UTF-7 相关资料。可以得知:一个 Unicode 字符串,在 UTF-7 编码下,可能有多种编码,甚至纯粹的 ASCII 字符串也可以有多种编码! + +那么事情就简单了。我们依照 Wikipedia 等参考资料给出的 UTF-7 编码算法,可以构造出 "flag" 的另一种“写法”。比如,选择 `f` 下手。 + +1. `f` 的 Unicode 码位是 0x66 +2. 将 0x66 写成 16 位二进制数:`0000 0000 0110 0110` +3. 重新分组:`000000 000110 011000` +4. 使用 [base64 的编码表](https://en.wikipedia.org/wiki/Base64#Base64_table),将每组变成一个字符:`AGY` + +那么最终 "flag" 的另一种 UTF-7 替代写法就是 `+AGY-lag`,输入即可得到 flag。 + +``` +flag{please_visit_www.utf8everywhere.org} +``` + +(是的,你应该去看这个网站!) diff --git a/official/233 同学的字符串工具/string_tool.py b/official/233 同学的字符串工具/string_tool.py new file mode 100644 index 0000000..3315ab3 --- /dev/null +++ b/official/233 同学的字符串工具/string_tool.py @@ -0,0 +1,41 @@ +import re + +def to_upper(s): + r = re.compile('[fF][lL][aA][gG]') + if r.match(s): + print('how dare you') + elif s.upper() == 'FLAG': + print('yes, I will give you the flag') + print(open('/flag1').read()) + else: + print('%s' % s.upper()) + +def to_utf8(s): + r = re.compile('[fF][lL][aA][gG]') + s = s.encode() # make it bytes + if r.match(s.decode()): + print('how dare you') + elif s.decode('utf-7') == 'flag': + print('yes, I will give you the flag') + print(open('/flag2').read()) + else: + print('%s' % s.decode('utf-7')) + +def main(): + print('Welcome to the best string tool here!') + print('Brought to you by 233 PROUDLY') + print('') + print('Which tool do you want?') + print('1. Convert my string to UPPERCASE!!') + print('2. Convert my UTF-7 string to UTF-8!!') + choice = input() + if choice[0] == '1': + print('Welcome to the capitalizer tool, please input your string: ') + to_upper(input()) + elif choice[0] == '2': + print('Welcome to the UTF-7->UTF-8 tool, please input your string: ') + to_utf8(input()) + else: + print('I am confused, madam') + +main()