use list instead of string, prevent injection attack. (#1009)
* fix issue: https://github.com/shadowsocks/shadowsocks/issues/995 Command Execution use list instead of string, prevent injection attack.
This commit is contained in:
parent
c668f44c68
commit
e332ec93e9
1 changed files with 28 additions and 15 deletions
|
@ -24,9 +24,17 @@
|
||||||
from __future__ import absolute_import, division, print_function, \
|
from __future__ import absolute_import, division, print_function, \
|
||||||
with_statement
|
with_statement
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
import sys
|
||||||
|
import socket
|
||||||
import argparse
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
def inet_pton(str_ip):
|
||||||
|
try:
|
||||||
|
return socket.inet_pton(socket.AF_INET, str_ip)
|
||||||
|
except socket.error:
|
||||||
|
return None
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(description='See README')
|
parser = argparse.ArgumentParser(description='See README')
|
||||||
|
@ -37,17 +45,22 @@ if __name__ == '__main__':
|
||||||
ips = {}
|
ips = {}
|
||||||
banned = set()
|
banned = set()
|
||||||
for line in sys.stdin:
|
for line in sys.stdin:
|
||||||
if 'can not parse header when' in line:
|
if 'can not parse header when' not in line:
|
||||||
ip = line.split()[-1].split(':')[-2]
|
continue
|
||||||
if ip not in ips:
|
ip_str = line.split()[-1].rsplit(':', 1)[0]
|
||||||
ips[ip] = 1
|
ip = inet_pton(ip_str)
|
||||||
print(ip)
|
if ip is None:
|
||||||
sys.stdout.flush()
|
continue
|
||||||
else:
|
if ip not in ips:
|
||||||
ips[ip] += 1
|
ips[ip] = 1
|
||||||
if ip not in banned and ips[ip] >= config.count:
|
sys.stdout.flush()
|
||||||
banned.add(ip)
|
else:
|
||||||
cmd = 'iptables -A INPUT -s %s -j DROP' % ip
|
ips[ip] += 1
|
||||||
print(cmd, file=sys.stderr)
|
if ip not in banned and ips[ip] >= config.count:
|
||||||
sys.stderr.flush()
|
banned.add(ip)
|
||||||
os.system(cmd)
|
print('ban ip %s' % ip_str)
|
||||||
|
cmd = ['iptables', '-A', 'INPUT', '-s', ip_str, '-j', 'DROP',
|
||||||
|
'-m', 'comment', '--comment', 'autoban']
|
||||||
|
print(' '.join(cmd), file=sys.stderr)
|
||||||
|
sys.stderr.flush()
|
||||||
|
subprocess.call(cmd)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue