Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/auxiliary/crand.rb
Views: 11784
module Msf12###3#4# This module provides a complete port of the libc rand() and srand() functions.5# It is used by the NETGEAR WNR2000v5 auxiliary and exploit modules, but might6# be useful for any other module that needs to emulate C's random number generator.7#8# Author: Pedro Ribeiro ([email protected]) / Agile Information Security9#10###11module Auxiliary::CRand1213attr_accessor :randtbl14attr_accessor :unsafe_state1516####################17# ported from https://git.uclibc.org/uClibc/tree/libc/stdlib/random.c18# and https://git.uclibc.org/uClibc/tree/libc/stdlib/random_r.c1920TYPE_3 = 321BREAK_3 = 12822DEG_3 = 3123SEP_3 = 32425def initialize(info = {})26super2728@randtbl =29[30# we omit TYPE_3 from here, not needed31-1726662223, 379960547, 1735697613, 1040273694, 1313901226,321627687941, -179304937, -2073333483, 1780058412, -1989503057,33-615974602, 344556628, 939512070, -1249116260, 1507946756,34-812545463, 154635395, 1388815473, -1926676823, 525320961,35-1009028674, 968117788, -123449607, 1284210865, 435012392,36-2017506339, -911064859, -370259173, 1132637927, 1398500161,37-205601318,38]3940@unsafe_state = {41"fptr" => SEP_3,42"rptr" => 0,43"state" => 0,44"rand_type" => TYPE_3,45"rand_deg" => DEG_3,46"rand_sep" => SEP_3,47"end_ptr" => DEG_348}49end5051# Emulate the behaviour of C's srand52def srandom_r (seed)53state = @randtbl54if seed == 055seed = 156end57state[0] = seed5859dst = 060word = seed61kc = DEG_362for i in 1..(kc-1)63hi = word / 12777364lo = word % 12777365word = 16807 * lo - 2836 * hi66if (word < 0)67word += 214748364768end69dst += 170state[dst] = word71end7273@unsafe_state['fptr'] = @unsafe_state['rand_sep']74@unsafe_state['rptr'] = 07576kc *= 1077kc -= 178while (kc >= 0)79random_r80kc -= 181end82end8384# Emulate the behaviour of C's rand85def random_r86buf = @unsafe_state87state = buf['state']8889fptr = buf['fptr']90rptr = buf['rptr']91end_ptr = buf['end_ptr']92val = @randtbl[fptr] += @randtbl[rptr]9394result = (val >> 1) & 0x7fffffff95fptr += 196if (fptr >= end_ptr)97fptr = state98rptr += 199else100rptr += 1101if (rptr >= end_ptr)102rptr = state103end104end105buf['fptr'] = fptr106buf['rptr'] = rptr107108result109end110111end112end113114115