2012-06-29 00:18:28 -05:00
# -*- coding: binary -*-
2011-03-26 00:33:05 +00:00
# Copyright (c) 2010, patrickHVE@googlemail.com
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * The names of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL patrickHVE@googlemail.com BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2011-03-26 00:45:52 +00:00
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2011-03-26 00:33:05 +00:00
module Rex
module Post
module Meterpreter
module Extensions
module Stdapi
module Railgun
#
# represents one function, e.g. MessageBoxW
#
2017-06-27 18:00:01 -04:00
class LibraryFunction
2011-03-26 00:33:05 +00:00
@@allowed_datatypes = {
2020-12-01 10:42:28 -05:00
'VOID' = > [ 'return' ] ,
'BOOL' = > [ 'in' , 'return' ] ,
'BYTE' = > [ 'in' , 'return' ] ,
'WORD' = > [ 'in' , 'return' ] ,
'DWORD' = > [ 'in' , 'return' ] ,
'LPVOID' = > [ 'in' , 'return' ] , # sf: for specifying a memory address (e.g. VirtualAlloc/HeapAlloc/...) where we don't want to back it up with actual mem ala PBLOB
'ULONG_PTR' = > [ 'in' , 'return' ] ,
'PDWORD' = > [ 'in' , 'out' , 'inout' ] , # todo: support for functions that return pointers to strings
2023-02-01 12:44:39 -06:00
'PULONG_PTR' = > [ 'in' , 'out' , 'inout' , 'return' ] ,
'PWCHAR' = > [ 'in' , 'out' , 'inout' , 'return' ] ,
'PCHAR' = > [ 'in' , 'out' , 'inout' , 'return' ] ,
2020-12-01 10:42:28 -05:00
'PBLOB' = > [ 'in' , 'out' , 'inout' ] ,
2011-03-26 00:33:05 +00:00
} . freeze
2013-08-30 16:28:33 -05:00
2017-06-27 18:00:01 -04:00
@@allowed_convs = [ 'stdcall' , 'cdecl' ]
2013-08-30 16:28:33 -05:00
2017-06-27 18:00:01 -04:00
@@directions = [ 'in' , 'out' , 'inout' , 'return' ] . freeze
2013-08-30 16:28:33 -05:00
2017-04-24 19:54:00 -04:00
attr_reader :return_type , :params , :remote_name , :calling_conv
2013-08-30 16:28:33 -05:00
2017-06-27 18:00:01 -04:00
def initialize ( return_type , params , remote_name , calling_conv = 'stdcall' )
2011-03-26 00:33:05 +00:00
check_return_type ( return_type ) # we do error checking as early as possible so the library is easier to use
check_params ( params )
2013-02-08 19:26:33 +00:00
check_calling_conv ( calling_conv )
2011-03-26 00:33:05 +00:00
@return_type = return_type
@params = params
2017-04-24 19:54:00 -04:00
@remote_name = remote_name
2013-02-08 19:26:33 +00:00
@calling_conv = calling_conv
2011-03-26 00:33:05 +00:00
end
2013-08-30 16:28:33 -05:00
2011-03-26 00:33:05 +00:00
private
2013-08-30 16:28:33 -05:00
2013-02-08 19:26:33 +00:00
def check_calling_conv ( conv )
if not @@allowed_convs . include? ( conv )
raise ArgumentError , " Calling convention unknown: #{ conv } . "
end
2013-02-08 21:02:37 +00:00
end
2013-08-30 16:28:33 -05:00
2020-12-01 10:42:28 -05:00
def check_type_exists ( type )
2011-03-26 00:33:05 +00:00
if not @@allowed_datatypes . has_key? ( type )
raise ArgumentError , " Type unknown: #{ type } . Allowed types: #{ PP . pp ( @@allowed_datatypes . keys , " " ) } "
end
end
2013-08-30 16:28:33 -05:00
2020-12-01 10:42:28 -05:00
def check_return_type ( type )
2011-03-26 00:33:05 +00:00
check_type_exists ( type )
if not @@allowed_datatypes [ type ] . include? ( " return " )
raise ArgumentError , " #{ type } is not allowed as a return type "
end
end
2013-08-30 16:28:33 -05:00
2020-12-01 10:42:28 -05:00
def check_params ( params )
2011-03-26 00:33:05 +00:00
params . each do | param |
2020-12-01 10:42:28 -05:00
raise ArgumentError , " each param must be described by a three-tuple [type,name,direction] " unless param . length == 3
2011-03-26 00:33:05 +00:00
type = param [ 0 ]
direction = param [ 2 ]
2013-08-30 16:28:33 -05:00
2011-03-26 00:33:05 +00:00
# Assert a valid type
check_type_exists ( type )
2013-08-30 16:28:33 -05:00
2011-03-26 00:33:05 +00:00
# Only our set of predefined directions are valid
unless @@directions . include? ( direction )
2011-11-20 11:46:35 +11:00
raise ArgumentError , " invalid direction: #{ direction } "
2011-03-26 00:33:05 +00:00
end
2013-08-30 16:28:33 -05:00
2011-03-26 00:33:05 +00:00
# 'return' is not a valid direction in this context
2020-12-01 10:42:28 -05:00
if direction == " return "
2011-11-20 11:46:35 +11:00
raise " direction 'return' is only for the return value of the function. "
2011-03-26 00:33:05 +00:00
end
end
end
end
end ; end ; end ; end ; end ; end