-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathGet-FuzzedInt.ps1
172 lines (163 loc) · 5.9 KB
/
Get-FuzzedInt.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
Add-Type @"
public class Shift {
public static int Right(int x, int count) { return x >> count; }
public static uint Right(uint x, int count) { return x >> count; }
public static long Right(long x, int count) { return x >> count; }
public static ulong Right(ulong x, int count) { return x >> count; }
public static int Left(int x, int count) { return x << count; }
public static uint Left(uint x, int count) { return x << count; }
public static long Left(long x, int count) { return x << count; }
public static ulong Left(ulong x, int count) { return x << count; }
}
"@
# [Byte] 1 or 0
function Return-Bool {
$(Get-Random -Maximum 10000)%2
}
# -128 to 127
# [sbyte] type
function Return-Int8 {
$RandSwitch = $(Get-Random -Maximum 10000)%10
$FuzzInt = switch ($RandSwitch) {
0 {-128}
1 {127}
2 {-1}
3 {1}
4 {0}
5 {0xA}
6 {0xF}
7 {0x41}
8 {$(Get-Random -Minimum -128 -Maximum 0)}
9 {$(Get-Random -Minimum 0 -Maximum 127)}
} [sbyte]"0x$("{0:X}" -f [sbyte]$FuzzInt)"
}
# -32768 to 32767
# 0xFFFF8000 to 0x7FFF
function Return-Int16 {
$RandSwitch = $(Get-Random -Maximum 10000)%11
$FuzzInt = switch ($RandSwitch) {
0 {-32768}
1 {32767}
2 {-1}
3 {1}
4 {0}
5 {[Shift]::Left(8,$((Get-Random -Maximum 10000)%9))} # 8 -> 0x1000
6 {[Shift]::Left(0x100,$((Get-Random -Maximum 10000)%6))} # 0x100 -> 0x4000
7 {$(Get-Random -Minimum -32768 -Maximum 32767)}
8 {$(Get-Random -Minimum 0x7f00 -Maximum 0x7FFF)}
9 {$(Get-Random -Minimum -32768 -Maximum -16384)}
10 {$(Get-Random -Minimum -16384 -Maximum 0)}
} [Int16]"0x$("{0:X}" -f [Int16]$FuzzInt)"
}
# -2147483648 to 2147483647
# 0x80000000 to 0x7FFFFFFF
function Return-Int32 {
$RandSwitch = $(Get-Random -Maximum 10000)%14
$FuzzInt = switch ($RandSwitch) {
0 {-2147483648}
1 {2147483647}
2 {-1}
3 {1}
4 {0}
5 {0x7fff0000}
6 {0x7fffe000}
7 {0x7fffffff - 0x1000}
8 {[Shift]::Left(8,$((Get-Random -Maximum 10000)%9))} # 8 -> 0x1000
9 {[Shift]::Left(0x100,$((Get-Random -Maximum 10000)%19))} # 0x100 -> 0x8000000
10 {$(Get-Random -Minimum -2147483648 -Maximum 2147483647)}
11 {$(Get-Random -Minimum 0x7f000000 -Maximum 0x7FFFFFFF)}
12 {$(Get-Random -Minimum -2147483648 -Maximum -1073741824)}
13 {$(Get-Random -Minimum -1073741824 -Maximum 0)}
} [Int32]"0x$("{0:X}" -f [Int32]$FuzzInt)"
}
# -9223372036854775808 to 9223372036854775807
# 0x8000000000000000 to 0x7FFFFFFFFFFFFFFF
function Return-Int64 {
$RandSwitch = $(Get-Random -Maximum 10000)%11
$FuzzInt = switch ($RandSwitch) {
0 {-9223372036854775808}
1 {9223372036854775807}
2 {-1}
3 {1}
4 {0}
5 {[Shift]::Left(8,$((Get-Random -Maximum 10000)%9))} # 8 -> 0x1000
6 {[Shift]::Left(0x100,$((Get-Random -Maximum 10000)%23))} # 0x100 -> 0x80000000
7 {$(Get-Random -Minimum -9223372036854775808 -Maximum 9223372036854775807)}
8 {$(Get-Random -Minimum 0x7f00000000000000 -Maximum 0x7FFFFFFFFFFFFFFF)}
9 {$(Get-Random -Minimum -9223372036854775808 -Maximum -4611686018427387904)}
10 {$(Get-Random -Minimum -4611686018427387904 -Maximum 0)}
} [Int64]"0x$("{0:X}" -f [Int64]$FuzzInt)"
}
# 0 to 255
# [byte] type
function Return-UInt8 {
$RandSwitch = $(Get-Random -Maximum 10000)%8
$FuzzInt = switch ($RandSwitch) {
0 {0}
1 {255}
2 {1}
3 {0xA}
4 {0xF}
5 {0x41}
6 {$(Get-Random -Minimum 0 -Maximum 127)}
7 {$(Get-Random -Minimum 127 -Maximum 255)}
} [byte]"0x$("{0:X}" -f [byte]$FuzzInt)"
}
# 0 to 65535
# 0x0 to 0xFFFF
function Return-UInt16 {
$RandSwitch = $(Get-Random -Maximum 10000)%9
$FuzzInt = switch ($RandSwitch) {
0 {65535}
1 {0}
2 {1}
3 {[Shift]::Left(8,$((Get-Random -Maximum 10000)%9))} # 8 -> 0x1000
4 {[Shift]::Left(0x100,$((Get-Random -Maximum 10000)%7))} # 0x100 -> 0x8000
5 {0xF000 + ([Shift]::Left(0xF,$((Get-Random -Maximum 10000)%8)))} # 0xF000 -> 0xFF00
6 {0xF000 + ([Shift]::Left(0xFF,$((Get-Random -Maximum 10000)%4)))} # 0xF000 -> 0xFFF0
7 {$(Get-Random -Minimum 0 -Maximum 32767)}
8 {$(Get-Random -Minimum 32767 -Maximum 65535)}
} [UInt16]"0x$("{0:X}" -f $FuzzInt)"
}
# 0 to 4294967295
# 0x0 to 0xFFFFFFFF
function Return-UInt32 {
$RandSwitch = $(Get-Random -Maximum 10000)%9
$FuzzInt = switch ($RandSwitch) {
0 {4294967295}
1 {0}
2 {1}
3 {[Shift]::Left(8,$((Get-Random -Maximum 10000)%9))} # 8 -> 0x1000
4 {[Shift]::Left(0x100,$((Get-Random -Maximum 10000)%19))} # 0x100 -> 0x8000000
5 {0xFF000000 + ([Shift]::Left(0xFF,$((Get-Random -Maximum 10000)%16)))} # 0xFF000000 -> 0xFFFF0000
6 {0xFF000000 + ([Shift]::Left(0xFFFF,$((Get-Random -Maximum 10000)%8)))} # 0xFF000000 -> 0xFFFFFF00
7 {$(Get-Random -Minimum 0 -Maximum 2147483647)}
8 {$(Get-Random -Minimum 2147483647 -Maximum 4294967295)}
}
if ($FuzzInt.GetType().Name -eq "Double") {
[UInt32]$FuzzInt = [math]::floor($FuzzInt)
} [UInt32]"0x$("{0:X}" -f $FuzzInt)"
}
# 0 to 18446744073709551615
# 0x0 to 0xFFFFFFFFFFFFFFFF
function Return-UInt64 {
$RandSwitch = $(Get-Random -Maximum 10000)%9
$FuzzInt = switch ($RandSwitch) {
0 {18446744073709551615}
1 {0}
2 {1}
3 {[Shift]::Left(8,$((Get-Random -Maximum 10000)%9))} # 8 -> 0x1000
4 {[Shift]::Left(0x100,$((Get-Random -Maximum 10000)%19))} # 0x100 -> 0x8000000
5 {0xFFFF000000000000 + ([Shift]::Left(0xFFFF,$((Get-Random -Maximum 10000)%16)))} # 0xFFFF000000000000 -> 0xFFFEFFFFFFFF0000
6 {0xFFFF000000000000 + ([Shift]::Left(0xFFFFFF,$((Get-Random -Maximum 10000)%8)))} # 0xFFFF000000000000 -> 0xFFFEFFFFFFFFFF00
7 {$(Get-Random -Minimum 0 -Maximum 9223372036854775807)}
8 {$(Get-Random -Minimum 9223372036854775807 -Maximum 18446744073709551615)}
}
if ($FuzzInt -Like "*E+*") {
[Uint64]"$FuzzInt"
} elseif ($FuzzInt -ge 10000000000000000000) {
[Uint64]"0x$("{0:X}" -f [Uint64]$FuzzInt)"
} else {
[UInt64]"0x$("{0:X}" -f $FuzzInt)"
}
}