集团聘请国内顶级安全厂商对企业信息化系统安全扫描,发现笔者运维的系统存在SQL注入式漏洞、数据库提权安全漏洞、密码明文及附件上传安全隐患。于是笔者对该系统实施了系列的安全加固工作,在安全厂商进行二次扫描时未再发现安全隐患。

具体应对措施如下:

1) 对于SQL注入式漏洞解决方案是这样的在用户对系统进行操作时首先进行非法字符校验,因为系统已经上线不可能在对每一个SQL问做PreparedStatement处理所以我通过添加一个过滤器action来实现屏蔽SQL注入漏洞,具体实现方法参见以下代码;

点击(此处)折叠或打开

  1. 1、防SQL注入代码如下所示:
  2. package action;
  3. public class StringUtil {
  4.     public StringUtil() {
  5.     }
  6.     public static String replace(String str, String substr, String restr) {
  7.         String[] tmp = split(str, substr);
  8.         String returnstr = null;
  9.         if (tmp.length != 0) {
  10.             returnstr = tmp[0];
  11.             for (int i = 0; i < tmp.length - 1; i++)
  12.                 returnstr = dealNull(returnstr) + restr + tmp[i + 1];
  13.         }
  14.         return dealNull(returnstr);
  15.     }
  16.     public static String[] split(String source, String div) {
  17.         int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
  18.         if (source.compareTo("") != 0) {
  19.             if (source.indexOf(div) != -1) {
  20.                 intIdx = source.indexOf(div);
  21.                 for (int intCount = 1;; intCount++) {
  22.                     if (source.indexOf(div, intIdx + div_length) != -1) {
  23.                         intIdx = source.indexOf(div, intIdx + div_length);
  24.                         arynum = intCount;
  25.                     } else {
  26.                         arynum += 2;
  27.                         break;
  28.                     }
  29.                 }
  30.             } else
  31.                 arynum = 1;
  32.         } else
  33.             arynum = 0;
  34.         intIdx = 0;
  35.         intIdex = 0;
  36.         String[] returnStr = new String[arynum];
  37.         if (source.compareTo("") != 0) {
  38.             if (source.indexOf(div) != -1) {
  39.                 intIdx = (int) source.indexOf(div);
  40.                 returnStr[0] = (String) source.substring(0, intIdx);
  41.                 for (int intCount = 1;; intCount++) {
  42.                     if (source.indexOf(div, intIdx + div_length) != -1) {
  43.                         intIdex = (int) source
  44.                                 .indexOf(div, intIdx + div_length);
  45.                         returnStr[intCount] = (String) source.substring(intIdx
  46.                                 + div_length, intIdex);
  47.                         intIdx = (int) source.indexOf(div, intIdx + div_length);
  48.                     } else {
  49.                         returnStr[intCount] = (String) source.substring(intIdx
  50.                                 + div_length, source.length());
  51.                         break;
  52.                     }
  53.                 }
  54.             } else {
  55.                 returnStr[0] = (String) source.substring(0, source.length());
  56.                 return returnStr;
  57.             }
  58.         } else {
  59.             return returnStr;
  60.         }
  61.         return returnStr;
  62.     }
  63.     public static boolean sql_inj(String str) {
  64.         String inj_str = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";
  65.         String inj_stra[] = split(inj_str, "|");
  66.         for (int i = 0; i < inj_stra.length; i++) {
  67.             if (str.indexOf(inj_stra[i]) >= 0) {
  68.                 return true;
  69.                 
  70.             }
  71.         }
  72.         return false;
  73.     }
  74.     private static String dealNull(String str) {
  75.         String returnstr = null;
  76.         if (str == null)
  77.             returnstr = "";
  78.         else
  79.             returnstr = str;
  80.         return returnstr;
  81.     }
  82. // public static void main(String[] args) {
  83. // if(sql_inj("test''")==true)
  84. // System.out.println("非法字符");
  85. // else
  86. // System.out.println("输入内容合法");
  87. //
  88. // }
  89. }
  90. 2、MD5加密代码如下所示:
  91. package action;
  92. import java.security.MessageDigest;
  93. /**
  94.  *


  95.  * Title:MD5加密和验证
  96.  *


  97.  *
  98.  *


  99.  * Description:
  100.  *


  101.  *
  102.  *


  103.  * Copyright: Copyright (c) 2006
  104.  *


  105.  *
  106.  *


  107.  * Company:
  108.  *


  109.  *
  110.  * @author not attributable
  111.  * @version 1.0
  112.  */
  113. public class MD5 {
  114.  public MD5() {
  115.  }
  116.  /**
  117.   * MD5加密 Computes the MD5 fingerprint of a string.
  118.   *
  119.   * @return the MD5 digest of the input String
  120.   */
  121.  public static String compute(String inStr) {
  122.   MessageDigest md5 = null;
  123.   try {
  124.    md5 = MessageDigest.getInstance("MD5");
  125.   } catch (Exception e) {
  126.    System.out.println(e.toString());
  127.    e.printStackTrace();
  128.    return "";
  129.   }
  130.   char[] charArray = inStr.toCharArray();
  131.   byte[] byteArray = new byte[charArray.length];
  132.   for (int i = 0; i < charArray.length; i++) {
  133.    byteArray[i] = (byte) charArray[i];
  134.   }
  135.   byte[] md5Bytes = md5.digest(byteArray);
  136.   StringBuffer hexValue = new StringBuffer();
  137.   for (int i = 0; i < md5Bytes.length; i++) {
  138.    int val = ((int) md5Bytes[i]) & 0xff;
  139.    if (val < 16) {
  140.     hexValue.append("0");
  141.    }
  142.    hexValue.append(Integer.toHexString(val));
  143.   }
  144.   return hexValue.toString();
  145.  }
  146.  /**
  147.   * 验证MD5
  148.   *
  149.   * @param compareStr
  150.   * String 要比较的字符串
  151.   * @param md5Str
  152.   * String 加密后的字符串
  153.   * @return boolean 验证通过返回true,否则返回false
  154.   */
  155.  public static boolean compare(String compareStr, String md5Str) {
  156.   String computeStr = compute(compareStr);
  157.   if (computeStr.equals(md5Str)) {
  158.    return true;
  159.   } else {
  160.    return false;
  161.   }
  162.  }
  163.  public static void main(String[] args) {
  164.   System.out.println("aa:==" + compute("aa"));
  165.   System.out.println(compare("aa", "4124bc0a9335c27f086f24ba207a4912"));
  166.  }
  167. }

2)对数据库提权漏洞问题:一、采取数据库系统账号与应用账号分离即创建应用程序需要访问的账号;二、采取收缩权限并设置指定IP地址可以远程访问数据库;

具体步骤:

A) delete from user where user="root"and host!="localhost";

B) flush privileges;

C) grant  select,insert,update,delete  on itwh.*  to myapp@"10.0.212.122" identified by "youpassword";

D) flush privileges;

3)对用户密码进行加密保存;

此处使用了md5加密算法实现用户信息使用密文保存,这样带来的好处是运维人员也无法在未经用户授权情况下登陆用户的系统,查看某用户涉密相关信息等。

4)上传附件校验有黑名单转变成白名单方式,具体实现方式因与本文关联不大在此不再赘述。

12-23 08:35