fixed sql injection & added a proper escaper that works like

addslashes() in php
This commit is contained in:
noother 2010-12-05 06:56:10 +01:00 committed by GreYFoXGTi
parent dd849d3915
commit e990c94dad
2 changed files with 26 additions and 42 deletions

View file

@ -20,7 +20,6 @@ CSqlScore::CSqlScore(CGameContext *pGameServer)
m_Port(g_Config.m_SvSqlPort) m_Port(g_Config.m_SvSqlPort)
{ {
str_copy(m_aMap, g_Config.m_SvMap, sizeof(m_aMap)); str_copy(m_aMap, g_Config.m_SvMap, sizeof(m_aMap));
ClearString(m_aMap);
NormalizeMapname(m_aMap); NormalizeMapname(m_aMap);
if(gs_SqlLock == 0) if(gs_SqlLock == 0)
@ -333,6 +332,8 @@ void CSqlScore::ShowRankThread(void *pUser)
try try
{ {
// check strings // check strings
char originalName[MAX_NAME_LENGTH];
strcpy(originalName,pData->m_aName);
pData->m_pSqlData->ClearString(pData->m_aName); pData->m_pSqlData->ClearString(pData->m_aName);
// check sort methode // check sort methode
@ -349,7 +350,7 @@ void CSqlScore::ShowRankThread(void *pUser)
if(pData->m_pSqlData->m_pResults->rowsCount() != 1) if(pData->m_pSqlData->m_pResults->rowsCount() != 1)
{ {
str_format(aBuf, sizeof(aBuf), "%s is not ranked", pData->m_aName); str_format(aBuf, sizeof(aBuf), "%s is not ranked", originalName);
pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf); pData->m_pSqlData->GameServer()->SendChatTarget(pData->m_ClientID, aBuf);
} }
else else
@ -473,47 +474,30 @@ void CSqlScore::ShowTop5(int ClientID, int Debut)
} }
// anti SQL injection // anti SQL injection
void CSqlScore::ClearString(char *pString) void CSqlScore::ClearString(char *pString)
{ {
// replace ' ' ' with ' \' ' and remove '\' char newString[MAX_NAME_LENGTH*2-1];
for(int i = 0; i < str_length(pString); i++) int pos = 0;
{
// replace '-' with '_'
if(pString[i] == '-')
pString[i] = '_';
if(pString[i] == '\'') for(int i=0;i<str_length(pString);i++) {
{ if(pString[i] == '\\') {
// count \ before the ' newString[pos++] = '\\';
int SlashCount = 0; newString[pos++] = '\\';
for(int j = i-1; j >= 0; j--) } else if(pString[i] == '\'') {
{ newString[pos++] = '\\';
if(pString[i] != '\\') newString[pos++] = '\'';
break; } else if(pString[i] == '"') {
newString[pos++] = '\\';
SlashCount++; newString[pos++] = '"';
} } else {
newString[pos++] = pString[i];
if(SlashCount % 2 == 0)
{
for(int j = str_length(pString)-1; j > i; j--)
{
pString[j] = pString[j-1];
}
pString[i] = '\\';
i++;
}
} }
} }
// aaand remove spaces and \ at the end xD newString[pos] = '\0';
for(int i = str_length(pString)-1; i >= 0; i--)
{ strcpy(pString,newString);
if(pString[i] == ' ' || pString[i] == '\\')
pString[i] = '\0';
else
break;
}
} }
void CSqlScore::NormalizeMapname(char *pString) { void CSqlScore::NormalizeMapname(char *pString) {

View file

@ -64,9 +64,9 @@ struct CSqlScoreData
CSqlScore *m_pSqlData; CSqlScore *m_pSqlData;
int m_ClientID; int m_ClientID;
#if defined(CONF_FAMILY_WINDOWS) #if defined(CONF_FAMILY_WINDOWS)
char m_aName[16]; char m_aName[47];
#else #else
char m_aName[MAX_NAME_LENGTH]; char m_aName[MAX_NAME_LENGTH*2-1];
#endif #endif
float m_Time; float m_Time;