- Web Hacking

File download via sql injection

n3015m 2008. 7. 25. 14:05

○ SQL Injection을 통한 파일 다운로드

Null-Based Union을 통한 파일 다운로드

웹 애플리케이션의 파일 다운로드 로직이 구현되어 있고 DB에 경로 정보가 저장되어 있을 경우 union을 통하여 경로 정보를 변경하여 파일 다운로드가 가능하다.

→ Filedownload.php?boardFileID=33 and 1=2 union select null,filepath,null....

    and 1=2는 앞의 질의 결과는 Null로 만들기 위함
    파일 다운로드 로직은 에러를 유발하였을 경우 절대경로 정보가 노출되는 경우가 많음

※ Filedownload.php 소스코드

<?
    $query = "SELECT * FROM board_file WHERE boardFileID=$boardFileID";
    $result = mysql_query($query);
    $row=mysql_fetch_array($result);

    $cutFilename = 22;

    $fileURL = $row[fileURL];
    $fileName = substr($row[fileURL], $cutFilename);


    header("Content-type: file/unknown");
    header("Content-Disposition: attachment; filename=".$fileName);
    header("Content-Description: PHP5 Generated Data");
    header("Pragma: no-cache");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
    header("Pragma: public");

    $fp = fopen("$fileURL", "r");
    if(!fpassthru($fp)) fclose($fp);
    exit;
?>