C# 使用Excel导出防止注入攻击
我在下面发布了一个Excel导出类的代码(不管怎么说,大部分都是这样)。我面临的问题是,当涉及注射攻击时,这种出口是不安全的。我可以很容易地用参数屏蔽初始命令,但是,当我将参数化命令传递给Export(string)方法时,它会丢失以前在参数中设置的值;它只传递文本字符串(即从表中选择*,其中列_NAME=@Parameter1)。 我试图找出的是一种防止此代码不安全的方法。我真的需要这个功能。在过去,由于应用程序的预期受众,它一直很好,但我不能使用此特定代码,因为它将面向客户,对公众使用的开放性将更低。:/ 对我如何实现这个目标有什么建议吗C# 使用Excel导出防止注入攻击,c#,sql,sql-injection,export-to-excel,ole,C#,Sql,Sql Injection,Export To Excel,Ole,我在下面发布了一个Excel导出类的代码(不管怎么说,大部分都是这样)。我面临的问题是,当涉及注射攻击时,这种出口是不安全的。我可以很容易地用参数屏蔽初始命令,但是,当我将参数化命令传递给Export(string)方法时,它会丢失以前在参数中设置的值;它只传递文本字符串(即从表中选择*,其中列_NAME=@Parameter1)。 我试图找出的是一种防止此代码不安全的方法。我真的需要这个功能。在过去,由于应用程序的预期受众,它一直很好,但我不能使用此特定代码,因为它将面向客户,对公众使用的开放
public static void Export(string CrossoverStatement = "SELECT * FROM TABLE")
{
// @@Parameters
//
// CrossoverStatement string :
// This is the string representation of the procedure executed to obtain the desired crossover query results.
//Create our database connection as well as the excel reference and workbook/worksheet we are using to export the data
string ODBCConnection = "SERVER INFO";
Application xls = new Application();
xls.SheetsInNewWorkbook = 1;
// Create our new excel application and add our workbooks/worksheets
Workbook Workbook = xls.Workbooks.Add();
Worksheet CrossoverPartsWorksheet = xls.Worksheets[1];
// Hide our excel object if it's visible.
xls.Visible = false;
// Turn off screen updating so our export will process more quickly.
xls.ScreenUpdating = false;
CrossoverPartsWorksheet.Name = "Crossover";
if (CrossoverStatement != string.Empty)
{
CrossoverPartsWorksheet.Select();
var xlsheet = CrossoverPartsWorksheet.ListObjects.AddEx(SourceType: XlListObjectSourceType.xlSrcExternal,
Source: ODBCConnection,
Destination: xls.Range["$A$1"]).QueryTable;
xlsheet.CommandText = CrossoverStatement;
xlsheet.RowNumbers = false;
xlsheet.FillAdjacentFormulas = false;
xlsheet.PreserveColumnInfo = true;
xlsheet.PreserveFormatting = true;
xlsheet.RefreshOnFileOpen = false;
xlsheet.BackgroundQuery = false;
xlsheet.SavePassword = false;
xlsheet.AdjustColumnWidth = true;
xlsheet.RefreshPeriod = 0;
xlsheet.RefreshStyle = XlCellInsertionMode.xlInsertEntireRows;
xlsheet.Refresh(false);
xlsheet.ListObject.ShowAutoFilter = false;
xlsheet.ListObject.TableStyle = "TableStyleMedium16";
// Unlink our table from the server and convert to a range.
xlsheet.ListObject.Unlink();
// Freeze our column headers.
xls.Application.Rows["2:2"].Select();
xls.ActiveWindow.FreezePanes = true;
xls.ActiveWindow.DisplayGridlines = false;
// Autofit our rows and columns.
xls.Application.Cells.EntireColumn.AutoFit();
xls.Application.Cells.EntireRow.AutoFit();
// Select the first cell in the worksheet.
xls.Application.Range["$A$2"].Select();
// Turn off alerts to prevent asking for 'overwrite existing' and 'save changes' messages.
xls.DisplayAlerts = false;
}
// Make our excel application visible
xls.Visible = true;
// Release our resources.
Marshal.ReleaseComObject(Workbook);
Marshal.ReleaseComObject(CrossoverPartsWorksheet);
Marshal.ReleaseComObject(xls);
}
这是我能提出的完成这个请求的最好方法。执行参数化查询并将结果数据传递给一个表,您可以在调用Export(datatable)时使用该表 问题解决了
public static void Export(System.Data.DataTable CrossoverDataTable)
{
// @@Parameters
//
// CrossoverDataTable DataTable :
// This is a data table containing information to be exported to our excel application.
// Requested as a way to circumvent sql injection opposed to the initial overload accepting only a string .commandtext.
Application xls = new Application();
xls.SheetsInNewWorkbook = 1;
// Create our new excel application and add our workbooks/worksheets
Workbook Workbook = xls.Workbooks.Add();
Worksheet CrossoverPartsWorksheet = xls.Worksheets[1];
// Hide our excel object if it's visible.
xls.Visible = false;
// Turn off screen updating so our export will process more quickly.
xls.ScreenUpdating = false;
// Turn off calculations if set to automatic; this can help prevent memory leaks.
xls.Calculation = xls.Calculation == XlCalculation.xlCalculationAutomatic ? XlCalculation.xlCalculationManual : XlCalculation.xlCalculationManual;
// Create an excel table and fill it will our query table.
CrossoverPartsWorksheet.Name = "Crossover Data";
CrossoverPartsWorksheet.Select();
{
// Create a row with our column headers.
for (int column = 0; column < CrossoverDataTable.Columns.Count; column++)
{
CrossoverPartsWorksheet.Cells[1, column + 1] = CrossoverDataTable.Columns[column].ColumnName;
}
// Export our datatable information to excel.
for (int row = 0; row < CrossoverDataTable.Rows.Count; row++)
{
for (int column = 0; column < CrossoverDataTable.Columns.Count; column++)
{
CrossoverPartsWorksheet.Cells[row + 2, column + 1] = (CrossoverDataTable.Rows[row][column].ToString());
}
}
}
// Freeze our column headers.
xls.Application.Rows["2:2"].Select();
xls.ActiveWindow.FreezePanes = true;
xls.ActiveWindow.DisplayGridlines = false;
// Autofit our rows and columns.
xls.Application.Cells.EntireColumn.AutoFit();
xls.Application.Cells.EntireRow.AutoFit();
// Select the first cell in the worksheet.
xls.Application.Range["$A$2"].Select();
// Turn off alerts to prevent asking for 'overwrite existing' and 'save changes' messages.
xls.DisplayAlerts = false;
// ******************************************************************************************************************
// This section is commented out for now but can be enabled later to have excel sheets show on screen after creation.
// ******************************************************************************************************************
// Make our excel application visible
xls.Visible = true;
// Turn screen updating back on
xls.ScreenUpdating = true;
// Turn automatic calulation back on
xls.Calculation = XlCalculation.xlCalculationAutomatic;
// Release our resources.
Marshal.ReleaseComObject(Workbook);
Marshal.ReleaseComObject(CrossoverPartsWorksheet);
Marshal.ReleaseComObject(xls);
}
公共静态无效导出(System.Data.DataTable CrossoverDataTable)
{
//参数
//
//CrossOverdata数据表:
//这是一个数据表,包含要导出到excel应用程序的信息。
//请求作为绕过sql注入的一种方式,而不是仅接受string.commandtext的初始重载。
应用程序xls=新应用程序();
xls.wWorkbook=1;
//创建新的excel应用程序并添加工作簿/工作表
工作簿=xls.Workbooks.Add();
工作表CrossoverPartsWorksheet=xls.工作表[1];
//如果excel对象可见,则隐藏它。
xls.Visible=false;
//关闭屏幕更新,以便我们的导出过程更快。
xls.screenUpdate=false;
//如果设置为“自动”,则关闭计算;这有助于防止内存泄漏。
xls.Calculation=xls.Calculation==XlCalculation.XlCalculation自动?XlCalculation.XlCalculation手动:XlCalculation.XlCalculation手动;
//创建一个excel表,并将其填充到我们的查询表中。
CrossoverPartsWorksheet.Name=“交叉数据”;
CrossoverPartsWorksheet.Select();
{
//用列标题创建一行。
for(int column=0;column
能否使用非SQL语法从文档中获取所需信息?您能否将信息存储在真实的数据库中而不是excel文件中(即使是客户端计算机上的本地数据库)?“有效”查询是否足够简单,可以充分限制用户输入数据的有效字符的白名单,即仅字母数字值?我认为不可能使用非SQL语法,因为所有信息都直接来自我们的SQL server数据库。excel文件是必需的,因为它将在用户请求时可用,因此存储信息的数据库在这一点上对我们没有好处。不幸的是,不存在限制输入或输入字符的可能性,因为有效输入是通过数千家具有自己专有命名约定的供应商收集的。通过这些部分,我几乎看到了所有可能的角色\