assemblyline: Wrong identification of Python code

Describe the bug As usual, there are a couple of Python code files that were not identified correctly 😃

cc: @gdesmar

Password for all files: zippy. As usual, they can contact dangerous code.

The following files were identified as text/plain:

  1. BitForger.py.zip
    • possible characteristics: import urllib.request, subprocess.run, urllib.request.urlretrieve
  2. _deobfuscated_code_FINAL.py (kopia).zip
    • characteristic executor: exec(lzma.decompress(
  3. init.py.zip
    • quite similar to the (1), but a little longer
  4. init.py(1).zip
    • import urllib.parse, import aiohttp
  5. init.py (kopia).zip
    • subprocess.Popen
  6. tools.py.zip
    • similar to (2), exec(lzma.decompress(base64.b64decode

The following files were identified as code/ps1. Those are more complicated, as they do contain PowerShell commands, but they are Python scripts:

  1. init.py.zip
  2. uidesign.py.zip

The same story, but with code/batch identification:

  1. 4a2353d4be195e06172985931534fe14dbe5746c452c7a27c1d4a5d51d516eb6.zip

To Reproduce Steps to reproduce the behavior:

  1. Upload and see the wrong file type

Expected behavior Identification as code/python.

Screenshots If applicable, add screenshots to help explain your problem.

Environment (please complete the following information if pertinent):

  • Assemblyline Version: 4.5.0.x - those files were collected for some time
  • Browser: [e.g. chrome, safari]

Additional context Add any other context about the problem here.

About this issue

Most upvoted comments

And a new one šŸ˜„ post_install.py.zip

I modified the first executor to allow for different encoding. It will now check for a zlib or lzma or base64 payload inside an execution, instead of a base64 payload inside an optional zlib or lzma step inside an execution. This will identify _deobfuscated_code_FINAL.py (kopia).zip as a python script. I don’t think it will cause too many false positives, as the rule have a default score that will lower the chances.

Regarding the mixed code scripts, I agree that having the list of locations where the yara strings were found (and hopefully their name) could be of great help to deconflict the two languages. I’ll try to look into it, but I think that’s already a great start. šŸ™‚

Eventually, it could be a score bump for ā€œwhat exist firstā€ - e.g. if the Python characteristic was found earlier in the file than ps1/batch AND was not after some not-Python indicators. I mean, if we have a strong Python indicator at the top of the file, and it’s not inside a PowerShell variable assignment, it’s reasonable to assume it has to be proceeded by Python. But recognizing if it was or wasn’t inside a string (or something like it), sounds too complex for YARA