Skip to content

Commit 51ce19b

Browse files
committed
Agent: Improve the speed of bit flipping code
- Remove a function call - Use a generator - Use a more efficient flip calculation (subtraction instead of xor) Issue #2123
1 parent 2edaf52 commit 51ce19b

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Changelog](https://keepachangelog.com/en/1.0.0/).
5555
- `/api/auth` endpoint to `/api/authenticate`. #2105
5656
- `/api/registration` endpoint to `/api/register`. #2105
5757
- `/api/file-upload` endpoit to `/api/pba/upload`. #2154
58+
- Improved the speed of ransomware encryption by 2-3x. #2123
5859

5960
### Removed
6061
- VSFTPD exploiter. #1533
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
def flip_bits(data: bytes) -> bytes:
2-
flipped_bits = bytearray(len(data))
1+
from typing import Iterable
2+
33

4-
for i, byte in enumerate(data):
5-
# TODO: The function call to flip_bits_in_single_byte() adds significant
6-
# overhead. While python is supposed to "inline" function calls
7-
# like this, I've yet to see any changes in runtime that indicate
8-
# this optimization is actually happening.
9-
#
10-
# The value of breaking this into separate functions is the unit
11-
# test that tests all possible bytes (0-255). This gives us
12-
# confidence that our bit-flip operation is correct.
13-
#
14-
# Remove the flip_bits_in_single_byte() function and rework the
15-
# unit tests so that we still have a high-degree of confidence
16-
# that this code is correct.
17-
#
18-
# EDIT: I believe PyPy will attempt to inline functions
19-
# automatically. I don't know that CPython makes any such
20-
# optimizations.
21-
flipped_bits[i] = flip_bits_in_single_byte(byte)
4+
def generate_flipped_bits(data: bytes) -> Iterable[int]:
5+
"""
6+
Yield bytes with the bits flipped
227
23-
return bytes(flipped_bits)
8+
:param data: The data whose bits to flip
9+
"""
10+
for byte in data:
11+
yield 255 - byte
2412

2513

26-
def flip_bits_in_single_byte(byte) -> int:
27-
# TODO: The operation `255 - byte` appears to be 12% faster than 255 ^ byte.
28-
# Switch the operator and thoroughly test the ransomware payload to
29-
# ensure this doesn't introduce any defects.
30-
return 255 ^ byte
14+
def flip_bits(data: bytes) -> bytes:
15+
"""
16+
Flip all bits in the given bytes
17+
18+
:param data: The bytes whose bits to flip
19+
:return: Bytes with the bits flipped
20+
"""
21+
return bytes(generate_flipped_bits(data))

monkey/tests/unit_tests/infection_monkey/utils/test_bit_manipulators.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
from infection_monkey.utils import bit_manipulators
2+
from infection_monkey.utils.bit_manipulators import flip_bits
3+
4+
5+
def test_flip_all_bits():
6+
input_bytes = bytes(
7+
b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
8+
b"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
9+
b"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
10+
b"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
11+
b"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
12+
b"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
13+
b"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
14+
b"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
15+
b"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
16+
b"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
17+
b"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
18+
b"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
19+
b"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
20+
b"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
21+
b"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
22+
b"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
23+
)
224

25+
expected_bytes = bytes(reversed(input_bytes))
326

4-
def test_flip_bits_in_single_byte():
5-
for i in range(0, 256):
6-
assert bit_manipulators.flip_bits_in_single_byte(i) == (255 - i)
27+
assert flip_bits(input_bytes) == expected_bytes
728

829

930
def test_flip_bits():

0 commit comments

Comments
 (0)