# ldap — ASN.1 BER Directory Binds & AD Recon Search Filters (`ldap`)

> **FrameAssembly v3.2.2** · Protocol 66 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/ldap

- **Full Protocol Name:** Lightweight Directory Access Protocol v3 (ASN.1 BER)
- **Keyword:** `ldap`
- **OSI Layer & Transport:** Layer 7 · TCP 389
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 4511
- **Wireshark Display Filter:** `ldap`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 389 -> LDAPv3`

## Overview

Test Active Directory reconnaissance detections (`filter "(adminCount=1)"`) and anonymous bind alerts over TCP `389` with real ASN.1 BER sequences and zero Domain Controller required.

Lightweight Directory Access Protocol (`ldap`, RFC 4511) queries user accounts, security groups, and organizational units in Microsoft Active Directory and OpenLDAP over **TCP port `389`** (or Global Catalog port `3268`). Unlike HTTP or SMTP, LDAP is a binary **ASN.1 BER** protocol.

FrameAssembly encodes valid ASN.1 BER `LDAPMessage` sequences for `bindreq`, `bindres`, `searchreq`, `searchentry`, `searchdone`, and `unbindreq`—allowing cybersecurity teams to simulate BloodHound/SharpHound-style reconnaissance filters, cleartext simple binds, or `resultcode 49` (`invalidCredentials`) failures without running enumeration tools on a production domain.

## Minimal Quickstart (`ldap_quickstart.frasm`)

```frameassembly
host app_srv { ip 10.10.30.25 }
host dc01    { ip 10.10.0.10 }

compile {
    app_srv -> dc01 tcp syn srcport 51389 dstport 389 wait 1ms
    app_srv <- dc01 tcp syn ack wait 1ms
    app_srv -> dc01 tcp ack wait 1ms
    app_srv -> dc01 ldap bindreq msgid 1 basedn "cn=svc_portal,ou=Services,dc=corp,dc=internal" wait 2ms
    app_srv <- dc01 ldap bindres msgid 1 resultcode 0
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `389` (`psh ack`) and wraps every operation in an ASN.1 BER `SEQUENCE` (`0x30`) with exact definite-length tags.
- Encodes RFC 4511 protocolOp tags (`0x60` BindRequest, `0x61` BindResponse, `0x63` SearchRequest, `0x64` SearchResultEntry, `0x65` SearchResultDone, `0x42` UnbindRequest).
- Encodes LDAP search filters (`filter`) so Wireshark displays the Base DN and filter expression cleanly.

## Practical Lab Example: AD Bind Failure (`49`), Authorized Bind, Privileged Group Search & Unbind (`ldap_ad_search_audit.frasm`)

Captures a rejected bind (`resultcode 49`), a successful bind (`resultcode 0`), an `(adminCount=1)` privileged-account search returning an entry + `searchdone`, and a clean `unbindreq`.

```frameassembly
host jump_ws { ip 172.16.30.44 }
host ad_dc   { ip 172.16.1.10 }

compile {
    jump_ws -> ad_dc tcp syn srcport 52389 dstport 389 win 65535 mss 1460 wait 1ms
    jump_ws <- ad_dc tcp syn ack win 65535 mss 1460 wait 1ms
    jump_ws -> ad_dc tcp ack wait 1ms
    // 1. Failed Simple Bind -> resultcode 49 (invalidCredentials)
    jump_ws -> ad_dc ldap bindreq msgid 1 basedn "cn=backup_ro,dc=corp,dc=internal" wait 2ms
    jump_ws <- ad_dc ldap bindres msgid 1 resultcode 49 wait 5ms
    // 2. Successful Bind -> Search privileged accounts -> Unbind
    jump_ws -> ad_dc ldap bindreq msgid 2 basedn "cn=backup_ro,dc=corp,dc=internal" wait 2ms
    jump_ws <- ad_dc ldap bindres msgid 2 resultcode 0 wait 2ms
    jump_ws -> ad_dc ldap searchreq msgid 3 basedn "dc=corp,dc=internal" filter "(adminCount=1)" wait 3ms
    jump_ws <- ad_dc ldap searchentry msgid 3 basedn "cn=Domain Admins,cn=Users,dc=corp,dc=internal" wait 1ms
    jump_ws <- ad_dc ldap searchdone msgid 3 resultcode 0 wait 2ms
    jump_ws -> ad_dc ldap unbindreq msgid 4
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show ldap_ad_search_audit.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly ldap_ad_search_audit.frasm ldap_ad_search_audit.pcap
frameassembly ldap_ad_search_audit.frasm ldap_ad_search_audit.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse ldap_ad_search_audit.pcap ldap_ad_search_audit_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r ldap_ad_search_audit.pcap
```

## All `ldap` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ldap [msgid <number>] [bindreq | bindres | searchreq | searchentry | searchdone | unbindreq] [basedn "<text>"] [filter "<text>"] [resultcode <number>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `bindreq \| bindres \| searchreq \| searchentry \| searchdone \| unbindreq` | LDAPv3 protocol operation keyword | `searchreq` | Selects the ASN.1 BER `protocolOp` tag inside `LDAPMessage`. |
| `msgid <number>` | LDAP MessageID integer (`1, 2, 3...`) | `1` | Correlates asynchronous LDAP requests (`searchreq`) with replies (`searchentry`, `searchdone`). |
| `basedn "<text>"` | Quoted Distinguished Name (`"dc=corp,dc=internal"`) | `"dc=example,dc=com"` | Sets the Bind DN on `bindreq`, Base Object on `searchreq`, or Object Name on `searchentry`. |
| `filter "<text>"` | Quoted LDAP search filter (`"(objectClass=*)"`, `"(sAMAccountName=jdoe)"`) | `"(objectClass=*)"` | Encodes the filter inside `searchreq` frames. |
| `resultcode <number>` | RFC 4511 LDAPResult code (`0` success, `32` noSuchObject, `49` invalidCredentials, `50` insufficientAccessRights) | `0 (success)` | Sets the `ENUMERATED` result code in `bindres` and `searchdone` replies. |

## Cross-Layer Options Valid with `ldap`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 389` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Match msgid Between Request and Response Pairs

- **Valid v3.2.2 Syntax:** `a -> b ldap searchreq msgid 3 ...
a <- b ldap searchdone msgid 3 resultcode 0`

Increment `msgid` for each new request (`1`, `2`, `3`) and echo the same `msgid` on its `bindres`, `searchentry`, and `searchdone` replies.
