How to mimic rand() function of C in python?

Question:

I am trying to reverse a logic of a C program with python.
Part of the C program is the following :

timeVar = time((time_t *)0x0)
seed = (uint)timeVar;
srand(seed);
random_value1 = rand();
random_value2 = rand();
random_value3 = rand();

There is no upper bound given in rand().

I have the seed used in the operations but i am not really sure how to implement the same operation in python, since random.randint() in python needs a lower and upper bound to work. Also, based on the lower and upper bound, it returns different numbers.

Basically i want to predict the possible values of rand in python while having the seed.

Is there a way to basically convert the C part of the code i supplied you with, in python?

EDIT : The seed is 5a35b162 in hex. The string that got "encrypted" based on some operations was "flag". The result of the encryption was 00f53e12 in hex. I want to reverse the operations upon the encrypted hex string to recover the string "flag". I have been given the seed so that i can predict the random values used in the encryption operations. I hope now its clearer what i want to achieve.

Asked By: bd55

||

Answers:

i want to basically convert the C code to python. The seed i will use is the same. I just want to get the same random numbers as in C

You cannot reliably do this in a broad sense, because the C program will not generate the same random numbers as itself on all machines. "The same random numbers as in C" is not well defined.

Getting the same random numbers as does the C program on the same host is a more approachable problem, but to the best of my knowledge, Python does not provide any built-in interface to the C implementation’s rand(). In particular, random.random() implements a specific algorithm which differs from the one provided by at least some C libraries’ rand(), and os.urandom() provides a randomness source that is more cryptographically secure than C’s rand() typically is (and therefore is different).

The easiest way to generate the same numbers that your C library’s rand() does would be to use C’s rand(). That is, write a native extension by which you can call the C library’s rand(), or use one of the other available mechanisms for accessing native functions.

If that’s not allowed, then to generate the same results that the system’s rand() does, you’ll need to reverse-engineer the system’s rand(). If you need to do that without reviewing its source, then perhaps you can find sufficient information in its documentation. If you need to do the reverse-engineering from scratch, then you probably have a difficult road ahead.

If you need to match the C library’s rand() without using it directly and without reverse-engineering the specific one you’re trying to mimic, then you’re pretty much toast. If this is an academic exercise and that’s what you think it’s asking, then you’ve probably misunderstood the requirements.

Answered By: John Bollinger

Using the ctypes module would let you call rand from libc. Whether this is the same rand as is being used by your program is a different matter (e.g. it might be statically linked to another implemention of rand) but it might work!

from ctypes import CDLL
from ctypes.util import find_library

libc = CDLL(find_library("c"))

libc.srand(0x5a35b162)
print(libc.rand(), libc.rand(), libc.rand())

Note that Python doesn’t know the prototypes of these methods, but given the’re small integer values it likely doesn’t matter much. But have a read of the section on foreign functions if you’re interested.

Answered By: Sam Mason

The rand() function in C generates a random number between 0 and RAND_MAX, which is typically a large number defined in the stdlib.h library. In Python, we can use the random module to generate random numbers. The random module provides several functions to generate random numbers, including randint(), uniform(), and random(). However, to mimic the rand() function of C, we can use the randint() function.

The randint() function in Python generates a random integer between two specified numbers. To mimic the rand() function of C, we can specify the range between 0 and RAND_MAX. We can obtain the value of RAND_MAX in Python by importing the sys module and accessing the maxsize attribute of the maxsize class. The maxsize attribute returns the largest positive integer that can be used as an index for a sequence.

Here is an example code that mimics the rand() function of C in Python using the randint() function:

import random
import sys

# define RAND_MAX
RAND_MAX = sys.maxsize

# generate random number between 0 and RAND_MAX
random_number = random.randint(0, RAND_MAX)

# print random number
print("Random number: ", random_number)

In this code, we first import the random module and the sys module. We define RAND_MAX as the maxsize attribute of the sys.maxsize class, which returns the largest positive integer that can be used as an index for a sequence. We then use the randint() function to generate a random integer between 0 and RAND_MAX, and store the result in the random_number variable. Finally, we print the value of the random_number variable.

The randint() function generates a random integer between the two specified numbers, inclusive. Therefore, the code above generates a random integer between 0 and the largest positive integer that can be used as an index for a sequence in Python, which is equivalent to the range of the rand() function in C.

In summary, to mimic the rand() function of C in Python, we can use the randint() function in the random module, specifying the range between 0 and the largest positive integer that can be used as an index for a sequence in Python, which can be obtained from the sys module.

Answered By: Dhruv Chauhan