-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewall_mac_while.py
More file actions
executable file
·184 lines (153 loc) · 6.12 KB
/
firewall_mac_while.py
File metadata and controls
executable file
·184 lines (153 loc) · 6.12 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
#!/usr/bin/python
#coding=utf-8
import cx_Oracle
import sys
import urllib
import os
import csv
import random
import time
import hashlib
var = 1
while var == 1:
#connect db select infomation section
#注意这里要设置默认的拒绝策略和白名单:
#iptables -A INPUT --source 196.128.1.0/24 --dport 80 -j REJECT
#iptables -I INPUT --source-mac --dport 80 -j REJECT
#############################配置白名单列表#################################
curs = orcl.cursor()
printHeader = True
sql = "select * from tab where tname='EXAM_KTXX_WHITE'"
curs.execute(sql)
for row_data in curs:
outputFile = open('mac_white_new.table','wb') # 'wb'
output = csv.writer(outputFile)
sql = "select mac from exam_ktxx_white "
curs2 = orcl.cursor()
curs2.execute(sql)
for row_data in curs2: # add table rows
output.writerow(row_data)
outputFile.close()
os.popen('dos2unix mac_white_new.table 2&> /dev/null')
#if file
def getHash(f): #定义函数md5
line=f.readline()
hash=hashlib.md5()
while(line):
hash.update(line)
line=f.readline()
return hash.hexdigest()
if os.access("mac_white_old.table", os.F_OK): #判断文件是否存在
f1=open("mac_white_new.table","rb")
f2=open("mac_white_old.table","rb")
str1=getHash(f1)
str2=getHash(f2)
if str1 == str2: #判断文件是否相同
print "white is same file" #相同不执行任何操作,不需要添加和删除规则
else: #如果不相同读取old文件内容执行delete,然后读取new重新添加规则
file = open("mac_white_old.table")
while 1:
lines = file.readlines(200)
if not lines:
break
for line in lines:
mac = line
strmac = str(mac)
os.popen('iptables -D INPUT -p tcp --dport 80 -m mac -j ACCEPT --mac-source %s'% (strmac))
os.popen('service iptables save')
#删除规则
#添加规则
file = open("mac_white_new.table")
while 1:
lines = file.readlines(200)
if not lines:
break
for line in lines:
mac = line
strmac = str(mac)
os.popen('iptables -I INPUT -p tcp --dport 80 -m mac -j ACCEPT --mac-source %s'% (strmac))
os.popen('service iptables save')
os.popen('cp mac_white_new.table mac_white_old.table')
else: #如果没有old文件,这是对于新创建的时候执行的。
#set iptables rules section
file = open("mac_white_new.table")
while 1:
lines = file.readlines(200)
if not lines:
break
for line in lines:
mac = line
strmac = str(mac)
os.popen('iptables -I INPUT -p tcp --dport 80 -m mac -j ACCEPT --mac-source %s'% (strmac))
os.popen('service iptables save')
os.popen('cp mac_white_new.table mac_white_old.table')
##############配置维考台机信任列表##########################################
curs = orcl.cursor()
printHeader = True
sql = "select * from tab where tname='EXAM_KTXX'"
curs.execute(sql)
for row_data in curs:
outputFile = open('mac_list_new.table','wb') # 'wb'
output = csv.writer(outputFile)
sql = "select mac from exam_ktxx "
curs2 = orcl.cursor()
curs2.execute(sql)
for row_data in curs2: # add table rows
output.writerow(row_data)
outputFile.close()
os.popen('dos2unix mac_list_new.table 2&> /dev/null')
#if file
def getHash(f): #定义函数md5
line=f.readline()
hash=hashlib.md5()
while(line):
hash.update(line)
line=f.readline()
return hash.hexdigest()
if os.access("mac_list_old.table", os.F_OK): #判断文件是否存在
f1=open("mac_list_new.table","rb")
f2=open("mac_list_old.table","rb")
str1=getHash(f1)
str2=getHash(f2)
if str1 == str2: #判断文件是否相同
print "ktj is same file" #相同不执行任何操作,不需要添加和删除规则
else: #如果不相同读取old文件内容执行delete,然后读取new重新添加规则
file = open("mac_list_old.table")
while 1:
lines = file.readlines(200)
if not lines:
break
for line in lines:
mac = line
strmac = str(mac)
os.popen('iptables -D INPUT -p tcp --dport 80 -m mac -j ACCEPT --mac-source %s'% (strmac))
os.popen('service iptables save')
#删除规则
#添加规则
file = open("mac_list_new.table")
while 1:
lines = file.readlines(200)
if not lines:
break
for line in lines:
mac = line
strmac = str(mac)
os.popen('iptables -I INPUT -p tcp --dport 80 -m mac -j ACCEPT --mac-source %s'% (strmac))
os.popen('service iptables save')
os.popen('cp mac_list_new.table mac_list_old.table')
else: #如果没有old文件,这是对于新创建的时候执行的。
#set iptables rules section
file = open("mac_list_new.table")
while 1:
lines = file.readlines(200)
if not lines:
break
for line in lines:
mac = line
strmac = str(mac)
os.popen('iptables -I INPUT -p tcp --dport 80 -m mac -j ACCEPT --mac-source %s'% (strmac))
os.popen('service iptables save')
os.popen('cp mac_list_new.table mac_list_old.table')
time.sleep(20)