1. 程式人生 > 實用技巧 >Apache目錄詳解

Apache目錄詳解

Apache的主要目錄和配置檔案理解

參考連結:http://httpd.apache.org/docs/2.4/misc/security_tips.html

一、Apache主要配置檔案註釋(演示)

Apache的主配置檔案位置:/etc/httpd/conf/httpd.conf,預設站點的主目錄:/var/www/html/

官方文件的第一句便告訴我們:The location of this file is set at compile-time, but may be overridden with the -f command line flag。And配置檔案的修改只有在重啟伺服器之後才會重新生效,當然htaccess

可以在臨時目錄下更改一些配置,這些是檔案上傳常常有的漏洞位置


基礎配置區

  • ServerRoot "/www/server/apache" 指定用於指定Apache的執行目錄,後面的所有相當路徑都是以此

  • mutex互斥鎖,程序除錯的一個概念,不會直接跳過

  • Listen 0.0.0.0:80 appache的監聽的ip和埠,可以防止阿帕奇監聽太多


模組區

  • <IfModule http2_module>
    ProtocolsHonorOrder On
    Protocols h2 http/1.1
    </IfModule>
    

    作用:檢測模組是否能夠使用,如果不能使用則會直接恢復期間的所有資料

    使用地點:當且僅當你的模組必須要配置檔案才能啟動時,才使用section。module可以是檔名

    可以在htaccess中使用

  • LoadModule

    LoadModule authn_file_module modules/mod_authn_file.so
    將這個動態載入庫載入進來並取名為module
    

    不可在htaccess中使用

    loads the named module from the modules subdirectory of the ServerRoot.

    漏洞:題目中可能在此處載入一些其他的直譯器進行繞過對PHP的限制(詳情:西湖挑戰杯)

    幾個重要模組:

    rewrite_module modules/mod_rewrite.so

    偽靜態模組,重寫模組,其中的原來我不是很明白,但是有兩點作用
    1.開啟URL匹配的規則,進行重定向,在URL無法解析的,執行其他的規則
    2.AllowOverride配合,實現過載部分配置的作用,使htacess生效
    
    原理解析:AllowOverride引數就是指明Apache伺服器是否去找.htacess檔案作為配置檔案,如果設定為none,那麼伺服器將忽略.htacess檔案,如果設定為All,那麼所有在.htaccess檔案裡有的指令都將被重寫。對於AllowOverride,還可以對它指定如下一些能被重寫的指令型別. 
    通常利用Apache的rewrite模組對 URL 進行重寫的時候, rewrite規則會寫在 .htaccess 檔案裡。但要使 apache 能夠正常的讀取.htaccess 檔案的內容,就必須對.htaccess 所在目錄進行配置。從安全性考慮,根目錄的AllowOverride屬性一般都配置成不允許任何Override
    

Scope of Directives

