how mysql treat the hex in sql statement - mysql

I know a little SQL injection about php and i read some posts about sqli!the posts say when hackers want to get the column names of a table,they will use
select group_concat(column_name) from information_schema.columns where table_name=0x7573657273
in the statement "0x7573657273" is the hex string of "users".surprisingly,when I execute this statement in mysql console,it will exactly return the colunms of table users.
does it mean mysql will automately convert hex to string,eg convert "0x7573657273" to "uses" or convert "0x3D" to "="?
does it mean mysql can understand what the hex means?

In string contexts, MySQL treats hexadecimal values as binary strings, where each pair of hex digits is converted to a character.
Refer link: http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-literals.html

Related

How to use AES_ENCRYPT mysql

I want to use AES_ENCRYPT in my query to insert encrypted data.
So i have write below query to convert text into encryption
SELECT AES_ENCRYPT('1234567#','testingpart') |
So i got the below output
0x9427909a49cf68afb9494b25363ab14d
But i want output like 9427909A49CF68AFB9494B25363AB14DRemove 0x and convert all character into capital.
You should use hex function
SELECT HEX(AES_ENCRYPT('1234567#','testingpart'))

Equivalent of MySQL HEX / UNHEX function in SQL Server?

Does SQL Server has an equivalent to the HEX and UNHEX MySQl functions?
Update 2022: This is outdated, read about CONVERT() together with binary types.
What are you going to do?
Something like a script generation?
There might be better approaches for your issue, but you do not provide many details...
Not quite as slim but this would work
--This will show up like needed, but it will not be a string
SELECT CAST('abc' AS VARBINARY(MAX))
--this is the string equivalent
SELECT sys.fn_varbintohexstr(CAST('abc' AS VARBINARY(MAX)));
--This will turn the string equivalent back to varbinary
SELECT sys.fn_cdc_hexstrtobin('0x616263')
--And this will return the original string
SELECT CAST(sys.fn_cdc_hexstrtobin('0x616263') AS VARCHAR(MAX));
###One hint
If you deal with UNICODE you can check the changes if you set N'abc' instead of 'abc' and in den final line you'd have to convert '0x610062006300' to NVARCHAR.
###Another hint
If you need this more often you might put this into an UDF, than it is as eays as with MySQL :-)

Is it possible to insert hexa values in a SQL query?

I have a form that takes names (in natural language) as default argument and compare those to a table of users.
Is it possible to send those names in their hexadecimal value equivalent ? Example: for user "sam" i would send 0x730x610x6d or 0x73616d, but neither of them works.
Any idea ?
p.s: in my target page for this form, i defined 'charset' parameter as "utf8"
Yes it is possible. You can do the below. You can use UNHEX() function to convert a hexstring with hex pairs to binary and HEX() to do the other way round.
HEX method used to convert each characters of the string to hexa decimal number.
UNHEX method used to convert each hexa decimal numbers to characters.
Try this :
INSERT INTO tbl (col) VALUES (UNHEX('4D2AFF'))
and
SELECT HEX(col) FROM tbl

SQL Server finding string in Unicode HEX

I need to find part of string which is stored in a varbinary field on SQL Server 2008. The data that is stored in the field is in HEX Binary.
For example the field has 0xA0000000001A000000000000000000000000000000000000000000000000000000850000002416002F002A002000480065006C006C006F0020002A002F0007
I can ignore the first 37 characters. When I look at this in a SQL application it reads as follows after the first 37 characters
/..H.e.l.l.o../
I know that the hex binary is stored in Unicode format.
My question is how can i search for the word 'Hello' using a SQL Statement.
I tried the following below but i do not get any results
SELECT CONVERT(varchar(max),fieldname) from tablename where fieldname like '%Hello%;
I would really appreciate any help that I can get
Thanks
If the binary value is in a Unicode encoding, you will need to cast it to NVARCHAR, and also use a Unicode string literal for the search value:
SELECT CONVERT(nvarchar(max),fieldname) from tablename where fieldname like N'%Hello%';

MySQL REGEXP not producing expected results (not multi byte safe?). Is there a work around?

I'm trying to write a MySQL query to identify first name fields that actually contain initials. The problem is that the query is picking up records that should not match.
I have tested against the POSIX ERE regex implementation in RegEx Buddy to confirm my regex string is correct, but when running in a MySQL query, the results differ.
For example, the query should identify strings such as:
'A.J.D' or 'A J D'.
But it is also matching strings like 'Ralph' or 'Terrance'.
The query:
SELECT *, firstname REGEXP '^[a-zA-z]{1}(([[:space:]]|\.)+[a-zA-z]{1})+([[:space:]]|\.)?$' FROM test_table
The 'firstname' field here is VARCHAR 255 if that's relevant.
I get the same result when running with a string literal rather than table data:
SELECT 'Ralph' REGEXP '^[a-zA-z]{1}(([[:space:]]|\.)+[a-zA-z]{1})+([[:space:]]|\.)?$'
The MySQL documentation warns about potential issues with REGEXP, I'm unsure if this is related to the problem I'm seeing:
Warning The REGEXP and RLIKE operators work in byte-wise fashion, so
they are not multi-byte safe and may produce unexpected results with
multi-byte character sets. In addition, these operators compare
characters by their byte values and accented characters may not
compare as equal even if a given collation treats them as equal.
Thanks in advance.
If you're testing this in the mysql client, you need to escape the backslashes. Each occurence of \. must turn into \\. This is necessary because your input is first processed by the mysql client, which turns \. into .. So you need to make it keep the backslashes by escaping them.