
Twisted Python에서의 원격 데스크톱 프로토콜

Twisted Python 기반의 원격 데스크톱 프로토콜.
RDPY는 Microsoft RDP(원격 데스크톱 프로토콜) 프로토콜의 순수 Python 구현체입니다(클라이언트 및 서버 측). RDPY는 이벤트 기반 네트워크 엔진 Twisted 위에 구축되었습니다. RDPY는 표준 RDP 보안 계층, SSL을 통한 RDP 및 NLA 인증(ntlmv2 인증 프로토콜 사용)을 지원합니다.
RDPY는 다음과 같은 RDP 및 VNC 바이너리를 제공합니다:
RDPY는 비트맵 압축 해제 알고리즘을 제외하고는 모두 Python으로 구현되었습니다. 해당 알고리즘은 성능 향상을 위해 C로 구현되었습니다.
종속성은 pyqt4 바이너리에만 필요합니다:
Debian 계열 시스템 예시:
sudo apt-get install python-qt4
OS X에서 homebrew로 PyQt 설치 예시:
$ 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
또는 PIP 사용:
$ pip install rdpy
virtualenv의 경우, qt4 라이브러리를 링크해야 합니다:
$ 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에는 매우 유용한 바이너리들이 포함되어 있습니다. 이 바이너리들은 Linux 및 Windows와 호환됩니다.
rdpy-rdpclient는 간단한 RDP Qt4 클라이언트입니다.
$ rdpy-rdpclient.py [-u username] [-p password] [-d domain] [-r rss_ouput_file] [...] XXX.XXX.XXX.XXX[:3389]
rdpy-rdpclient는 rdpy-rdphoneypot에서 사용되는 녹화 세션 시나리오(Recorder Session Scenario)에서 사용할 수 있습니다.
rdpy-vncclient는 간단한 VNC Qt4 클라이언트입니다.
$ rdpy-vncclient.py [-p password] XXX.XXX.XXX.XXX[:5900]
rdpy-rdpscreenshot은 로그인 화면을 파일로 저장합니다.
$ rdpy-rdpscreenshot.py [-w width] [-l height] [-o output_file_path] XXX.XXX.XXX.XXX[:3389]
rdpy-vncscreenshot은 첫 번째 화면 업데이트를 파일로 저장합니다.
$ rdpy-vncscreenshot.py [-p password] [-o output_file_path] XXX.XXX.XXX.XXX[:5900]
rdpy-rdpmitm은 RDP 프로토콜에 대한 중간자 공격을 수행할 수 있는 RDP 프록시입니다. 세션 시나리오를 rss 파일에 기록하며, 이 파일은 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]
출력 디렉토리는 다음 형식(YYYYMMDDHHMMSS_ip_index.rss)으로 rss 파일을 저장하는 데 사용됩니다. 개인 키 파일과 인증서 파일은 SSL 연결을 위한 일반적인 암호화 파일입니다. RDP 프로토콜은 자체 보안 계층을 협상할 수 있습니다. 두 매개변수 중 하나라도 생략하면 서버는 표준 RDP를 보안 계층으로 사용합니다.
rdpy-rdphoneypot은 RDP 허니팟입니다. 녹화된 세션 시나리오를 사용하여 RDP 프로토콜을 통해 시나리오를 재생합니다.
$ rdpy-rdphoneypot.py [-l listen_port] [-k private_key_file_path] [-c certificate_file_path] rss_file_path_1 ... rss_file_path_N
개인 키 파일과 인증서 파일은 SSL 연결을 위한 일반적인 암호화 파일입니다. RDP 프로토콜은 자체 보안 계층을 협상할 수 있습니다. 두 매개변수 중 하나라도 생략하면 서버는 표준 RDP를 보안 계층으로 사용합니다. 더 일반적인 화면 크기에 맞추기 위해 여러 개의 파일을 지정할 수 있습니다.
rdpy-rssplayer는 rdpy-rdpmitm 또는 rdpy-rdpclient 바이너리에 의해 생성된 녹화 세션 시나리오(rss) 파일을 재생하는 데 사용됩니다.
$ rdpy-rssplayer.py rss_file_path
RDPY는 rdpy.ui.qt4.QRemoteDesktop 클래스를 통해 Qt 위젯으로도 사용할 수 있습니다. 자신의 Qt 애플리케이션에 포함시킬 수 있습니다. Twisted와 Qt가 함께 작동하려면 애플리케이션에서 qt4reactor를 사용해야 합니다. 자세한 내용은 rdpy-rdpclient의 소스를 참조하십시오.
요약하면 RDPY는 트위스트 엔진과 함께 프로토콜 라이브러리로 사용될 수 있습니다.
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()