命令注入攻击及其防范措施
创作时间:
作者:
@小白创作中心
命令注入攻击及其防范措施
引用
CSDN
1.
https://blog.csdn.net/kaka_buka/article/details/139225755
命令注入攻击是一种通过有漏洞的应用程序在主机操作系统上执行任意命令的攻击。 当应用程序将不安全的用户输入数据(如表单、cookie、HTTP 标头等)传递给系统命令执行时,可能会发生命令注入攻击。
命令注入攻击的原理
命令注入攻击利用了应用程序执行系统命令时未对用户输入进行适当的验证或过滤的漏洞。攻击者通过将恶意命令注入到用户输入中,迫使应用程序执行这些恶意命令,从而获取未授权的访问权限或执行恶意操作。
示例攻击场景
假设有一个Web应用程序允许用户通过HTTP请求执行系统命令,而用户输入未经过严格的验证。在这种情况下,攻击者可以通过注入恶意命令来执行任意系统命令。
public String executeSystemCommand(HttpServletRequest request) throws ServletException, IOException {
String commandResult = "";
String userCommand = request.getParameter("Command");
try {
ProcessBuilder builder = new ProcessBuilder("/bin/sh/", "-c", userCommand);
Process subProc = builder.start();
BufferedReader irProcOutput = new BufferedReader(new InputStreamReader(subProc.getInputStream()));
String line = null;
while ((line = irProcOutput.readLine()) != null)
commandResult += line;
irProcOutput.close();
} catch (Exception ex) {
handleExceptions(ex);
}
return commandResult;
}
在以上代码中,如果 userCommand
包含恶意命令,攻击者就可以利用这一漏洞执行任意系统命令。
防范命令注入攻击的有效措施
1. 避免直接的Shell命令执行
最有效的防范措施之一是避免在代码中直接执行Shell命令。相反,可以使用平台提供的API或库调用来实现相同的功能。
public String performSpecificAction_LibraryAPI(HttpServletRequest request) throws ServletException, IOException {
String result = "";
String userParam = request.getParameter("Parameter");
try {
OpenSysLibrary api = new OpenSysLibrary();
result = api.doSpecificAction(userParam);
} catch (Exception ex) {
handleExceptions(ex);
}
return result;
}
2. 仅执行静态命令
如果必须执行命令,确保仅执行静态命令,不包含动态、用户可控制的参数。
3. 过滤输入中的危险字符
从用户输入中过滤掉危险字符,例如:
- &
- ,
- &&
- |
- ||
- ;
- 换行符(0x0a 或 \n)。
public String executeSystemCommand_WithParameters(HttpServletRequest request) throws ServletException, IOException {
String commandResult = "";
String userCommand = request.getParameter("Command");
userCommand = userCommand.replaceAll("[^A-Za-z0-9]", "");
try {
ProcessBuilder builder = new ProcessBuilder("/bin/sh/", "-c", PROGRAM_NAME, userCommand);
Process subProc = builder.start();
BufferedReader irProcOutput = new BufferedReader(new InputStreamReader(subProc.getInputStream()));
String line = null;
while ((line = irProcOutput.readLine()) != null)
commandResult += line;
irProcOutput.close();
} catch (Exception ex) {
handleExceptions(ex);
}
return commandResult;
}
4. 使用白名单验证
对允许输入的值进行白名单验证,仅允许合法的输入值通过验证。
5. 限制应用程序的权限
根据最小权限原则,限制应用程序所使用的操作系统用户的权限,使其仅能执行必要的系统命令。
参考代码示例
以下是Java和C#中调用外部程序的安全代码示例:
Java 示例
public String executeSystemCommand_WithParameters(HttpServletRequest request) throws ServletException, IOException {
String commandResult = "";
String userCommand = request.getParameter("Command");
userCommand = userCommand.replaceAll("[^A-Za-z0-9]", "");
try {
ProcessBuilder builder = new ProcessBuilder("/bin/sh/", "-c", PROGRAM_NAME, userCommand);
Process subProc = builder.start();
BufferedReader irProcOutput = new BufferedReader(new InputStreamReader(subProc.getInputStream()));
String line = null;
while ((line = irProcOutput.readLine()) != null)
commandResult += line;
irProcOutput.close();
} catch (Exception ex) {
handleExceptions(ex);
}
return commandResult;
}
C# 示例
public string ExecuteSystemCommand_WithParameters(HttpRequest request) {
string output;
string userParams = request.Form["ExeParams"];
using (Process subProc = new Process()) {
subProc.StartInfo.FileName = PATH_TO_EXTERNAL_PROGRAM;
subProc.StartInfo.Arguments = sanitizeForProcess(userParams);
subProc.StartInfo.UseShellExecute = false;
subProc.StartInfo.RedirectStandardOutput = true;
subProc.Start();
output = subProc.StandardOutput.ReadToEnd();
subProc.WaitForExit(MAX_TIMEOUT);
}
return output;
}
通过实施这些防范措施,可以有效地降低命令注入攻击的风险,保护应用程序和系统的安全。
参考链接
- OWASP Command Injection
- Java ProcessBuilder Documentation
- C# Process Class Documentation
本文原文来自CSDN
热门推荐
开发商违约不按时交房怎么处理楼盘烂尾楼
[CoMat] 解决文本与图像之间的差异
新港人必备!香港生活全攻略,带你轻松融入!
多肉雅乐之舞的栽种技巧和日常养护
《GTA5》官方MOD工具正在开发中!重磅更新即将上线
晚期肝病患者如何管理饮食
破产清算和破产重组有什么区别
太阳核心温度只有1500万摄氏度,为何能发生核聚变反应?
【华为】静态NAT、动态NAT、NAPT、Easy IP、NAT Server
探索代表健康长寿的植物(走进寿命长的植物世界)
南沙全民文化体育综合体(大湾区文化体育中心)即将竣工
竹芋类植物有哪些品种?如何养护?
《花样年华》重映,苏丽珍和周慕云的爱情再续
楼房防潮用什么材料?科学选择打造宜居环境
Word文档菜单栏一直显示的多种方法
严防信用卡套现:法律风险与防范对策
人到中年:保持旺盛代谢的“8”个好习惯
7個改善盆骨前傾運動讓你重拾完美身材
2024年,降压药已经发展到什么程度了?4大新型降压药进展盘点
陈振龙如何将红薯引入中国?
肺多发结节太常见了,临床到底该怎样决策?
黄斑病变导致眼底出血的原因是什么?如何有效治疗此症状?
《说文解字》540个部首
不是感冒!孩子鼻塞、流鼻涕总不见好,这几种情况千万别大意!
人类预期寿命还能大幅增长吗?
如何充分利用 iPhone 上的相机控制
含羞草电视剧背景故事揭秘:深刻的人物塑造与社会现实的精彩呈现
小儿积食中医辨证治疗
当代社恐青年的社交解药:跟AI唠嗑上瘾
1687牛顿《自然哲学的数学原理》