Docker映像/容器中的Python代码混淆

zu0ti5jz  于 2022-12-01  发布在  Python
关注(0)|答案(2)|浏览(159)

我正在尝试用python在它的模糊形式建立docker图像,所以我尝试了下面的方法

FROM ubuntu:bionic

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
  && apt-get install -y python3-pip python3-dev \
  && cd /usr/local/bin \
  && ln -s /usr/bin/python3 python \
  && pip3 install --upgrade pip

COPY hello-world.py /
COPY requirments.txt /
RUN pip install -r requirments.txt
RUN pyarmor obfuscate 'hello-world.py'
RUN rm -rf hello-world.py
RUN cd dist
CMD ["python", "hello-world.py"]

我得到错误在pyarmor命令

INFO     Start obfuscating the scripts...
INFO        ello-world.py -> dist/ello-world.py
ERROR    [Errno 2] No such file or directory: '/ello-world.py'

我需要一些帮助

4uqofj5v

4uqofj5v1#

将原始文件放在根目录(/)之外似乎可以解决此问题:
第一个

csbfibhn

csbfibhn2#

在构建Docker映像时,务必记住,任何语句(FROMCOPYRUN等)都会创建一个新层,并且所有层都可供以后访问该映像的任何人访问:

$ docker history obf-hello
IMAGE          CREATED              CREATED BY                                      SIZE      COMMENT
faf859dcd93b   45 seconds ago       /bin/sh -c #(nop)  CMD ["python" "dist/hello…   0B        
db189a78c052   46 seconds ago       /bin/sh -c rm -rf hello-world.py                0B        
bb39d058fd4f   47 seconds ago       /bin/sh -c pyarmor obfuscate hello-world.py     1.22MB    
c2b29debdf25   49 seconds ago       /bin/sh -c pip install -r requirements.txt      10.1MB    
dbf9cde1f691   52 seconds ago       /bin/sh -c #(nop) COPY file:5bf416045dde3b2a…   15B       
8e632b7679ea   53 seconds ago       /bin/sh -c #(nop) COPY file:8c0ca98b3d5632b9…   46B       
2020b38099a5   54 seconds ago       /bin/sh -c #(nop) WORKDIR /app                  0B        
b8eba45cc77a   55 seconds ago       /bin/sh -c apt-get update   && apt-get insta…   432MB     
13d7ab1c648b   About a minute ago   /bin/sh -c #(nop)  ENV DEBIAN_FRONTEND=nonin…   0B        
dcf4d4bef137   2 weeks ago          /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      2 weeks ago          /bin/sh -c #(nop) ADD file:c6039a4f004b6b6c2…   63.2MB    
$ docker run --rm bb39d058fd4f cat hello-world.py
#!/usr/bin/env python

print("HELLO WORLD!")

为了避免泄露源代码(无论您使用Python、Java、Go还是其他任何工具),请使用Docker文档https://docs.docker.com/develop/develop-images/multistage-build/中描述的多阶段构建
对于最初的问题,我会提出这样的建议:

FROM ubuntu:bionic as build

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
  && apt-get install -y python3-pip python3-dev \
  && cd /usr/local/bin \
  && ln -s /usr/bin/python3 python \
  && pip3 install --upgrade pip

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

FROM build as temp
COPY hello-world.py .
RUN pyarmor obfuscate hello-world.py

FROM build as dist
COPY --from=temp /app/dist/ dist/
CMD ["python", "dist/hello-world.py"]

请注意,我已经删除了RUN rm -rf hello-world.py,因为这只是一个没有意义的额外步骤。

相关问题