Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve deleted objects from Active Directory with Ruby?

From the research I've done, it appears I need to send a special OID with my request (1.2.840.113556.1.4.417) in order to access the Deleted Objects container.

I couldn't find a way to send a specific control with a request using the "net-ldap" gem. Does anyone know if this is possible?

There is another gem, ruby-ldap, which appears to be more flexible and it seems I can send controls with my request (e.g. using the search_ext2() method).

However, no matter what I try, I am not getting back any objects, even though I know they haven't been garbage collected yet.

I'm including the filter "isDeleted=TRUE" with my requests as well.

like image 303
up_the_irons Avatar asked Jun 03 '15 09:06

up_the_irons


People also ask

How do I recover a deleted object in Active Directory?

Restoring deleted objects using the AD Administrative Center. Open the Active Directory Administrative Center from the Start menu. In the left pane, click the domain name and select the Deleted Objects container under it. Select the deleted object, and click the Restore button in the right pane.

Where do deleted users go in Active Directory?

Navigate to Reports > Active Directory > User Management > Recently deleted users.


1 Answers

OK, I finally figured it out. One will need to use the ruby-ldap gem. The reason my controls were not being sent was because the LDAP Protocol Version (LDAP::LDAP_OPT_PROTOCOL_VERSION) had defaulted to v2 and apparently it must be v3.

The following is a snippet that works:

require 'ldap'

conn = LDAP::Conn.new('yourserver.example.com', 389)
conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)
conn.bind("CN=Administrator,CN=Users,DC=example,DC=com", "sekritpass")

# controlType: 1.2.840.113556.1.4.417 (LDAP_SERVER_SHOW_DELETED_OID)
control = LDAP::Control.new('1.2.840.113556.1.4.417')

conn.search_ext2('CN=Deleted Objects,DC=example,DC=com', LDAP::LDAP_SCOPE_SUBTREE, "(isDeleted=*)", nil, false, [control], nil)

The filter (isDeleted=*) isn't necessarily required, you could also simply use (objectClass=*). You can also use the scope LDAP::LDAP_SCOPE_ONELEVEL if desired.

like image 88
up_the_irons Avatar answered Oct 24 '22 00:10

up_the_irons