将字符串mysql_real_escape与冲刺组合
Combine mysql_real_escape string with sprintf
也许我太愚蠢了,无法再次搜索。
无论如何,情况是这样的。
为了防止SQL注入,我需要使用mysql_real_escape_string
,但是这个函数非常笨重,需要"大量"额外的代码。我想将函数保留在本质上sprintf
函数的包装下。
这个想法:每当sprintf
遇到%s
时,它都会在相应的va_arg上运行mysql_real_escape_string
,然后将其添加到目标字符串中。
例:
doQuery("SELECT * FROM `table` WHERE name LIKE '%%s%%';", input);
假设input
是一个类似于Tom's diner
的字符串,完整的查询将如下所示:
SELECT * FROM `table` WHERE name LIKE '%Tom's diner%';
我找到了一种相当优雅的方式来实现我想要的东西,但是存在与之相关的安全风险,我想知道是否有更好的方法。
这是我正在尝试的:
void doQuery(const char *Format, ...) {
char sQuery[1024], tQuery[1024], *pQuery = sQuery, *pTemp = tQuery;
va_list val;
strcpy(sQuery, Format);
while((pQuery = strchr(pQuery, ''')) != NULL) *pQuery = 1;
va_start(val, Format);
vsprintf(tQuery, sQuery, val);
va_end(val);
pQuery = sQuery;
do {
if(*pTemp == 1) {
char *pSearch = strchr(pTemp, 1);
if(!pSearch) return; //Error, missing second placeholder
else {
*pQuery++ = ''';
mysql_real_escape_string(sql, pQuery, pTemp, pSearch - pTemp);
pQuery += strlen(pQuery);
*pQuery++ = ''';
pTemp = pSearch;
}
} else *pQuery++ = *pTemp;
} while(*pTemp++);
//Execute query, return result, etc.
}
这个函数是从内存中写出来的,我不是 100% 确定它的正确性,但我想你明白了。无论如何,明显的安全风险在于占位符 1。如果攻击者想到将所述 1(数值,而不是字符"1")放入输入字符串中,他将自动拥有一个攻击点,即非转义撇号。
现在,有没有人知道,我如何解决这个问题并仍然获得我想要的行为,最好不要为我要发送到数据库的每个字符串分配额外的缓冲区?如果可能的话,我还想避免覆盖整个 sprintf 函数。
谢谢。
在思考了一会儿问题之后,我相信已经找到了一个相当简单的答案,这将很好地达到目的。
我只需要计算我用占位符替换的撇号的出现次数,然后在解析格式化字符串时向后计数。如果我发现占位符的频率超过我在第一次传递时计数的频率,我将知道其中一个参数包含非法字符,因此无效,不应传递到数据库。
编辑:太晚了,但我认为现在(当我再次偶然发现相同的问题时)我找到了一种好方法。笨重,但可行。
bool SQL::vQuery(const char *Format, va_list val) {
bool Ret = true, bExpanded = false;
if(strchr(Format, '%') != NULL) { //Is there any expanding to be done here?
int32_t ReqLen = vsnprintf(NULL, 0, Format, val) + 1; //Determine the required buffer length.
if(ReqLen < 2) Ret = false; //Lengthquery successful?
else {
char *Exp = new char[ReqLen]; //Evaluation requires a sufficiently large buffer.
bExpanded = true; //Tell the footer of this function to free the query buffer.
vsprintf(Exp, Format, val); //Expand the string into the first buffer.
if(strchr(Format, ''') == NULL) Format = Exp; //No apostrophes found in the format(!) string? No escaping necessary.
else if(strchr(Exp, 1)) Ret = false; //Illegal character detected. Abort.
else {
char *pExp = Exp,
*Query = new char[ReqLen * 2], //Reserves (far more than) enough space for escaping.
*pQuery = Query;
strcpy(Query, Format); //Copy the format string to the (modifiable) Query buffer.
while((pQuery = strchr(pQuery, ''')) != NULL)
*pQuery = 1; //Replace the character with the control character.
vsprintf(Exp, Query, val); //Expand the whole thing AGAIN, this time with the substitutions.
pQuery = Query; //And rewind the pointer.
while(char *pEnd = strchr(pExp, 1)) { //Look for the text-delimiter.
*pEnd = 0; //Terminate the string at this point.
strcpy(pQuery, pExp); //Copy the unmodified string to the final buffer.
pQuery += pEnd - pExp; //And advance the pointer to the new end.
pExp = ++pEnd; //Beginning of the 'To be escaped' string.
if((pEnd = strchr(pExp, 1)) != NULL) { //And what about the end?
*pEnd = 0; //Terminate the string at this point.
*pQuery++ = ''';
pQuery += mysql_real_escape_string(pSQL, pQuery, pExp, pEnd - pExp);
*pQuery++ = ''';
pExp = ++pEnd; //End of the 'To be escaped' string.
} else Ret = false; //Malformed query string.
}
strcpy(pQuery, pExp); //No more '? Just copy the rest.
Format = Query; //And please use the Query-Buffer instead of the raw Format.
delete[] Exp; //Get rid of the expansion buffer.
}
}
}
if(Ret) {
if(result) mysql_free_result(result); //Gibt ein ggf. bereits vorhandenes Ergebnis wieder frei.
Ret = mysql_query(pSQL, Format, result);
columns = (result) ? mysql_num_fields(result) : 0;
row = NULL;
}
if(bExpanded) delete[] Format; //Query completed -> Dispose of buffer.
return Ret;
}
这个怪物所做的是以下步骤:
- 确定是否有任何格式要完成。(如果没有任何论据,浪费周期在扩展和东西上没有多大用处)
- 确定所需的长度(包括 0)和错误检查。
- 为一次完全扩展保留足够的空间,设置标记并扩展。
- 检查是否有任何预期的"要转义的字符串"(用"标记)
- 检查控制字符 1 是否在扩展中的某个位置。如果是这样,您将知道这是一次未遂攻击,并且可以在那里拒绝它。
- 将撇号的每个匹配项替换为您选择的控制字符。(不得作为参数传递的那个)
- 使用更改的格式字符串再次展开查询。
- 查找控制字符作为字符串的分隔符,并将整个内容复制到最终缓冲区中,然后将其传递给 mysql
- 释放过期的动态记忆。
我想我终于对这个解决方案感到满意了...我只花了 1.5 年就弄清楚了。:D
我希望这也对其他人有所帮助。
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 模板元编程:如何将参数包组合成新的参数包
- 检查向量是否具有所有可能的字符组合
- 如何在加密++中将两个源组合成新的源
- 根中的组合
- 更改 C++ 中的组合分类变量
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 组合字符串不适用于 libCurl,C++
- 测试两个类型列表中的所有组合
- 将多个 for 循环组合成单个迭代器
- 如何从组合指数中找到仓位
- 防止组合框被关闭
- 将字符串mysql_real_escape与冲刺组合