Prevention of LDAP injection attacks
def search_ldap(query)
conn = Net::LDAP.new( :host => ldap_host, :port => ldap_port, :auth => { :method => :simple, :username => ldap_username, :password => ldap_password })
filter = Net::LDAP::Filter.construct("(&(objectclass=*)(cn=#{query}))")
treebase = "dc=example,dc=com"
conn.search(:base => treebase, :filter => filter)
end
In the above code, the search_ldap method constructs an LDAP filter using untrusted data (the query parameter). The filter is constructed by interpolating the query directly into the filter string. If an attacker can control the
query
parameter, they can potentially modify the LDAP filter in unexpected ways, leading to an LDAP Injection vulnerability.
For example, an attacker could provide a
query
value of
*))(|(cn=*
, which would modify the filter to return all objects in the directory, regardless of their cn attribute. This could lead to unauthorized disclosure of information or manipulation of the LDAP directory.
The vulnerability lies in the line:
filter = Net::LDAP::Filter.construct("(&(objectclass=*)(cn=#{query}))")
query
parameter is directly embedded into the LDAP filter without any sanitization or encoding, allowing potential LDAP Injection attacks.
def search_ldap(query)
conn = Net::LDAP.new( :host => ldap_host, :port => ldap_port, :auth => { :method => :simple, :username => ldap_username, :password => ldap_password })
query = Net::LDAP::Filter.escape(query)
filter = Net::LDAP::Filter.construct("(&(objectclass=*)(cn=#{query}))")
treebase = "dc=example,dc=com"
conn.search(:base => treebase, :filter => filter)
end
The updated code now includes a call to
Net::LDAP::Filter.escape(query)
. This method call is used to sanitize the user input
query
before it is used in the LDAP query. The
Net::LDAP::Filter.escape
method escapes any special characters in the input string that could be used to manipulate the LDAP query.
This is a critical step in preventing LDAP injection attacks, as it ensures that user input cannot modify the structure of the LDAP query. By escaping special characters, we ensure that they are treated as literal characters in the query string, rather than being interpreted as LDAP query syntax.
This is a simple and effective way to prevent LDAP injection attacks, but it should be used in conjunction with other security measures, such as input validation, using parameterized queries or prepared statements, and implementing strong authentication and authorization mechanisms. Regularly updating and patching the LDAP server, monitoring and logging LDAP queries, and educating developers about secure coding practices are also important steps in maintaining a secure system.