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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
| from pwn import *
strings = "The quick brown fox jumps over the lazy dog" + "\x80" + "\x00" * 0xC + "\x2b" + "\x00" * 0x7
table = [ 0x0, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF6000000000000000000000000000001, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF600000000000000000000000000000B, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF6000000000000000000000000000015, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF600000000000000000000000000001F, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000029, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF6000000000000000000000000000033, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF600000000000000000000000000003D, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000047, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF6000000000000000000000000000051, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF600000000000000000000000000005B, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF6000000000000000000000000000065, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF600000000000000000000000000006F, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000079, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF6000000000000000000000000000083, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF600000000000000000000000000008D, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF6000000000000000000000000000097, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000A1, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000AB, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000B5, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000BF, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000C9, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000D3, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000DD, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000E7, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000F1, 0xF60000000000000000000000000000FB, 0xF60000000000000000000000000000FB, 0xF60000000000000000000000000000FB, 0xF60000000000000000000000000000FB, 0xF60000000000000000000000000000FB, 0xF60000000000000000000000000000FB ]
def calc(low_bytes, high_bytes): a1 = low_bytes a2 = high_bytes v2 = (((((a2 & 0x5555555555555555) + ((a2 >> 1) & 0x5555555555555555)) & 0x3333333333333333) \ + ((((a2 & 0x5555555555555555) + ((a2 >> 1) & 0x5555555555555555)) >> 2) & 0x3333333333333333)) & 0xF0F0F0F0F0F0F0F) \ + ((((((a2 & 0x5555555555555555) + ((a2 >> 1) & 0x5555555555555555)) & 0x3333333333333333) \ + ((((a2 & 0x5555555555555555) + ((a2 >> 1) & 0x5555555555555555)) >> 2) & 0x3333333333333333)) >> 4) & 0xF0F0F0F0F0F0F0F) v2 = v2 & 0xFFFFFFFFFFFFFFFF v3 = ((((v2 & 0xFF00FF00FF00FF) + ((v2 >> 8) & 0xFF00FF00FF00FF)) >> 16) & 0xFFFF0000FFFF) \ + (((v2 & 0xFF00FF00FF00FF) + ((v2 >> 8) & 0xFF00FF00FF00FF)) & 0xFFFF0000FFFF) v3 = v3 & 0xFFFFFFFFFFFFFFFF v4 = (((a1 & 0x5555555555555555) + ((a1 >> 1) & 0x5555555555555555)) & 0x3333333333333333) \ + ((((a1 & 0x5555555555555555) + ((a1 >> 1) & 0x5555555555555555)) >> 2) & 0x3333333333333333) v4 = v4 & 0xFFFFFFFFFFFFFFFF v5 = (v4 & 0xF0F0F0F0F0F0F0F) + ((v4 >> 4) & 0xF0F0F0F0F0F0F0F) v5 = v5 & 0xFFFFFFFFFFFFFFFF
return (v3 & 0xFFFFFFFF) \ + (((v5 & 0xFF00FF) + ((v5 >> 8) & 0xFF00FF)) & 0xFFFF) \ + (((v5 & 0xFF00FF) + ((v5 >> 8) & 0xFF00FF)) >> 16) \ + (((((v5 & 0xFF00FF00FF00FF) + ((v5 >> 8) & 0xFF00FF00FF00FF)) & 0xFFFF0000FFFF) \ + ((((v5 & 0xFF00FF00FF00FF) + ((v5 >> 8) & 0xFF00FF00FF00FF)) >> 16) & 0xFFFF0000FFFF)) >> 32) \ + (v3 >> 32)
def process(depth, high_bytes, low_bytes): res = [0 for i in range(depth + 1)] count = [0 for i in range(depth + 1)] for i in range(1, depth + 1): if i == 1: res[i] = calc(high_bytes, low_bytes) count[i] = 0 elif i == 2: res[i] = calc(high_bytes ^ 1, low_bytes) count[i] = 1 else: val_1 = res[i - 1] val_2 = res[i - 2] count[i] = (count[i - 1] + count[i - 2]) & 0xFFFFFFFFFFFFFFFF tmp = calc(high_bytes ^ (count[i]), low_bytes) tmp += val_1 + val_2 tmp &= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF while tmp > table[i]: tmp = tmp - table[i] res[i] = tmp print("set {long long}($rsp + 0xb0)= " + hex(count[depth])) return res[depth]
v27 = 0x9C8A210019B32312 v28 = 0x342D41A3B6410FF for i in range(0, len(strings), 0x10): print("loop " + str(i // 0x10 + 1) + ":")
value = process(0x100, u64(strings[i:i+8]), u64(strings[i+8:i+0x10])) high = value >> 64 low = value & 0xFFFFFFFFFFFFFFFF print("set {long long}($rsp + 0xa0) = " + hex(low)) print("set {long long}($rsp + 0xa8) = " + hex(high)) print
v27 ^= low + u64(strings[i:i+8]) v28 ^= high + u64(strings[i+8:i+0x10])
|