放在主配置檔案(行為對根目錄的限制),放在這個型別中的(對指定目錄下許可權限制)

  • <Directory>

    <Directory />     #行為對根目錄的限制
        Options FollowSymLinks   # followsymlinks表示允許使用符號連結,預設為禁用
        AllowOverride None     # 表示禁止使用者對目錄配置檔案(.htaccess進行修改)過載,普通站點不建議開啟
    </Directory>
    
    <Directory "/var/www/html">#行為僅對/var/www/html目錄生效
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    

    options中Indexes表示當網頁不存在的時候允許索引顯示目錄中的檔案,FollowSymLinks是否允許訪問符號連結檔案。有的選項有ExecCGI表是否使用CGI,如Options Includes ExecCGI FollowSymLinks表示允許伺服器執行CGI及SSI,禁止列出目錄。SymLinksOwnerMatch表示當符號連結的檔案和目標檔案為同一使用者擁有時才允許訪問。

    AllowOverrideNone表示不允許這個目錄下的訪問控制檔案來改變這裡的配置,這也意味著不用檢視這個目錄下的訪問控制檔案,修改為:AllowOverride All 表示允許.htaccess。Order對頁面的訪問控制順序後面的一項是預設選項,如allow,deny則預設是deny,Allowfromall表示允許所有的使用者,通過和上一項結合可以控制對網站的訪問控制。具體引數參考如下:

    Apply directive AllowOverride None (disabling .htaccess files).
    Apply directive AllowOverride FileInfo (for directory /home).
    Apply any FileInfo directives in /home/.htaccess, /home/web/.htaccess and /home/web/dir/.htaccess in that order.
    
    • AllowOverride 不多說了(使htaccess(注意不只是這個檔案)中的哪些配置生效)對於不同的配置設定,在htaccess檔案中生效的配置,Only available in sections

      針對於他的三種配置結果有很多的配置生效問題是需要考慮
      1.ALL 不用多說
      2.None 直接就沒有讀取htaccess
      3.directive-type
       3.1 AuthConfig
       3.2 FileInfo
       3.3 Indexes
       3.4 Limit
       3.5 Nonfatal=[Override|Unknown|All]
       3.6 Options
      
      下面是無腦配置:
      1.只要支援這個配置項就能生效的配置(擷取)
      <Files>,<FilesMatch> 匹配檔案
      LimitRequestBody 限制請求的大小 LimitXMLRequestBody
      LuaHookLog 看到這個就要想到該死LUA繞過disfunc
      2.AuthConfig(提供一些使用者名稱,密碼的檢測機制)
      Anonymous_* 檢測後面的*型別是不是*型別 檢測關鍵字
      

      有用的配置內容(懂的都懂)

      FileInfo(對伺服器提供的響應和元資料進行廣泛的控制)

      Action(為特定處理程式或內容型別啟用CGI指令碼)

      eg:Action image/gif /cgi-bin/images.cgi
      用iamges.cgi來處理image/GIF檔案
      AddHandler my-file-type .xyz(用my來處理.xyz檔案)
      Action my-file-type "/cgi-bin/program.cgi"(用program.cgi來付給my)
      SetHandler news-handler
      Action news-handler "/cgi-bin/news.cgi" virtual
      

      AddCharset(為特定檔案設定編碼方式)

      預設的編碼方式:AddDefaultCharset

      這個可以用UTF-7等特殊的編碼方式來繞過對於PHP檔案的內容進行查殺的SHELL進行繞過 AddEncoding(為檔案增加encode方式)

      AddEncoding x-gzip .gz
      AddEncoding x-compress .Z
      這裡的x-很快就被取消掉了,直接使用GZIP和compress的壓縮方式就可以了。
      

      **AddHandler和SetHandler **( handler

      這個會和PHP的執行不同可能不能採取,看是否是handler模式執行
      Set:強制所有匹配的檔案由處理程式處理
      <Location "/status">
        SetHandler server-status
      </Location> http://servername/status was called. 必須放在主配置中
      其他的引數就不再多說了,注意檢視上面的handler中的內容
      Add:將副檔名對映到指定的處理程式
      AddHandler cgi-script .cgi 這裡的cgi-script可以通過上面的action
      

      **AddType和ForceType **(將給定的檔名副檔名對映到指定的內容型別)

      注意和上面的區別

      CGIMapExtension(設定CGI指令碼的直譯器)

      PassEnv和SetEnv和SetEnvIfExpr(環境變數的問題)

      PASS 是指從shell傳遞環境變數
      Set 是指設定一個環境變數(設定一個內部環境變數,該變數隨後可用於Apache HTTP Server模組,並傳遞給CGI指令碼和SSI頁面)這個設定的時間是比較晚的,如果要早設定,用SetEnvIf.
      SetEnv SPECIAL_PATH /foo/bin
      Unset 可以刪除
      SetEnvIfExpr 通過一個表示式設定環境變數,字串
      ENV就是環境變數的意識,其他的就是增加了一些條件判斷
      

      Rewrite

      Indexes(控制伺服器提供的目錄索引頁面包括自動索引生成)

      Limit(授權命令,限制訪問)

      Options(訪問Option和類似的指令以及控制過濾器)

      FilterChain(設定過濾器鏈)

      SSLOptions(SSL連結控制器)

      Options(配置特定目錄中可用的特性)

      1.All
      2.ExecCGI(使用cgi指令碼)
      3.FollowSymLinks
      就是允許你的網頁資料夾下的連結檔案連結到首頁目錄以外的檔案。舉例來說,如果你把首頁目錄設定為/var/www/html,那麼你的網頁程式最多隻能訪問到/var/www/html目錄,上層目錄是不可見的。但是你可以通過連結把檔案連結到/var/www/html目錄以外的檔案以訪問該檔案,如果FollowSymLinks被設定的話
      Even though the server follows the symlink it does not change the pathname used to match against <Directory> sections.
      The FollowSymLinks and SymLinksIfOwnerMatch Options work only in <Directory> sections or .htaccess files
      4.Includes
      Server-side includes provided by mod_include are permitted.
      開啟伺服器端的包含
      eg:
      AddType text/html .shtml
      AddOutputFilter INCLUDES .shtml
      Options +Includes
      5.IncludesNOEXEC
      Server-side includes are permitted, but the #exec cmd and #exec cgi are disabled. It is still possible to #include virtual CGI scripts from ScriptAliased directories.
      伺服器端包括是允許的,但#exec cmd和#exec cgi是禁用的。禁用系統命令
      6.Indexes(在目錄下沒有index.html時是否顯示目錄結構)
      7.MultiViews
      Content negotiated "MultiViews" are allowed using mod_negotiation.
      Note
      This option gets ignored if set anywhere other than <Directory>, as mod_negotiation needs real resources to compare against and evaluate from.
      
      8.SymLinksIfOwnerMatch
      The server will only follow symbolic links for which the target file or directory is owned by the same user id as the link.
      Note
      The FollowSymLinks and SymLinksIfOwnerMatch Options work only in <Directory> sections or .htaccess files.
      
      This option should not be considered a security restriction, since symlink testing is subject to race conditions that make it circumventable.
      
      Normally, if multiple Options could apply to a directory, then the most specific one is used and others are ignored; the options are not merged. (See how sections are merged.) However if all the options on the Options directive are preceded by a + or - symbol, the options are merged. Any options preceded by a + are added to the options currently in force, and any options preceded by a - are removed from the options currently in force.
      
      Note
      Mixing Options with a + or - with those without is not valid syntax and will be rejected during server startup by the syntax check with an abort.
      
      For example, without any + and - symbols:
      
      <Directory "/web/docs">
        Options Indexes FollowSymLinks
      </Directory>
      
      <Directory "/web/docs/spec">
        Options Includes
      </Directory>
      then only Includes will be set for the /web/docs/spec directory. However if the second Options directive uses the + and - symbols:
      
      <Directory "/web/docs">
        Options Indexes FollowSymLinks
      </Directory>
      
      <Directory "/web/docs/spec">
        Options +Includes -Indexes
      </Directory>
      then the options FollowSymLinks and Includes are set for the /web/docs/spec directory.
      
      Note
      Using -IncludesNOEXEC or -Includes disables server-side includes completely regardless of the previous setting.
      
      The default in the absence of any other settings is FollowSymlinks.
      
  • <DirectoryMatch "^/var/www/combined/(?<sitename>[^/]+)">
        Require ldap-group cn=%{env:MATCH_SITENAME},ou=combined,o=Example
    </DirectoryMatch>
    

    In order to prevent confusion, numbered (unnamed) backreferences are ignored. Use named groups instead.

    和上面的標籤一樣,都不能在htaccess中使用

  • 對檔案的單獨操作,可以在htaccess中使用,並且在htaccess載入完後才會載入,加個match也是一樣的效果。

  • 匹配URL,加個match也是一樣的,在file載入完後才會載入

  • 可以使用虛擬主機上下文中允許的任何指令。當伺服器接收到對特定虛擬主機上的文件的請求時,它使用包含在<VirtualHost>部分中的配置指令。
    
    <VirtualHost 10.1.2.3:80>
      ServerAdmin [email protected]
      DocumentRoot "/www/docs/host.example.com"
      ServerName host.example.com
      ErrorLog "logs/host.example.com-error_log"
      TransferLog "logs/host.example.com-access_log"#這個日誌檔案如果是別人可以寫入的,那就需要重新衡量一下這個安全等級了。
    </VirtualHost>
    

    一些都是正常的配置命名,不用細說的命令。


.htaccess檔案

參考連結:1.http://httpd.apache.org/docs/2.4/howto/htaccess.html

2.http://httpd.apache.org/docs/2.4/mod/overrides.html

  • AccessFileName

    AccessFileName .acl
    

    Before returning the document /usr/local/web/index.html, the server will read /.acl, /usr/.acl, /usr/local/.acl and /usr/local/web/.acl for directives unless they have been disabled with

    Name of the distributed configuration file(分散式的指令碼配置檔案)可以在htaccess中使用
    

    可以繞過對於htaccess檔案的限制

參考連結:

1.核心功能:http://httpd.apache.org/docs/2.4/mod/core.html

2.mod中的mod_mime:https://www.jb51.net/tools/onlinetools/apache-chs/mod/mod_mime.html

二、Apache的語法規則

SetEnv SPECIAL_PATH /foo/bin
這是設定環境變數
  • AcceptPathInfo

    Off(當且僅當URL指向真實存在的地址時返回正確內容,如果有PATHINFO就返回404)
    A request will only be accepted if it maps to a literal path that exists. Therefore a request with trailing pathname information after the true filename such as /test/here.html/more in the above example will return a 404 NOT FOUND error.
    On(類比與THinkPHP來考慮)
    A request will be accepted if a leading path component maps to a file that exists. The above example /test/here.html/more will be accepted if /test/here.html maps to a valid file.
    Default
    The treatment of requests with trailing pathname information is determined by the handler responsible for the request. The core handler for normal files defaults to rejecting PATH_INFO requests. Handlers that serve scripts, such as cgi-script and isapi-handler, generally accept PATH_INFO by default.
    
    PathInfo模式:PATH_INFO的方式的URL也是從協議開始,然後後面跟上域名,域名後面是入口檔案,入口檔案後依次為模組,控制器,方法;然後是依次傳入的引數
    

    AllowEncodedSlashes(作用:是否允許%2Ffor / and additionally %5C for \ )經常和他同時使用

  • AddDefaultCharset

    設定頁面的預設編碼
    可以在htaccess中使用
    

    漏洞:配合上面這點,配合我們的輸入內容,可以構建一個XSS攻擊。

    同類型的有:AddCharset可以對不同字尾名的檔案實現編碼

    AddCharset EUC-JP .euc
    AddCharset ISO-2022-JP .jis
    AddCharset SHIFT_JIS .sjis
    

三、Module mod_mime

這是一個關於對於content-typelanguage解析的模組,這個模組在老版本的apache中經常出現解析問題,但是經過這麼多年了,很多的漏洞都被修復了,但是其自帶的一些天然的解析特點依然可以被我們用在繞過WAF的過程中,這樣或許可以達到事半功倍的效果,這是我把這一部分單獨拿出來的原因,廢話不多說,我們進入正題。

Files with Multiple Extensions

當一個檔案具有多個字尾名的時候,如果兩個字尾名的含義是不一樣的,指向的是不同型別的處理器,這樣不會留下什麼隱患,比如word.html.enword.en.html這兩者都將被解析成為media-type text/htmlContent-Language: en

但是當這樣的檔名出現的時候就會出現問題world.php.jpg。這個檔案最後會被解析成為一個php檔案,原因是,apache的模組在解析的時候,是從右向左的,後面的結果會覆蓋後面的結果。所以當我們在對於不同的字尾名指定直譯器的時候,就應該使用sethandler,而不是AddHandler

Content encoding

Content-encoding: pkzip

解碼格式

AddType

AddType image/gif .gif
AddType image/jpeg jpeg jpg jpe

MultiviewsMatch

這個配置的兩個選項,一個允許多重字尾名,一個不允許多重字尾名。

MultiviewsMatch Any|NegotiatedOnly|Filters|Handlers [Handlers|Filters]

當其設定為handlersfilters這樣就可以匹配直譯器來運行了。