JAVA开发常见安全问题:命令注入
·
专栏链接
一、数据的校验
二、认证与授权
三、Cookie与会话管理
四、错误处理
五、日志安全
六、未验证的重定向
数据的校验:命令注入
漏洞描述:
命令注入,此处所指的是操作系统命令注入,由于命令的部分或全部指令来自用户输入的数据,一旦被攻击者利用,往往可被彻底拿下服务器,后果不堪设想。
检测方法:
检查代码中是否使用 Runtime.getRuntime().exec()方法, 传入方法的参数是否可控,如果系统命令拼接语句中的变量来自不可信输入,则存在漏洞。
不合规的代码示例:
String fileName = (String) equest.getParameter("fileName");
String source_name = (String) request.getParameter("source_name");
// 上传到 wl 路径
String webpath =Configuration.getValue("PATH_UPLOAD2WEB").trim();
String sErrorMessage = " ";
int len1 = 0, len2 = 0;
do {
len1 = fileName.length();
fileName = fileName.replaceAll("\\.\\./", "");
len2 = fileName.length();
} while (len1 != len2);
String sSaveName = webpath + "/" + fileName;
int upLoadFlag = 0;
String ErrorInfo = "";
// 新建一个 SmartUpload 对象
SmartUpload mySmartUpload = new SmartUpload();
try {
// 上传初始化
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
}catch (Exception ex){
upLoadFlag = -1;
sErrorMessage = "上载文件传输中出错!";
}
try {
com.jspsmart.upload.File file1 = mySmartUpload.getFiles().getFile(0);
if (!file1.isMissing()){
file1.saveAs(sSaveName);}
String _shellpath = Configuration.getValue("PATH_SHELL");
String exePath = "sh " + _shellpath;
String commandString = "WebToAll.sh " + fileName;
String exeString = exePath + commandString;
// 执行命令
Process p = Runtime.getRuntime().exec(exeString);
p.waitFor();
}catch (Exception ex){
upLoadFlag = -1;
sErrorMessage = "上载文件存储时出错!";
}
不合规说明:
程序在此处执行系统命令时,未对 fileName 参数过滤“&”、 “|”、“;”。
合规的代码示例:
String fileName = (String) request.getParameter("fileName");
String source_name = (String) request.getParameter("source_name");
// 上传到 wl 路径
String webpath =Configuration.getValue("PATH_UPLOAD2WEB").trim();
String sErrorMessage = " ";
int len1 = 0, len2 = 0;
do {
len1 = fileName.length();
fileName = fileName.replaceAll("\\.\\./", "").replaceAll("[&\\|;]", "");
len2 = fileName.length();
} while (len1 != len2);
String sSaveName = webpath + "/" + fileName;
int upLoadFlag = 0;
String ErrorInfo = "";
// 新建一个 SmartUpload 对象
SmartUpload mySmartUpload = new SmartUpload();
try {
// 上传初始化
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
}catch (Exception ex) {
upLoadFlag = -1;
sErrorMessage = "上载文件传输中出错!";
}
try {
com.jspsmart.upload.File file1 = mySmartUpload.getFiles().getFile(0);
if (!file1.isMissing()){
file1.saveAs(sSaveName);}
String _shellpath = Configuration.getValue("PATH_SHELL");
String exePath = "sh " + _shellpath;
String commandString = "WebToAll.sh " + fileName;
String exeString = exePath + commandString;
// 执行命令
Process p = Runtime.getRuntime().exec(exeString);
p.waitFor();
}catch (Exception ex){
upLoadFlag = -1;
sErrorMessage = "上载文件存储时出错!";
}
合规说明:
对 fileName 参数过滤危险字符“&”、“|”和“;”。
更多推荐



所有评论(0)