MySQL安全加固实战

来源:互联网 发布:淘宝导航自定义代码 编辑:程序博客网 时间:2024/05/21 19:49

集团聘请国内顶级安全厂商对企业信息化系统安全扫描,发现笔者运维的系统存在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[+ 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)上传附件校验有黑名单转变成白名单方式,具体实现方式因与本文关联不大在此不再赘述。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 单位社保登陆密码忘记了怎么办 12306的登录密码忘了怎么办 网银支付密码忘了怎么办 邮政网银密码忘了怎么办 12306新注册待核验怎么办 建行网银盾密码忘了怎么办 建行网银登陆密码忘了怎么办 建行网银密码忘了怎么办 建行手机网银密码忘了怎么办 移动宽带账号密码忘了怎么办 移动宽带忘记账号密码怎么办 宽带账号密码忘了怎么办 不知道宽带账号密码怎么办 宽带的账号密码忘记了怎么办 wifi登录名忘记了怎么办 苹果手机微信图纸打不开怎么办 手机qq邮箱文件打不开怎么办 12360忘记用户名和密码怎么办 刚开店铺没生意怎么办 淘宝账户不符合注销条件怎么办 网易邮箱登录密码忘记了怎么办 q号密码忘记了怎么办 志愿者注册忘记密码和用户名怎么办 w10电脑语言栏不见了怎么办 w10美式键盘没了怎么办 xp电脑开机密码忘记了怎么办 电脑开机密码到期忘记改怎么办 电脑账户数据库密码忘了怎么办 微信不能拍摄了怎么办 华硕笔记本用户名密码忘了怎么办 学信网登录密码用户名搞忘怎么办 电脑密码输入错误会被锁怎么办 电脑被锁机软件设了密码怎么办 电脑密码被锁了怎么办 电脑xp密码忘了怎么办 三星账户账号密码忘了怎么办 三星账户密码忘记了怎么办 w7账号被禁用了怎么办 笔记本用户名密码忘记了怎么办 笔记本忘记用户名和密码怎么办 苹果手机忘记用户名和密码怎么办