question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decoding of payload of attachment

See original GitHub issue

Hello,

I am have following code in my project:


decoder_map = {'base64': lambda payload: base64.b64decode(payload),
               '': lambda payload: payload.encode('utf-8'),
               '7bit': lambda payload: payload.encode('utf-8'),
               'quoted-printable': lambda payload: quopri.decodestring(payload)}
for msg_id in data[0].decode('utf-8').split():
    result_fetch, data = client.fetch(msg_id, "(RFC822)")

    if result_fetch != 'OK':
        raise Exception("Fetch failed!")

    raw_mail = data[0][1]
    mail = mailparser.parse_from_bytes(raw_mail)
    for attachment in mail.attachments:
        if attachment['content_transfer_encoding'] not in decoder_map:
            msg = "Unsupported Content-Transfer Encoding ({}) in msg {}.".format(attachment['content_transfer_encoding'], msg_id)
            raise RuntimeError(msg)

        decoder = decoder_map[attachment['content_transfer_encoding']]
        try:
            fp, fname = find_filename(attachment['filename'], args.attachment_dir)
            fp.write(decoder(attachment['payload']))
        except binascii.Error as e:
            print("Unable to parse attachment '{}'".format(attachment['filename']))
        finally:
            fp.close()

I think that this is not a fully-handled case of use. I had to introduce quite a lot of logic to save attachments that can be coded in a variety of ways.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fedelemantuanocommented, Jan 13, 2019

I added the store attachments function in mail-parser command line:

$ mailparser -f my_mail -sa -ap /tmp/attachments
1reaction
fedelemantuanocommented, Mar 18, 2018

Hi @ad-m,

in mail-parser results there is a binary flag. If it’s true the sample is a binary, else it’s false is not binary. If you read this part of code you can see:

...
charset = p.get_content_charset('utf-8') # get the charset
...

            if filename:
                    binary = False
                    mail_content_type = ported_string(p.get_content_type())
                    transfer_encoding = ported_string(
                        p.get('content-transfer-encoding', '')).lower()

                    if transfer_encoding == "base64" or \
                            (transfer_encoding == "quoted-printable" and
                             "application" in mail_content_type):
                        payload = p.get_payload(decode=False)
                        binary = True
                    else:
                        payload = ported_string(
                            p.get_payload(decode=True), encoding=charset)

mail-parser gives you the correct payload, so you should do:

            if binary:
                with open(sample, "wb") as f:
                    f.write(payload.decode("base64"))
            else:
                with open(sample, "w") as f:
                    f.write(payload.encode("utf-8"))

That’s it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsing or decoding .pluginPayloadAttachment files?
pluginPayloadAttachment " extension. I have no idea how to parse these. Running them through xxd reveals some indecipherable binary/hex, except ...
Read more >
Decryption of Business Chat Attach…
Here an example how you can access the encoded attachment data by calling the /preDownload endpoint, decode the attachment payload and process it....
Read more >
JSON Payload-Attachment as Byte format-How to Scan
Hi, If we have scenario where the attachment is sent as byte format inside JSON Payload and need to scan for content inside...
Read more >
Decoding from base64 an xml attachment from a multipart ...
This article explains how the decoding from base64 should be done for an xml attachment received in a multipart response (from a SOAP...
Read more >
python - Need help decoding a WAV file email attachment
I tried to decode using 'base64' or the "encoding property in the payload" but I keep getting this error: "UnicodeDecodeError: 'utf8' codec ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found