-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSohoMiniServer.py
More file actions
1353 lines (1205 loc) · 53.4 KB
/
Copy pathSohoMiniServer.py
File metadata and controls
1353 lines (1205 loc) · 53.4 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import os
import sys
import subprocess
import time
import platform
import ipaddress
from pylxd import Client
fw_ip = "1"
ad1_ip = "2"
ad2_ip = "3"
fs_ip = "4"
vpn_ip = "5"
fw_name = "fw"
ad1_name = "ad1"
ad2_name = "ad2"
fs_name = "fs"
vpn_name = "vpn"
def wait(t):
for i in range(t,-1,-1):
print ("\033[A \033[A")
print(i)
time.sleep(1)
print ("\033[A \033[A")
def get_domain_name():
global domain
global realm
control = 0
while control == 0:
realm = input("Digite el nombre de dominio: \n")
domain = realm.split(".")
if (len(domain)) > 3:
print("El dombre de dominio debe ser: ejemplo.com")
print("o : subdominio.ejemplo.com")
elif (len(domain)) < 2:
print("El dombre de dominio debe ser: ejemplo.com")
print("o : subdominio.ejemplo.com")
elif "" in domain:
print("El dombre de dominio debe ser: ejemplo.com")
print("o : subdominio.ejemplo.com")
else:
#verificar si meta dominio tiene numeros
#if len(domain) == 2 and has domain:
if len(domain[0]) > 15:
print("El nombre ", domain[0], "tiene", len(domain[0]), "caracteres, maximo 15")
else:
#print("Ok, el nombre de dominio sera: ", domain[0])
#print("El nombre de realm (ambito) sera: ", realm)
control = 1
def get_lan_addr():
global lan_addr
global lan_netmask
global lan_prefix
global lan_total_addr
global lan_bcast
control = 0
while control == 0:
try:
lan = input("Digite la direccion de la red LAN en formato CIRD (max prefijo de red /26), p.e 10.0.0.0/16: ")
ipaddress.ip_network(lan)
if ipaddress.ip_network(lan).prefixlen > 26:
print("Formato incorrecto o prefijo de red LAN incorrecto ")
print("La direccion de la red LAN debe estar en formato CIRD, p.e 10.0.0.0/16 ")
print("El prefijo de red es: ", ipaddress.ip_network(lan).prefixlen, " el prefijo de red maximo es de 26 ")
else:
control = 1
except ValueError:
print("Direccion de red IP no valida ")
lan_addr = ipaddress.ip_network(lan).network_address
lan_netmask = ipaddress.ip_network(lan).netmask
lan_prefix = ipaddress.ip_network(lan).prefixlen
lan_total_addr = ipaddress.ip_network(lan).num_addresses
lan_bcast = ipaddress.ip_network(lan).broadcast_address
def get_wan_addr():
global wan_ip
global wan_mask
global wan_gw
type_wan_ip = input("Se usara una direccion WAN asignada de forma automatica (dhcp)? (s/n)")
if type_wan_ip == "n":
control = 0
while control == 0:
try:
wan_ip = input("Digite la direccion ip WAN: ")
ipaddress.ip_address(wan_ip)
print("Ip valida, y su tipo es: ", type(wan_ip))
control = 1
except ValueError:
print("IP no valida")
control = 0
while control == 0:
wan_mask = input("Digite la mascara de red:" )
try:
wan_mask = int(wan_mask)
print("Es un numero")
if wan_mask < 8 or wan_mask > 30:
print("La mascara debe ser un numero entero entre 8 y 30")
else:
ip_eval = str(wan_ip) + "/" + str(wan_mask)
try:
ipaddress.ip_network(ip_eval)
print("No es una direccion ip, es una direccion de red ip")
except ValueError:
print("Direccion ip WAN valida")
control = 1
except ValueError:
print("No numero La mascara debe ser un numero entero entre 8 y 30")
control = 0
while control == 0:
try:
wan_gw = input("Digite la direccion ip de la puerta de enlace (gateway): ")
ipaddress.ip_address(wan_gw)
print("Ip valida, y su tipo es: ", type(wan_gw))
ip_eval = str(wan_gw) + "/" + str(wan_mask)
try:
ipaddress.ip_network(ip_eval)
print("La direccion gateway digitada es una direccion de red ip")
except ValueError:
print("Direccion Gateway WAN valida")
control = 1
except ValueError:
print("IP no valida")
else:
wan_ip = "dhcp"
def get_nics():
global wan_nic
global lan_nic
ifaces = os.listdir('/sys/class/net/')
print("Seleccione las interfaces de red WAN y LAN")
print("Interfaces de red disponibles: ")
print(*ifaces, sep="\t")
control = 0
while control == 0:
wan_nic = input("Digite interfaz de red WAN: ")
if wan_nic not in ifaces:
print("Interfaz de red incorrecta")
else:
print("Ok, la WAN usara la interfaz de red: ", wan_nic)
control = 1
control = 0
while control == 0:
lan_nic = input("Digite interfaz de red LAN: ")
if (lan_nic not in ifaces) or (lan_nic == wan_nic):
print("Interfaz de red inexistente o igual a la WAN")
else:
print("Ok, la LAN usara la interfaz de red: ", lan_nic)
control = 1
def summary_report():
print("==" * 100 )
print("Informacion de Dominio: \n")
print("-----> Nombre de dominio sera: ", domain[0])
print("-----> Realm (ambito) sera: ", realm, "\n")
print("Informacion de direccionamiento LAN: \n")
print("-----> Direccion LAN: ", lan_addr)
print("-----> Mascara de red: ", lan_netmask, "prefijo", lan_prefix)
print("-----> Total direcciones para esta red: ", lan_total_addr)
print("-----> Direccion de bcast: ", lan_bcast, "\n")
print("Informacion de direccionamiento WAN: \n")
if wan_ip == "dhcp":
print("EL direccionamiento WAN sera tomado usando DHCP: ", "\n")
else:
print("-----> Direccion WAN: ", wan_ip)
print("-----> Mascara de red para la direccion WAN: ", wan_mask)
print("-----> Gateway o puerta de enlace: ", wan_gw, "\n")
print("Informacion de interfaces de red: \n")
print("-----> La interfaz WAN sera: ", wan_nic)
print("-----> La interfaz LAN sera: ", lan_nic)
def set_arch():
global tmin
global tmed
global tmax
global rb_hw
rb_hw = False
rb_pi3 = "Raspberry Pi 3"
rb_pi4 = "Raspberry Pi 4"
if platform.machine() == "x86_64":
tmin = 5
tmed = 15
tmax = 30
if platform.machine() == "armv7l":
try:
rb = subprocess.check_output(["cat", "/sys/firmware/devicetree/base/model"])
rb = str(rb)
except subprocess.CalledProcessError as e:
print("No Raspberry Pi")
rb_hw = False
if rb.find(rb_pi4) > 0:
print("******************** Detectado Raspberry Pi 4 ************************* \n")
rb_hw = True
tmin = 5
tmed = 10
tmax = 15
subprocess.run(["apt", "install", "hostapd", "-y"])
hostap_default()
hostap_conf()
if rb.find(rb_pi3) > 0:
print("******************** Detectado Raspberry Pi 3 ************************* \n")
rb_hw = True
tmin = 15
tmed = 30
tmax = 45
subprocess.run(["apt", "install", "hostapd", "-y"])
hostap_default()
hostap_conf()
def provision():
client = Client()
plantillas = ['fw', 'samba', 'vpn']
for i in plantillas:
if platform.machine() == "x86_64":
config = {'name': i, 'source': {'type': 'image', "mode": "pull", "server":
"https://cloud-images.ubuntu.com/daily", "protocol": "simplestreams", 'alias': 'focal/amd64'},
'profiles': ['default'] }
if platform.machine() == "armv7l":
config = {'name': i, 'source': {'type': 'image', "mode": "pull", "server":
"https://cloud-images.ubuntu.com/daily", "protocol": "simplestreams", 'alias': 'focal/armhf'},
'profiles': ['default'] }
print("Creando maquina: ", i)
a = client.containers.create(config, wait=True)
print("Encendiendo maquina: ", i + "\n")
a.start()
a = client.containers.all()
l = len(a)
print("Numero de maquinas a provisionar: ",l)
print("Esperando ",tmax , " segundos que levanten la configuracion inicial de red: \n")
wait(tmax)
for x in range(len(a)):
print("Provisionando plnatilla de contenedor", a[x].name)
if a[x].name == "fw":
print("Buscando actualizaciones sobre: ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'update'])
print(so)
print("Aplicando actualizaciones sobre: ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'upgrade', '-y'])
print(so)
print("Instalando paquetes sobre: ", a[x].name)
sw = ['acl', 'attr', 'samba', 'samba-dsdb-modules', 'samba-vfs-modules', 'winbind', 'libpam-winbind', 'libnss-winbind', 'libpam-krb5', 'krb5-config', 'krb5-user', 'smbclient', 'isc-dhcp-server']
for i in sw:
print("Instalando: ", i, " sobre ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'install', i , '-yq'], environment={'DEBIAN_FRONTEND': 'noninteractive'})
if e == 0:
print("Instalacion OK")
else:
print("La instalacion fallo, revise su conexion a internet, ejecute CleanUP.py y repita el proceso")
sys.exit()
subprocess.run(['lxc', 'exec', 'fw', '--', 'bash', '-c', 'apt-get install freeradius -y'])
a[x].stop()
time.sleep(5)
subprocess.run(["lxc", "publish", a[x].name, "--alias", a[x].name])
print("Imagen de ", a[x].name + " se genero")
print("Borrando contenedor: ", a[x].name, "\n")
a[x].delete()
wait(tmed)
if a[x].name == "samba":
print("Buscando actualizaciones sobre: ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'update'])
print(so)
print("Aplicando actualizaciones sobre: ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'upgrade', '-y'])
print(so)
print("Instalando paquetes sobre: ", a[x].name)
sw = ['acl', 'attr', 'samba', 'samba-dsdb-modules', 'samba-vfs-modules', 'winbind', 'libpam-winbind', 'libnss-winbind', 'libpam-krb5', 'krb5-config', 'krb5-user', 'smbclient']
for i in sw:
print("Instalando: ", i, " sobre ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'install', i , '-yq'], environment={'DEBIAN_FRONTEND': 'noninteractive'})
if e == 0:
print("Instalacion OK")
else:
print("La instalacion fallo, revise su conexion a internet, ejecute CleanUP.py y repita el proceso")
sys.exit()
print("Generando imagen de: ",a[x].name)
a[x].stop()
time.sleep(5)
subprocess.run(["lxc", "publish", a[x].name, "--alias", a[x].name])
print("Imagen de ", a[x].name + " se genero")
print("Borrando contenedor: ", a[x].name, "\n")
a[x].delete()
wait(tmed)
if a[x].name == "vpn":
print("Buscando actualizaciones sobre: ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'update'])
print(so)
print("Aplicando actualizaciones sobre: ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'upgrade', '-y'])
print(so)
print("Instalando paquetes sobre: ", a[x].name)
sw = ['build-essential']
for i in sw:
print("Instalando: ", i, " sobre ", a[x].name)
e, so, se = a[x].execute(['apt-get', 'install', i , '-yq'], environment={'DEBIAN_FRONTEND': 'noninteractive'})
if e == 0:
print("Instalacion OK")
else:
print("La instalacion fallo, revise su conexion a internet, ejecute CleanUP.py y repita el proceso")
sys.exit()
print("Descargando VPN Server.....: \n")
if platform.machine() == "x86_64":
subprocess.run(['lxc', 'exec', 'vpn', '--', 'bash', '-c', 'wget https://www.softether-download.com/files/softether/v4.34-9745-rtm-2020.04.05-tree/Linux/SoftEther_VPN_Server/64bit_-_Intel_x64_or_AMD64/softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-x64-64bit.tar.gz -O /usr/local/softether-vpnserver.tar.gz'])
if platform.machine() == "armv7l":
subprocess.run(['lxc', 'exec', 'vpn', '--', 'bash', '-c', 'wget https://www.softether-download.com/files/softether/v4.34-9745-rtm-2020.04.05-tree/Linux/SoftEther_VPN_Server/32bit_-_ARM_EABI/softether-vpnserver-v4.34-9745-rtm-2020.04.05-linux-arm_eabi-32bit.tar.gz -O /usr/local/softether-vpnserver.tar.gz'])
print("Generando imagen de: ",a[x].name)
a[x].stop()
time.sleep(5)
subprocess.run(["lxc", "publish", a[x].name, "--alias", a[x].name])
print("Imagen de ", a[x].name + " se genero")
print("Borrando contenedor: ", a[x].name, "\n")
a[x].delete()
wait(tmed)
def netplan_raspberry():
lanbcast = str(lan_bcast)
tmp = lanbcast.split(".")
lanaddr = str(lan_addr)
tmp2 = lanaddr.split(".")
last_lan_ip = int(tmp[3]) - 1
filename = "50-cloud-init.yaml"
l1 = "network:"
l2 = " version: 2"
l3 = " ethernets:"
l4 = " " + lan_nic + ":"
l5 = " dhcp4: no"
#l6 = " match:"
#l7 = " macaddress: " + mac0
#l8 = " set-name: eth0"
l9 = " bridges:"
l10 = " LAN:"
l11 = " addresses: " + "[" + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + str(last_lan_ip) + "/" + str(lan_prefix) + "]"
l12 = " gateway4: " + tmp2[0] + "." + tmp2[1] + "." + tmp2[2] + "." + fw_ip
l13 = " nameservers:"
l14 = " search: " + "[" + realm + "]"
l15 = " addresses: " + "[" + tmp2[0] + "." + tmp2[1] + "." + tmp2[2] + "." + ad1_ip + "," + tmp2[0] + "." + tmp2[1] + "." + tmp2[2] + "." + ad2_ip + "]"
l16 = " interfaces:"
l17 = " - " + lan_nic
l18 = " ethernets:"
l19 = " " + wan_nic + ":"
l20 = " dhcp4: no"
#l21 = " match:"
#l22 = " macaddress: " + mac1
#l23 = " set-name: eth1"
l24 = " bridges:"
l25 = " WAN:"
l26 = " interfaces:"
l27 = " - " + wan_nic
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
#out.write(l6 + '\n')
#out.write(l7 + '\n')
#out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.write(l15 + '\n')
out.write(l16 + '\n')
out.write(l17 + '\n')
out.write(l18 + '\n')
out.write(l19 + '\n')
out.write(l20 + '\n')
#out.write(l21 + '\n')
#out.write(l22 + '\n')
#out.write(l23 + '\n')
out.write(l24 + '\n')
out.write(l25 + '\n')
out.write(l26 + '\n')
out.write(l27 + '\n')
out.close()
subprocess.run(["rm", "/etc/netplan/50-cloud-init.yaml"])
subprocess.run(["cp", "50-cloud-init.yaml", "/etc/netplan/50-cloud-init.yaml"])
subprocess.run(["netplan", "apply"])
def profiles():
client = Client()
profile = client.profiles.create("WAN", config = {}, devices={"eth0": {"name": "eth0", "nictype":
"bridged", "parent": "LAN", "type": "nic"}, "eth1": {"name": "eth1", "nictype": "bridged",
"parent": "WAN", "type": "nic"}, "root": {"path": "/", "pool": "default", "type": "disk"}})
profile = client.profiles.create("AD", config = {"security.privileged": "True"}, devices={"eth0": {"name": "eth0", "nictype":
"bridged", "parent": "LAN", "type": "nic"}, "root": {"path": "/", "pool": "default", "type": "disk"}})
profile = client.profiles.create("LAN", config = {}, devices={"eth0": {"name": "eth0", "nictype":
"bridged", "parent": "LAN", "type": "nic"}, "root": {"path": "/", "pool": "default", "type": "disk"}})
def resolv(machine_name):
lanaddr = str(lan_addr)
tmp = lanaddr.split(".")
filename = "files" + "/" + realm + "-" + machine_name + "-" + "resolv.conf"
l1 = "search " + realm
if machine_name == "ad1":
l2 = "nameserver " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad1_ip
elif machine_name == "ad2":
l2 = "nameserver " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad2_ip
else:
l2 = "nameserver " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad1_ip
l3 = "nameserver " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad2_ip
if machine_name == "ad1" or machine_name == "ad2":
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.close()
if machine_name == "fw" or machine_name == "fs" or machine_name == "vpn":
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.close()
def hosts(machine_ip, machine_name):
lanaddr = str(lan_addr)
tmp = lanaddr.split(".")
filename = "files" + "/" + realm + "-" + machine_name + "-" + "hosts"
l1 = "127.0.0.1 localhost"
l2 = tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + machine_ip + " " + machine_name + "." + realm + " " + machine_name
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.close()
def netplan(machine_ip, machine_name):
lanaddr = str(lan_addr)
tmp = lanaddr.split(".")
filename = "files" + "/" + realm + "-" + machine_name + "-"+ "50-cloud-init.yaml"
l1 = "network:"
l2 = " version: 2"
l3 = " ethernets:"
l4 = " " + "eth0" + ":"
l5 = " addresses:"
l6 = " " + "-" + " " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + machine_ip + "/" + str(lan_prefix)
l7 = " gateway4:" + " " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + fw_ip
l8 = " nameservers:"
l9 = " search:" + " " + "[" + realm + "]"
l10 = " addresses:" + " " + "[" + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad1_ip + "," + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad2_ip + "]"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.close()
def netplanfw(machine_ip, machine_name):
lanaddr = str(lan_addr)
tmp = lanaddr.split(".")
filename = "files" + "/" + realm + "-" + machine_name + "-"+ "50-cloud-init.yaml"
l1 = "network:"
l2 = " version: 2"
l3 = " ethernets:"
l4 = " " + "eth0" + ":"
l5 = " addresses:"
l6 = " " + "-" + " " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + machine_ip + "/" + str(lan_prefix)
l7 = " nameservers:"
l8 = " search:" + " " + "[" + realm + "]"
l9 = " addresses:" + " " + "[" + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad1_ip + "," + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad2_ip + "]"
l10 = " ethernets:"
l11 = " " + "eth1" + ":"
if wan_ip != "dhcp":
l12 = " addresses:"
l13 = " " + "-" + " " + str(wan_ip) + "/" + str(lan_prefix)
l14 = " gateway4:" + " " + str(wan_gw)
if wan_ip == "dhcp":
l12 = " dhcp4: true"
l13 = " dhcp4-overrides:"
l14 = " use-dns: false"
if wan_ip != "dhcp":
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.close()
if wan_ip == "dhcp":
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.close()
def samba_inst_ad1(machine_name):
filename = "files" + "/" + realm + "-" + machine_name + "-"+ "samba.sh"
r = realm.split(".")
l1 = "#!/bin/bash"
l2 = "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
l3 = "systemctl stop smbd nmbd winbind systemd-resolved"
l4 = "killall nmbd winbindd smbd"
l5 = "rm /etc/krb5.conf"
l6 = "rm /etc/samba/smb.conf"
l7 = "echo \"nameserver 1.1.1.1\" > /etc/resolv.conf"
l8 = "samba-tool domain provision --server-role=dc --use-rfc2307 --dns-backend=SAMBA_INTERNAL --realm=" + realm + " " + "--domain=" + r[0] + " " + "--adminpass=Abcd1234"
l9 = "cp /var/lib/samba/private/krb5.conf /etc/"
l10 = "systemctl stop smbd nmbd winbind systemd-resolved"
l11 = "systemctl disable smbd nmbd winbind systemd-resolved"
l12 = "systemctl unmask samba-ad-dc"
l13 = "rm /etc/resolv.conf"
l14 = "systemctl enable samba-ad-dc"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.close()
def samba_inst_ad2(machine_name):
filename = "files" + "/" + realm + "-" + machine_name + "-"+ "samba.sh"
r = realm.split(".")
l1 = "#!/bin/bash"
l2 = "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
l3 = "systemctl stop smbd nmbd winbind systemd-resolved"
l4 = "killall nmbd winbindd smbd"
l5 = "rm /etc/samba/smb.conf"
l6 = "samba-tool domain join" + " " + realm + " " + "DC -U\"" + r[0] + "\\administrator\" --password=Abcd1234"
l7 = "systemctl stop smbd nmbd winbind systemd-resolved"
l8 = "systemctl disable smbd nmbd winbind systemd-resolved"
l9 = "systemctl unmask samba-ad-dc"
l10 = "systemctl enable samba-ad-dc"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.close()
def samba_ws_file():
filename = "files" + "/" + realm + "-" + "sambawsfile"
tmp = realm.upper()
r = tmp.split(".")
l1 = "security = ADS"
l2 = "workgroup =" + " " + r[0]
l3 = "realm =" + " " + realm.upper()
l4 = ""
l5 = "log file = /var/log/samba/%m.log"
l6 = "log level = 1"
l7 = ""
l8 = "idmap config * : backend = tdb"
l9 = "idmap config * : range = 3000-7999"
l10 = ""
l11 = "idmap config" + " " + r[0] + " " + ": backend = rid"
l12 = "idmap config" + " " + r[0] + " " + ": range = 10000-999999"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.close()
def samba_fs_setup():
filename = "files" + "/" + realm + "-" + "samba_fs_setup"
r = (realm.upper()).split(".")
U = "Users"
l1 = "echo \"Abcd1234\" | net rpc rights grant \"" + r[0] + "\Domain Admins\" SeDiskOperatorPrivilege -U \"" + r[0] + "\\administrator\""
l2 = "mkdir /Users"
l3 = "sleep 15"
l4 = "chown " + r[0] + "\\\\" + "administrator" + ":" + r[0] + "\\\\" + "domain\ users" + " /Users"
l5 = "chmod 2750 /Users"
l6 = "echo \"vfs objects = acl_xattr\" >> /etc/samba/smb.conf"
l7 = "echo \"map acl inherit = yes\" >> /etc/samba/smb.conf"
l8 = "echo [Users] >> /etc/samba/smb.conf"
l9 = "echo \"path = /Users\" >> /etc/samba/smb.conf"
l10 = "echo \"read only = no\" >> /etc/samba/smb.conf"
l11 = "echo \"force create mode = 0600\" >> /etc/samba/smb.conf"
l12 = "echo \"force directory mode = 0700\" >> /etc/samba/smb.conf"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.close()
def krb5():
filename = "files" + "/" + realm + "-" + "krb5"
r = realm.upper()
l1 = "[" + "libdefaults" + "]"
l2 = " dns_lookup_realm = false"
l3 = " dns_lookup_kdc = true"
l4 = " default_realm = " + r
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.close()
def nsswitch():
filename = "files" + "/" + realm + "-" + "nsswitch"
l1 = "passwd: compat winbind"
l2 = "group: compat winbind"
l3 = "shadow: compat"
l4 = "gshadow: files"
l5 = "hosts: files dns"
l6 = "networks: files"
l7 = "protocols: db files"
l8 = "services: db files"
l9 = "ethers: db files"
l10 = "rpc: db files"
l11 = "netgroup: nis"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.close()
def dhcpd(machine_name):
filename = "files" + "/" + realm + "-" + "dhcpd"
l = list(ipaddress.ip_network(str(lan_addr) + "/" + str(lan_prefix)).hosts())
real_lan_total_addr = lan_total_addr - 3
if lan_prefix >= 16:
total_addr_range = int((real_lan_total_addr * 70) / 100)
bottom_addr = real_lan_total_addr - total_addr_range
top_addr_lan = real_lan_total_addr
text1 = " ---- Total direcciones a entregar por el dhcp server (cerca de el 70% del total): "
else:
total_addr_range = 60000
bottom_addr = real_lan_total_addr - total_addr_range
top_addr_lan = real_lan_total_addr
print(" ---- La direccion de la red es: ", lan_addr)
print(" ---- La mascara de red es: ", lan_netmask)
print(" ---- El prefijo de red es: ", lan_prefix)
print(" ---- El numero de hosts usable para esta red es: ", lan_total_addr)
print(" ---- La direccion de bcast es: ", lan_bcast)
print(" ---- Direccion de bcast en string", str(lan_bcast))
print("Rango de direcciones para dhcp server: ")
print(" ---- Total direcciones a entregar por el dhcp server: ", total_addr_range)
print(" ---- Primera IP del rango es: ", l[bottom_addr])
last_ip = str(lan_bcast).split(".")
print(" ---- Ultima IP del rango es: ", last_ip[0] + "." + last_ip[1] + "." + last_ip[2] + "." + str(int(last_ip[3]) - 3))
tmp = str(lan_addr)
tmp2 = str(lan_netmask)
l1 = "INTERFACES=\"eth0\";"
l2 = "default-lease-time 600;"
l3 = "max-lease-time 7200;"
l4 = ""
l5 = "subnet " + tmp + " " + "netmask " + tmp2 + "{"
l6 = " range " + str(l[bottom_addr]) + " " + last_ip[0] + "." + last_ip[1] + "." + last_ip[2] + "." + str(int(last_ip[3]) - 3) + ";"
tmp = str(lan_addr).split(".")
l7 = " option routers " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + fw_ip + ";"
l8 = " option domain-name-servers " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad1_ip + ", " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + ad2_ip + ";"
l9 = " option domain-name \"" + realm + "\"" + ";"
l10 = "}"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.close()
def radius_ntlm_auth():
filename = "files" + "/" + realm + "-" + "radius_ntlm_auth"
r = (realm.upper()).split(".")
l1 = "exec ntlm_auth {"
l2 = " wait = yes"
l3 = " program = \"/usr/bin/ntlm_auth --request-nt-key --domain=" + r[0] + " --username=%{mschap:User-Name} --password=%{User-Password}\""
l4 = "}"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.close()
def radius_mschap():
filename = "files" + "/" + realm + "-" + "radius_mschap"
r = (realm.upper()).split(".")
l1 = "mschap {"
l2 = " ntlm_auth = \"/usr/bin/ntlm_auth --request-nt-key --username=%{mschap:User-Name:-None} --domain=%{%{mschap:NT-Domain}:-" + r[0] + "} --challenge=%{mschap:Challenge:-00} --nt-response=%{mschap:NT-Response:-00}\""
l3 = " pool {"
l4 = " start = ${thread[pool].start_servers}"
l5 = " min = ${thread[pool].min_spare_servers}"
l6 = " max = ${thread[pool].max_servers}"
l7 = " spare = ${thread[pool].max_spare_servers}"
l8 = " uses = 0"
l9 = " retry_delay = 30"
l10 = " lifetime = 86400"
l11 = " cleanup_interval = 300"
l12 = " idle_timeout = 600"
l13 = " }"
l14 = " passchange {"
l15 = " }"
l16 = "}"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.write(l15 + '\n')
out.write(l16 + '\n')
out.close()
def radius_client():
filename = "files" + "/" + realm + "-" + "radius_client"
l1 = "client LAN {"
l2 = " ipaddr = " + str(lan_addr) + "/" + str(lan_prefix)
l3 = " secret = qwertyui"
l4 = "}"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.close()
def fw_rules():
filename = "files" + "/" + realm + "-" + "fw_rules"
lanaddr = str(lan_addr)
tmp = lanaddr.split(".")
l1 = "#!/bin/bash"
l2 = "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
l3 = "iptables -t nat -A PREROUTING -p tcp -m tcp -i eth1 --dport 443 -j DNAT --to-destination " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + vpn_ip
l4 = "iptables -t nat -A PREROUTING -p udp -m udp -m multiport -i eth1 --dports 500,1701,4500 -j DNAT --to-destination " + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + vpn_ip
l5 = "iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE"
#bloquear todo trafico entrante / block all incoming traffic
l6 = "iptables -P INPUT DROP"
l7 = "iptables -P FORWARD DROP"
l8 = "iptables -P OUTPUT ACCEPT"
#permitir todo sobre localhost / allow all on localhost
l9 = "iptables -A INPUT -i lo -j ACCEPT"
#Aceptar ping request sonbre la WAN / Accept ping request on WAN
l10 = "iptables -A INPUT -i eth1 -p icmp --icmp-type 8/0 -j ACCEPT"
l11 = "iptables -A INPUT -i eth1 -p icmp --icmp-type 0/0 -j ACCEPT"
#Permitir ssh / allow ssh
l12 = "iptables -A INPUT -i eth1 -p tcp --dport 22 -j ACCEPT"
#Permitir trafico iniciado en el fw / allow traffci from fw to any
l13 = "iptables -I INPUT -i eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT"
#Permitir a la LAN ir a cualquier parte
l14 = "iptables -A FORWARD -s " + str(lan_addr) + "/" + str(lan_prefix) + " -j ACCEPT"
l15 = "iptables -A FORWARD -d " + str(lan_addr) + "/" + str(lan_prefix) + " -j ACCEPT"
l16 = "iptables -A INPUT -s " + str(lan_addr) + "/" + str(lan_prefix) + " -j ACCEPT"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.write(l15 + '\n')
out.write(l16 + '\n')
out.close()
def hostap_default():
filename = "files" + "/" + realm + "-" + "hostap_default"
l1 = "DAEMON_CONF=\"/etc/hostapd/hostapd.conf\""
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.close()
def hostap_conf():
lanaddr = (str(lan_addr)).split(".")
filename = "files" + "/" + realm + "-" + "hostap_conf"
l1 = "interface=wlan0"
l2 = "bridge=LAN"
l3 = "driver=nl80211"
l4 = "ssid=" + realm
l5 = "hw_mode=g"
l6 = "channel=7"
l7 = "ieee8021x=1"
l8 = "auth_algs=1"
l9 = "eap_server=0"
l10 = "eapol_key_index_workaround=1"
l11 = "auth_server_addr=" + lanaddr[0] + "." + lanaddr[1] + "." + lanaddr[2] + "." + fw_ip
l12 = "auth_server_port=1812"
l13 = "auth_server_shared_secret=qwertyui"
l14 = "wpa=1"
l15 = "wpa_key_mgmt=WPA-EAP"
l16 = "wpa_pairwise=TKIP CCMP"
l17 = "wpa_group_rekey=300"
l18 = "wpa_gmk_rekey=640"
with open(filename, "w+") as out:
out.write(l1 + '\n')
out.write(l2 + '\n')
out.write(l3 + '\n')
out.write(l4 + '\n')
out.write(l5 + '\n')
out.write(l6 + '\n')
out.write(l7 + '\n')
out.write(l8 + '\n')
out.write(l9 + '\n')
out.write(l10 + '\n')
out.write(l11 + '\n')
out.write(l12 + '\n')
out.write(l13 + '\n')
out.write(l14 + '\n')
out.write(l15 + '\n')
out.write(l16 + '\n')
out.write(l17 + '\n')
out.write(l18 + '\n')
out.close()
def genera_lxd():
client = Client()
machine = ['ad1', 'ad2', 'fs', 'vpn', 'fw'];
for i in machine:
print(i)
if i == "ad1":
print("Generando maquina con perfil AD", i + "\n")
config = ({'name': i, 'source': {'type': 'image', 'alias': 'samba'}, 'profiles': ['AD']})
a = client.containers.create(config, wait=True)
a.start()
print("Se genero y encendio la maquina", a.name + "\n")
wait(tmed)
netplan(ad1_ip, ad1_name)
hosts(ad1_ip, ad1_name)
resolv(ad1_name)
samba_inst_ad1(ad1_name)