{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Title: msticpy - Base64 Decoder\n", "## Description:\n", "This module allows you to extract base64 encoded content from a string or columns of a Pandas DataFrame.\n", "The library returns the following information:\n", "- decoded string (if decodable to utf-8 or utf-16)\n", "- hashes of the decoded segment (MD5, SHA1, SHA256)\n", "- string of printable byte values (e.g. for submission to a disassembler)\n", "- the detected decoded file type (limited)\n", "\n", "If the results of the decoding contain further encoded strings these will be decoded recursively. If the encoded string appears to be a zip, gzip or tar archive, the contents will be decompressed after decoding. In the case of zip and tar, the contents of the archive will also be checked for base64 encoded content and decoded/decompressed if possible.\n" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.111019Z", "start_time": "2020-02-06T01:06:45.189863Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mpmagics\n" ] } ], "source": [ "# Imports\n", "import sys\n", "MIN_REQ_PYTHON = (3,6)\n", "if sys.version_info < MIN_REQ_PYTHON:\n", " print('Check the Kernel->Change Kernel menu and ensure that Python 3.6')\n", " print('or later is selected as the active kernel.')\n", " sys.exit(\"Python %s.%s or later is required.\\n\" % MIN_REQ_PYTHON)\n", "\n", "\n", "from IPython.display import display\n", "import pandas as pd\n", "\n", "# Import Base64 module\n", "from msticpy.nbtools import *\n", "from msticpy.sectools import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.128628Z", "start_time": "2020-02-06T01:06:47.112010Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CommandLine
0.\\ftp -s:C:\\RECYCLER\\xxppyy.exe
1.\\reg not /domain:everything that /sid:shines...
2cmd /c \"systeminfo && systeminfo\"
3.\\rundll32 /C 42424.exe
4.\\rundll32 /C c:\\users\\MSTICAdmin\\42424.exe
\n", "
" ], "text/plain": [ " CommandLine\n", "0 .\\ftp -s:C:\\RECYCLER\\xxppyy.exe\n", "1 .\\reg not /domain:everything that /sid:shines...\n", "2 cmd /c \"systeminfo && systeminfo\"\n", "3 .\\rundll32 /C 42424.exe\n", "4 .\\rundll32 /C c:\\users\\MSTICAdmin\\42424.exe" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Load test data\n", "process_tree = pd.read_csv('data/process_tree.csv',\n", " parse_dates=[\"TimeGenerated\"],\n", " infer_datetime_format=True)\n", "process_tree[['CommandLine']].head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Contents](#contents)\n", "## Decoding Base64 String\n", "\n", "Base64 decode an input string.\n", "\n", "```\n", " Base64 decode an input string.\n", "\n", " Parameters\n", " ----------\n", " input_string : str, optional\n", " single string to decode (the default is None)\n", " trace : bool, optional\n", " Show additional status (the default is None)\n", "\n", " Returns\n", " -------\n", " Tuple[str, Optional[List[BinaryRecord]]]\n", " Decoded string and additional metadata\n", "\n", " Notes\n", " -----\n", " Items that decode to utf-8 or utf-16 strings will be returned as decoded\n", " strings replaced in the original string. If the encoded string is a\n", " known binary type it will identify the file type and return the hashes\n", " of the file. If any binary types are known archives (zip, tar, gzip) it\n", " will unpack the contents of the archive.\n", " For any binary it will return the decoded file as a byte array, and as a\n", " printable list of byte values. If the input is a string the function\n", " returns:\n", "\n", " - decoded string: this is the input string with any decoded sections\n", " replaced by the results of the decoding\n", "```" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.142805Z", "start_time": "2020-02-06T01:06:47.129618Z" } }, "outputs": [ { "data": { "text/plain": [ "'.\\\\powershell -enc JAB0ACAAPQAgACcAZABpAHIAJwA7AA0ACgAmACAAKAAnAEkAbgB2AG8AawBlACcAKwAnAC0ARQB4AHAAcgBlAHMAcwBpAG8AbgAnACkAIAAkAHQA'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# get a commandline from our data set\n", "cmdline = process_tree['CommandLine'].loc[39]\n", "cmdline" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.161188Z", "start_time": "2020-02-06T01:06:47.143800Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(\".\\\\powershell -enc $\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x00'\\x00;\\x00\\r\\x00\\n\\x00&\\x00 \\x00(\\x00'\\x00I\\x00n\\x00v\\x00o\\x00k\\x00e\\x00'\\x00+\\x00'\\x00-\\x00E\\x00x\\x00p\\x00r\\x00e\\x00s\\x00s\\x00i\\x00o\\x00n\\x00'\\x00)\\x00 \\x00$\\x00t\\x00\", reference original_string file_name \\\n", "0 (, 1., 1) JAB0ACAAPQAgACcAZABpAHIAJwA7AA0ACgAmACAAKAAnAE... unknown \n", "\n", " file_type input_bytes \\\n", "0 None b\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0... \n", "\n", " decoded_string encoding_type \\\n", "0 $\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000... utf-8 \n", "\n", " file_hashes \\\n", "0 {'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's... \n", "\n", " md5 sha1 \\\n", "0 6cd1486db221e532cc2011c9beeb4ffc 6e485467d7e06502046b7c84a8ef067cfe1512ad \n", "\n", " sha256 \\\n", "0 d3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433... \n", "\n", " printable_bytes \n", "0 24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0... )\n" ] } ], "source": [ "# Decode the string\n", "base64_dec_str = base64.unpack(input_string=cmdline)\n", "\n", "# Print decoded string\n", "print(base64_dec_str)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Contents](#toc)\n", "## Using a DataFrame as Input\n", "You can replace to base64.unpack_df() to pass a DataFrame as an argument.\n", "Use the ```column``` parameter to specify which column to process.\n", "\n", "In the case of DataFrame input, the output DataFrame contains these additional columns:\n", " - src_index - the index of the row in the input dataframe from which the data came.\n", " - full_decoded_string - the full decoded string with any decoded replacements. This is only really useful for top-level items, since nested items will only show the 'full' string representing the child fragment.\n", "\n", "```\n", "Base64 decode strings taken from a pandas dataframe.\n", "\n", "Parameters\n", "----------\n", "data : pd.DataFrame\n", " dataframe containing column to decode\n", "column : str\n", " Name of dataframe text column\n", "trace : bool, optional\n", " Show additional status (the default is None)\n", "\n", "Returns\n", "-------\n", "pd.DataFrame\n", " Decoded string and additional metadata in dataframe\n", "```\n", "\n", "### Notes\n", "\n", "Items that decode to utf-8 or utf-16 strings will be returned as decoded\n", "strings replaced in the original string. If the encoded string is a\n", "known binary type it will identify the file type and return the hashes\n", "of the file. If any binary types are known archives (zip, tar, gzip) it\n", "will unpack the contents of the archive.\n", "For any binary it will return the decoded file as a byte array, and as a\n", "printable list of byte values." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.222153Z", "start_time": "2020-02-06T01:06:47.162187Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
referenceoriginal_stringfile_namefile_typeinput_bytesdecoded_stringencoding_typefile_hashesmd5sha1sha256printable_bytessrc_indexfull_decoded_string
0(, 1., 1)JAB0ACAAPQAgACcAZABpAHIAJwA7AA0ACgAmACAAKAAnAE...unknownNoneb\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0...$\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000...utf-8{'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's...6cd1486db221e532cc2011c9beeb4ffc6e485467d7e06502046b7c84a8ef067cfe1512add3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433...24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0...39.\\powershell -enc <decoded type='string' name...
1(, 1., 1)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaunknownNoneb'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9...Nonebinary{'md5': '9a45b2520e930dc9186f6d93a7798a13', 's...9a45b2520e930dc9186f6d93a7798a13f526c90fa0744e3a63d84421ff25e3f5a3d697cbc1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605...69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6...40cmd /c \"echo # <decoded value='binary' name=...
2(, 1., 1)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaunknownNoneb'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9...Nonebinary{'md5': '9a45b2520e930dc9186f6d93a7798a13', 's...9a45b2520e930dc9186f6d93a7798a13f526c90fa0744e3a63d84421ff25e3f5a3d697cbc1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605...69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6...41cmd /c \"echo # <decoded value='binary' name=...
3(, 1., 1)81ed03caf6901e444c72ac67d192fb9cunknownNoneb'\\xf3W\\x9d\\xd3w\\x1a\\x7f\\xaft\\xd5\\xee8\\xe1\\xce...Nonebinary{'md5': '1c8cc6299bd654bbcd85710968d6a87c', 's...1c8cc6299bd654bbcd85710968d6a87c55377391141f59a2ff5ae4765d9f0b4438adfd73fd80ceba7cfb49d296886c10d9a3497d63c89a589587cd...f3 57 9d d3 77 1a 7f af 74 d5 ee 38 e1 ce f6 6...44implant.exe <decoded value='binary' name='[N...
\n", "
" ], "text/plain": [ " reference original_string file_name \\\n", "0 (, 1., 1) JAB0ACAAPQAgACcAZABpAHIAJwA7AA0ACgAmACAAKAAnAE... unknown \n", "1 (, 1., 1) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa unknown \n", "2 (, 1., 1) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa unknown \n", "3 (, 1., 1) 81ed03caf6901e444c72ac67d192fb9c unknown \n", "\n", " file_type input_bytes \\\n", "0 None b\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0... \n", "1 None b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9... \n", "2 None b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9... \n", "3 None b'\\xf3W\\x9d\\xd3w\\x1a\\x7f\\xaft\\xd5\\xee8\\xe1\\xce... \n", "\n", " decoded_string encoding_type \\\n", "0 $\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000... utf-8 \n", "1 None binary \n", "2 None binary \n", "3 None binary \n", "\n", " file_hashes \\\n", "0 {'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's... \n", "1 {'md5': '9a45b2520e930dc9186f6d93a7798a13', 's... \n", "2 {'md5': '9a45b2520e930dc9186f6d93a7798a13', 's... \n", "3 {'md5': '1c8cc6299bd654bbcd85710968d6a87c', 's... \n", "\n", " md5 sha1 \\\n", "0 6cd1486db221e532cc2011c9beeb4ffc 6e485467d7e06502046b7c84a8ef067cfe1512ad \n", "1 9a45b2520e930dc9186f6d93a7798a13 f526c90fa0744e3a63d84421ff25e3f5a3d697cb \n", "2 9a45b2520e930dc9186f6d93a7798a13 f526c90fa0744e3a63d84421ff25e3f5a3d697cb \n", "3 1c8cc6299bd654bbcd85710968d6a87c 55377391141f59a2ff5ae4765d9f0b4438adfd73 \n", "\n", " sha256 \\\n", "0 d3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433... \n", "1 c1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605... \n", "2 c1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605... \n", "3 fd80ceba7cfb49d296886c10d9a3497d63c89a589587cd... \n", "\n", " printable_bytes src_index \\\n", "0 24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0... 39 \n", "1 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6... 40 \n", "2 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6... 41 \n", "3 f3 57 9d d3 77 1a 7f af 74 d5 ee 38 e1 ce f6 6... 44 \n", "\n", " full_decoded_string \n", "0 .\\powershell -enc [Contents](#contents)\n", "## Interpreting the DataFrame output.\n", "For simple strings the Base64 decoded output is straightforward. However for nested encodings this can get a little complex and difficult to represent in a tabular format.\n", "\n", "**Columns**\n", " - reference - The index of the row item in dotted notation in depth.seq pairs (e.g. 1.2.2.3 would be the 3 item at depth 3 that is a child of the 2nd item found at depth 1). This may not always be an accurate notation - it is mainly use to allow you to associate an individual row with the reference value contained in the full_decoded_string column of the topmost item).\n", " - original_string - the original string before decoding.\n", " - file_name - filename, if any (only if this is an item in zip or tar file).\n", " - file_type - a guess at the file type (this is currently elementary and only includes a few file types).\n", " - input_bytes - the decoded bytes as a Python bytes string.\n", " - decoded_string - the decoded string if it can be decoded as a UTF-8 or UTF-16 string. Note: binary sequences may often successfully decode as UTF-16 strings but, in these cases, the decodings are meaningless.\n", " - encoding_type - encoding type (UTF-8 or UTF-16) if a decoding was possible, otherwise 'binary'.\n", " - file_hashes - collection of file hashes for any decoded item.\n", " - md5 - md5 hash as a separate column.\n", " - sha1 - sha1 hash as a separate column.\n", " - sha256 - sha256 hash as a separate column.\n", " - printable_bytes - printable version of input_bytes as a string of \\xNN values\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Contents](#contents)\n", "### SourceIndex column allows you to merge the results with the input DataFrame\n", "Where an input row has multiple decoded elements (e.g. a nested encoding or a zip or other archive file), the output of this merge will result in duplicate rows from the input (one per element match). The DataFrame index from the source is preserved in the `src_index` column.\n", "\n", "Note: you may need to force the type of the `src_index` column to be the same type as the original DataFrame in order to merge. In the example below case we are matching with the default numeric index so we force the type to be numeric. In cases where you are using an index of a different dtype you will need to convert the `src_index` (dtype=object) to match the type of your index column." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.258155Z", "start_time": "2020-02-06T01:06:47.223152Z" }, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TenantIdAccountEventIDTimeGeneratedComputerSubjectUserSidSubjectUserNameSubjectDomainNameSubjectLogonIdNewProcessId...input_bytesdecoded_stringencoding_typefile_hashesmd5sha1sha256printable_bytessrc_indexfull_decoded_string
SourceIndex
39802d39e1-9d70-404d-832c-2de5e2478edaMSTICAlertsWin1\\MSTICAdmin46882019-01-15 05:15:13.567MSTICAlertsWin1S-1-5-21-996632719-2361334927-4038480536-500MSTICAdminMSTICAlertsWin10xfaac270x1684...b\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0...$\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000...utf-8{'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's...6cd1486db221e532cc2011c9beeb4ffc6e485467d7e06502046b7c84a8ef067cfe1512add3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433...24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0...39.0.\\powershell -enc <decoded type='string' name...
40802d39e1-9d70-404d-832c-2de5e2478edaMSTICAlertsWin1\\MSTICAdmin46882019-01-15 05:15:13.683MSTICAlertsWin1S-1-5-21-996632719-2361334927-4038480536-500MSTICAdminMSTICAlertsWin10xfaac270x16b8...b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9...Nonebinary{'md5': '9a45b2520e930dc9186f6d93a7798a13', 's...9a45b2520e930dc9186f6d93a7798a13f526c90fa0744e3a63d84421ff25e3f5a3d697cbc1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605...69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6...40.0cmd /c \"echo # <decoded value='binary' name=...
41802d39e1-9d70-404d-832c-2de5e2478edaMSTICAlertsWin1\\MSTICAdmin46882019-01-15 05:15:13.793MSTICAlertsWin1S-1-5-21-996632719-2361334927-4038480536-500MSTICAdminMSTICAlertsWin10xfaac270x16ec...b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9...Nonebinary{'md5': '9a45b2520e930dc9186f6d93a7798a13', 's...9a45b2520e930dc9186f6d93a7798a13f526c90fa0744e3a63d84421ff25e3f5a3d697cbc1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605...69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6...41.0cmd /c \"echo # <decoded value='binary' name=...
44802d39e1-9d70-404d-832c-2de5e2478edaMSTICAlertsWin1\\MSTICAdmin46882019-01-15 05:15:12.003MSTICAlertsWin1S-1-5-21-996632719-2361334927-4038480536-500MSTICAdminMSTICAlertsWin10xfaac270x1250...b'\\xf3W\\x9d\\xd3w\\x1a\\x7f\\xaft\\xd5\\xee8\\xe1\\xce...Nonebinary{'md5': '1c8cc6299bd654bbcd85710968d6a87c', 's...1c8cc6299bd654bbcd85710968d6a87c55377391141f59a2ff5ae4765d9f0b4438adfd73fd80ceba7cfb49d296886c10d9a3497d63c89a589587cd...f3 57 9d d3 77 1a 7f af 74 d5 ee 38 e1 ce f6 6...44.0implant.exe <decoded value='binary' name='[N...
\n", "

4 rows × 36 columns

\n", "
" ], "text/plain": [ " TenantId Account \\\n", "SourceIndex \n", "39 802d39e1-9d70-404d-832c-2de5e2478eda MSTICAlertsWin1\\MSTICAdmin \n", "40 802d39e1-9d70-404d-832c-2de5e2478eda MSTICAlertsWin1\\MSTICAdmin \n", "41 802d39e1-9d70-404d-832c-2de5e2478eda MSTICAlertsWin1\\MSTICAdmin \n", "44 802d39e1-9d70-404d-832c-2de5e2478eda MSTICAlertsWin1\\MSTICAdmin \n", "\n", " EventID TimeGenerated Computer \\\n", "SourceIndex \n", "39 4688 2019-01-15 05:15:13.567 MSTICAlertsWin1 \n", "40 4688 2019-01-15 05:15:13.683 MSTICAlertsWin1 \n", "41 4688 2019-01-15 05:15:13.793 MSTICAlertsWin1 \n", "44 4688 2019-01-15 05:15:12.003 MSTICAlertsWin1 \n", "\n", " SubjectUserSid SubjectUserName \\\n", "SourceIndex \n", "39 S-1-5-21-996632719-2361334927-4038480536-500 MSTICAdmin \n", "40 S-1-5-21-996632719-2361334927-4038480536-500 MSTICAdmin \n", "41 S-1-5-21-996632719-2361334927-4038480536-500 MSTICAdmin \n", "44 S-1-5-21-996632719-2361334927-4038480536-500 MSTICAdmin \n", "\n", " SubjectDomainName SubjectLogonId NewProcessId ... \\\n", "SourceIndex ... \n", "39 MSTICAlertsWin1 0xfaac27 0x1684 ... \n", "40 MSTICAlertsWin1 0xfaac27 0x16b8 ... \n", "41 MSTICAlertsWin1 0xfaac27 0x16ec ... \n", "44 MSTICAlertsWin1 0xfaac27 0x1250 ... \n", "\n", " input_bytes \\\n", "SourceIndex \n", "39 b\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0... \n", "40 b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9... \n", "41 b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9... \n", "44 b'\\xf3W\\x9d\\xd3w\\x1a\\x7f\\xaft\\xd5\\xee8\\xe1\\xce... \n", "\n", " decoded_string encoding_type \\\n", "SourceIndex \n", "39 $\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000... utf-8 \n", "40 None binary \n", "41 None binary \n", "44 None binary \n", "\n", " file_hashes \\\n", "SourceIndex \n", "39 {'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's... \n", "40 {'md5': '9a45b2520e930dc9186f6d93a7798a13', 's... \n", "41 {'md5': '9a45b2520e930dc9186f6d93a7798a13', 's... \n", "44 {'md5': '1c8cc6299bd654bbcd85710968d6a87c', 's... \n", "\n", " md5 \\\n", "SourceIndex \n", "39 6cd1486db221e532cc2011c9beeb4ffc \n", "40 9a45b2520e930dc9186f6d93a7798a13 \n", "41 9a45b2520e930dc9186f6d93a7798a13 \n", "44 1c8cc6299bd654bbcd85710968d6a87c \n", "\n", " sha1 \\\n", "SourceIndex \n", "39 6e485467d7e06502046b7c84a8ef067cfe1512ad \n", "40 f526c90fa0744e3a63d84421ff25e3f5a3d697cb \n", "41 f526c90fa0744e3a63d84421ff25e3f5a3d697cb \n", "44 55377391141f59a2ff5ae4765d9f0b4438adfd73 \n", "\n", " sha256 \\\n", "SourceIndex \n", "39 d3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433... \n", "40 c1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605... \n", "41 c1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605... \n", "44 fd80ceba7cfb49d296886c10d9a3497d63c89a589587cd... \n", "\n", " printable_bytes src_index \\\n", "SourceIndex \n", "39 24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0... 39.0 \n", "40 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6... 40.0 \n", "41 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6... 41.0 \n", "44 f3 57 9d d3 77 1a 7f af 74 d5 ee 38 e1 ce f6 6... 44.0 \n", "\n", " full_decoded_string \n", "SourceIndex \n", "39 .\\powershell -enc [Contents](#contents)\n", "## Decoding Nested Base64/Archives\n", "The module will try to follow nested encodings. It uses the following algorithm:\n", "1. Search for a pattern in the input that looks like a Base64 encoded string\n", "2. If not a known undecodable_string, try to decode the matched pattern.\n", " - If the base 64 string matches a known archive type (zip, tar, gzip) also decompress or unpack\n", " - For multi-item archives (zip, tar) process each contained item recursively (i.e. go to item 1. with \n", " child item as input)\n", " - For anything that decodes to a UTF-8 or UTF-16 string replace the input pattern with the decoded string\n", " - Recurse over resultant output (i.e. submit decoded/replaced string to 1.)\n", "3. If decoding fails, add to list of undecodable_strings (prevents infinite looping over something that looks like a base64 string but isn't)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.275122Z", "start_time": "2020-02-06T01:06:47.260131Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "powershell.exe -nop -w hidden -encodedcommand \n", "\n", "3e 7b f4 bf 50 41 33 30 01 23 9d 3f 8d 4c d4 01 b0 5e 08 d0 3f c4 0c 01 a0 71 00 50 08 21 9c a6 12 1a 66 81 4b 3f a9 a6 d3 9e 53 60 80 22 01 03 00 00 80 00 00 00 00 00 00 00 00 18 c0 83 f6 fc 01 60 2d aa aa aa aa aa aa aa aa aa aa aa 0a aa aa 1a 1a 80 a1 2d aa aa aa aa aa aa aa aa aa 2a a2 11 fa c8 00 e8 7f 01 60 fd 07 c0 ff 05 80 ff 07 c0 ff 05 40 ff 01 46 00 b3 03 40 28 87 91 69 76 00 c8 20 a3 03 00 20 62 13\n", "Base64 encoded string in zip file\n", "Unencoded text file in zip\n" ] } ], "source": [ "encoded_cmd = '''\n", "powershell.exe -nop -w hidden -encodedcommand \n", "UEsDBBQAAAAIAGBXkk3LfdszdwAAAIoAAAAJAAAAUGVEbGwuZGxss6v+sj/A0diA\n", "UXmufa/PFcYNcRwX7I/wMC4oZAjgUJyzTEgqrdHbfuWyy/OCExqUGJkZGBoYoEDi\n", "QPO3P4wJuqsQgGvVKimphoUIIa1Fgr9OMLyoZ0z4y37gP2vDfxDp8J/RjWEzs4NG\n", "+8TMMoYTCouZGRSShAFQSwMEFAAAAAAAYYJrThx8YzUhAAAAIQAAAAwAAABiNjRp\n", "bnppcC5mb29CYXNlNjQgZW5jb2RlZCBzdHJpbmcgaW4gemlwIGZpbGVQSwMEFAAA\n", "AAAAi4JrTvMfsJUaAAAAGgAAABIAAABQbGFpblRleHRJblppcC5kbGxVbmVuY29k\n", "ZWQgdGV4dCBmaWxlIGluIHppcFBLAQIUABQAAAAIAGBXkk3LfdszdwAAAIoAAAAJ\n", "AAAAAAAAAAAAIAAAAAAAAABQZURsbC5kbGxQSwECFAAUAAAAAABhgmtOHHxjNSEA\n", "AAAhAAAADAAAAAAAAAABACAAAACeAAAAYjY0aW56aXAuZm9vUEsBAhQAFAAAAAAA\n", "i4JrTvMfsJUaAAAAGgAAABIAAAAAAAAAAQAgAAAA6QAAAFBsYWluVGV4dEluWmlw\n", "LmRsbFBLBQYAAAAAAwADALEAAAAzAQAAAAA='''\n", "\n", "import re\n", "dec_string, dec_df = base64.unpack(input_string=encoded_cmd)\n", "print(dec_string.replace('\\n\\n powershell.exe -nop -w hidden -encodedcommand\\n \\n \\n 3e 7b f4 bf 50 41 33 30 01 23 9d 3f 8d 4c d4 01 b0 5e 08 d0 3f c4 0c 01 a0 71 00 50 08 21 9c a6 12 1a 66 81 4b 3f a9 a6 d3 9e 53 60 80 22 01 03 00 00 80 00 00 00 00 00 00 00 00 18 c0 83 f6 fc 01 60 2d aa aa aa aa aa aa aa aa aa aa aa 0a aa aa 1a 1a 80 a1 2d aa aa aa aa aa aa aa aa aa 2a a2 11 fa c8 00 e8 7f 01 60 fd 07 c0 ff 05 80 ff 07 c0 ff 05 40 ff 01 46 00 b3 03 40 28 87 91 69 76 00 c8 20 a3 03 00 20 62 13\\n \\n \\n Base64 encoded string in zip file\\n \\n \\n Unencoded text file in zip\\n \\n \\n'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%b64 --pretty --out dec_xml\n", "powershell.exe -nop -w hidden -encodedcommand \n", "UEsDBBQAAAAIAGBXkk3LfdszdwAAAIoAAAAJAAAAUGVEbGwuZGxss6v+sj/A0diA\n", "UXmufa/PFcYNcRwX7I/wMC4oZAjgUJyzTEgqrdHbfuWyy/OCExqUGJkZGBoYoEDi\n", "QPO3P4wJuqsQgGvVKimphoUIIa1Fgr9OMLyoZ0z4y37gP2vDfxDp8J/RjWEzs4NG\n", "+8TMMoYTCouZGRSShAFQSwMEFAAAAAAAYYJrThx8YzUhAAAAIQAAAAwAAABiNjRp\n", "bnppcC5mb29CYXNlNjQgZW5jb2RlZCBzdHJpbmcgaW4gemlwIGZpbGVQSwMEFAAA\n", "AAAAi4JrTvMfsJUaAAAAGgAAABIAAABQbGFpblRleHRJblppcC5kbGxVbmVuY29k\n", "ZWQgdGV4dCBmaWxlIGluIHppcFBLAQIUABQAAAAIAGBXkk3LfdszdwAAAIoAAAAJ\n", "AAAAAAAAAAAAIAAAAAAAAABQZURsbC5kbGxQSwECFAAUAAAAAABhgmtOHHxjNSEA\n", "AAAhAAAADAAAAAAAAAABACAAAACeAAAAYjY0aW56aXAuZm9vUEsBAhQAFAAAAAAA\n", "i4JrTvMfsJUaAAAAGgAAABIAAAAAAAAAAQAgAAAA6QAAAFBsYWluVGV4dEluWmlw\n", "LmRsbFBLBQYAAAAAAwADALEAAAAzAQAAAAA=" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.294164Z", "start_time": "2020-02-06T01:06:47.291113Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", " powershell.exe -nop -w hidden -encodedcommand\n", " \n", " \n", " 3e 7b f4 bf 50 41 33 30 01 23 9d 3f 8d 4c d4 01 b0 5e 08 d0 3f c4 0c 01 a0 71 00 50 08 21 9c a6 12 1a 66 81 4b 3f a9 a6 d3 9e 53 60 80 22 01 03 00 00 80 00 00 00 00 00 00 00 00 18 c0 83 f6 fc 01 60 2d aa aa aa aa aa aa aa aa aa aa aa 0a aa aa 1a 1a 80 a1 2d aa aa aa aa aa aa aa aa aa 2a a2 11 fa c8 00 e8 7f 01 60 fd 07 c0 ff 05 80 ff 07 c0 ff 05 40 ff 01 46 00 b3 03 40 28 87 91 69 76 00 c8 20 a3 03 00 20 62 13\n", " \n", " \n", " Base64 encoded string in zip file\n", " \n", " \n", " Unencoded text file in zip\n", " \n", " \n", "\n" ] } ], "source": [ "print(dec_xml[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pandas Extension\n", "\n", "The decoding functionality is also available in a pandas extension `mp_b64`.\n", "This supports a single method `extract()`. \n", "\n", "This supports the same syntax\n", "as `unpack_df` (described earlier)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-02-06T01:06:47.360891Z", "start_time": "2020-02-06T01:06:47.295155Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
referenceoriginal_stringfile_namefile_typeinput_bytesdecoded_stringencoding_typefile_hashesmd5sha1sha256printable_bytessrc_indexfull_decoded_string
0(, 1., 1)JAB0ACAAPQAgACcAZABpAHIAJwA7AA0ACgAmACAAKAAnAE...unknownNoneb\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0...$\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000...utf-8{'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's...6cd1486db221e532cc2011c9beeb4ffc6e485467d7e06502046b7c84a8ef067cfe1512add3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433...24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0...39.\\powershell -enc <decoded type='string' name...
1(, 1., 1)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaunknownNoneb'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9...Nonebinary{'md5': '9a45b2520e930dc9186f6d93a7798a13', 's...9a45b2520e930dc9186f6d93a7798a13f526c90fa0744e3a63d84421ff25e3f5a3d697cbc1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605...69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6...40cmd /c \"echo # <decoded value='binary' name=...
2(, 1., 1)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaunknownNoneb'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9...Nonebinary{'md5': '9a45b2520e930dc9186f6d93a7798a13', 's...9a45b2520e930dc9186f6d93a7798a13f526c90fa0744e3a63d84421ff25e3f5a3d697cbc1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605...69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6...41cmd /c \"echo # <decoded value='binary' name=...
3(, 1., 1)81ed03caf6901e444c72ac67d192fb9cunknownNoneb'\\xf3W\\x9d\\xd3w\\x1a\\x7f\\xaft\\xd5\\xee8\\xe1\\xce...Nonebinary{'md5': '1c8cc6299bd654bbcd85710968d6a87c', 's...1c8cc6299bd654bbcd85710968d6a87c55377391141f59a2ff5ae4765d9f0b4438adfd73fd80ceba7cfb49d296886c10d9a3497d63c89a589587cd...f3 57 9d d3 77 1a 7f af 74 d5 ee 38 e1 ce f6 6...44implant.exe <decoded value='binary' name='[N...
\n", "
" ], "text/plain": [ " reference original_string file_name \\\n", "0 (, 1., 1) JAB0ACAAPQAgACcAZABpAHIAJwA7AA0ACgAmACAAKAAnAE... unknown \n", "1 (, 1., 1) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa unknown \n", "2 (, 1., 1) aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa unknown \n", "3 (, 1., 1) 81ed03caf6901e444c72ac67d192fb9c unknown \n", "\n", " file_type input_bytes \\\n", "0 None b\"$\\x00t\\x00 \\x00=\\x00 \\x00'\\x00d\\x00i\\x00r\\x0... \n", "1 None b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9... \n", "2 None b'i\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9ai\\xa6\\x9... \n", "3 None b'\\xf3W\\x9d\\xd3w\\x1a\\x7f\\xaft\\xd5\\xee8\\xe1\\xce... \n", "\n", " decoded_string encoding_type \\\n", "0 $\u0000t\u0000 \u0000=\u0000 \u0000'\u0000d\u0000i\u0000r\u0000'\u0000;\u0000\\r\u0000\\n\u0000&\u0000 \u0000(\u0000'\u0000I\u0000n\u0000v\u0000o\u0000k\u0000... utf-8 \n", "1 None binary \n", "2 None binary \n", "3 None binary \n", "\n", " file_hashes \\\n", "0 {'md5': '6cd1486db221e532cc2011c9beeb4ffc', 's... \n", "1 {'md5': '9a45b2520e930dc9186f6d93a7798a13', 's... \n", "2 {'md5': '9a45b2520e930dc9186f6d93a7798a13', 's... \n", "3 {'md5': '1c8cc6299bd654bbcd85710968d6a87c', 's... \n", "\n", " md5 sha1 \\\n", "0 6cd1486db221e532cc2011c9beeb4ffc 6e485467d7e06502046b7c84a8ef067cfe1512ad \n", "1 9a45b2520e930dc9186f6d93a7798a13 f526c90fa0744e3a63d84421ff25e3f5a3d697cb \n", "2 9a45b2520e930dc9186f6d93a7798a13 f526c90fa0744e3a63d84421ff25e3f5a3d697cb \n", "3 1c8cc6299bd654bbcd85710968d6a87c 55377391141f59a2ff5ae4765d9f0b4438adfd73 \n", "\n", " sha256 \\\n", "0 d3291dab1ae552b91e6b50d7460ceaa39f6f92b2cda433... \n", "1 c1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605... \n", "2 c1f6c05bdbe28a58557a9477cd0fa96fbc5e7c54ceb605... \n", "3 fd80ceba7cfb49d296886c10d9a3497d63c89a589587cd... \n", "\n", " printable_bytes src_index \\\n", "0 24 00 74 00 20 00 3d 00 20 00 27 00 64 00 69 0... 39 \n", "1 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6... 40 \n", "2 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 69 a6 9a 6... 41 \n", "3 f3 57 9d d3 77 1a 7f af 74 d5 ee 38 e1 ce f6 6... 44 \n", "\n", " full_decoded_string \n", "0 .\\powershell -enc [Contents](#contents)\n", "## To-Do Items\n", "- Use more comprehensive list of binary magic numbers and match on byte values after decoding to get better file typing\n", "- Output nested decodings in a more readable output\n" ] } ], "metadata": { "celltoolbar": "Tags", "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": { "height": "318.996px", "width": "320.994px" }, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "165px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "position": { "height": "406.193px", "left": "1468.4px", "right": "20px", "top": "120px", "width": "456.572px" }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }