Skip to content

Commit a669b7e

Browse files
committed
Apply simple mode
1 parent 84200ac commit a669b7e

File tree

3 files changed

+142
-18
lines changed

3 files changed

+142
-18
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# FOSSLight Binary analysis script
4+
# Copyright (c) 2024 LG Electronics Inc.
5+
# SPDX-License-Identifier: Apache-2.0
6+
import os
7+
import re
8+
import logging
9+
from datetime import datetime
10+
import fosslight_util.constant as constant
11+
from fosslight_util.write_txt import write_txt_file
12+
from fosslight_util.download import compression_extension
13+
import fosslight_binary.binary_analysis as fl_bin
14+
from fosslight_util.set_log import init_log
15+
16+
_REMOVE_FILE_EXTENSION_SIMPLE = ['ttf', 'otf', 'png', 'gif', 'jpg', 'bmp', 'jpeg']
17+
logger = logging.getLogger(constant.LOGGER_NAME)
18+
19+
20+
def exclude_bin_for_simple_mode(binary_list):
21+
bin_list = []
22+
compressed_list = []
23+
compressed_ext_wo_dot = [ext.lstrip('.') for ext in compression_extension]
24+
25+
for bin in binary_list:
26+
file_lower_case = bin.bin_name_with_path.lower()
27+
extension = os.path.splitext(file_lower_case)[1][1:].strip()
28+
29+
for compress_ext in compressed_ext_wo_dot:
30+
if extension == compress_ext:
31+
compressed_list.append(bin.bin_name_with_path)
32+
33+
remove_file_ext_list = _REMOVE_FILE_EXTENSION_SIMPLE + compressed_ext_wo_dot
34+
if any(extension == remove_ext for remove_ext in remove_file_ext_list):
35+
continue
36+
if not (re.search(r".*source\.jar", bin.bin_name_with_path.lower()) or bin.exclude):
37+
continue
38+
39+
bin_list.append(bin.bin_name_with_path)
40+
41+
return compressed_list, bin_list
42+
43+
44+
def convert_list_to_str(input_list):
45+
output_text = '\n'.join(map(str, input_list))
46+
return output_text
47+
48+
49+
def check_output_path(output='', start_time):
50+
compressed_list_txt = ""
51+
simple_bin_list_txt = ""
52+
output_path = ""
53+
54+
if output != "":
55+
if not os.path.isdir(output) and output.endswith('.txt'):
56+
output_path = os.path.dirname(output)
57+
basename = os.path.basename(output)
58+
basename_file, _ = os.path.splitext(basename)
59+
compressed_list_txt = f"{basename_file}_compressed_list.txt"
60+
simple_bin_list_txt = f"{basename_file}.txt"
61+
else:
62+
output_path = output
63+
compressed_list_txt = f"compressed_list_{start_time}.txt"
64+
simple_bin_list_txt = f"binary_list_{start_time}.txt"
65+
else:
66+
compressed_list_txt = f"compressed_list_{start_time}.txt"
67+
simple_bin_list_txt = f"binary_list_{start_time}.txt"
68+
69+
if output_path == "":
70+
output_path = os.getcwd()
71+
else:
72+
output_path = os.path.abspath(output_path)
73+
74+
compressed_list_txt = os.path.join(output_path, compressed_list_txt)
75+
simple_bin_list_txt = os.path.join(output_path, simple_bin_list_txt)
76+
77+
return output_path, compressed_list_txt, simple_bin_list_txt
78+
79+
80+
def init_simple(output_file_name, pkg_name, start_time):
81+
global logger
82+
83+
_result_log = {
84+
"Tool Info": pkg_name,
85+
"Mode": "Simple mode"
86+
}
87+
88+
output_path, compressed_list_txt, simple_bin_list_txt = check_output_path(output_file_name, start_time)
89+
90+
log_file = os.path.join(output_path, f"fosslight_log_bin_{start_time}.txt")
91+
logger, _result_log = init_log(log_file, True, logging.INFO, logging.DEBUG,
92+
pkg_name)
93+
94+
return _result_log, compressed_list_txt, simple_bin_list_txt
95+
96+
97+
def print_simple_mode(compressed_list_txt, simple_bin_list_txt, compressed_list, bin_list):
98+
if compressed_list:
99+
success, error = write_txt_file(compressed_list_txt, convert_list_to_str(compressed_list))
100+
if not success:
101+
logger.info(f"Error to write compressed list file for simple mode : {error}")
102+
if bin_list:
103+
success, error = write_txt_file(simple_bin_list_txt, convert_list_to_str(bin_list))
104+
if not success:
105+
logger.info(f"Error to write binary list file for simple mode : {error}")
106+
return success
107+
108+
109+
def filter_binary(bin_list):
110+
compressed_list, bin_list = exclude_bin_for_simple_mode(bin_list)
111+
return compressed_list, bin_list

