-
Notifications
You must be signed in to change notification settings - Fork 179
/
scan.py
164 lines (149 loc) · 8.33 KB
/
scan.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
from veinmind import *
import re
import pytoml as toml
from veinmind_common.service.report import *
report_list = []
instruct_set = (
"FROM", "CMD", "RUN", "LABEL", "MAINTAINER", "EXPOSE", "ENV", "ADD", "COPY", "ENTRYPOINT", "VOLUME", "USER",
"WORKDIR",
"ARG", "ONBUILD", "STOPSIGNAL", "HEALTHCHECK", "SHELL")
def load_rules():
global rules
with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), "rules.toml"), encoding="utf8") as f:
rules = toml.load(f)
def tab_print(printstr: str):
if len(printstr) < 95:
print(("| " + printstr + "\t|").expandtabs(100))
else:
char_count = 0
printstr_temp = ""
for char in printstr:
char_count = char_count + 1
printstr_temp = printstr_temp + char
if char_count == 95:
char_count = 0
print(("| " + printstr_temp + "\t|").expandtabs(100))
printstr_temp = ""
print(("| " + printstr_temp + "\t|").expandtabs(100))
@command.group()
@command.option("--output", default="stdout", help="output format e.g. stdout/json")
def cli(output):
load_rules()
pass
@cli.image_command()
def scan_images(image):
"""scan image abnormal history instruction"""
image_report = None
refs = image.reporefs()
if len(refs) > 0:
ref = refs[0]
else:
ref = image.id()
log.info("start scan: " + ref)
ocispec = image.ocispec_v1()
if 'history' in ocispec.keys() and len(ocispec['history']) > 0:
for history in ocispec['history']:
if 'created_by' in history.keys():
created_by = history['created_by']
created_by_split = created_by.split("#(nop)")
if len(created_by_split) > 1:
command = "#(nop)".join(created_by_split[1:])
command = command.lstrip()
command_split = command.split()
if len(command_split) == 2:
instruct = command_split[0]
command_content = command_split[1]
for r in rules["rules"]:
if r["instruct"] == instruct:
if re.match(r["match"], command_content):
detail = AlertDetail()
detail.history_detail = HistoryDetail(
instruction=instruct, content=command_content,
description=r["match"]
)
image_report = ReportEvent(id=image.id(),
level=Level.High.value, detect_type=DetectType.Image.value,
event_type=EventType.Risk.value,
alert_type=AlertType.AbnormalHistory.value,
alert_details=[detail], native_object=image)
report(image_report)
break
else:
instruct = command_split[0]
command_content = " ".join(command_split[1:])
for r in rules["rules"]:
if r["instruct"] == instruct:
if re.match(r["match"], command_content):
detail = AlertDetail()
detail.history_detail = HistoryDetail(
instruction=instruct, content=command_content,
description=r["match"]
)
image_report = ReportEvent(id=image.id(),
level=Level.High.value, detect_type=DetectType.Image.value,
event_type=EventType.Risk.value,
alert_type=AlertType.AbnormalHistory.value,
alert_details=[detail], native_object=image)
report(image_report)
break
else:
command_split = created_by.split()
if command_split[0] in instruct_set:
for r in rules["rules"]:
if r["instruct"] == command_split[0]:
if re.match(r["match"], " ".join(command_split[1:])):
detail = AlertDetail()
detail.history_detail = HistoryDetail(
instruction=command_split[0],
content=" ".join(command_split[1:]),
description=r["match"]
)
image_report = ReportEvent(id=image.id(),
level=Level.High.value, detect_type=DetectType.Image.value,
event_type=EventType.Risk.value,
alert_type=AlertType.AbnormalHistory.value,
alert_details=[detail], native_object=image)
report(image_report)
break
else:
for r in rules["rules"]:
if r["instruct"] == "RUN":
if re.match(r["match"], created_by):
detail = AlertDetail()
detail.history_detail = HistoryDetail(
instruction="RUN", content=created_by,
description=r["match"]
)
image_report = ReportEvent(id=image.id(),
level=Level.High.value, detect_type=DetectType.Image.value,
event_type=EventType.Risk.value,
alert_type=AlertType.AbnormalHistory.value,
alert_details=[detail], native_object=image)
report(image_report)
break
if image_report != None:
report_list.append(image_report)
@cli.resultcallback()
def callback(result, output):
if output == "stdout" and len(report_list) > 0:
print("# ================================================================================================= #")
tab_print("Scan Image Total: " + str(len(report_list)))
tab_print("Unsafe Image List: ")
for r in report_list:
if len(r.alert_details) == 0:
continue
print(
"+---------------------------------------------------------------------------------------------------+")
tab_print("ImageName: " + r.id)
tab_print("Abnormal History Total: " + str(len(r.alert_details)))
for detail in r.alert_details:
if detail.history_detail:
tab_print("History: " + detail.history_detail.content)
print("+---------------------------------------------------------------------------------------------------+")
elif output == "json":
with open("output.json", mode="w") as f:
f.write(jsonpickle.dumps(report_list))
if __name__ == '__main__':
cli.add_info_command(manifest=command.Manifest(name="veinmind-history", author="veinmind-team", description="veinmind-history scan image abnormal history"))
cli()