Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

はてなリモートインターン2020 コンテナ 講義資料

はてなリモートインターン2020 コンテナ 講義資料

Hatena

May 31, 2022
Tweet

More Decks by Hatena

Other Decks in Technology

Transcript

  1. ˖ .PVOU/BNFTQBDF ˖ ؿ؋؎ٕءأذيךوؐٝزه؎ٝزךꥫꨄ ˖ QJWPU@SPPU ˖ 1*%/BNFTQBDF ˖ فٗإأ*%殢〾瑞꟦ךꥫꨄ

    ˖ せ⵸瑞꟦ⰻד剑ⴱךفٗإأכQJE ˖ /procQSPDGTכ׉ך1*%せ⵸瑞꟦ⰻך فٗإأחך׫،ؙإأדֹ׷
  2. TFDDPNQ ˖ فٗإأך涪遤דֹ׷ءأذي؝٦ٕ׾ⵖꣲׅ׷➬穈׫ ˖ TUSJDUٌ٦سSFBE XSJUF @FYJU TJHSFUVSOך׫ ˖ MUFSٌ٦سCQGח״׷ؿ؍ٕةָ〳腉

    ˖ %PDLFSדرؿٕؓزדⵖꣲׁ׸גְ׷ءأذي؝٦ٕך♧鿇 perf_event_open, pivot_root, process_vm_readv, process_vm_writev, ptrace
  3. ⴓד׻ַ׷%PDLFS$-* ءٕؑך酡㸣ך鏣㹀 IUUQTEPDTEPDLFSDPNDPNQPTFDPNQMFUJPO 倜׋ח؝ٝذش׾㹋遤ׅ׷ % docker run !"rm -ti <image>

    <command> 饯⹛׃גְ׷؝ٝذشⰻד؝وٝس׾㹋遤ׅ׷ % docker exec -ti <container id> <command>
  4. ؝ٝذشⰻךؿ؋؎ٕ׾مأزח؝ؾ٦ % docker cp <container id!"<src path> <dst path> 饯⹛׃גְ׷؝ٝذشך♧鋮

    % docker ps ٖآأزٔ%PDLFS)VCַ׵؎ً٦آ׾تؐٝٗ٦س % docker pull <image>
  5. # syntax = docker/dockerfile:experimental FROM golang:1.14-alpine AS builder RUN apk

    !"update add make WORKDIR /services/blog COPY go.mod go.sum ./ RUN go mod download COPY Makefile ./ RUN make setup COPY . . RUN !"mount=type=cache,target=/root/.cache/go-build make build FROM alpine COPY !"from=builder /(snip)/server /(snip)/server RUN adduser -D -u 1000 app USER 1000 ENTRYPOINT ["/services/blog/bin/server"] %PDLFSMF ˖ %PDLFS؎ً٦آכ %PDLFSMFַ׵docker build؝وٝسד欰䧭 % docker build -f Dockerfile .
  6. # syntax = docker/dockerfile:experimental FROM golang:1.14-alpine AS builder RUN apk

    !"update add make WORKDIR /services/blog COPY go.mod go.sum ./ RUN go mod download COPY Makefile ./ RUN make setup COPY . . RUN !"mount=type=cache,target=/root/.cache/go-build make build FROM alpine COPY !"from=builder /(snip)/server /(snip)/server RUN adduser -D -u 1000 app USER 1000 ENTRYPOINT ["/services/blog/bin/server"] ٖ؎ٍَؗحءُ ˖ ؽٕس儗꟦ך瀉簭ך׋׭ծ ㄏ⟀⽃⡘דٍؗحءׁُ׸ ׷ ˖ 㹋遤ח儗꟦ַַָ׶ծ㢌刿 걼䏝ך㼰זְ׮ך׾⯓ח㹋 遤ׅ׷
  7. # syntax = docker/dockerfile:experimental FROM golang:1.14-alpine AS builder RUN apk

    !"update add make WORKDIR /services/blog COPY go.mod go.sum ./ RUN go mod download COPY Makefile ./ RUN make setup COPY . . RUN !"mount=type=cache,target=/root/.cache/go-buil make build FROM alpine COPY !"from=builder /(snip)/server /(snip)/server RUN adduser -D -u 1000 app USER 1000 ENTRYPOINT ["/services/blog/bin/server"] NVMUJTUBHFCVJMET ˖ ؟؎ؤ׾㼭ֻׁ⥂א׋׭ ˖ ،فٔ؛٦ءّٝך⹛⡲ח 䗳銲ז⣛㶷ך׫ろ׭׷ ˖ docker buildך!" targetؔفءّٝ
  8. # syntax = docker/dockerfile:experimental FROM golang:1.14-alpine AS builder RUN apk

    !"update add make WORKDIR /services/blog COPY go.mod go.sum ./ RUN go mod download COPY Makefile ./ RUN make setup COPY . . RUN !"mount=type=cache,target=/root/.cache/go-build make build FROM alpine COPY !"from=builder /(snip)/server /(snip)/server RUN adduser -D -u 1000 app USER 1000 ENTRYPOINT ["/services/blog/bin/server"] CVJMELJU ˖ %PDLFS״׶姻䒭堣腉 הז׏׋倜׃ְؽٕت٦ ˖ DOCKER_BUILDKIT=1 ˖ ؽٕس儗חٍؗحءُךو ؐٝزָדֹ׷
  9. !"" 2e3d6c9f566f06ae7e9a74b69483b8cb783b1bee48beb02b6524fbcb4de48f71 # !"" VERSION # !"" json # $""

    layer.tar !"" 6a28bc9521cd43cb1bbba4facfe4676649681c81ab252d09ad906ca11669d4ca # !"" VERSION # !"" json # $"" layer.tar !"" 83bc3862525ff9d3b82a85ec3369f8cab40f7e716e36f3db84f15763a11af2fe.json !"" da0ea11a16c18578358add538c445cd5408e29ec0f06a7196c51ee7b7e46662d # !"" VERSION # !"" json # $"" layer.tar !"" manifest.json $"" repositories
  10. % docker history hatena/apply-for-internship-2020 IMAGE CREATED CREATED BY SIZE 83bc3862525f

    2 months ago /bin/sh -c !"nop) ENTRYPOINT ["./apply-for-… 0B <missing> 2 months ago /bin/sh -c !"nop) COPY file:c47498027cbfc590… 10.4MB <missing> 2 months ago /bin/sh -c !"nop) COPY multi:46d4249576ac663… 405B <missing> 2 months ago /bin/sh -c !"nop) WORKDIR /root/ 0B <missing> 2 months ago /bin/sh -c !"nop) CMD ["bash"] 0B <missing> 2 months ago /bin/sh -c !"nop) ADD file:7780c81c33e6cc5b6… 69.2MB
  11. 5SJWZ ˖ IUUQTHJUIVCDPNBRVBTFDVSJUZUSJWZ ˖ %PDLFS؎ً٦آծؿ؋؎ٕءأذيծHJUٔهآزٔח㼎׃גأ ָٍؗٝדֹ׷ % trivy image !"severity

    HIGH hatena/apply-for-internship-2020:latest 2020-08-05T08:44:37.496+0900 WARN You should avoid using the :latest tag as it is cached. You need to specify '!"clear-cache' option when :latest image is changed 2020-08-05T08:44:40.616+0900 INFO Detecting Debian vulnerabilities!!# hatena/apply-for-internship-2020:latest (debian 10.4) ===================================================== Total: 1 (HIGH: 1) +-----------+------------------+----------+-------------------+------------------+--------------------------------+ | LIBRARY | VULNERABILITY ID | SEVERITY | INSTALLED VERSION | FIXED VERSION | TITLE | +-----------+------------------+----------+-------------------+------------------+--------------------------------+ | perl-base | CVE-2020-10878 | HIGH | 5.28.1-6 | 5.28.1-6+deb10u1 | perl: corruption of | | | | | | | intermediate language state | | | | | | | of compiled regular expression | | | | | | | due to!!# | +-----------+------------------+----------+-------------------+------------------+--------------------------------+
  12. ؙ؎ؤך儗꟦דׅ % docker run !"rm -i hatena/intern-2020-docker-quiz ˖ Ⰻ㉏姻鍑׃ծ !

    ָ⳿׷הؙٔ، ˖ 㔭׏׋הֹכ ˖ " docker run !"rm -i hatena/intern-2020- docker-quiz -hint
  13. # -q2 ΦϓγϣϯҾ਺Λ౉ͯ͠ىಈͤΑ % docker run !"rm -i hatena/intern-2020-docker-quiz -q2

    # ͜ͷΠϝʔδͷentrypointͱͯ͠ࢦఆ͞Ε͍ͯΔίϚϯυͷϑϧύε͸Կ͔ % docker inspect hatena/intern-2020-docker-quiz | jq '.[].Config.Entrypoint' # /app/flag.txt Λϗετʹίϐʔͯ͠ϑΝΠϧͷ಺༰ΛऔಘͤΑ % docker cp <container id!#/app/flag.txt . # γΣϧΛىಈͯ͠ /app/get_flag2.exe Λ࣮ߦͤΑ % docker run !"rm -ti !"entrypoint /bin/sh hatena/intern-2020-docker-quiz $ ./get_flag2.exe # ͜ͷΠϝʔδʹؚ·ΕΔpythonʹ͍ͭͯɺTrivyʹΑͬͯݕग़͞ΕΔseverity͕HIGHͷ੬ऑੑͷCVE൪߸Λ౴͑Α % trivy i !"severity HIGH hatena/intern-2020-docker-quiz
  14. ֿך؎ً٦آךFOUSZQPJOUה׃ג䭷㹀ׁ׸גְ׷؝وٝسךا٦أ؝٦س׾䗁⯋ ׃גծֿך㉏겗ך瘶ִ׾《䖤ׇ״ % docker history hatena/intern-2020-docker-quiz !"no-trunc COPY /app/docker_quiz.go.enc /app/password!"delete_me_after_decrypting

    ./ RUN /bin/sh -c openssl enc -d -aes-256-cbc -pbkdf2 -in docker_quiz.go.enc \ -out docker_quiz.go -pass file:password!"delete_me_after_decrypting !# \ go build docker_quiz.go !# \ rm -rf /root/.cache !# \ rm docker_quiz.go !# \ rm password!"delete_me_after_decrypting ˖ docker_quiz.go.encכ"&4$#$ד农〾⻉ׁ׸׋ؿ؋؎ٕ ˖ QBTTXPSEה䙼׻׸׷ؿ؋؎ٕכ嶊ׁ׸גְ׷״ֲח׫ִ׷ ˖ ؎ً٦آٖ؎َה׃ג婍׏גְ׷ךד《׶⳿ֿׅהָדֹ׷
  15. % docker save hatena/intern-2020-docker-quiz > docker-quiz.tar 㾜Ꟛ䖓ծMBZFSUBSך⚥ַ׵ password!"delete_me_after_decrypting׾䱱ׅ % find

    . -name layer.tar | xargs -p -I{} tar tf {} QBTTXPSEָろת׸׋ؿ؋؎ٕ׾䩛חⰅ׸׋ךד docker_quiz.go.enc׾䗁〾׃גا٦أ؝٦س׾Ⰵ䩛
  16. { "͜ͷΠϝʔδͷentrypointͱͯ͠ࢦఆ͞Ε͍ͯΔίϚϯυͷιʔείʔυΛ෮ݩͯ͠ɺ͜ͷ໰୊ͷ౴͑ΛऔಘͤΑ", "docker history", []byte{0xb9, 0xb3, 0xbe, 0xb8, 0xa0, 0xce,

    0xc9, 0x8a, 0xca, 0x95, 0x88, 0x9d, 0x8d}, XOR, false, }, (snip) for i !" 0; i < n; i!# { xor[i] !$ 0xff } ̕ % python -c 'print(bytearray([x ^ 0xff for x in \ [0xb9, 0xb3, 0xbe, 0xb8, 0xa0, 0xce, 0xc9, 0x8a, 0xca, 0x95, 0x88, 0x9d, 0x8d]]))'