Skip to content

Commit 0116006

Browse files
committed
fake server: improve logging
1 parent eddfec1 commit 0116006

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

pyrdp/logging/log.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class LOGGER_NAMES:
1717
PYRDP = "pyrdp"
1818
MITM = f"{PYRDP}.mitm"
1919
MITM_CONNECTIONS = f"{MITM}.connections"
20+
MITM_FAKE_SERVER = f"{MITM}.fake_server"
2021
PLAYER = f"{PYRDP}.player"
2122
PLAYER_UI = f"{PLAYER}.ui"
2223
NTLMSSP = f"ntlmssp"

pyrdp/mitm/FakeServer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
# Copyright (C) 2022
44
# Licensed under the GPLv3 or later.
55
#
6-
import multiprocessing, os, random, shutil, socket, subprocess, threading, time
6+
import logging, multiprocessing, os, random, shutil, socket, subprocess, threading, time
77

88
from tkinter import *
99
from PIL import Image, ImageTk
1010
from pyvirtualdisplay import Display
1111

12-
from logging import LoggerAdapter
12+
from pyrdp.logging import SessionLogger, LOGGER_NAMES
1313

1414
BACKGROUND_COLOR = "#044a91"
1515
IMAGES_DIR = os.path.dirname(__file__) + "/images"
@@ -159,11 +159,14 @@ def show_loading_animation(self, index):
159159

160160

161161
class FakeServer(threading.Thread):
162-
def __init__(self, targetHost: str, targetPort: int, log: LoggerAdapter):
162+
def __init__(self, targetHost: str, targetPort: int = 3389, sessionID: str = None):
163163
super().__init__()
164164
self.targetHost = targetHost
165165
self.targetPort = targetPort
166-
self.log = log
166+
self.log = SessionLogger(
167+
logging.getLogger(LOGGER_NAMES.MITM_FAKE_SERVER), sessionID
168+
)
169+
self.log.info("test")
167170

168171
self._launch_display()
169172

pyrdp/mitm/RDPMITM.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self, mainLogger: SessionLogger, crawlerLogger: SessionLogger, conf
7878
self.statCounter = StatCounter()
7979
"""Class to keep track of connection-related statistics such as # of mouse events, # of output events, etc."""
8080

81-
self.state = state if state is not None else RDPMITMState(self.config, self.log.sessionID, self.getLog)
81+
self.state = state if state is not None else RDPMITMState(self.config, self.log.sessionID)
8282
"""The MITM state"""
8383

8484
self.client = RDPLayerSet()

pyrdp/mitm/state.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
# Licensed under the GPLv3 or later.
55
#
66

7-
from typing import Callable, Dict, List, Optional
7+
from typing import Dict, List, Optional
88

99
from Crypto.PublicKey import RSA
1010

1111
from pyrdp.enum import NegotiationProtocols, ParserMode
1212
from pyrdp.layer import FastPathLayer, SecurityLayer, TLSSecurityLayer
13-
from pyrdp.logging import SessionLogger
1413
from pyrdp.parser import createFastPathParser
1514
from pyrdp.pdu import ClientChannelDefinition
1615
from pyrdp.security import RC4CrypterProxy, SecuritySettings
@@ -22,7 +21,7 @@ class RDPMITMState:
2221
State object for the RDP MITM. This is for data that needs to be shared across components.
2322
"""
2423

25-
def __init__(self, config: MITMConfig, sessionID: str, getLog: Callable[[str], SessionLogger]):
24+
def __init__(self, config: MITMConfig, sessionID: str):
2625
self.requestedProtocols: Optional[NegotiationProtocols] = None
2726
"""The original request protocols"""
2827

@@ -94,9 +93,6 @@ def __init__(self, config: MITMConfig, sessionID: str, getLog: Callable[[str], S
9493
self.fakeServer = None
9594
"""The current fake server"""
9695

97-
self.getLog = getLog
98-
"""Function to create additional loggers"""
99-
10096
self.securitySettings.addObserver(self.crypters[ParserMode.CLIENT])
10197
self.securitySettings.addObserver(self.crypters[ParserMode.SERVER])
10298

@@ -139,9 +135,12 @@ def useRedirectionHost(self):
139135

140136
def useFakeServer(self):
141137
from pyrdp.mitm.FakeServer import FakeServer
138+
142139
self.fakeServer = FakeServer(
143-
self.config.targetHost, self.config.targetPort, self.getLog("")
140+
self.config.targetHost,
141+
targetPort=self.config.targetPort,
142+
sessionID=self.sessionID,
144143
)
145144
self.effectiveTargetHost = "127.0.0.1"
146145
self.effectiveTargetPort = self.fakeServer.port
147-
self.fakeServer.start()
146+
self.fakeServer.start()

test/test_prerecorded.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def sendBytesStub(_: bytes):
122122
config.outDir = output_directory
123123

124124
# replay_transport = FileLayer(output_path)
125-
state = RDPMITMState(config, log.sessionID, lambda name : log.createChild(name))
125+
state = RDPMITMState(config, log.sessionID)
126126
super().__init__(log, log, config, state, CustomMITMRecorder([], state))
127127

128128
self.client.tcp.sendBytes = sendBytesStub

0 commit comments

Comments
 (0)