
Remote Desktop-Protokoll in Twisted Python

Remote Desktop Protocol in Twisted Python.
RDPY ist eine reine Python-Implementierung des Microsoft RDP (Remote Desktop Protocol) (Client- und Server-Seite). RDPY basiert auf der ereignisgesteuerten Netzwerk-Engine Twisted. RDPY unterstützt die standardmäßige RDP-Sicherheitsschicht, RDP über SSL und NLA-Authentifizierung (über das ntlmv2-Authentifizierungsprotokoll).
RDPY stellt die folgenden RDP- und VNC-Binärdateien bereit:
RDPY ist vollständig in Python implementiert, mit Ausnahme des Bitmap-Dekompressionsalgorithmus, der aus Leistungsgründen in C implementiert ist.
Abhängigkeiten werden nur für die pyqt4-Binärdateien benötigt:
Beispiel für Debian-basierte Systeme:
sudo apt-get install python-qt4
Beispiel für OS X zur Installation von PyQt mit Homebrew:
$ brew install qt sip pyqt
$ git clone https://github.com/citronneur/rdpy.git rdpy
$ pip install twisted pyopenssl qt4reactor service_identity rsa pyasn1
$ python rdpy/setup.py install
Oder mit PIP:
$ pip install rdpy
Für virtualenv musst du die qt4-Bibliothek darauf verlinken:
$ ln -s /usr/lib/python2.7/dist-packages/PyQt4/ $VIRTUAL_ENV/lib/python2.7/site-packages/
$ ln -s /usr/lib/python2.7/dist-packages/sip.so $VIRTUAL_ENV/lib/python2.7/site-packages/
RDPY enthält einige sehr nützliche Binärdateien. Diese Binärdateien sind mit Linux und Windows kompatibel.
rdpy-rdpclient ist ein einfacher RDP-Qt4-Client.
$ rdpy-rdpclient.py [-u username] [-p password] [-d domain] [-r rss_ouput_file] [...] XXX.XXX.XXX.XXX[:3389]
Du kannst rdpy-rdpclient in einem Aufzeichnungssitzungs-Szenario verwenden, das in rdpy-rdphoneypot verwendet wird.
rdpy-vncclient ist ein einfacher VNC-Qt4-Client.
$ rdpy-vncclient.py [-p password] XXX.XXX.XXX.XXX[:5900]
rdpy-rdpscreenshot speichert den Anmeldebildschirm in einer Datei.
$ rdpy-rdpscreenshot.py [-w width] [-l height] [-o output_file_path] XXX.XXX.XXX.XXX[:3389]
rdpy-vncscreenshot speichert die erste Bildschirmaktualisierung in einer Datei.
$ rdpy-vncscreenshot.py [-p password] [-o output_file_path] XXX.XXX.XXX.XXX[:5900]
rdpy-rdpmitm ist ein RDP-Proxy, der einen Man-in-the-Middle-Angriff auf das RDP-Protokoll ermöglicht. Zeichnet Sitzungsszenarien in RSS-Dateien auf, die mit rdpy-rssplayer abgespielt werden können.
$ rdpy-rdpmitm.py -o output_dir [-l listen_port] [-k private_key_file_path] [-c certificate_file_path] [-r (für XP oder Server 2003 Client)] target_host[:target_port]
Das Ausgabeverzeichnis wird verwendet, um die RSS-Datei im folgenden Format zu speichern (YYYYMMDDHHMMSS_ip_index.rss). Die private Schlüsseldatei und die Zertifikatsdatei sind klassische kryptografische Dateien für SSL-Verbindungen. Das RDP-Protokoll kann seine eigene Sicherheitsschicht aushandeln. Wenn einer der beiden Parameter weggelassen wird, verwendet der Server standardmäßig RDP als Sicherheitsschicht.
rdpy-rdphoneypot ist ein RDP-Honeypot. Verwendet aufgezeichnete Sitzungsszenarien, um Szenarien über das RDP-Protokoll abzuspielen.
$ rdpy-rdphoneypot.py [-l listen_port] [-k private_key_file_path] [-c certificate_file_path] rss_file_path_1 ... rss_file_path_N
Die private Schlüsseldatei und die Zertifikatsdatei sind klassische kryptografische Dateien für SSL-Verbindungen. Das RDP-Protokoll kann seine eigene Sicherheitsschicht aushandeln. Wenn einer der beiden Parameter weggelassen wird, verwendet der Server standardmäßig RDP als Sicherheitsschicht. Du kannst mehr als eine Datei angeben, um häufigere Bildschirmgrößen abzudecken.
rdpy-rssplayer wird verwendet, um aufgezeichnete Sitzungsszenario-Dateien (RSS) abzuspielen, die von rdpy-rdpmitm oder rdpy-rdpclient erstellt wurden.
$ rdpy-rssplayer.py rss_file_path
RDPY kann auch als Qt-Widget über die Klasse rdpy.ui.qt4.QRemoteDesktop verwendet werden. Es kann in deine eigene Qt-Anwendung eingebettet werden. Für das Zusammenspiel von Twisted und Qt muss qt4reactor in deiner App verwendet werden. Weitere Details findest du in den Quellen von rdpy-rdpclient.
Kurz gesagt, RDPY kann als Protokollbibliothek mit einer Twisted-Engine verwendet werden.
from rdpy.protocol.rdp import rdp
class MyRDPFactory(rdp.ClientFactory):
def clientConnectionLost(self, connector, reason):
reactor.stop()
def clientConnectionFailed(self, connector, reason):
reactor.stop()
def buildObserver(self, controller, addr):
class MyObserver(rdp.RDPClientObserver):
def onReady(self):
"""
@summary: Call when stack is ready
"""
#send 'r' key
self._controller.sendKeyEventUnicode(ord(unicode("r".toUtf8(), encoding="UTF-8")), True)
#mouse move and click at pixel 200x200
self._controller.sendPointerEvent(200, 200, 1, true)
def onUpdate(self, destLeft, destTop, destRight, destBottom, width, height, bitsPerPixel, isCompress, data):
"""
@summary: Notify bitmap update
@param destLeft: xmin position
@param destTop: ymin position
@param destRight: xmax position because RDP can send bitmap with padding
@param destBottom: ymax position because RDP can send bitmap with padding
@param width: width of bitmap
@param height: height of bitmap
@param bitsPerPixel: number of bit per pixel
@param isCompress: use RLE compression
@param data: bitmap data
"""
def onSessionReady(self):
"""
@summary: Windows session is ready
"""
def onClose(self):
"""
@summary: Call when stack is close
"""
return MyObserver(controller)
from twisted.internet import reactor
reactor.connectTCP("XXX.XXX.XXX.XXX", 3389, MyRDPFactory())
reactor.run()
from rdpy.protocol.rdp import rdp
class MyRDPFactory(rdp.ServerFactory):
def buildObserver(self, controller, addr):
class MyObserver(rdp.RDPServerObserver):
def onReady(self):
"""
@summary: Call when server is ready
to send and receive messages
"""
def onKeyEventScancode(self, code, isPressed):
"""
@summary: Event call when a keyboard event is catch in scan code format
@param code: scan code of key
@param isPressed: True if key is down
@see: rdp.RDPServerObserver.onKeyEventScancode
"""
def onKeyEventUnicode(self, code, isPressed):
"""
@summary: Event call when a keyboard event is catch in unicode format
@param code: unicode of key
@param isPressed: True if key is down
@see: rdp.RDPServerObserver.onKeyEventUnicode
"""
def onPointerEvent(self, x, y, button, isPressed):
"""
@summary: Event call on mouse event
@param x: x position
@param y: y position
@param button: 1, 2, 3, 4 or 5 button
@param isPressed: True if mouse button is pressed
@see: rdp.RDPServerObserver.onPointerEvent
"""
def onClose(self):
"""
@summary: Call when human client close connection
@see: rdp.RDPServerObserver.onClose
"""
return MyObserver(controller)
from twisted.internet import reactor
reactor.listenTCP(3389, MyRDPFactory())
reactor.run()
from rdpy.protocol.rfb import rfb
class MyRFBFactory(rfb.ClientFactory):
def clientConnectionLost(self, connector, reason):
reactor.stop()
def clientConnectionFailed(self, connector, reason):
reactor.stop()
def buildObserver(self, controller, addr):
class MyObserver(rfb.RFBClientObserver):
def onReady(self):
"""
@summary: Event when network stack is ready to receive or send event
"""
def onUpdate(self, width, height, x, y, pixelFormat, encoding, data):
"""
@summary: Implement RFBClientObserver interface
@param width: width of new image
@param height: height of new image
@param x: x position of new image
@param y: y position of new image
@param pixelFormat: pixefFormat structure in rfb.message.PixelFormat
@param encoding: encoding type rfb.message.Encoding
@param data: image data in accordance with pixel format and encoding
"""
def onCutText(self, text):
"""
@summary: event when server send cut text event
@param text: text received
"""
def onBell(self):
"""
@summary: event when server send biiip
"""
def onClose(self):
"""
@summary: Call when stack is close
"""
return MyObserver(controller)
from twisted.internet import reactor
reactor.connectTCP("XXX.XXX.XXX.XXX", 3389, MyRFBFactory())
reactor.run()