import
sys
import
random
from
pexpect
import
run
from
pipes
import
quote
def
get_bytes(filename):
f
=
open
(filename,
"rb"
).read()
return
bytearray(f)
def
bit_flip(data):
num_of_flips
=
int
((
len
(data)
-
4
)
*
0.01
)
indexes
=
range
(
4
, (
len
(data)
-
4
))
chosen_indexes
=
[]
counter
=
0
while
counter < num_of_flips:
chosen_indexes.append(random.choice(indexes))
counter
+
=
1
for
x
in
chosen_indexes:
current
=
data[x]
current
=
(
bin
(current).replace(
"0b"
,""))
current
=
"0"
*
(
8
-
len
(current))
+
current
indexes
=
range
(
0
,
8
)
picked_index
=
random.choice(indexes)
new_number
=
[]
for
i
in
current:
new_number.append(i)
if
new_number[picked_index]
=
=
"1"
:
new_number[picked_index]
=
"0"
else
:
new_number[picked_index]
=
"1"
current
=
''
for
i
in
new_number:
current
+
=
i
current
=
int
(current,
2
)
data[x]
=
current
return
data
def
magic(data):
magic_vals
=
[
(
1
,
255
),
(
1
,
255
),
(
1
,
127
),
(
1
,
0
),
(
2
,
255
),
(
2
,
0
),
(
4
,
255
),
(
4
,
0
),
(
4
,
128
),
(
4
,
64
),
(
4
,
127
)
]
picked_magic
=
random.choice(magic_vals)
length
=
len
(data)
-
8
index
=
range
(
0
, length)
picked_index
=
random.choice(index)
if
picked_magic[
0
]
=
=
1
:
if
picked_magic[
1
]
=
=
255
:
data[picked_index]
=
255
elif
picked_magic[
1
]
=
=
127
:
data[picked_index]
=
127
elif
picked_magic[
1
]
=
=
0
:
data[picked_index]
=
0
elif
picked_magic[
0
]
=
=
2
:
if
picked_magic[
1
]
=
=
255
:
data[picked_index]
=
255
data[picked_index
+
1
]
=
255
elif
picked_magic[
1
]
=
=
0
:
data[picked_index]
=
0
data[picked_index
+
1
]
=
0
elif
picked_magic[
0
]
=
=
4
:
if
picked_magic[
1
]
=
=
255
:
data[picked_index]
=
255
data[picked_index
+
1
]
=
255
data[picked_index
+
2
]
=
255
data[picked_index
+
3
]
=
255
elif
picked_magic[
1
]
=
=
0
:
data[picked_index]
=
0
data[picked_index
+
1
]
=
0
data[picked_index
+
2
]
=
0
data[picked_index
+
3
]
=
0
elif
picked_magic[
1
]
=
=
128
:
data[picked_index]
=
128
data[picked_index
+
1
]
=
0
data[picked_index
+
2
]
=
0
data[picked_index
+
3
]
=
0
elif
picked_magic[
1
]
=
=
64
:
data[picked_index]
=
64
data[picked_index
+
1
]
=
0
data[picked_index
+
2
]
=
0
data[picked_index
+
3
]
=
0
elif
picked_magic[
1
]
=
=
127
:
data[picked_index]
=
127
data[picked_index
+
1
]
=
255
data[picked_index
+
2
]
=
255
data[picked_index
+
3
]
=
255
return
data
def
create_new(data):
f
=
open
(
"mutated.jpg"
,
"wb+"
)
f.write(data)
f.close()
def
exif(counter,data):
command
=
"/home/arahat0/fuzz_learn/fuzz_handwriting/exif/exif mutated.jpg -verbose"
out, returncode
=
run(
"sh -c "
+
quote(command), withexitstatus
=
1
)
if
b
"Segmentation"
in
out:
f
=
open
(
"crashes/crash.{}.jpg"
.
format
(
str
(counter)),
"ab+"
)
f.write(data)
if
counter
%
100
=
=
0
:
print
(counter, end
=
"\r"
)
if
len
(sys.argv) <
2
:
print
(
"Usage: JPEGfuzz.py <valid_jpg>"
)
else
:
filename
=
sys.argv[
1
]
counter
=
0
while
counter <
1000
:
data
=
get_bytes(filename)
functions
=
[
0
,
1
]
picked_function
=
random.choice(functions)
if
picked_function
=
=
0
:
mutated
=
magic(data)
create_new(mutated)
exif(counter,mutated)
else
:
mutated
=
bit_flip(data)
create_new(mutated)
exif(counter,mutated)
counter
+
=
1