Programing

SQL 주입이란 무엇입니까?

lottogame 2020. 9. 3. 23:38
반응형

SQL 주입이란 무엇입니까? [복제]


이 질문에 이미 답변이 있습니다.

누군가 SQL 주입을 설명 할 수 있습니까? 어떻게 취약점을 유발합니까? SQL이 주입되는 지점은 정확히 어디에 있습니까?


누군가 SQL 인 젝턴을 설명 할 수 있습니까?

SQL 삽입은 일부 콘텐츠를 SQL 쿼리 문자열로 보간 할 때 발생하며 결과는 의도하지 않은 방식으로 쿼리 구문을 수정합니다.

악의적 일 필요는 없으며 사고 일 수 있습니다. 그러나 우발적 인 SQL 주입은 취약점보다 오류를 일으킬 가능성이 더 큽니다.

유해한 콘텐츠는 사용자로부터 올 필요가 없으며 애플리케이션이 모든 소스에서 가져 오거나 코드에서 스스로 생성하는 콘텐츠 일 수 있습니다.

어떻게 취약점을 유발합니까?

공격자가 SQL 문자열에 삽입 될 것으로 알고있는 애플리케이션에 값을 보낼 수 있기 때문에 취약점으로 이어질 수 있습니다. 매우 영리하여 쿼리 결과를 조작하거나, 데이터를 읽거나, 허용해서는 안되는 데이터를 변경할 수도 있습니다.

PHP의 예 :

$password = $_POST['password'];
$id = $_POST['id'];
$sql = "UPDATE Accounts SET PASSWORD = '$password' WHERE account_id = $id";

이제 공격자가 POST 요청 매개 변수를 " password=xyzzy"및 " id=account_id"로 설정하여 다음 SQL을 생성 한다고 가정합니다 .

UPDATE Accounts SET PASSWORD = 'xyzzy' WHERE account_id = account_id

내가 $id정수일 것으로 예상했지만 공격자는 열 이름 인 문자열을 선택했습니다. 물론 이제 모든에서 조건이 참 이므로 공격자는 모든 계정에 대해 암호를 설정했습니다 . 이제 공격자는 권한있는 사용자를 포함하여 모든 사람의 계정에 로그인 할 수 있습니다.

SQL이 주입되는 지점은 정확히 어디에 있습니까?

삽입 된 것은 SQL이 아니라 SQL 문자열에 삽입 ( "삽입 된") 된 내용으로, 의도 한 것과 다른 종류의 쿼리를 생성합니다. 동적 콘텐츠를 확인하지 않고 신뢰하고 결과 SQL 쿼리를 맹목적으로 실행했습니다. 그것이 문제가 시작되는 곳입니다.

SQL 주입은 일반적으로 데이터베이스 나 데이터베이스 액세스 라이브러리 또는 프레임 워크가 아닌 애플리케이션 코드의 결함입니다.

쿼리 매개 변수를 사용하면 대부분의 SQL 삽입 사례를 피할 수 있습니다. PHP에서 SQL 삽입을 어떻게 방지 할 수 있습니까?를 참조하십시오 . 예를 들어.


SQL 주입은 응용 프로그램 사용자가 데이터베이스 쿼리의 의미에 영향을 줄 수있을 때 발생합니다. 이것은 종종 사용자 입력의 임의 문자열이 연결되어 데이터베이스에 공급되는 SQL을 생성 할 때 발생합니다. 예를 들어, 사용자 로그인을 처리하는 데 사용할 수있는 다음 코드 (PHP에서는 동일하지만 모든 언어에 적용됨)가 있다고 가정 해 보겠습니다.

$sql = "SELECT  FROM users WHERE username='".$_GET['username']."' AND password='".$_GET['password']."'";

사용자가 다음과 같이 입력하면 피해가 발생합니다.

administrator'; --

... 사용자 이름. 적절한 인코딩이 없으면 쿼리는 다음과 같이됩니다.

