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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
#!/usr/bin/env python3
import socket
import ssl
import time
import argparse
import sys
import os
HEADER_LENGTH = 64
KEY_LENGTH = 64
DISCONNECT_LENGTH = ACK_LENGTH = 3
ACK_MESSAGE = "ACK"
DISCONNECT_MESSAGE = "BYE"
FORMAT = "utf-8"
import configparser
def die(msg):
print(msg)
sys.exit(1)
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--server",type=str,help="IP of MFA Server")
parser.add_argument("--port",type=int,help="Port to connect to")
parser.add_argument("--config",type=str,help="Path to config file",\
default="/etc/mfa/mfa.conf")
parser.add_argument("--key",type=str,help="Client connection key")
parser.add_argument("--plain",action="store_true",help="Connect without TLS")
parser.add_argument("--insecure",action="store_true",\
help="Accept invalid TLS certificates")
return parser.parse_args()
def prompt_user(prompt):
# Prompt user for input and return input
print(prompt)
result = input("> ")
return result
def init_connection_tls(mfa_server, client_port, client_key, insecure=False):
# Attempts to connect to MFA server with provided address,port, and key.
# Repeats attempt once a seconds until timeout is reached.
# Returns socket or None if unable to connect
connection = None
timeout = 0
timeout_length = 5
sleep_length = 1
context = ssl.create_default_context()
if insecure:
context.check_hostname = False
context.verify_mode = 0
while connection == None and timeout < timeout_length:
try:
#connection = socket.create_connection((mfa_server,client_port))
connection = context.wrap_socket(socket.socket(socket.AF_INET),
server_hostname=mfa_server)
connection.connect((mfa_server,int(client_port)))
connection.send(client_key.encode(FORMAT))
response = connection.recv(ACK_LENGTH).decode(FORMAT)
if response == ACK_MESSAGE:
print("connected to mfa server")
elif response == DISCONNECT_MESSAGE:
print("server terminated connection")
sys.exit(1)
except ConnectionError:
time.sleep(sleep_length)
timeout += sleep_length
except ssl.SSLCertVerificationError:
die("error: server presented invalid certificate")
return connection
def init_connection(mfa_server, client_port, client_key):
connection = None
timeout = 0
timeout_length = 5
sleep_length = 1
while connection == None and timeout < timeout_length:
try:
connection = socket.create_connection((mfa_server,client_port))
connection.send(client_key.encode(FORMAT))
response = connection.recv(ACK_LENGTH).decode(FORMAT)
if response == ACK_MESSAGE:
print("connected to mfa server")
elif response == DISCONNECT_MESSAGE:
print("server terminated connection")
sys.exit(1)
except ConnectionError:
time.sleep(sleep_length)
timeout += sleep_length
return connection
def read_config(config_file):
parser = configparser.ConfigParser(inline_comment_prefixes="#")
parser.read(config_file)
return parser
def get_vars(args,confparser):
if not os.path.exists(args.config):
print("Unable to open config file")
sys.exit(1)
server = None
port = None
key = None
plain = None
insecure = None
# Set values from config file first
if confparser.has_section("client"):
server = confparser.get("client","server",fallback=None)
port = confparser.get("client","port",fallback=None)
key = confparser.get("client","key",fallback=None)
plain = confparser.get("client","plain",fallback=False)
insecure = confparser.get("client","insecure",fallback=False)
if plain.lower() == "false":
plain = False
if insecure.lower() == "false":
insecure = False
# Let command line args overwrite any values
if args.server != None:
server = args.server
if args.port != None:
port = args.port
if args.key != None:
key = args.key
if args.plain:
plain = args.plain
if args.insecure:
insecure = args.insecure
# Exit if any value is null
if None in [server,port,key]:
print("error: one or more items unspecified")
sys.exit(1)
return server,port,key,plain,insecure
def main():
# Get arguments, exit if unable to connect
args = parse_arguments()
confparser = read_config(args.config)
mfa_server,client_port,client_key,plain,insecure = get_vars(args,confparser)
# Exit if invalid key is provided
if len(client_key) != KEY_LENGTH:
print("invalid key")
sys.exit(1)
# Open connection to server
if plain:
conn = init_connection(mfa_server,client_port,client_key)
else:
conn = init_connection_tls(mfa_server,client_port,client_key,insecure)
if conn == None:
print("timed out attempting to connect to server")
sys.exit(1)
# Main loop
running = True
while running:
# Receive MFA prompt from server
header = conn.recv(HEADER_LENGTH).decode(FORMAT)
if header == "":
die("error: lost connection to server")
prompt_len = int(header)
prompt = conn.recv(prompt_len).decode(FORMAT)
# Ask user for response
answer = prompt_user(prompt)
# Send answer to MFA server
answer_length = len(answer)
length_msg = str(answer_length)
length_msg += ' ' * (HEADER_LENGTH - len(length_msg))
conn.send(length_msg.encode(FORMAT))
conn.send(answer.encode(FORMAT))
if __name__ == '__main__':
main()
|