2
2
# -*- coding: utf-8 -*-
3
3
# Copyright (c) 2020 LG Electronics Inc.
4
4
# SPDX-License-Identifier: Apache-2.0
5
- import hashlib
6
- import tlsh
7
- from io import open
5
+ from fosslight_util .oss_item import FileItem
8
6
9
- _EXCLUDE_TRUE_VALUE = "Exclude"
10
- _TLSH_CHECKSUM_NULL = "0"
11
-
12
-
13
- class OssItem :
14
- name = ""
15
- version = ""
16
- license = ""
17
- dl_url = ""
18
- comment = ""
19
- exclude = False
20
-
21
- def __init__ (self , name , version , license , dl_url = "" ):
22
- self .name = name
23
- self .version = version
24
- self .license = license
25
- self .dl_url = dl_url
26
- self .exclude = False
27
- self .comment = ""
28
-
29
- def set_comment (self , value ):
30
- if self .comment :
31
- self .comment = f"{ self .comment } / { value } "
32
- else :
33
- self .comment = value
34
-
35
- def set_exclude (self , value ):
36
- self .exclude = value
37
-
38
- def get_comment (self ):
39
- return self .comment
7
+ EXCLUDE_TRUE_VALUE = "Exclude"
8
+ TLSH_CHECKSUM_NULL = "0"
40
9
41
10
42
11
class VulnerabilityItem :
@@ -50,119 +19,84 @@ def __init__(self, file_path, id, url):
50
19
self .nvd_url = url
51
20
52
21
53
- class BinaryItem :
54
- bin_name = ""
55
- binary_name_without_path = ""
56
- binary_strip_root = "" # Value of binary name column
57
- tlsh = _TLSH_CHECKSUM_NULL
58
- checksum = _TLSH_CHECKSUM_NULL
59
- oss_items = []
60
- vulnerability_items = []
61
- exclude = False
62
- comment = ""
63
- found_in_owasp = False
64
-
22
+ class BinaryItem (FileItem ):
65
23
def __init__ (self , value ):
24
+ super ().__init__ ("" )
66
25
self .exclude = False
67
- self .binary_strip_root = ""
68
- self .checksum = _TLSH_CHECKSUM_NULL
69
- self .tlsh = _TLSH_CHECKSUM_NULL
70
- self .oss_items = []
26
+ self .source_name_or_path = ""
27
+ self .checksum = TLSH_CHECKSUM_NULL
28
+ self .tlsh = TLSH_CHECKSUM_NULL
71
29
self .vulnerability_items = []
72
30
self .binary_name_without_path = ""
73
- self .set_bin_name (value )
31
+ self .bin_name_with_path = value
32
+ self .found_in_owasp = False
33
+ self .is_binary = True
74
34
75
35
def __del__ (self ):
76
36
pass
77
37
78
38
def set_oss_items (self , new_oss_list , exclude = False , exclude_msg = "" ):
79
39
if exclude :
80
40
for oss in new_oss_list :
81
- oss .set_exclude ( True )
82
- oss .set_comment ( exclude_msg )
41
+ oss .exclude = True
42
+ oss .comment = exclude_msg
83
43
# Append New input OSS
84
44
self .oss_items .extend (new_oss_list )
85
45
86
- def get_oss_items (self ):
87
- return self .oss_items
88
-
89
- def set_vulnerability_items (self , vul_list ):
90
- if vul_list is not None :
91
- self .vulnerability_items .extend (vul_list )
92
-
93
46
def get_vulnerability_items (self ):
94
47
nvd_url = [vul_item .nvd_url for vul_item in self .vulnerability_items ]
95
48
return ", " .join (nvd_url )
96
49
97
- def set_comment (self , value ):
98
- if self .comment :
99
- self .comment = f"{ self .comment } / { value } "
100
- else :
101
- self .comment = value
102
-
103
- def set_bin_name (self , value ):
104
- self .bin_name = value
105
-
106
- def set_exclude (self , value ):
107
- self .exclude = value
108
-
109
- def set_checksum (self , value ):
110
- self .checksum = value
111
-
112
- def set_tlsh (self , value ):
113
- self .tlsh = value
114
-
115
- def get_comment (self ):
116
- return self .comment
117
-
118
50
def get_print_binary_only (self ):
119
- return (self .binary_strip_root + "\t " + self .checksum + "\t " + self .tlsh )
51
+ return (self .source_name_or_path + "\t " + self .checksum + "\t " + self .tlsh )
120
52
121
- def get_oss_report (self ):
122
- comment = ""
123
- if len ( self .oss_items ) > 0 :
53
+ def get_print_array (self ):
54
+ items = []
55
+ if self .oss_items :
124
56
for oss in self .oss_items :
125
- exclude = _EXCLUDE_TRUE_VALUE if (self .exclude or oss .exclude ) else ""
57
+ lic = "," .join (oss .license )
58
+ exclude = EXCLUDE_TRUE_VALUE if (self .exclude or oss .exclude ) else ""
126
59
nvd_url = self .get_vulnerability_items ()
127
-
128
- if self .comment :
129
- if oss .comment :
130
- comment = f"{ self .comment } / { oss .comment } "
131
- else :
132
- comment = self .comment
133
- else :
134
- comment = oss .comment
135
-
136
- yield [self .binary_strip_root , oss .name , oss .version ,
137
- oss .license , oss .dl_url , '' , '' , exclude , comment ,
138
- nvd_url , self .tlsh , self .checksum ]
60
+ items .append ([self .source_name_or_path , oss .name , oss .version ,
61
+ lic , oss .download_location , oss .homepage ,
62
+ oss .copyright , exclude , oss .comment ,
63
+ nvd_url , self .tlsh , self .checksum ])
139
64
else :
140
- exclude = _EXCLUDE_TRUE_VALUE if self .exclude else ""
141
- yield [self .binary_strip_root , '' ,
142
- '' , '' , '' , '' , '' , exclude , self .comment , '' , self .tlsh , self .checksum ]
143
-
144
- def set_checksum_tlsh (self ):
145
- self .checksum , self .tlsh , error , msg = get_checksum_and_tlsh (
146
- self .bin_name )
147
- return error , msg
148
-
149
-
150
- def get_checksum_and_tlsh (bin_with_path ):
151
- checksum_value = _TLSH_CHECKSUM_NULL
152
- tlsh_value = _TLSH_CHECKSUM_NULL
153
- error_msg = ""
154
- error = False
155
- try :
156
- f = open (bin_with_path , "rb" )
157
- byte = f .read ()
158
- sha1_hash = hashlib .sha1 (byte )
159
- checksum_value = str (sha1_hash .hexdigest ())
160
- try :
161
- tlsh_value = str (tlsh .hash (byte ))
162
- except :
163
- tlsh_value = _TLSH_CHECKSUM_NULL
164
- f .close ()
165
- except Exception as ex :
166
- error_msg = f"(Error) Get_checksum, tlsh: { ex } "
167
- error = True
168
- return checksum_value , tlsh_value , error , error_msg
65
+ exclude = EXCLUDE_TRUE_VALUE if self .exclude else ""
66
+ items .append ([self .source_name_or_path , '' ,
67
+ '' , '' , '' , '' , '' , exclude , self .comment , '' ,
68
+ self .tlsh , self .checksum ])
69
+ return items
70
+
71
+ def get_print_json (self ):
72
+ items = []
73
+ if self .oss_items :
74
+ for oss in self .oss_items :
75
+ json_item = {}
76
+ json_item ["name" ] = oss .name
77
+ json_item ["version" ] = oss .version
78
+
79
+ if self .source_name_or_path :
80
+ json_item ["source path" ] = self .source_name_or_path
81
+ if len (oss .license ) > 0 :
82
+ json_item ["license" ] = oss .license
83
+ if oss .download_location :
84
+ json_item ["download location" ] = oss .download_location
85
+ if oss .homepage :
86
+ json_item ["homepage" ] = oss .homepage
87
+ if oss .copyright :
88
+ json_item ["copyright text" ] = oss .copyright
89
+ if self .exclude or oss .exclude :
90
+ json_item ["exclude" ] = True
91
+ if oss .comment :
92
+ json_item ["comment" ] = oss .comment
93
+ items .append (json_item )
94
+ else :
95
+ json_item = {}
96
+ if self .source_name_or_path :
97
+ json_item ["source path" ] = self .source_name_or_path
98
+ if self .exclude :
99
+ json_item ["exclude" ] = True
100
+ if self .comment :
101
+ json_item ["comment" ] = self .comment
102
+ return items
0 commit comments