SELECT * FROM users WHERE username='administrator'; -- AND password=''

여기서 문제는 사용자 이름의 '가 사용자 이름 필드를 닫은 다음-데이터베이스 서버가 나머지 문자열을 무시하도록하는 SQL 주석을 시작한다는 것입니다. 결과적으로 사용자는 이제 암호를 몰라도 관리자로 로그인 할 수 있습니다. SQL Inection은 UPDATE, DELETE 또는 DROP 쿼리를 실행하고 실제로 데이터베이스를 손상시키는 데 사용할 수도 있습니다.

SQL 주입은 매개 변수화 된 쿼리를 사용하거나 언어 / 툴킷의 이스케이프 함수 (예 : PHP의 mysql_real_escape_string ())를 적용하여 방지 할 수 있습니다.

SQL Injection을 이해하면 이 만화에 대한 농담을 받게 될 것 입니다.


SQL 인젝션은 데이터로 간주되는 것이 SQL 코드로 처리되지 않는 경우입니다.

예를 들어,

mysql_query("SELECT * FROM posts WHERE postid=$postid");

일반적으로 주어진 ID를 가진 게시물을 얻지 만 $postid문자열로 설정되어 있다고 가정 합니다 10; DROP TABLE posts --. 갑자기 보내는 실제 쿼리는

mysql_query("SELECT * FROM posts WHERE postid=10; DROP TABLE posts --");

악의적 인 사용자로 인해 전체 게시물 테이블을 잃게 될 것이기 때문에 이것은 상당히 문제입니다.

이를 방지하는 가장 쉬운 방법은 PDO 또는 MySQLi를 통해 준비된 문을 사용하는 것 입니다.

The equivalent example in PDO would then be

$statement = $db->prepare('SELECT * FROM posts WHERE postid = :postid');
$statement->bindValue(':postid', $postid);
$statement->execute();

Doing this ensures that the database system knows that $postid is to be treated as data and not code, and will thus be handled appropriately.


This question has been answered many times on StackOverflow, but it's an important topic for everyone to know about, so I'm not going to vote to close this question.

Here are links to some of my past answers on this topic:

I also gave a presentation at the MySQL Conference this month, and my slides are online:


SQL injection is where a malicious user will put SQL into input fields to try and run the SQL on your server.

The #1 advice that I adhere to is to use parameterized stored procedures rather than building raw SQL in code.

Stored Procedure parameters don't get executed, making them safe in most cases.


I found this paper to be an extremely good read about SQL injection techniques (link is to PDF): Advanced SQL Injection In SQL Server Applications.

Despite the title saying "Advanced", it's quite readable even if you don't have much knowledge about SQL injection.


To get some general background check out the Wikipedia article on SQL Injection.

In short SQL injection attacks can leave you vulnerable to all manor of database data theft and destruction. The exact details of what can be done to your system depend on the details of the system itself.

Any time you pass input from your users to your database you have a potential injection point. Web applications are often lacking in the this regard, as new programmers often do not understand the risks of handling input from users, and web applications are attacked by very smart people you never thought would find your program.


You will like this article from code project ; )

Summary

  • Encrypt sensitive data.
  • Access the database using an account with the least privileges necessary.
  • Install the database using an account with the least privileges necessary.
  • Ensure that data is valid.
  • Do a code review to check for the possibility of second-order attacks.
  • Use parameterised queries.
  • Use stored procedures.
  • Re-validate data in stored procedures.
  • Ensure that error messages give nothing away about the internal architecture of the application or the database.

The point where SQL is injected is any point that your application accepts input from the user.

Whether this becomes a dangerous vulnerability for your web application depends on whether this input is later used as part of an SQL query without properly checking its type and escaping it if necessary.

Without proper escaping, some SQL code 'injected' by the user could be executed by the SQL engine as SQL code, rather than a simple string or value.

참고URL : https://stackoverflow.com/questions/601300/what-is-sql-injection

반응형