-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvncserver.py
More file actions
executable file
·132 lines (112 loc) · 4.7 KB
/
vncserver.py
File metadata and controls
executable file
·132 lines (112 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pyvncs
from argparse import ArgumentParser
from threading import Thread
from time import sleep
import sys
import socket
import ssl
import signal
from lib import log
_debug = log.debug
def signal_handler(signal, frame):
_debug("Exiting on %s signal..." % signal)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
class ClientThread(Thread):
def __init__(self, sock, ip, port, vnc_config):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
self.setDaemon(True)
self.vnc_config = vnc_config
def __del__(self):
_debug("ClientThread died")
def run(self):
_debug("[+] New server socket thread started for " + self.ip + ":" + str(self.port))
server = pyvncs.server.VNCServer(self.sock,
auth_type=self.vnc_config.auth_type,
password=self.vnc_config.vnc_password,
pem_file=self.vnc_config.pem_file,
vnc_config=self.vnc_config
)
#server.vnc_config.eightbitdither = self.vnc_config.eightbitdither
status = server.init()
if not status:
_debug("Error negotiating client init")
return False
server.handle_client()
def main(argv):
class vnc_config:
pass
parser = ArgumentParser()
parser.add_argument("-l", "--listen-address", dest="listen_addr",
help="Listen in this address, default: %s" % ("0.0.0.0"), required=False, default='0.0.0.0')
parser.add_argument("-p", "--port", dest="listen_port",
help="Listen in this port, default: %s" % ("5901"), type=int, required=False, default='5901')
parser.add_argument("-A", "--auth-type",
help="Sets VNC authentication type (supported: 2(vnc), 19(vencrypt))",
required=False,
type=int,
default=2,
dest="auth_type"
)
parser.add_argument("-C", "--cert-file",
help="SSL PEM file",
required=False,
type=str,
default='',
dest='pem_file'
)
parser.add_argument("-P", "--vncpassword", help="Sets authentication password", required=True, dest="vnc_password")
parser.add_argument("-8", "--8bitdither", help="Enable 8 bit dithering", required=False, action='store_true', dest="dither")
parser.add_argument("-O", "--output-file", help="Redirects all debug output to file", required=False, dest="outfile")
parser.add_argument("-t", "--title", help="VNC Window title", required=False, dest="win_title", default="pyvncs")
args = parser.parse_args()
if args.outfile is not None:
try:
fsock = open(args.outfile, 'w')
except Exception as ex:
print("Error:", ex, file=sys.stderr)
sys.exit(1)
sys.stdout = sys.stderr = fsock
vnc_config.vnc_password = args.vnc_password
vnc_config.eightbitdither = args.dither
vnc_config.auth_type = args.auth_type
vnc_config.pem_file = args.pem_file
vnc_config.win_title = args.win_title
sockServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sockServer.bind((args.listen_addr, args.listen_port))
_debug("Multithreaded Python server : Waiting for connections from TCP clients...")
_debug("Runing on:", sys.platform)
# FIXME run_as_admin() is not working on windows
# if sys.platform in ['win32', 'win64']:
# from lib.oshelpers import windows as win32
# if not win32.is_admin():
# ret = win32.run_as_admin()
# if ret is None:
# log.debug("Respawning with admin rights")
# sys.exit(0)
# elif ret is True:
# # admin rights
# log.debug("Running with admin rights!")
# else:
# print('Error(ret=%d): cannot elevate privilege.' % (ret))
# sys.exit(1)
while True:
sockServer.listen(4)
(conn, (ip,port)) = sockServer.accept()
newthread = ClientThread(sock=conn, ip=ip, port=port, vnc_config=vnc_config)
newthread.setDaemon(True)
newthread.start()
if __name__ == "__main__2":
try:
main(sys.argv)
except KeyboardInterrupt as e:
_debug("Exiting on ctrl+c...")
sys.exit()
if __name__ == "__main__":
main(sys.argv)