-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
268 lines (229 loc) · 8.83 KB
/
dev.py
File metadata and controls
268 lines (229 loc) · 8.83 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import subprocess
import sys
import os
import socket
from sys import exit
#script for making distributable
#build project as folder
#pyinstaller --add-data 'cmd:.' dev.py
#executable located at /dist/dev/dev
#
#or
#build project as single file
#pyinstaller -F dev.py
#cp cmd ./dist/
#executable located at /dist/dev
#routines
#check for internet connection
#testing on google servers
#return true if connected
def is_connected():
try:
# connect to the host -- tells us if the host is actually
# reachable
socket.create_connection(("www.google.com", 80))
return True
except OSError:
pass
return False
#add YubiKey to System
def adding_key(pathToKeys):
#Ensure YubiKey
userResponse=''
print("This module installation process requires present Yubikey")
print("Please Insert Yubikey and Continue")
while(userResponse=='' or (userResponse[0] not in ['y','Y','n','N'])):
userResponse=input("Is yubikey present? [Y]es/[N]o\n")
if userResponse[0]in ['y', 'Y']:
print('Please touch the metal plate on YubiKey to continue')
os.system('pamu2fcfg > '+pathToKeys)
print('Key saved at', pathToKeys)
elif userResponse[0] in ['n', 'N']:
print('Exit Installation')
exit()
else:
print("Input Invalid")
#add backup YubiKey to System
def adding_backup(pathToKeys):
userResponse=''
print("Please Insert Yubikey and Continue")
while(userResponse=='' or (userResponse[0] not in ['y','Y','n','N'])):
userResponse=input("Is your backup yubikey present? [Y]es/[N]o\n")
if userResponse[0]in ['y', 'Y']:
print('Please touch the metal plate on YubiKey to continue')
os.system('pamu2fcfg -n >> '+pathToKeys)
print('Key added at', pathToKeys)
elif userResponse[0] in ['n', 'N']:
print('Exit Adding Backup')
return
else:
print("Input Invalid")
#uninstall package and erase trace when installation failed
#NOTE: At this point, privileges of folder /etc/pam.d and its content are modified
def testingUninstaller(pathToBackup, pathToYubico):
pathToSudo=os.path.join(pathToBackup,'sudo')
subprocess.run(['rm', '/etc/pam.d/sudo'])
subprocess.run(['cp', pathToSudo, '/etc/pam.d'])
subprocess.run(['rm', '-r',pathToYubico])
subprocess.run(['sudo','-k'])
subprocess.run(['sudo','chown', 'root:root', '/etc/pam.d/sudo'])
subprocess.run(['sudo','chmod', '755', '/etc/pam.d'])
subprocess.run(['sudo','chmod', '644', '/etc/pam.d/sudo'])
os.system('sudo apt-get purge libpam-u2f')
#uninstall package from a system with fully installed YubiKey U2F authentication.
def Uninstaller(pathToBackup, pathToYubico, filename):
pathToFile=os.path.join(pathToBackup,filename)
originFile=os.path.join('/etc/pam.d/',filename)
subprocess.run(['sudo','chmod', '777', '/etc/pam.d'])
subprocess.run(['sudo','chmod', '777', '/etc/pam.d/sudo'])
subprocess.run(['sudo','rm', originFile])
subprocess.run(['cp', pathToFile, '/etc/pam.d'])
testingUninstaller(pathToBackup, pathToYubico)
subprocess.run(['sudo','chown', 'root:root', originFile])
subprocess.run(['sudo','chmod', '644', originFile])
print("Uninstallation Finished")
print("Cleaning Up Finished")
#determining the version of system automatically
def autoVersionChecking():
#for autochecking
fd=open('/etc/issue','r')
lines=fd.readlines()
return float(lines[0][7:12])>17.05
#determining the version of system based on user's manual input
def manualVersionInput():
userResponse=''
while(userResponse=='' or (userResponse[0] not in ['y','Y','n','N'])):
userResponse=input("Is your Ubuntu System Version 17.10 or newer? [Y]es/[N]o\n")
if userResponse[0]in ['y', 'Y']:
return True
elif userResponse[0]in ['n', 'N']:
return False
else:
print("Input invalid")
#write line to file
def fileIO(pathToFile):
with open(pathToFile,'r+') as fd:
lineread=''
while(not lineread[:20] =='@include common-auth'):
lineread=fd.readline()
pos=fd.tell()
content=fd.read()
fd.seek(pos)
fd.write(lineToInsert)
fd.write(content)
#global variable
commandFileName='cmd'
backupFolder='bkup'
isNewVersion=True
#Resolve path
pathToFile=sys.argv[0]
pathToFolder=os.path.dirname(pathToFile)
pathToFolder=os.path.join('.',pathToFolder)
#pathToKeys
home=os.path.expanduser('~')
pathToYubico=os.path.join(home,'.config/Yubico')
subprocess.run(['mkdir',pathToYubico])
pathToKeys=os.path.join(pathToYubico,'u2f_keys')
#pathToBackup
pathToBackup=os.path.join(pathToFolder,backupFolder)
if not os.path.exists(pathToBackup):
os.mkdir(pathToBackup)
#pathToCmd
pathToCmd=os.path.join(pathToFolder,commandFileName)
#lineToInset
lineToInsert='auth required pam_u2f.so\n'
filePath='/etc/pam.d/gdm-password'
fileName='gdm-password'
#Step 1 gathering system information
#Checking Version of Ubuntu
userResponse=''
print("It is important to have correct version of Ubuntu")
print("For non-Ubuntu user, please check https://support.yubico.com/support/solutions/articles/15000011356-ubuntu-linux-login-guide-u2f and select version at discretion.\n")
print("You can manually select version or let this tool auto-detect")
while(userResponse=='' or (userResponse[0] not in ['y','Y','n','N'])):
userResponse=input("Do you want to use autocheck? [Y]es/[N]o\n")
if userResponse[0] in ['y', 'Y']:
isNewVersion=autoVersionChecking()
elif userResponse[0] in ['n', 'N']:
#manually input ubuntu version
isNewVersion=manualVersionInput()
else:
print("Input Invalid")
print("Using newer version: ",isNewVersion)
if(not isNewVersion):
filePath='/etc/pam.d/lightdm'
fileName='lightdm'
#Step 2: ask user what to do
userResponse=''
while(userResponse=='' or (userResponse[0] not in ['I','i','U','u','t','T'])):
userResponse=input("What Do you want to do? [I]nstall/[U]ninstall/[T]esting uninstall\n")
if userResponse[0] in ['U','u']:
print('Uninstalling Yubikey')
#step 3 for uninstalation process
Uninstaller(pathToBackup, pathToYubico, fileName)
exit()
elif userResponse[0]in ['t','T']:
#step 3 for uninstallation process after a failed installation
print('Undoing Changes Made for Testing')
testingUninstaller(pathToBackup, pathToYubico)
print('Done')
exit()
elif userResponse[0]in ['i', 'I']:
print('Installing')
else:
print('Invalid Input')
#Step 3 for installation
#checking internet connection
if not is_connected():
print("No network present!")
exit()
subprocess.run(['chmod', '+x', pathToCmd])
print("Executing Commands")
subprocess.run(['sudo',pathToCmd])
#install necessary file and modules
print("Setting Up Modules")
adding_key(pathToKeys)
userResponse=''
while(userResponse=='' or (userResponse[0] not in ['y','Y','n','N'])):
userResponse=input("Do you want to add extra backup keys? [Y]es/[N]o\n")
if userResponse[0]in ['y', 'Y']:
print('Adding Backup')
adding_backup(pathToKeys)
userResponse=''
elif userResponse[0]in ['n', 'N']:
print('')
#backup files
print("Backing Up Important System Configuration Files")
#temporarily unlock permission for folder etc/pam.d
subprocess.run(['sudo','chmod', '777', '/etc/pam.d'])
subprocess.run(['sudo','chmod', '777', '/etc/pam.d/sudo'])
#copy all necessary file to bkup
subprocess.run(['cp','/etc/pam.d/sudo', pathToBackup])
subprocess.run(['cp','/etc/pam.d/sudo', pathToBackup])
#Testing
print('Testing configuration with sudo')
###Running testing code and ask for input
fileIO('/etc/pam.d/sudo')
print('If testing failed, please run [T]esting Uninstaller to revert back to normal working system.')
print('Authenticate on YubiKey after typing in admin password')
subprocess.run(['sudo','-k'])
subprocess.run(['sudo','echo','test'])
###Finalizing Changes
userResponse=''
while(userResponse=='' or (userResponse[0] not in ['y','Y','n','N'])):
userResponse=input("Does YubiKey work successfully? [Y]es/[N]o\n")
if userResponse[0]in ['y', 'Y']:
print('Finalizing Yubikey Installation')
subprocess.run(['sudo','chmod', '777', filePath])
subprocess.run(['cp',filePath, pathToBackup])
fileIO(filePath)
#changing permission
subprocess.run(['sudo','chmod', '755', '/etc/pam.d'])
subprocess.run(['sudo','chmod', '644', '/etc/pam.d/sudo'])
subprocess.run(['sudo','chmod', '644', filePath])
elif userResponse[0]in ['n', 'N']:
testingUninstaller(pathToBackup, pathToYubico)
print("Installation Failed")
print("Cleaning Up Finished")
else:
print("Input Invalid")