Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts

Monday, November 28, 2016

SQL injection

SQL injection is a technique where malicious users can inject SQL commands into an SQL statement, via web page input.
Injected SQL commands can alter SQL statement and compromise the security of a web application.

txtUserId = getRequestString("UserId");
txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
The original purpose of the code was to create an SQL statement to select a user with a given user id.
If there is nothing to prevent a user from entering "wrong" input, the user can enter some "smart" input like this: UserId:

Server Result: SELECT * FROM Users WHERE UserId = 105 or 1=1
The SQL above is valid. It will return all rows from the table Users, since WHERE 1=1 is always true.

A smart hacker might get access to user names and passwords in a database by simply inserting " or ""=" into the user name or password text box:
User Name:

Password:

The code at the server will create a valid SQL statement like this:
 SELECT * FROM Users WHERE Name ="" or ""="" AND Pass ="" or ""=""
The result SQL is valid. It will return all rows from the table Users, since  
WHERE ""="" is always true.

 SQL Injection Based on Batched SQL Statements 
The following input:
User id:

The code at the server would create a valid SQL statement like this:
 SELECT * FROM Users WHERE UserId = 105; DROP TABLE Suppliers

Parameters for Protection

How to build parameterized queries in some common web languages?INSERT INTO STATEMENT IN PHP:
$stmt = $dbh->prepare("INSERT INTO Customers (CustomerName,Address,City)
VALUES (:nam, :add, :cit)");
$stmt->bindParam(':nam', $txtNam);
$stmt->bindParam(':add', $txtAdd);
$stmt->bindParam(':cit', $txtCit);
$stmt->execute();
-------------------------------------------------
@reference_0

外键约束 Foreign Key Constraints

@reference_1   @reference_2

Sunday, November 27, 2016

insert values (` !== ", ` !== ')

INSERT INTO t_users (`t_username`, `t_password`, `t_name`, `t_email`) VALUES (`qqqqq`, `3bad6af0fa4b8b330d162e19938ee981`, `qqqq`, `qqqq`)
报错:  #1054 - Unknown column 'qqqqq' in 'field list'


 INSERT INTO t_users (`t_username`, `t_password`, `t_name`, `t_email`) VALUES ("qqqqq", "3bad6af0fa4b8b330d162e19938ee981", "qqqq", "qqqq")
或者
INSERT INTO t_users (`t_username`, `t_password`, `t_name`, `t_email`) VALUES ('qqqqq', '3bad6af0fa4b8b330d162e19938ee981', 'qqqq', 'qqqq')
执行成功

总结:插入值引号用"或',不能用` 。(` !== ", ` !== ')

Friday, November 4, 2016

UPDATE table


UPDATE `t_users` SET `t_logintimes`=`t_logintimes`+1,`t_lastip`='127.0.0.1',`t_lasttime`=NOW() WHERE `t_username` = 'user0'

>>>>` != '<<<<

Thursday, November 3, 2016

ALTER TABLE

ALTER TABLE email_list ADD id INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (id)


  (from: Head First PHP and MySQL P211/251)