Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go言語で行うメール解析

Kyo Sato
July 07, 2024

 Go言語で行うメール解析

OPTiM × every Golang Developer Night 2024/07/02
https://optim.connpass.com/event/321331/

Kyo Sato

July 07, 2024
Tweet

More Decks by Kyo Sato

Other Decks in Technology

Transcript

  1. Copyright © 2015 every, Inc. All rights reserved. 2 ࣗݾ঺հ

    - ໊લ - ͖ΐʔɻ - ෦ॺ - ։ൃຊ෦ɺDELISH KITCHEN։ൃ෦ - ࠷ۙ஥ྑ͘ͳͬͨGoͷύοέʔδ - math/rand - Ωʔϫʔυ - ٶ৓ग़਎ - 3౓ͷ൧ͱ೔ຊञ - όΠΫ - Ϥʔϩούཱྀߦ - ίʔώʔ - Vtuber - ήʔϜ - Ξχϝ
  2. Copyright © 2015 every, Inc. All rights reserved. 3 ຊ೔ͷ͓඼ॻ͖

    - GoݴޠͰߦ͏ϝʔϧղੳ - ಋೖ - ϝʔϧϔομʔʹ͍ͭͯ - ࣮ࡍʹղੳͯ͠ΈΑ͏ - ͲͷΑ͏ͳ৔໘Ͱϝʔϧղੳ͍͔ͨ͠ - ·ͱΊ
  3. Copyright © 2015 every, Inc. All rights reserved. 8 Έͳ͞ΜϝʔϧΛૹͬͨ͜ͱ͸͋Γ·͔͢ʁ

    Telnet - ϦϞʔτίϯϐϡʔλͱର࿩͢ΔͨΊʹߏங͞ΕͨίϯϐϡʔλϓϩτίϧͷҰͭ - ଞͷϓϩτίϧͷྫ - SSH - SFTP - VNC
  4. Copyright © 2015 every, Inc. All rights reserved. 9 $

    docker run -d -p 8025:8025 -p 1025:1025 --name test_mailhog mailhog/mailhog Header - MAIL FROM: - RCPT TO: - SUBJECT: - FROM: - TO: Body - Hello, World! . ੺Ͱғ͍ͬͯΔ෦෼ɿࣗ෼͕ೖྗͨ͠Օॴ
  5. Copyright © 2015 every, Inc. All rights reserved. 10 $

    docker run -d -p 8025:8025 -p 1025:1025 --name test_mailhog mailhog/mailhog [email protected] [email protected]
  6. Copyright © 2015 every, Inc. All rights reserved. 11 $

    docker run -d -p 8025:8025 -p 1025:1025 --name test_mailhog mailhog/mailhog Q. - ͳΓ͢·͠ରࡦ͸Ͳ͏΍ͬͯΔͷʁ - ͲΜͳϝʔϧϔομʔ͕͋ͬͯɺͳΜͷͨΊʹ࢖ΘΕ͍ͯΔͷʁ - GoͰϝʔϧΛղੳ͢Δʹ͸Ͳ͏͢Ε͹ྑ͍ʁ
  7. Copyright © 2015 every, Inc. All rights reserved. 13 ͦ΋ͦ΋ϝʔϧϔομʔͱ͸ʁ

    - ϝʔϧϔομͱ͸ɺϝʔϧͷৄࡉ৘ใ͕ॻ͔Ε͍ͯΔ෦෼ͷ͜ͱ - ϝʔϧ͕഑ૹ͞Εͨܦ࿏΍࣌ؒɺܦ༝ͨ͠αʔόʔͳͲ͕ه࿥͞Ε͍ͯΔ - From: - ૹ৴ऀͷϝʔϧΞυϨε - To: - ड৴ऀͷϝʔϧΞυϨε - Subject: - ϝʔϧͷ໊݅ - Received: - ϝʔϧ͕ܦ༝ͨ͠αʔόʔͱͦͷ೔࣌ - Content-Type: - ϝʔϧͷຊจͷܗࣜʢྫɿtext/plain, text/htmlʣ - MIME-Version: - ϝʔϧ͕ MIMEن֨Λ࢖༻͍ͯ͠Δ৔߹ͷόʔδϣϯ ϝʔϧϔομʔ͸ɺϝʔϧͷτϥϒϧγϡʔςΟϯάɺεύϜͷݕग़ɺηΩϡϦςΟ෼ੳͳͲʹ࢖༻͞ΕΔ
  8. Copyright © 2015 every, Inc. All rights reserved. 15 -

    RFC2822 Ͱنఆ͞Εͨϝʔϧϓϩ τίϧͰߏ੒͞Ε͍ͯΔ - ASCII ίʔυͰߏ੒͞ΕΔ͜ͱ - Ұߦ͸ 78 จࣈҎԼ͕ਪ঑ - Header ϑΟʔϧυ͸ɺϑΟʔϧυ ໊ͷޙʹίϩϯ(":")ɺϑΟʔϧυ ຊମ͕ଓ͖ɺCRLF Ͱऴྃ - Body ͕࢝·Δલ͸ۭߦʹ͢Δ
  9. Copyright © 2015 every, Inc. All rights reserved. 17 ࣮ࡍʹղੳͯ͠Έͨ

    - net/mail - Go Packages - Goͷඪ४ύοέʔδͷҰͭ - ϝʔϧϝοηʔδͷղੳΛఏڙ͍ͯ͠Δ - جຊతͳ࢖͍ํ
  10. Copyright © 2015 every, Inc. All rights reserved. 18 ⚠

    ݸਓ৘ใؚ͕·Ε͍ͯΔ෦෼͸ ɹফͯ͋͠Γ·͢
  11. Copyright © 2015 every, Inc. All rights reserved. 19 Message.Header

    Delivered-To: ϝʔϧ͕࠷ऴతʹ഑৴͞Εͨड৴ऀͷϝʔϧΞυϨε Received: ϝʔϧ͕ܦ༝֤ͨ͠αʔόʔʹΑͬͯ௥Ճ͞Εɺϝʔϧͷసૹܦ࿏ʢෳ਺ͷReceivedϔομʔ͕ଘࡏ͢Δ͜ͱ͕͋Δʣ ARC-Seal, ARC-Message-Signature, ARC-Authentication-Results: Authenticated Received Chain (ARC)ʹؔ࿈͢Δϔομʔʢϝʔϧ͕సૹ͞ΕΔաఔͰͷೝূ݁ՌΛอ࣋ʣ Return-Path: ό΢ϯεϝοηʔδʢ഑৴ෆೳ௨஌ʣ͕ૹΒΕΔࡍͷฦ৴ઌΞυϨε Received-SPF: Sender Policy Framework (SPF)ͷݕূ݁Ռʢϝʔϧ͕ਖ਼౰ͳૹ৴ݩ͔ΒૹΒΕ͔ͨͲ͏͔Λ֬ೝ͢Δʣ Authentication-Results: ϝʔϧ͕ೝূϓϩηεʢDKIMɺSPFɺDMARCͳͲʣΛ௨ա͔ͨ͠Ͳ͏͔ͷ݁Ռ DKIM-Signature: DomainKeys Identified Mail (DKIM)ͷॺ໊ʢϝʔϧͷ಺༰͕ૹ৴ऀʹΑͬͯॺ໊͞Εɺ్தͰվ͟Μ͞Ε͍ͯͳ͍͜ͱΛอূʣ X-Google-DKIM-Signature: Google͕௥Ճͨ͠DKIMॺ໊ʢGmail͕ϝʔϧΛॲཧͨ͜͠ͱΛࣔ͢ʣ X-Gm-Message-State: GmailʹΑͬͯ௥Ճ͞Εͨϔομʔʢϝʔϧͷঢ়ଶʹؔ͢Δ৘ใΛؚΉʣ X-Google-Smtp-Source: GmailʹΑͬͯ௥Ճ͞Εͨϔομʔʢϝʔϧͷૹ৴ʹؔ͢Δ௥Ճ৘ใΛؚΉʣ MIME-Version: MIME (Multipurpose Internet Mail Extensions)ͷόʔδϣϯʢϝʔϧ͕ςΩετҎ֎ͷίϯςϯπΛؚΉ͜ͱΛࣔ͢ʣ From: ϝʔϧͷૹ৴ऀͷ໊લͱϝʔϧΞυϨε Date: ϝʔϧ͕ૹ৴͞Εͨ೔࣌ Message-ID: ϝʔϧʹҰҙʹׂΓ౰ͯΒΕͨIDʢϝʔϧͷ௥੻ʹ࢖༻͞ΕΔʣ Subject: ϝʔϧͷ໊݅ To: ϝʔϧͷѼઌΞυϨε Content-Type: ϝʔϧͷຊจ΍ఴ෇ϑΝΠϧͷܗࣜʢMIMEλΠϓʣ Content-Disposition: ఴ෇ϑΝΠϧͷදࣔํ๏΍ϑΝΠϧ໊ Content-Transfer-Encoding: ϝʔϧͷຊจ΍ఴ෇ϑΝΠϧ͕ͲͷΑ͏ʹΤϯίʔυ͞Ε͍ͯΔ͔ Content-ID: ఴ෇ϑΝΠϧʹؔ࿈෇͚ΒΕͨҰҙͷIDʢϝʔϧ಺Ͱࢀর͢Δࡍʹ࢖༻͞ΕΔʣ X-Attachment-Id: Gmail͕ఴ෇ϑΝΠϧʹׂΓ౰ͯͨIDʢϝʔϧ಺Ͱͷఴ෇ϑΝΠϧͷࢀরʹ࢖༻͞ΕΔʣ
  12. Copyright © 2015 every, Inc. All rights reserved. 20 Message.Header

    Delivered-To: ϝʔϧ͕࠷ऴతʹ഑৴͞Εͨड৴ऀͷϝʔϧΞυϨε Received: ϝʔϧ͕ܦ༝֤ͨ͠αʔόʔʹΑͬͯ௥Ճ͞Εɺϝʔϧͷసૹܦ࿏ʢෳ਺ͷReceivedϔομʔ͕ଘࡏ͢Δ͜ͱ͕͋Δʣ ARC-Seal, ARC-Message-Signature, ARC-Authentication-Results: Authenticated Received Chain (ARC)ʹؔ࿈͢Δϔομʔʢϝʔϧ͕సૹ͞ΕΔաఔͰͷೝূ݁ՌΛอ࣋ʣ Return-Path: ό΢ϯεϝοηʔδʢ഑৴ෆೳ௨஌ʣ͕ૹΒΕΔࡍͷฦ৴ઌΞυϨε Received-SPF: Sender Policy Framework (SPF)ͷݕূ݁Ռʢϝʔϧ͕ਖ਼౰ͳૹ৴ݩ͔ΒૹΒΕ͔ͨͲ͏͔Λ֬ೝ͢Δʣ Authentication-Results: ϝʔϧ͕ೝূϓϩηεʢDKIMɺSPFɺDMARCͳͲʣΛ௨ա͔ͨ͠Ͳ͏͔ͷ݁Ռ DKIM-Signature: DomainKeys Identified Mail (DKIM)ͷॺ໊ʢϝʔϧͷ಺༰͕ૹ৴ऀʹΑͬͯॺ໊͞Εɺ్தͰվ͟Μ͞Ε͍ͯͳ͍͜ͱΛอূʣ X-Google-DKIM-Signature: Google͕௥Ճͨ͠DKIMॺ໊ʢGmail͕ϝʔϧΛॲཧͨ͜͠ͱΛࣔ͢ʣ X-Gm-Message-State: GmailʹΑͬͯ௥Ճ͞Εͨϔομʔʢϝʔϧͷঢ়ଶʹؔ͢Δ৘ใΛؚΉʣ X-Google-Smtp-Source: GmailʹΑͬͯ௥Ճ͞Εͨϔομʔʢϝʔϧͷૹ৴ʹؔ͢Δ௥Ճ৘ใΛؚΉʣ MIME-Version: MIME (Multipurpose Internet Mail Extensions)ͷόʔδϣϯʢϝʔϧ͕ςΩετҎ֎ͷίϯςϯπΛؚΉ͜ͱΛࣔ͢ʣ From: ϝʔϧͷૹ৴ऀͷ໊લͱϝʔϧΞυϨε Date: ϝʔϧ͕ૹ৴͞Εͨ೔࣌ Message-ID: ϝʔϧʹҰҙʹׂΓ౰ͯΒΕͨIDʢϝʔϧͷ௥੻ʹ࢖༻͞ΕΔʣ Subject: ϝʔϧͷ໊݅ To: ϝʔϧͷѼઌΞυϨε Content-Type: ϝʔϧͷຊจ΍ఴ෇ϑΝΠϧͷܗࣜʢMIMEλΠϓʣ Content-Disposition: ఴ෇ϑΝΠϧͷදࣔํ๏΍ϑΝΠϧ໊ Content-Transfer-Encoding: ϝʔϧͷຊจ΍ఴ෇ϑΝΠϧ͕ͲͷΑ͏ʹΤϯίʔυ͞Ε͍ͯΔ͔ Content-ID: ఴ෇ϑΝΠϧʹؔ࿈෇͚ΒΕͨҰҙͷIDʢϝʔϧ಺Ͱࢀর͢Δࡍʹ࢖༻͞ΕΔʣ X-Attachment-Id: Gmail͕ఴ෇ϑΝΠϧʹׂΓ౰ͯͨIDʢϝʔϧ಺Ͱͷఴ෇ϑΝΠϧͷࢀরʹ࢖༻͞ΕΔʣ ܦ༝ͨ͠αʔόʔɺIPΞυϨεͳͲ͔Β ෆ৹ͳαʔόʔ͔ΒૹΒΕ͍ͯͳ͍͔֬ೝͰ͖Δ ͳΓ͢·͠ରࡦ ͲͷΑ͏ͳίϯςϯπɺܗࣜͳͷ͔Θ͔Δ
  13. Copyright © 2015 every, Inc. All rights reserved. 22 Message.Body

    Content-Type: multipart/mixed; boundary="000000000000367976061c1205d3" MIME Ϛϧνύʔτϝοηʔδ - ςΩετ΍ htmlɺը૾ͳͲͦΕ ͧΕҟͳΔύʔτʹ෼͚ɺͦΕ ΒΛ૊Έ߹Θͤߏ੒͞Εͨ΋ͷ - ෳ਺ͷϑΝΠϧΛిࢠϝʔϧʹ ఴ෇͢Δͱ͖ͳͲʹ࢖༻͞ΕΔ
  14. Copyright © 2015 every, Inc. All rights reserved. 23 Message.Body

    Content-Type: multipart/mixed; boundary="000000000000367976061c1205d3" MIME Ϛϧνύʔτϝοηʔδ - ςΩετ΍ htmlɺը૾ͳͲͦΕ ͧΕҟͳΔύʔτʹ෼͚ɺͦΕ ΒΛ૊Έ߹Θͤߏ੒͞Εͨ΋ͷ - ෳ਺ͷϑΝΠϧΛిࢠϝʔϧʹ ఴ෇͢Δͱ͖ͳͲʹ࢖༻͞ΕΔ
  15. Copyright © 2015 every, Inc. All rights reserved. 25 ྫ͑͹

    - ސ٬αϙʔτͷࣗಈԽ - αϙʔτʹૹΒΕͯ͘ΔϝʔϧΛղੳɺ಺༰ʹج͍ͮͯαϙʔτνʔϜ΁సૹ - https://tech.every.tv/entry/2024/06/06/192547 - Α͋͘Δ࣭໰ʹରͯࣗ͠ಈԠ౴ AWSͰड͚औͬͨϝʔϧΛసૹ͢Δྫ→ - ϝʔϧαϚϦʔੜ੒ - ௕͍ϝʔϧ͔ΒॏཁͳϙΠϯτ͚ͩΛநग़ https://aws.amazon.com/jp/blogs/messaging-and-targeting/forward-incoming-email-to-an-external-destination/
  16. Copyright © 2015 every, Inc. All rights reserved. 27 ·ͱΊ

    - ීஈ͋·Γҙࣝͯ͜͠ͳ͔ͬͨϝʔϧͷ಺༰ɾߏ੒ʹ͍ͭͯ஌Εͨྑ͍ػձʹͳͬ ͨʂ - ը૾΍ಈըͳͲͷఴ෇ϑΝΠϧ͕ͲͷΑ͏ʹૹΒΕ͍ͯΔͷֶ͔Μͩ - ͲͷΑ͏ʹͳΓ͢·͠ରࡦ͕ߦΘΕ͍ͯΔͷ͔Λϝʔϧϔομʔ͔ΒֶΜͩ͜ͱͰɺ ϝʔϧΛసૹ΍ฦ৴͢ΔࡍʹඞཁͳॲཧΛߟ͑Δ͖͔͚ͬʹͳͬͨ - GoΛ࢖͍ͳ͕ΒखΛಈֶ͔ͯ͠΂Δͷָ͍͠ - ໘ന͔ͬͨษڧ಺༰ͳͲ͋Γ·ͨ͠Βڭ͍͑ͯͩ͘͞ʂ