Raised This Month: $51 Target: $400
 12% 

Threaded SQL prepared statements?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-15-2014 , 07:39   Threaded SQL prepared statements?
Reply With Quote #1

Is the equivalent of
PHP Code:
$sql 'SELECT * FROM `users` WHERE `username`=?';
     
    
/* Prepare statement */
    
$stmt $conn->prepare($sql);
    if(
$stmt === false) {
      
trigger_error('Wrong SQL: ' $sql ' Error: ' $conn->errno ' ' $conn->errorE_USER_ERROR);
    }

     
    
/* Bind parameters. Types: s = string, i = integer, d = double,  b = blob */
    
$stmt->bind_param('s'$player);
     
    
/* Execute statement */
    
$stmt->execute();

    
$res $stmt->get_result();
    
$row $res->fetch_assoc(); 
possible in amxmodx? I'm building a website - server link and I can't have people registering on the site with query breaking names(or even worse, injections), then logging in ingame and mess things up
This be my current auth
Code:
public client_connect(id)     {     new nume[32], szTemp[512]     get_user_name(id, nume, 31)     new Data[1]     Data[0] = id     format(szTemp,charsmax(szTemp),"SELECT `password`,`admlevel` FROM `accounts` WHERE (`auth` = '%s')", nume)     SQL_ThreadQuery(g_SqlTuple,"register_client",szTemp,Data,1) } public register_client(FailState,Handle:Query,Error[],Errcode,Data[],DataSize) {     if(FailState == TQUERY_CONNECT_FAILED)     {         log_amx("Load - Could not connect to SQL database.  [%d] %s", Errcode, Error)     }     else if(FailState == TQUERY_QUERY_FAILED)     {         log_amx("Load Query failed. [%d] %s", Errcode, Error)     }     new id     id = Data[0]     if(SQL_NumResults(Query) < 1)     {         show_menu(id,0)     }     else     {         SQL_ReadResult(Query, 0, password, 31)         admlevel = SQL_ReadResult(Query, 1)         get_user_info(id, "_r", savedpassword, 31)         if(equal(password,savedpassword))         {             flagbits = read_flags ( admranks[admlevel] )             set_user_flags(id, flagbits)         }         else         {             show_menu(id,1)         }     }     SQL_FreeHandle(Query)     return PLUGIN_HANDLED }

I really don't want to get to character filtering
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.

Last edited by aron9forever; 11-15-2014 at 07:39.
aron9forever is offline
GordonFreeman (RU)
Veteran Member
Join Date: Jan 2010
Location: Uzbekistan
Old 11-15-2014 , 07:53   Re: Threaded SQL prepared statements?
Reply With Quote #2

