todo 第三步在整个系统中发现了两个可进行代码注入的漏洞,第一个为pickl反序列化用户登录信息的时候没有做校验,这样当对应的存储介质(memcache、redis)没有开启登录认证并且暴漏在公网中很容易注入代码。第二个为在系统中一些配置直接使用eval函数执行配置中的Python代码进行注入。 todo 反向shell介绍
如何安全编码
严格控制输入,过滤所有危险模块,遇到非法字符直接返回。
使用ast.literal_eval()代替eval()
安全使用pickle
下面就着几个点来说一下:
eval()方法注释:
1 2
eval(source[, globals[, locals]]) -> value Evaluate the source in the context of globals and locals. The source may be a string representing a Python expression or a code object as returned by compile(). The globals must be a dictionary and locals can be any mapping, defaulting to the current globals and locals. If only globals is given, locals defaults to it.
ast.literal_eval()方法注释:
1
Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
使用ast.literal_eval()代替eval()对比:
1 2 3 4 5 6 7
ast.literal_eval("1+1") # ValueError: malformed string ast.literal_eval("[1, 2, 3]") # [1, 2, 3] ast.literal_eval("{1: 1, 2: 2, 3: 3}") # {1: 1, 2: 2, 3: 3} ast.literal_eval("__import__('os').system('uname -a')") # ValueError: malformed string eval("__import__('os').system('uname -a')") # Linux zhangxu-ThinkPad-T450 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux eval("__import__('os').system('uname -a')", {}, {}) # Linux zhangxu-ThinkPad-T450 3.13.0-92-generic #139-Ubuntu SMP Tue Jun 28 20:42:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux eval("__import__('os').system('uname -a')", {"__builtins__": {}}, {}) # NameError: name '__import__' is not defined
What is nmap? Nmap (Network Mapper) is a security scanner originally written by Gordon Lyon used to discover hosts and services on a computer network, thus creating a “map” of the network.
-A: Enable OS detection, version detection, script scanning, and traceroute -v: Increase verbosity level (use -vv or more for greater effect) -p : Only scan specified ports
root@bt:~# nmap -v -A *.*.*.* -p 1-65535 Starting Nmap 6.25 ( http://nmap.org ) at 2016-07-26 13:30 EDT ...... Not shown: 65527 filtered ports PORT STATE SERVICE VERSION 139/tcp open netbios-ssn 1723/tcp open pptp Microsoft 8891/tcp open http nginx 1.4.4 9090/tcp closed zeus-admin 13228/tcp open http Microsoft IIS httpd 7.5 14580/tcp closed unknown 36666/tcp open unknown 64380/tcp open unknown ...... Device type: general purpose|storage-misc Running (JUST GUESSING): Linux 2.4.X (99%), Microsoft Windows 7 (95%), BlueArc embedded (91%) OS CPE: cpe:/o:linux:linux_kernel:2.4 cpe:/o:microsoft:windows_7:::enterprise cpe:/h:bluearc:titan_2100 Aggressive OS guesses: DD-WRT v24-sp2 (Linux 2.4.37) (99%), Microsoft Windows 7 Enterprise (95%), BlueArc Titan 2100 NAS device (91%) No exact OS matches for host (test conditions non-ideal). Network Distance: 2 hops TCP Sequence Prediction: Difficulty=259 (Good luck!) IP ID Sequence Generation: Incremental Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows ...... NSE: Script Post-scanning. Read data files from: /usr/local/bin/../share/nmap OS and Service detection performed. Please report any incorrect results at http://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 895.44 seconds Raw packets sent: 262711 (11.560MB) | Rcvd: 55220 (2.209MB)