-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManagement.py
More file actions
114 lines (100 loc) · 3.58 KB
/
UserManagement.py
File metadata and controls
114 lines (100 loc) · 3.58 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
#!/usr/bin/env python3
from getpass import getpass
import crypt
import sys
import os
import random
def checking_root():
if ( os.getuid() != 0 ):
print ("[!]The Script should be run as root")
sys.exit()
else:
pass
#Nos devuelve el hash encriptado <str>
def generate_password(clear_password):
password = crypt.crypt(clear_password)
return password
#Crea el directorio home y cambia los permisos
def user_prerequisites(username):
home_dir = "mkdir /home/%s"%username
h_dir="/home/%s"%username
change_permissions = "chown %s:%s %s && chmod 700 %s"%(username,username,home_dir.split()[1],h_dir)
try:
os.system(home_dir)
os.system(change_permissions)
except:
print ("[!] Something went wrong with the user %s")%username
def check_for_users(username):
passwd = open("/etc/passwd", 'r')
passwd_data = passwd.readlines()
for user in passwd_data:
if username == user.split(":")[0]:
print ("[!] User already on the system")
sys.exit()
#Agrega el usuario a passwd ( uid debe de ser el comienzo del rango de uid)
def add_passwd(username,uid=None):
check_for_users(username)
passwd = open("/etc/passwd", 'r')
passwd_data = passwd.readlines()
if ( uid == None ):
print ("[!] The UID Will generate automatically")
uid = random.randrange(1000,6000)
else:
for user in passwd_data:
try:
if ( uid in user.split(":")[2] ):
uid = uid +1
except:
pass
home_directory = "/home/%s"%(username)
terminal = "/bin/zsh"
user_line = "%s:x:%s:%s:%s:%s:%s"%(username,uid,uid,username,home_directory,terminal)
passwd = open("/etc/passwd", 'a')
passwd.write(str(user_line)+"\n")
passwd.close()
#Creamos el grupo para el usuario invidual
group_line = "%s:x:%s:"%(username,uid)
group = open("/etc/group",'a')
group.write(group_line+"\n")
group.close()
def add_shadow(username,password):
encr_password = generate_password(password)
user_line = "%s:%s:17495:0:99999:7:::"%(username,encr_password)
try:
shadow = open("/etc/shadow", 'a')
shadow.write(user_line+"\n")
except:
print("[!] Failure with /etc/shadow")
def add_user(username,password, uid=None):
add_passwd(username,uid)
if (password == " " ):
password = getpass()
else:
password = password
add_shadow(username,password)
user_prerequisites(username)
if __name__ == "__main__":
checking_root()
if ( len(sys.argv) != 3 ):
print ("[!]Usage: UManagement.py id-user user1:uid --> ADD with custom UID")
print ("[!]Usage: UManagemet.py users user1:user2 --> ADD A list with random UID")
print ("[!]Usage: UManagement.py list wordlist_usernames --> ADD a list of users")
print ("[!] INFO: List should be user:password format")
else:
if ( sys.argv[1] == "id-user" ):
u_data = sys.argv[2].split(":")
username = u_data[0]
uid = u_data[1]
add_user(username," ",uid)
if ( sys.argv[1] == "users" ):
for user in sys.argv[2].split(":"):
add_user(user, " ")
if ( sys.argv[1] == "list"):
file_open = open(sys.argv[2], 'r')
file_data = file_open.readlines()
for user in file_data:
userpass = user.strip().split(":")
username = userpass[0]
password = userpass[1]
print ( username +" "+password )
add_user(username,password)