You can use this function from AMXBans:
Code:
/*********    mysql escape functions     ************/ mysql_escape_string(dest[],len) {     //copy(dest, len, source);     replace_all(dest,len,"\\","\\\\");     replace_all(dest,len,"\0","\\0");     replace_all(dest,len,"\n","\\n");     replace_all(dest,len,"\r","\\r");     replace_all(dest,len,"\x1a","\Z");     replace_all(dest,len,"'","\'");     replace_all(dest,len,"^"","\^""); }
This will make string safe for sql query. Don't forget about buffer size. You need *3 buffer size for filtred string.
__________________
The functional way is the right way
GordonFreeman (RU) is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-15-2014 , 07:57   Re: Threaded SQL prepared statements?
Reply With Quote #3

Quote:
Originally Posted by GordonFreeman (RU) View Post
You can use this function from AMXBans:
Code:
/*********    mysql escape functions     ************/ mysql_escape_string(dest[],len) {     //copy(dest, len, source);     replace_all(dest,len,"\\","\\\\");     replace_all(dest,len,"\0","\\0");     replace_all(dest,len,"\n","\\n");     replace_all(dest,len,"\r","\\r");     replace_all(dest,len,"\x1a","\Z");     replace_all(dest,len,"'","\'");     replace_all(dest,len,"^"","\^""); }
This will make string safe for sql query. Don't forget about buffer size. You need *3 buffer size for filtred string.
so by adding \ to mark it as a literal string im safe?
i doubt it mon
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
GordonFreeman (RU)
Veteran Member
Join Date: Jan 2010
Location: Uzbekistan
Old 11-15-2014 , 08:06   Re: Threaded SQL prepared statements?
Reply With Quote #4

Learn about SQL injections and SQL syntax.
For example, some l33th4x0r enter hisqlinject');DROP TABLE `accounts`;, your plugin will query following:
SELECT `password`,`admlevel` FROM `accounts` WHERE (`auth` = 'hisqlinject');
DROP TABLE `accounts`;
');
__________________
The functional way is the right way
GordonFreeman (RU) is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-15-2014 , 08:40   Re: Threaded SQL prepared statements?
Reply With Quote #5

Quote:
Originally Posted by GordonFreeman (RU) View Post
Learn about SQL injections and SQL syntax.
For example, some l33th4x0r enter hisqlinject');DROP TABLE `accounts`;, your plugin will query following:
SELECT `password`,`admlevel` FROM `accounts` WHERE (`auth` = 'hisqlinject');
DROP TABLE `accounts`;
');
and how exactly would your function filter this? you don't need `` to define a table
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
YamiKaitou
Has a lovely bunch of coconuts
Join Date: Apr 2006
Location: Texas
Old 11-15-2014 , 10:39   Re: Threaded SQL prepared statements?
Reply With Quote #6

Quote:
Originally Posted by aron9forever View Post
and how exactly would your function filter this? you don't need `` to define a table
Because your query will then look like this (and it will not drop the table) and will most likely cause an error.
Code:
SELECT `password`,`admlevel` FROM `accounts` WHERE (`auth` = 'hisqlinject\');DROP TABLE `accounts`;');
It will basically search for "hisqlinject\');DROP TABLE `accounts`;" as the value of auth.


If you are trying to prevent SQLInjections, please learn how they are performed first.



But to address your original question, no you cannot use Prepared Statements with SQLx
__________________
ProjectYami Laboratories

I do not browse the forums regularly anymore. If you need me for anything (asking questions or anything else), then PM me (be descriptive in your PM, message containing only a link to a thread will be ignored).
YamiKaitou is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-15-2014 , 12:42   Re: Threaded SQL prepared statements?
Reply With Quote #7

Quote:
Originally Posted by YamiKaitou View Post
If you are trying to prevent SQLInjections, please learn how they are performed first
leading people on the right path since 2006

but seriously, I didn't know you can real escape strings in a mysql syntax, im sorry
that will have to work I guess
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 01-27-2015 , 01:32   Re: Threaded SQL prepared statements?
Reply With Quote #8

I'm back, forgot about this crap.
The above function doesn't seem to work(but I'm just probably using it wrong)

Here's the implementation
PHP Code:
format(szTemp,charsmax(szTemp),"UPDATE `orewest` SET minutes='%d' WHERE `auth` = '%s';",iMinutes[id], mysql_escape_string(nume,charsmax(nume))) 
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-27-2015 , 04:29   Re: Threaded SQL prepared statements?
Reply With Quote #9

mysql_escape_string() doesn't return a string...

Call it before, and use "nume" in format().
__________________
Arkshine is offline
Lycode
Junior Member
Join Date: Aug 2014
Old 01-27-2015 , 05:56   Re: Threaded SQL prepared statements?
Reply With Quote #10

Quote:
Originally Posted by GordonFreeman (RU) View Post
You can use this function from AMXBans:
Don't forget about buffer size. You need *3 buffer size for filtred string.
Can you elaborate on that? What do you mean by "3 times buffer size"?
__________________
-This too shall pass
Lycode is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:45.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode