
Protocole de bureau à distance dans Twisted Python

Protocole de bureau à distance en Python Twisted.
RDPY est une implémentation pure Python du protocole Microsoft RDP (Remote Desktop Protocol) (côté client et serveur). RDPY est construit sur le moteur réseau piloté par événements Twisted. RDPY prend en charge la couche de sécurité RDP standard, RDP sur SSL et l'authentification NLA (via le protocole d'authentification ntlmv2).
RDPY fournit les binaires RDP et VNC suivants :
RDPY est entièrement implémenté en Python, à l'exception de l'algorithme de décompression bitmap qui est implémenté en C pour des raisons de performance.
Les dépendances ne sont nécessaires que pour les binaires pyqt4 :
Exemple pour les systèmes basés sur Debian :
sudo apt-get install python-qt4
Exemple pour OS X pour installer PyQt avec 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
Ou utilisez PIP :
$ pip install rdpy
Pour virtualenv, vous devrez lier la bibliothèque qt4 à celui-ci :
$ 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 est livré avec des binaires très utiles. Ces binaires sont compatibles Linux et Windows.
rdpy-rdpclient est un client RDP Qt4 simple.
$ rdpy-rdpclient.py [-u username] [-p password] [-d domain] [-r rss_ouput_file] [...] XXX.XXX.XXX.XXX[:3389]
Vous pouvez utiliser rdpy-rdpclient dans un scénario de session d'enregistrement, utilisé dans rdpy-rdphoneypot.
rdpy-vncclient est un client VNC Qt4 simple.
$ rdpy-vncclient.py [-p password] XXX.XXX.XXX.XXX[:5900]
rdpy-rdpscreenshot enregistre l'écran de connexion dans un fichier.
$ rdpy-rdpscreenshot.py [-w width] [-l height] [-o output_file_path] XXX.XXX.XXX.XXX[:3389]
rdpy-vncscreenshot enregistre la première mise à jour d'écran dans un fichier.
$ rdpy-vncscreenshot.py [-p password] [-o output_file_path] XXX.XXX.XXX.XXX[:5900]
rdpy-rdpmitm est un proxy RDP qui vous permet de réaliser une attaque Man In The Middle sur le protocole RDP. Enregistre un scénario de session dans un fichier rss qui peut être rejoué par rdpy-rssplayer.
$ rdpy-rdpmitm.py -o output_dir [-l listen_port] [-k private_key_file_path] [-c certificate_file_path] [-r (for XP or server 2003 client)] target_host[:target_port]
Le répertoire de sortie est utilisé pour enregistrer le fichier rss avec le format suivant (YYYYMMDDHHMMSS_ip_index.rss) Le fichier de clé privée et le fichier de certificat sont des fichiers cryptographiques classiques pour les connexions SSL. Le protocole RDP peut négocier sa propre couche de sécurité. Si l'un des deux paramètres est omis, le serveur utilise le RDP standard comme couche de sécurité.
rdpy-rdphoneypot est un pot de miel RDP. Utilise un scénario de session enregistré pour rejouer le scénario via le protocole RDP.
$ rdpy-rdphoneypot.py [-l listen_port] [-k private_key_file_path] [-c certificate_file_path] rss_file_path_1 ... rss_file_path_N
Le fichier de clé privée et le fichier de certificat sont des fichiers cryptographiques classiques pour les connexions SSL. Le protocole RDP peut négocier sa propre couche de sécurité. Si l'un des deux paramètres est omis, le serveur utilise le RDP standard comme couche de sécurité. Vous pouvez spécifier plusieurs fichiers pour correspondre à des tailles d'écran plus courantes.
rdpy-rssplayer est utilisé pour rejouer les fichiers de scénario de session enregistrée (rss) générés par les binaires rdpy-rdpmitm ou rdpy-rdpclient.
$ rdpy-rssplayer.py rss_file_path
RDPY peut également être utilisé comme widget Qt via la classe rdpy.ui.qt4.QRemoteDesktop. Il peut être intégré dans votre propre application Qt. qt4reactor doit être utilisé dans votre application pour que Twisted et Qt fonctionnent ensemble. Pour plus de détails, consultez les sources de rdpy-rdpclient.
En résumé, RDPY peut être utilisé comme une bibliothèque de protocole avec un moteur twisted.
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()