-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from HisAtri/dev
Feature: simple waf and new api
- Loading branch information
Showing
18 changed files
with
373 additions
and
58 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
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
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
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,98 @@ | ||
""" | ||
WAF基本防火墙,承担基本的防火墙功能 | ||
防注入/恶意文件读取 | ||
""" | ||
from api import * | ||
|
||
import re | ||
from flask import request, abort | ||
|
||
|
||
@app.before_request | ||
def check(): | ||
""" | ||
检查请求是否合法 | ||
:return: | ||
""" | ||
# 获取请求的URL的路径+参数部分,不包括域名 | ||
path = request.path | ||
if waf(path): | ||
logger.warning(f"检测到恶意请求: {path}") | ||
abort(403) | ||
|
||
|
||
def waf(req: str): | ||
""" | ||
:param req: | ||
:return: | ||
""" | ||
NN_RULES = r"""\.\./ | ||
\:\$ | ||
\$\{ | ||
[\\/]proc[\\/]self[\\/](environ|cmdline|maps) | ||
(?i)select.+(from|limit) | ||
(?i)d(?:elete|rop|ump).+table | ||
(?:(union(.*?)select)) | ||
having|rongjitest | ||
sleep\((\s*)(\d*)(\s*)\) | ||
benchmark\((.*)\,(.*)\) | ||
base64_decode\( | ||
(?:from\W+information_schema\W) | ||
(?:(?:current_)user|database|schema|connection_id)\s*\( | ||
(?:etc\/\W*passwd) | ||
into(\s+)+(?:dump|out)file\s* | ||
group\s+by.+\( | ||
xwork.MethodAccessor | ||
xwork\.MethodAccessor | ||
(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/ | ||
java\.lang | ||
\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[ | ||
\<(iframe|script|body|img|layer|div|meta|style|base|object|input) | ||
(onmouseover|onerror|onload)\= | ||
\.\./\.\./ | ||
/\* | ||
\:\$ | ||
\$\{ | ||
(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|char|chr|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\( | ||
\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[ | ||
\s+(or|xor|and)\s+.*(=|<|>|'|") | ||
(?i)select.+(from|limit) | ||
(?:(union(.*?)select)) | ||
sleep\((\s*)(\d*)(\s*)\) | ||
benchmark\((.*)\,(.*)\) | ||
(?:from\W+information_schema\W) | ||
(?:(?:current_)user|database|schema|connection_id)\s*\( | ||
into(\s+)+(?:dump|out)file\s* | ||
group\s+by.+\( | ||
\<(iframe|script|body|img|layer|div|meta|style|base|object|input) | ||
@eval.*GET(.*])""" | ||
for re_str in NN_RULES.split("\n"): | ||
if re.search(re_str, req): | ||
# 匹配到恶意请求 | ||
logger.warning(f"匹配规则: {re_str}") | ||
return True | ||
# 测试集均为恶意请求,返回False意味着存在漏报 | ||
return False | ||
|
||
|
||
def test(): | ||
DATAS = [ | ||
"/../../", # 目录穿越 | ||
"/proc/self/maps", # 读取系统信息 | ||
"/etc/passwd", # 读取密码文件 | ||
"/etc/shadow", # 读取密码文件 | ||
"php://input", # PHP流协议 | ||
"SELECT * FROM", # SQL注入 | ||
"DROP TABLE", # SQL注入 | ||
"SeleCt * fRoM", # SQL注入,大小写混合 | ||
"sleep(3)", # SQL注入 | ||
"@@version", # SQL注入 | ||
"S%e%l%e%c%t * F%rom", # SQL注入,百分号编码 | ||
] | ||
for data in DATAS: | ||
if not waf(data): | ||
print(f"有恶意请求未被拦截: {data}") | ||
|
||
|
||
if __name__ == "__main__": | ||
test() |
Empty file.
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 |
---|---|---|
|
@@ -75,7 +75,3 @@ def load_file(file_spec, err='raise'): | |
'AudioFile', | ||
'load_file', | ||
] | ||
|
||
## | ||
## EOF | ||
## |
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,33 @@ | ||
from concurrent import futures | ||
|
||
from mod.searchx import api, kugou | ||
|
||
|
||
def search_all(title, artist, album, timeout=30): | ||
funcs = [api, kugou] | ||
results = [] | ||
|
||
def request(task): | ||
res: list = task.search(title, artist, album) | ||
if isinstance(res, list): | ||
results.extend(res) | ||
|
||
with futures.ThreadPoolExecutor() as executor: | ||
_futures = [] | ||
for func in funcs: | ||
_futures.append(executor.submit(request, func)) | ||
|
||
# 等待所有任务完成,或回收超时任务,处理TimeoutError | ||
for future in futures.as_completed(_futures, timeout=timeout): | ||
future.result() | ||
# 回收超时任务 | ||
for future in _futures: | ||
if future.done() and future.exception(): | ||
future.result() | ||
else: | ||
future.cancel() | ||
return results | ||
|
||
|
||
if __name__ == "__main__": | ||
print(search_all("大地", "Beyond", "")) |
Oops, something went wrong.