如果没有用户定义的数据通过它们,在 ASP/VBScript 中 eval 和 execute 仍然是危险的吗?
Is eval and execute still dangerous in ASP/VBScript if no user-defined data get passed through them?
这更像是一个 "ethical" 问题,而不是技术问题。
很明显,如果您允许用户定义的 data/variables 通过它们,那么 Eval()
和 Execute()
是危险的。然而,我一直觉得使用这些功能无论如何都是不受欢迎的,只能作为最后的手段使用。
无论如何,当它可以使编码更高效和动态时,我已经在这里和那里使用它们......但我始终确保我知道通过函数传递的内容是受控的,而不是用户定义的。你会考虑这种糟糕的编码吗?有没有办法即使黑客没有读取由 Request
或 Session
变量或任何其他用户定义的数据定义的任何内容,也可以利用这些功能?
如果有帮助的话,我用这个函数在一定程度上替代了Eval。它允许您指定哪些函数在 Eval 中有效。要使用它,只需调用 Calculate(
expression)
.
'Calculation limitations for use with Calculate function...
Const C_VALID_CALC_CHARS = "0123456789.+-*/()^\=,"
'NOTE: Deliberately broken this const so that it is readable in Whosebug...
Const C_VALID_CALC_FUNCTIONS = " Abs Asc Atn CBool CByte CCur CDate
CDbl Chr CInt CLng Cos CSng CStr Date DateAdd DateDiff DatePart
DateSerial DateValue Day Escape Exp FormatCurrency FormatDateTime
FormatNumber FormatPercent GetLocale Hex Hour InStr InStrRev Int Fix
IsDate IsEmpty IsNull IsNumeric LCase Left Len Log LTrim RTrim Trim
Maths Mid Minute Month MonthName Now Oct Replace Right Rnd Round Second
Sgn Sin Space Split Sqr StrComp String StrReverse Tan Time Timer
TimeSerial TimeValue UCase Unescape Weekday WeekdayName Year "
'Calculate
' Calculates the expression string and returns a value.
'Usage:
' value = Calculate("Sin(43) * Cos(75)")
'Parameters:
' expression (string) - A string value containing the expression to be evaluated.
'Notes:
' This function provides a controlled method for evaluating specific
' functions but has been severly limited to minimise negative effects
' from hacking attempts. A complete list of available functions and
' symbols can be found in the two variables C_VALID_CALC_CHARS and
' C_VALID_CALC_FUNCTIONS.
Function Calculate(expression)
Dim rV
rV = ""
If expression & "" <> "" Then
Dim t, c, v
'Validate first...
t = expression
'Strip out all standard characters...
For c = 1 to Len(C_VALID_CALC_CHARS)
t = Replace(t, Mid(C_VALID_CALC_CHARS, c, 1), " ")
Next 'c
'Strip out multiple spaces...
Do While Instr(t, " ") > 0
t = Replace(t, " ", " ")
Loop
t = Trim(t)
'Check what we're left with...
v = t = ""
If Not v Then
Dim f
f = Split(t, " ")
v = True
For c = 0 To UBound(f, 1)
v = Instr(C_VALID_CALC_FUNCTIONS, f(c)) > 0
Next 'f
End If
'Define the return value...
If v Then
rV = Eval(expression)
Else
rV = "Invalid Expression!"
End If
End If
Calculate = rV
End Function
这可能不是最快的方法,尤其是当您经常使用它时,但您可以将其用作在启动方程式之前验证方程式的一种方式。
我刚才测试过,但如果您有任何问题,请告诉我。
如果您传入 Eval() 或 Execute() 的内容纯粹是您自己的字符串,没有任何输入用户可以影响任何内容,那么它应该是安全的。
然而,这也使得 Eval() 和 Execute() 的很多可能性变得无用。
例如,使用 Eval() 和 Execute() 来创建类似 API 的函数是非常诱人的,用户在查询字符串中调用函数,而您简单地使用 Eval() 代替为每个可能的电话使用大 select...案例。
我还看到它在 CSV 解析中使用,其中列名使用 eval() 映射到记录集列,同样,非常有用,但非常危险,但你已经在你的问题中证明你知道这一点。
如果您绝对确定解析后的代码在您的完全控制之下,那将是非常强大的。
这更像是一个 "ethical" 问题,而不是技术问题。
很明显,如果您允许用户定义的 data/variables 通过它们,那么 Eval()
和 Execute()
是危险的。然而,我一直觉得使用这些功能无论如何都是不受欢迎的,只能作为最后的手段使用。
无论如何,当它可以使编码更高效和动态时,我已经在这里和那里使用它们......但我始终确保我知道通过函数传递的内容是受控的,而不是用户定义的。你会考虑这种糟糕的编码吗?有没有办法即使黑客没有读取由 Request
或 Session
变量或任何其他用户定义的数据定义的任何内容,也可以利用这些功能?
如果有帮助的话,我用这个函数在一定程度上替代了Eval。它允许您指定哪些函数在 Eval 中有效。要使用它,只需调用 Calculate(
expression)
.
'Calculation limitations for use with Calculate function...
Const C_VALID_CALC_CHARS = "0123456789.+-*/()^\=,"
'NOTE: Deliberately broken this const so that it is readable in Whosebug...
Const C_VALID_CALC_FUNCTIONS = " Abs Asc Atn CBool CByte CCur CDate
CDbl Chr CInt CLng Cos CSng CStr Date DateAdd DateDiff DatePart
DateSerial DateValue Day Escape Exp FormatCurrency FormatDateTime
FormatNumber FormatPercent GetLocale Hex Hour InStr InStrRev Int Fix
IsDate IsEmpty IsNull IsNumeric LCase Left Len Log LTrim RTrim Trim
Maths Mid Minute Month MonthName Now Oct Replace Right Rnd Round Second
Sgn Sin Space Split Sqr StrComp String StrReverse Tan Time Timer
TimeSerial TimeValue UCase Unescape Weekday WeekdayName Year "
'Calculate
' Calculates the expression string and returns a value.
'Usage:
' value = Calculate("Sin(43) * Cos(75)")
'Parameters:
' expression (string) - A string value containing the expression to be evaluated.
'Notes:
' This function provides a controlled method for evaluating specific
' functions but has been severly limited to minimise negative effects
' from hacking attempts. A complete list of available functions and
' symbols can be found in the two variables C_VALID_CALC_CHARS and
' C_VALID_CALC_FUNCTIONS.
Function Calculate(expression)
Dim rV
rV = ""
If expression & "" <> "" Then
Dim t, c, v
'Validate first...
t = expression
'Strip out all standard characters...
For c = 1 to Len(C_VALID_CALC_CHARS)
t = Replace(t, Mid(C_VALID_CALC_CHARS, c, 1), " ")
Next 'c
'Strip out multiple spaces...
Do While Instr(t, " ") > 0
t = Replace(t, " ", " ")
Loop
t = Trim(t)
'Check what we're left with...
v = t = ""
If Not v Then
Dim f
f = Split(t, " ")
v = True
For c = 0 To UBound(f, 1)
v = Instr(C_VALID_CALC_FUNCTIONS, f(c)) > 0
Next 'f
End If
'Define the return value...
If v Then
rV = Eval(expression)
Else
rV = "Invalid Expression!"
End If
End If
Calculate = rV
End Function
这可能不是最快的方法,尤其是当您经常使用它时,但您可以将其用作在启动方程式之前验证方程式的一种方式。
我刚才测试过,但如果您有任何问题,请告诉我。
如果您传入 Eval() 或 Execute() 的内容纯粹是您自己的字符串,没有任何输入用户可以影响任何内容,那么它应该是安全的。 然而,这也使得 Eval() 和 Execute() 的很多可能性变得无用。
例如,使用 Eval() 和 Execute() 来创建类似 API 的函数是非常诱人的,用户在查询字符串中调用函数,而您简单地使用 Eval() 代替为每个可能的电话使用大 select...案例。
我还看到它在 CSV 解析中使用,其中列名使用 eval() 映射到记录集列,同样,非常有用,但非常危险,但你已经在你的问题中证明你知道这一点。
如果您绝对确定解析后的代码在您的完全控制之下,那将是非常强大的。