-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpymodulevalidator.py
More file actions
62 lines (57 loc) · 1.95 KB
/
pymodulevalidator.py
File metadata and controls
62 lines (57 loc) · 1.95 KB
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
import ast
import os
from pathlib import Path
import error_handler
import logger
marks=['exec', 'eval', 'open', '__import__', 'import']
def get_libs_and_functions(code):
log=logger.Logger("Extension validator")
code_ast=ast.parse(code)
modules=[]
functions=[]
has_handler=False
for node in ast.walk(code_ast):
if type(node)==ast.FunctionDef:
if node.name=='handler':
if len(node.args.args)==2:
has_handler=True
if type(node)==ast.Import:
names=node.names
for i in names:
modules.append(i.name)
if type(node)==ast.ImportFrom:
modules.append(node.module)
if type(node)==ast.Call:
try:
func=node.func
if func.value.id=='getattr':
if len(node.args)>1 and node.args[1].id=='__builtins__':
error_handler.handleSkippable(log, "Module is either incredibly strangely designed or intentionally malicious.")
if func.id in marks:
functions.append(func.id)
except AttributeError:
pass
if type(node)==ast.Assign:
func=node.value
if 'id' in func.__dict__.keys() and func.id in marks:
functions.append(func)
if type(node)==ast.Attribute:
try:
if node.value.id=='__builtins__' and node.attr in marks:
functions.append(node.attr)
except AttributeError:
pass
return list(set(modules)), list(set(functions)), has_handler
def discover_extra_files(module):
files=[]
for i in os.walk(module):
files=i[2]
files=[file for file in files if file.split(".")[1]=='py']
return files
def get_code(module, files):
code=[]
modpath=module
for i in files:
with open(modpath/i,'r') as file:
code.append(file.read())
return code