src/fosslight_binary/binary_analysis.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
from fosslight_util.set_log import init_log
1515
import fosslight_util.constant as constant
1616
from fosslight_util.output_format import check_output_formats, write_output_file
17-
from ._binary_dao import get_oss_info_from_db
1817
from ._binary import BinaryItem, TLSH_CHECKSUM_NULL
18+
from ._binary_dao import get_oss_info_from_db
1919
from ._jar_analysis import analyze_jar_file, merge_binary_list
20+
from ._simple_mode import print_simple_mode, filter_binary, init_simple
2021
from fosslight_util.correct import correct_with_yaml
2122
from fosslight_util.oss_item import ScannerItem
2223
import hashlib
@@ -40,7 +41,7 @@
4041
_REMOVE_DIR = [os.path.sep + dir_name + os.path.sep for dir_name in _REMOVE_DIR]
4142
_error_logs = []
4243
_root_path = ""
43-
_start_time = ""
44+
start_time = ""
4445
windows = False
4546
BYTES = 2048
4647
BIN_EXT_HEADER = {'BIN_FL_Binary': ['ID', 'Binary Path', 'OSS Name',
@@ -71,18 +72,14 @@ def get_checksum_and_tlsh(bin_with_path):
7172

7273

7374
def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
74-
global _root_path, logger, _start_time
75+
global logger
7576

7677
_json_ext = ".json"
77-
_start_time = datetime.now().strftime('%y%m%d_%H%M')
7878
_result_log = {
79-
"Tool Info": PKG_NAME
79+
"Tool Info": PKG_NAME,
80+
"Mode": "Normal Mode"
8081
}
8182

82-
_root_path = path_to_find_bin
83-
if not path_to_find_bin.endswith(os.path.sep):
84-
_root_path += os.path.sep
85-
8683
success, msg, output_path, output_files, output_extensions = check_output_formats(output_file_name, formats)
8784

8885
if success:
@@ -96,16 +93,16 @@ def init(path_to_find_bin, output_file_name, formats, path_to_exclude=[]):
9693
for i, output_extension in enumerate(output_extensions):
9794
if output_files[i] is None or output_files[i] == "":
9895
if output_extension == _json_ext:
99-
output_files[i] = f"fosslight_opossum_bin_{_start_time}"
96+
output_files[i] = f"fosslight_opossum_bin_{start_time}"
10097
else:
101-
output_files[i] = f"fosslight_report_bin_{_start_time}"
98+
output_files[i] = f"fosslight_report_bin_{start_time}"
10299

103100
combined_paths_and_files = [os.path.join(output_path, file) for file in output_files]
104101
else:
105102
logger.error(f"Format error - {msg}")
106103
sys.exit(1)
107104

108-
log_file = os.path.join(output_path, f"fosslight_log_bin_{_start_time}.txt")
105+
log_file = os.path.join(output_path, f"fosslight_log_bin_{start_time}.txt")
109106
logger, _result_log = init_log(log_file, True, logging.INFO, logging.DEBUG,
110107
PKG_NAME, path_to_find_bin, path_to_exclude)
111108

@@ -161,9 +158,19 @@ def get_file_list(path_to_find, abs_path_to_exclude):
161158

162159
def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=False,
163160
correct_mode=True, correct_filepath="", path_to_exclude=[]):
161+
global start_time, _root_path
164162

165-
_result_log, result_reports, output_extensions = init(
166-
path_to_find_bin, output_dir, formats, path_to_exclude)
163+
start_time = datetime.now().strftime('%y%m%d_%H%M')
164+
165+
_root_path = path_to_find_bin
166+
if not path_to_find_bin.endswith(os.path.sep):
167+
_root_path += os.path.sep
168+
169+
if simple_mode:
170+
_result_log, compressed_list_txt, simple_bin_list_txt = init_simple(output_dir, PKG_NAME, start_time)
171+
else:
172+
_result_log, result_reports, output_extensions = init(
173+
path_to_find_bin, output_dir, formats, path_to_exclude)
167174

168175
total_bin_cnt = 0
169176
total_file_cnt = 0
@@ -191,9 +198,15 @@ def find_binaries(path_to_find_bin, output_dir, formats, dburl="", simple_mode=F
191198
exit=True)
192199
total_bin_cnt = len(return_list)
193200
if simple_mode:
194-
bin_list = [bin.bin_name_with_path for bin in return_list]
201+
try:
202+
compressed_list, bin_list = filter_binary(return_list)
203+
success = print_simple_mode(compressed_list_txt, simple_bin_list_txt, compressed_list, bin_list)
204+
except Exception as ex:
205+
error_occured(error_msg=f"Failed to run simple mode: {ex}",
206+
result_log=_result_log,
207+
exit=True)
195208
else:
196-
scan_item = ScannerItem(PKG_NAME, _start_time)
209+
scan_item = ScannerItem(PKG_NAME, start_time)
197210
scan_item.set_cover_pathinfo(path_to_find_bin, path_to_exclude)
198211
try:
199212
# Run OWASP Dependency-check
@@ -307,7 +320,7 @@ def print_result_log(success=True, result_log={}, file_cnt="", bin_file_cnt="",
307320
if "Running time" in result_log:
308321
start_time = result_log["Running time"]
309322
else:
310-
start_time = _start_time
323+
start_time = start_time
311324
result_log["Running time"] = start_time + " ~ " + \
312325
datetime.now().strftime('%Y%m%d_%H%M%S')
313326
result_log["Execution result"] = 'Success' if success else 'Error occurred'

src/fosslight_binary/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def main():
4949
print_package_version(_PKG_NAME, "FOSSLight Binary Scanner Version:")
5050
sys.exit(0)
5151

52-
if args.simple:
52+
if args.simple: # -s option
5353
simple_mode = True
5454

5555
if args.path: # -p option

0 commit comments

Comments
 (0)