-
Notifications
You must be signed in to change notification settings - Fork 0
/
papazolaControl.py
143 lines (126 loc) · 5.32 KB
/
papazolaControl.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
import requests
from os import system
import subprocess
import mysql.connector
# Variable Area
domain = str(subprocess.Popen("hostname", shell=True,
stdout=subprocess.PIPE).stdout.read())
# Function Area
def mailservice(x):
print(x)
# Database Function
try:
db = mysql.connector.connect(
host='localhost',
user='root',
passwd='your_password',
database='papazolaControl'
)
cursor = db.cursor()
except Exception as e:
mailservice(str(e))
def getall_datatable(table):
sql = 'SELECT * FROM '+table
cursor.execute(sql)
result = cursor.fetchall()
return result
def search_datatable(table, col, search):
sql = 'SELECT * FROM '+table+' WHERE '+col+"='"+search+"'"
cursor.execute(sql)
result = cursor.fetchone()
return result
def update_row(sql, val):
try:
cursor.execute(sql, val)
db.commit()
except Exception as e:
mailservice(str(e))
# Get And Input to variable
portssh = str(search_datatable('info', 'type', 'PortSSH')[2])
master = str(search_datatable('info', 'type', 'Master')[2])
# Program Function
def launch():
# reboot
reboot_val = str(search_datatable('info', 'type', 'reboot')[2])
if reboot_val == '2':
sql = "UPDATE info SET value=%s WHERE type=%s"
val = ('1', 'reboot')
update_row(sql, val)
system('reboot')
# icmp
icmp_val = str(search_datatable('network_rule', 'name', 'icmp')[2])
icmp_exec = str(search_datatable('network_rule', 'name', 'icmp')[3])
if icmp_val == '2':
if icmp_exec == '1':
sql = "UPDATE network_rule SET `execute_val`=%s WHERE `name`=%s"
val = ("2", "icmp")
update_row(sql, val)
system("iptables -A INPUT -p icmp --source "+master+" -j ACCEPT")
system("iptables -A INPUT -p icmp -j DROP")
elif icmp_val == '1':
if icmp_exec == '1':
sql = "UPDATE network_rule SET `execute_val`=%s WHERE name=%s"
val = ('2', 'icmp')
update_row(sql, val)
system("iptables -D INPUT -p icmp --source "+master+" -j ACCEPT")
system("iptables -D INPUT -p icmp -j DROP")
# ssh
ssh_val = str(search_datatable('network_rule', 'name', 'ssh-publickey')[2])
ssh_exec = str(search_datatable('network_rule', 'name', 'ssh-publickey')[3])
if ssh_val == '2':
if ssh_exec == '1':
sql = "UPDATE network_rule SET execute_val=%s WHERE name=%s"
val = ('2', 'ssh-publickey')
update_row(sql, val)
system("sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/g' /etc/ssh/sshd_config")
system("sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config")
# system("iptables -A INPUT -p tcp --dport " +portssh+" --source "+master+" -j ACCEPT")
# system("iptables -A INPUT -p tcp --dport "+portssh+" -j DROP")
elif ssh_val == '1':
if ssh_exec == '1':
sql = "UPDATE network_rule SET execute_val=%s WHERE name=%s"
val = ('2', 'ssh-publickey')
update_row(sql, val)
system("sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config")
# system("iptables -D INPUT -p tcp --dport " +portssh+" --source "+master+" -j ACCEPT")
# system("iptables -D INPUT -p tcp --dport "+portssh+" -j DROP")
# Service
# ssh
ssh_svc = str(search_datatable('services', 'name', 'ssh')[2])
ssh_x = str(search_datatable('services', 'name', 'ssh')[3])
if ssh_svc == '1':
if ssh_x == '1':
system('service ssh stop')
status = subprocess.Popen('service ssh status | grep Active:', shell=True, stdout=subprocess.PIPE).stdout.read()
sql = "UPDATE services SET `execute`=%s,`information`=%s WHERE `name`=%s"
val = ('2',str(status),'ssh')
update_row(sql, val)
if ssh_svc == '2':
if ssh_x == '1':
system('service ssh start')
status = subprocess.Popen('service ssh status | grep Active:', shell=True, stdout=subprocess.PIPE).stdout.read()
sql = "UPDATE services SET `execute`=%s,`information`=%s WHERE `name`=%s"
val = ('2',str(status),'ssh')
update_row(sql, val)
# portsentry
portsentry_svc = str(search_datatable('services', 'name', 'portsentry')[2])
portsentry_x = str(search_datatable('services', 'name', 'portsentry')[3])
if portsentry_svc == '1':
if portsentry_x == '1':
system('service portsentry stop')
status = subprocess.Popen('service portsentry status | grep Active:', shell=True, stdout=subprocess.PIPE).stdout.read()
sql = "UPDATE services SET `execute`=%s,`information`=%s WHERE `name`=%s"
val = ('2',str(status),'portsentry')
update_row(sql, val)
if portsentry_svc == '2':
if portsentry_x == '1':
system('service portsentry start')
status = subprocess.Popen('service portsentry status | grep Active:', shell=True, stdout=subprocess.PIPE).stdout.read()
sql = "UPDATE services SET `execute`=%s,`information`=%s WHERE `name`=%s"
val = ('2',str(status),'portsentry')
update_row(sql, val)
# Start Action
try:
launch()
except Exception as e:
mailservice(str(e))