Skip to content

Instantly share code, notes, and snippets.

@limhoff-r7
Created July 15, 2014 14:29
Show Gist options
  • Save limhoff-r7/bf16ce48b298f82559ad to your computer and use it in GitHub Desktop.
Save limhoff-r7/bf16ce48b298f82559ad to your computer and use it in GitHub Desktop.
Postgres INET contains (<<) operator search using MetasploitDataModels::IPAddress::CIDR
attribute = Mdm::Host.arel_table[:address]
value = MetasploitDataModels::IPAddress::V4::CIDR.new(value: '1.2.3.4/8')
formatted_value = "#{value.address}/#{value.prefix_length}"
cast_argument = Arel::Nodes::As.new(formatted_value, Arel::Nodes::SqlLiteral.new('INET'))
# @see https://gist.github.com/mrpunkin/1996454
value_as_inet = Arel::Nodes::NamedFunction.new('CAST', [cast_argument])
operation = Arel::Nodes::InfixOperation.new(
'<<',
attribute,
value_as_inet
)
operation.to_sql # "\"hosts\".\"address\" << CAST('1.2.3.4/8' AS INET)"
@limhoff-r7
Copy link
Author

I had to read AREL's source to figure out how to do AS on a literal string, but thanks to @mrpunkin for how to use CAST in AREL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment