Peach Fuzzer small fixes

- fix for download and copy peach fuzzer files
- removed fuzzed command prefix/suffix and encoding

Signed-off-by: Katarzyna Treder <katarzyna.treder@h-partners.com>
This commit is contained in:
Katarzyna Treder 2024-08-30 14:32:46 +02:00
parent 521d1dd4c9
commit 3b875165d9

View File

@ -1,10 +1,10 @@
# #
# Copyright(c) 2021 Intel Corporation # Copyright(c) 2021 Intel Corporation
# Copyright(c) 2024 Huawei Technologies Co., Ltd.
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
# #
import os import os
import wget
import base64 import base64
import posixpath import posixpath
import random import random
@ -15,6 +15,7 @@ from collections import namedtuple
from core.test_run import TestRun from core.test_run import TestRun
from test_tools import fs_utils from test_tools import fs_utils
from test_tools.fs_utils import create_directory, check_if_file_exists, write_file from test_tools.fs_utils import create_directory, check_if_file_exists, write_file
from test_utils import os_utils
class PeachFuzzer: class PeachFuzzer:
@ -28,7 +29,7 @@ class PeachFuzzer:
"peach-3.0.202-linux-x86_64-release.zip" "peach-3.0.202-linux-x86_64-release.zip"
base_dir = "/root/Fuzzy" base_dir = "/root/Fuzzy"
peach_dir = "peach-3.0.202-linux-x86_64-release" peach_dir = "peach-3.0.202-linux-x86_64-release"
xml_config_template = posixpath.join(posixpath.dirname(__file__), "config_template.xml") xml_config_template = os.path.join(os.path.dirname(__file__), "config_template.xml")
xml_config_file = posixpath.join(base_dir, "fuzzerConfig.xml") xml_config_file = posixpath.join(base_dir, "fuzzerConfig.xml")
xml_namespace = "http://peachfuzzer.com/2012/Peach" xml_namespace = "http://peachfuzzer.com/2012/Peach"
fuzzy_output_file = posixpath.join(base_dir, "fuzzedParams.txt") fuzzy_output_file = posixpath.join(base_dir, "fuzzedParams.txt")
@ -40,8 +41,7 @@ class PeachFuzzer:
def get_fuzzed_command(cls, command_template: bytes, count: int): def get_fuzzed_command(cls, command_template: bytes, count: int):
""" """
Generate command with fuzzed parameter provided on command_template. Generate command with fuzzed parameter provided on command_template.
Command is ready to be executed with test executor :param command_template: string with command to be executed.
:param command_template: byte string with command to be executed.
parameter to be replaced with fuzzed string has to be tested_param_placeholder parameter to be replaced with fuzzed string has to be tested_param_placeholder
:param count: amount of fuzzed commands to generate :param count: amount of fuzzed commands to generate
:returns: named tuple with fuzzed param and CLI ready to be executed with Test-Framework :returns: named tuple with fuzzed param and CLI ready to be executed with Test-Framework
@ -49,14 +49,18 @@ class PeachFuzzer:
""" """
TestRun.LOGGER.info(f"Try to get commands with fuzzed parameters") TestRun.LOGGER.info(f"Try to get commands with fuzzed parameters")
FuzzedCommand = namedtuple('FuzzedCommand', ['param', 'command']) FuzzedCommand = namedtuple('FuzzedCommand', ['param', 'command'])
command_template = command_template.encode("ascii")
if cls.tested_param_placeholder not in command_template: if cls.tested_param_placeholder not in command_template:
TestRun.block("No param placeholder is found in command template!") TestRun.block("No param placeholder is found in command template!")
cmd_prefix = b"echo "
cmd_suffix = b" | base64 --decode | sh"
for fuzzed_parameter in cls.generate_peach_fuzzer_parameters(count): for fuzzed_parameter in cls.generate_peach_fuzzer_parameters(count):
yield FuzzedCommand(fuzzed_parameter, yield FuzzedCommand(
cmd_prefix + base64.b64encode(command_template.replace( fuzzed_parameter,
cls.tested_param_placeholder, fuzzed_parameter)) + cmd_suffix) command_template.replace(
cls.tested_param_placeholder,
fuzzed_parameter
)
)
@classmethod @classmethod
def generate_peach_fuzzer_parameters(cls, count: int): def generate_peach_fuzzer_parameters(cls, count: int):
@ -83,7 +87,7 @@ class PeachFuzzer:
# process fuzzy output file locally on the controller as it can be very big # process fuzzy output file locally on the controller as it can be very big
local_fuzzy_file = tempfile.NamedTemporaryFile(delete=False) local_fuzzy_file = tempfile.NamedTemporaryFile(delete=False)
local_fuzzy_file.close() local_fuzzy_file.close()
TestRun.executor.rsync_from(cls.fuzzy_output_file, local_fuzzy_file.name) TestRun.executor.copy_from(cls.fuzzy_output_file, local_fuzzy_file.name)
with open(local_fuzzy_file.name, "r") as fd: with open(local_fuzzy_file.name, "r") as fd:
for fuzzed_param_line in fd: for fuzzed_param_line in fd:
fuzzed_param_bytes = base64.b64decode(fuzzed_param_line) fuzzed_param_bytes = base64.b64decode(fuzzed_param_line)
@ -112,7 +116,7 @@ class PeachFuzzer:
value: 'true' value: 'true'
""" """
if not posixpath.exists(cls.xml_config_template): if not os.path.exists(cls.xml_config_template):
TestRun.block("Peach fuzzer xml config template not found!") TestRun.block("Peach fuzzer xml config template not found!")
root = etree.parse(cls.xml_config_template) root = etree.parse(cls.xml_config_template)
data_model = root.find(f'{{{cls.xml_namespace}}}DataModel[@name="Value"]') data_model = root.find(f'{{{cls.xml_namespace}}}DataModel[@name="Value"]')
@ -127,7 +131,7 @@ class PeachFuzzer:
and just passed as is to PeachFuzzer. and just passed as is to PeachFuzzer.
:param config_file: Peach Fuzzer XML config to be copied to the DUT :param config_file: Peach Fuzzer XML config to be copied to the DUT
""" """
if not posixpath.exists(config_file): if not os.path.exists(config_file):
TestRun.block("Peach fuzzer xml config to be copied doesn't exist!") TestRun.block("Peach fuzzer xml config to be copied doesn't exist!")
create_directory(cls.base_dir, True) create_directory(cls.base_dir, True)
TestRun.executor.rsync_to(config_file, cls.xml_config_file) TestRun.executor.rsync_to(config_file, cls.xml_config_file)
@ -150,14 +154,14 @@ class PeachFuzzer:
""" """
Install Peach Fuzzer on the DUT Install Peach Fuzzer on the DUT
""" """
peach_archive = wget.download(cls.peach_fuzzer_3_0_url)
create_directory(cls.base_dir, True) create_directory(cls.base_dir, True)
TestRun.executor.rsync_to(f"\"{peach_archive}\"", f"{cls.base_dir}") peach_archive = os_utils.download_file(
cls.peach_fuzzer_3_0_url, destination_dir=cls.base_dir
)
TestRun.executor.run_expect_success( TestRun.executor.run_expect_success(
f'cd {cls.base_dir} && unzip -u "{peach_archive}"') f'cd {cls.base_dir} && unzip -u "{peach_archive}"')
if cls._is_installed(): if cls._is_installed():
TestRun.LOGGER.info("Peach fuzzer installed successfully") TestRun.LOGGER.info("Peach fuzzer installed successfully")
os.remove(peach_archive)
else: else:
TestRun.block("Peach fuzzer installation failed!") TestRun.block("Peach fuzzer installation failed!")