source: src/nextrand.c @ 1

Last change on this file since 1 was 1, checked in by tmorgan, 12 years ago

moved to dedicated repository

File size: 1.7 KB
Line 
1/*
2Simple tool to generate the next [num] subsequent random numbers using Java
3Random.nextInt(), given any two sequential outputs of this method.
4
5Copyright (C) 2012 Virtual Security Research, LLC
6Author: Dan J. Rosenberg
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License, version 3,
10 as published by the Free Software Foundation.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23
24#define MULTIPLIER 25214903917L
25#define ADDEND 11L
26#define MASK ((1L << 48) - 1)
27
28unsigned long long seed;
29
30int nextInt()
31{
32
33        seed = (seed * MULTIPLIER + ADDEND) & MASK;
34        return (int) (seed >> 16);
35
36}
37
38
39int main(int argc, char **argv)
40{
41
42        int i, num;
43        unsigned long long r1, r2;
44
45
46        if (argc != 4) {
47                printf("[-] Usage: %s rand1 rand2 num\n", argv[0]);
48                return 1;
49        }
50
51        r1 = atoi(argv[1]);
52        r2 = atoi(argv[2]);
53        num = atoi(argv[3]);
54
55        for (i = 0; i < 65536; i++) {
56                seed = (r1 << 16) + i;
57                if ((unsigned int)(((seed * MULTIPLIER + ADDEND) & MASK) >> 16) == (unsigned int)r2) {
58                        break;
59                }
60                seed = 0;
61        }
62
63        if (!seed) {
64                printf("[-] Seed not found.\n");
65                return 1;
66        }
67
68        /* Uncomment to print the first two values, which were already provided */
69//      printf("%d\n", (int)r1);
70//      printf("%d\n", nextInt());
71
72        for (i = 0; i < num; i++) {
73                printf("%d\n", nextInt());
74        }
75
76        return 0;
77
78}
Note: See TracBrowser for help on using the repository browser.