-
Notifications
You must be signed in to change notification settings - Fork 0
/
crd_connect.py
160 lines (112 loc) · 4.18 KB
/
crd_connect.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""This script connects to a Linux remote machine via CRD.
The command needs to be retrieved from https://remotedesktop.google.com/headless/
Example:
$ python crd_connect.py <copy/pasted command from CRD>
This script is run on a VM instance only.
"""
import argparse
import random
import subprocess
from logging_utils import CloudAndConsoleLogger
# Set up logging
cnc_logger = CloudAndConsoleLogger(module_name=__name__)
def create_parser():
"""Creates a parser for the command line arguments.
Returns:
argparse.ArgumentParser: The parser for the command line arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--code",
help="Unique code to allow connection via CRD with specific Google login.",
type=str,
default=None,
)
return parser
def construct_command(args):
"""Constructs the Linux CRD command to connect to a remote machine.
Args:
args (argparse.Namespace): The command line arguments.
Returns:
str: The Linux CRD command to connect to a remote machine.
"""
redirect_url = "'https://remotedesktop.google.com/_/oauthredirect'"
name = "$(hostname)"
command = "DISPLAY= /opt/google/chrome-remote-desktop/start-host"
command += f" --code={args.code}"
command += f" --redirect-url={redirect_url}"
command += f" --name={name}"
return command
def enforce_pin_type(pin) -> str:
"""Enforces that the pin is a string of 6 digits.
Converts integers to strings and removes leading/trailing whitespace.
Returns:
str: The pin as a string if it was a string or integer otherwise None.
"""
if isinstance(pin, int):
pin = str(pin)
if isinstance(pin, str):
pin = pin.strip()
else:
pin = None
return pin
def generate_pin(pin_length=6):
"""Generates a random 6-digit PIN.
Args:
pin_length (int): The length of the PIN.
Returns:
str: The generated PIN.
"""
return "".join(random.choice("0123456789") for _ in range(pin_length))
def reconstruct_command(command: str = None):
"""Reconstructs the Chrome Remote Desktop command.
Args:
command (str): The command to connect to the remote machine.
"""
if command is None:
args_to_parse = None # Uses sys.argv
else:
args_to_parse = command.split()
# Parse the command line arguments
parser = create_parser()
args, _ = parser.parse_known_args(args=args_to_parse)
cnc_logger.info("Args:")
cnc_logger.pprint(vars(args))
# Construct the command to connect to the remote machine
command = construct_command(args)
cnc_logger.info(f"Command: {command}")
return command
def connect_to_crd(command=None, pin=None, run=True):
"""Connects to a remote machine via CRD.
Args:
command (str): The command to connect to the remote machine.
pin (str): The PIN to use to connect to the remote machine.
run (bool): Whether to run the command or not.
"""
# Parse the command line arguments
command = reconstruct_command(command)
# Generate a sequence of 6 random integers between 0 and 9
pin = enforce_pin_type(pin)
if pin is None or len(pin) != 6:
cnc_logger.warning("Generating a random PIN...")
pin = generate_pin()
cnc_logger.info(f"Pin: {pin}")
# Add a newline to the pin and then add the pin again for verification
pin_with_newline = pin + "\n"
pin_with_verification = pin_with_newline + pin_with_newline
if run:
# Run the command and capture the output
result = subprocess.run(
command,
input=pin_with_verification,
shell=True,
capture_output=True,
text=True,
)
cnc_logger.debug("Output:\n" + result.stdout)
if result.stderr:
cnc_logger.error("Error:" + result.stderr)
if __name__ == "__main__":
# command = "DISPLAY= /opt/google/chrome-remote-desktop/start-host --code='4/3fQi9BR_wLaDbpFpEM7dHOAMpkWj07OHvWkIg' --redirect-url='https://remotedesktop.google.com/_/oauthredirect' --name=$(hostname)"
# connect_to_crd(command=command, run=False)
connect_to_crd(run=True)