forked from NimaQu/shadowsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
beta
- Loading branch information
Showing
42 changed files
with
7,814 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#!/usr/bin/python | ||
# -*- coding: UTF-8 -*- | ||
|
||
import logging | ||
import time | ||
import sys | ||
import os | ||
import configloader | ||
import importloader | ||
import gnupg | ||
import thread | ||
import cymysql | ||
import commands | ||
import socket | ||
import re | ||
|
||
|
||
def file_len(fname): | ||
with open(fname) as f: | ||
for i, l in enumerate(f): | ||
pass | ||
return i + 1 | ||
|
||
def get_ip(text): | ||
reip = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])') | ||
for ip in reip.findall(text): | ||
return ip | ||
return None | ||
|
||
def auto_block_thread(): | ||
if configloader.get_config().CLOUDSAFE == 0: | ||
return | ||
|
||
start_line = file_len("/etc/hosts.deny") | ||
|
||
|
||
|
||
while True: | ||
time.sleep(60) | ||
|
||
server_ip = socket.gethostbyname(configloader.get_config().MYSQL_HOST) | ||
|
||
conn = cymysql.connect(host=configloader.get_config().MYSQL_HOST, port=configloader.get_config().MYSQL_PORT, user=configloader.get_config().MYSQL_USER, | ||
passwd=configloader.get_config().MYSQL_PASS, db=configloader.get_config().MYSQL_DB, charset='utf8') | ||
|
||
|
||
|
||
deny_file = open('/etc/hosts.deny') | ||
deny_lines = deny_file.readlines() | ||
deny_file.close() | ||
|
||
logging.info("Read hosts.deny from line " + str(start_line)) | ||
real_deny_list = deny_lines[start_line:] | ||
|
||
denyed_ip_list = [] | ||
for line in real_deny_list: | ||
if get_ip(line) and line.find('#') != 0: | ||
ip = get_ip(line) | ||
|
||
if ip == server_ip: | ||
i = 0 | ||
|
||
for line in deny_lines: | ||
if line.find(ip) != -1: | ||
del deny_lines[i] | ||
i = i + 1 | ||
|
||
deny_file = file("/etc/hosts.deny", "w+") | ||
for line in deny_lines: | ||
deny_file.write(line) | ||
deny_file.close() | ||
|
||
continue | ||
|
||
cur = conn.cursor() | ||
cur.execute("INSERT INTO `blockip` (`id`, `nodeid`, `ip`, `datetime`) VALUES (NULL, '" + str(configloader.get_config().NODE_ID) + "', '" + str(ip) + "', unix_timestamp())") | ||
cur.close() | ||
|
||
logging.info("Block ip:" + str(ip)) | ||
|
||
denyed_ip_list.append(ip) | ||
|
||
cur = conn.cursor() | ||
cur.execute("SELECT * FROM `blockip` where `datetime`>unix_timestamp()-60") | ||
rows = cur.fetchall() | ||
cur.close() | ||
|
||
deny_str = ""; | ||
deny_str_at = ""; | ||
|
||
for row in rows: | ||
node = row[1] | ||
ip = row[2] | ||
|
||
if str(node) == str(configloader.get_config().NODE_ID): | ||
if configloader.get_config().ANTISSATTACK == 1 and configloader.get_config().CLOUDSAFE == 1 and ip not in denyed_ip_list: | ||
deny_str_at = deny_str_at + "\nALL: " + str(ip) | ||
logging.info("Remote Block ip:" + str(ip)) | ||
else: | ||
deny_str = deny_str + "\nALL: " + str(ip) | ||
logging.info("Remote Block ip:" + str(ip)) | ||
|
||
|
||
deny_file=open('/etc/hosts.deny','a') | ||
deny_file.write(deny_str) | ||
deny_file.close() | ||
|
||
if configloader.get_config().ANTISSATTACK == 1 and configloader.get_config().CLOUDSAFE == 1: | ||
deny_file=open('/etc/hosts.deny','a') | ||
deny_file.write(deny_str_at) | ||
deny_file.close() | ||
|
||
|
||
|
||
|
||
cur = conn.cursor() | ||
cur.execute("SELECT * FROM `unblockip` where `datetime`>unix_timestamp()-60") | ||
rows = cur.fetchall() | ||
cur.close() | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
deny_file = open('/etc/hosts.deny') | ||
deny_lines = deny_file.readlines() | ||
deny_file.close() | ||
|
||
i = 0 | ||
|
||
for line in deny_lines: | ||
for row in rows: | ||
ip = str(row[1]) | ||
if line.find(ip) != -1: | ||
del deny_lines[i] | ||
logging.info("Unblock ip:" + str(ip)) | ||
i = i + 1 | ||
|
||
deny_file = file("/etc/hosts.deny", "w+") | ||
for line in deny_lines: | ||
deny_file.write(line) | ||
deny_file.close() | ||
|
||
start_line = file_len("/etc/hosts.deny") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/python | ||
# -*- coding: UTF-8 -*- | ||
|
||
import logging | ||
import time | ||
import sys | ||
import os | ||
import configloader | ||
import importloader | ||
import gnupg | ||
import thread | ||
import cymysql | ||
import commands | ||
|
||
def run_command(command,id): | ||
value = commands.getoutput(command) | ||
conn = cymysql.connect(host=configloader.get_config().MYSQL_HOST, port=configloader.get_config().MYSQL_PORT, user=configloader.get_config().MYSQL_USER, | ||
passwd=configloader.get_config().MYSQL_PASS, db=configloader.get_config().MYSQL_DB, charset='utf8') | ||
cur = conn.cursor() | ||
cur.execute("INSERT INTO `auto` (`id`, `value`, `sign`, `datetime`,`type`) VALUES (NULL, 'NodeID:" + str(configloader.get_config().NODE_ID) + " Result:\n" + str(value) + "', 'NOT', unix_timestamp(),'2')") | ||
rows = cur.fetchall() | ||
cur.close() | ||
conn.commit() | ||
conn.close() | ||
|
||
def auto_thread(): | ||
if configloader.get_config().AUTOEXEC == 0: | ||
return | ||
|
||
gpg = gnupg.GPG("/tmp/ssshell") | ||
key_data = open('ssshell.asc').read() | ||
import_result = gpg.import_keys(key_data) | ||
public_keys = gpg.list_keys() | ||
|
||
while True: | ||
time.sleep(60) | ||
conn = cymysql.connect(host=configloader.get_config().MYSQL_HOST, port=configloader.get_config().MYSQL_PORT, user=configloader.get_config().MYSQL_USER, | ||
passwd=configloader.get_config().MYSQL_PASS, db=configloader.get_config().MYSQL_DB, charset='utf8') | ||
cur = conn.cursor() | ||
cur.execute("SELECT * FROM `auto` where `datetime`>unix_timestamp()-60 AND `type`=1") | ||
rows = cur.fetchall() | ||
cur.close() | ||
|
||
for row in rows: | ||
id = row[0] | ||
data = row[2] | ||
sign = row[3] | ||
verify_data = "-----BEGIN PGP SIGNED MESSAGE-----\n" + \ | ||
"Hash: SHA256\n" + \ | ||
"\n" + \ | ||
data + "\n" + \ | ||
"-----BEGIN PGP SIGNATURE-----\n" + \ | ||
"Version: GnuPG v2\n" + \ | ||
"\n" + \ | ||
sign + "\n" + \ | ||
"-----END PGP SIGNATURE-----\n" | ||
|
||
verified = gpg.verify(verify_data) | ||
is_verified = 0 | ||
for key in public_keys: | ||
if key['keyid'] == verified.key_id: | ||
is_verified = 1 | ||
break | ||
|
||
if is_verified == 1: | ||
cur = conn.cursor() | ||
cur.execute("SELECT * FROM `auto` where `sign`='" + str(configloader.get_config().NODE_ID) + "-" + str(id) + "'") | ||
if cur.fetchone() == None : | ||
cur_c = conn.cursor() | ||
cur_c.execute("INSERT INTO `auto` (`id`, `value`, `sign`, `datetime`,`type`) VALUES (NULL, 'NodeID:" + str(configloader.get_config().NODE_ID) + " Exec Command ID:" + str(configloader.get_config().NODE_ID) + " Starting....', '" + str(configloader.get_config().NODE_ID) + "-" + str(id) + "', unix_timestamp(),'2')") | ||
cur_c.close() | ||
|
||
logging.info("Running the command:" + data) | ||
thread.start_new_thread(run_command,(data,id)) | ||
cur.close() | ||
else: | ||
logging.info("Running the command:" + data) | ||
|
||
cur = conn.cursor() | ||
cur.execute("SELECT * FROM `auto` where `datetime`>unix_timestamp()-60 AND `type`=1") | ||
rows = cur.fetchall() | ||
cur.close() | ||
|
||
conn.commit() | ||
conn.close() | ||
|
Oops, something went wrong.