-
Notifications
You must be signed in to change notification settings - Fork 0
/
Powershell_Analyzer.py
66 lines (57 loc) · 2.89 KB
/
Powershell_Analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
try:
from rich.table import Table
from rich.console import Console
import re
import json
import argparse
import hashlib
except:
print("Missing modules!")
sys.exit(1)
def get_md5(filetoinput):
fin = open(filetoinput, 'rb')
data = fin.read()
md5_results = hashlib.md5(data).hexdigest()
print(f"[*] MD5 hash of the file is: {md5_results}\n")
print(f"[*] Beginning PowerShell Analysis for {filetoinput}...\n\n")
print("PowerShell Script Indicators extracted split by categories:")
def powershell_scanner(filetoinput):
ps_indicators = json.load(open("ps_script_indicators.json"))
fin = open(filetoinput, 'r')
file = fin.read()
for pattern in ps_indicators:
patternT = Table()
patternT.add_column(f"Suspicious indicators for {pattern}", justify="center")
# iterating patterns now
for code in ps_indicators[pattern]["script_IOCs"]:
strings_extracted = file.replace('\n', '')
matching_IOCs = re.findall(code, strings_extracted, re.IGNORECASE) # The .findall() method iterates over a string to find a subset of characters that match a specified pattern, returns list of every pattern match that occurs in a given string.
# print(matching_IOCs)
if matching_IOCs != []:
patternT.add_row(code)
ps_indicators[pattern]["counter"] += 1 # counter
if ps_indicators[pattern]["counter"] != 0:
console = Console()
console.print(patternT)
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog='PS_Analyzer_Module', description='PowerShell Analyzer', usage='%(prog)s -f file')
parser.add_argument("-f", "--file", required=True, help="powershell file to analyze")
args = parser.parse_args()
filetoinput = args.file
if not args.file:
print('error: must specify -f file')
sys.exit(1)
art = r"""
__________ .__ .__ .__ _____ .__
\______ \______ _ __ ___________ _____| |__ ____ | | | | / _ \ ____ _____ | | ___.__.________ ___________
| ___/ _ \ \/ \/ // __ \_ __ \/ ___/ | \_/ __ \| | | | / /_\ \ / \\__ \ | |< | |\___ // __ \_ __ \
| | ( <_> ) /\ ___/| | \/\___ \| Y \ ___/| |_| |__ / | \ | \/ __ \| |_\___ | / /\ ___/| | \/
|____| \____/ \/\_/ \___ >__| /____ >___| /\___ >____/____/ \____|__ /___| (____ /____/ ____|/_____ \\___ >__|
\/ \/ \/ \/ \/ \/ \/ \/ \/ \/
BY Davincico
"""
print(art)
print("[*] Obtaining MD5 Hash of the file ...\n")
get_md5(filetoinput)
powershell_scanner(filetoinput)