@@ -20,38 +20,38 @@ def check_ELF_PIE(executable):
20
20
'''
21
21
Check for position independent executable (PIE), allowing for address space randomization.
22
22
'''
23
- p = subprocess .Popen ([READELF_CMD , '-h' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE )
23
+ p = subprocess .Popen ([READELF_CMD , '-h' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
24
24
(stdout , stderr ) = p .communicate ()
25
25
if p .returncode :
26
26
raise IOError ('Error opening file' )
27
27
28
28
ok = False
29
- for line in stdout .split ( b' \n ' ):
29
+ for line in stdout .splitlines ( ):
30
30
line = line .split ()
31
- if len (line )>= 2 and line [0 ] == b 'Type:' and line [1 ] == b 'DYN' :
31
+ if len (line )>= 2 and line [0 ] == 'Type:' and line [1 ] == 'DYN' :
32
32
ok = True
33
33
return ok
34
34
35
35
def get_ELF_program_headers (executable ):
36
36
'''Return type and flags for ELF program headers'''
37
- p = subprocess .Popen ([READELF_CMD , '-l' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE )
37
+ p = subprocess .Popen ([READELF_CMD , '-l' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
38
38
(stdout , stderr ) = p .communicate ()
39
39
if p .returncode :
40
40
raise IOError ('Error opening file' )
41
41
in_headers = False
42
42
count = 0
43
43
headers = []
44
- for line in stdout .split ( b' \n ' ):
45
- if line .startswith (b 'Program Headers:' ):
44
+ for line in stdout .splitlines ( ):
45
+ if line .startswith ('Program Headers:' ):
46
46
in_headers = True
47
- if line == b '' :
47
+ if line == '' :
48
48
in_headers = False
49
49
if in_headers :
50
50
if count == 1 : # header line
51
- ofs_typ = line .find (b 'Type' )
52
- ofs_offset = line .find (b 'Offset' )
53
- ofs_flags = line .find (b 'Flg' )
54
- ofs_align = line .find (b 'Align' )
51
+ ofs_typ = line .find ('Type' )
52
+ ofs_offset = line .find ('Offset' )
53
+ ofs_flags = line .find ('Flg' )
54
+ ofs_align = line .find ('Align' )
55
55
if ofs_typ == - 1 or ofs_offset == - 1 or ofs_flags == - 1 or ofs_align == - 1 :
56
56
raise ValueError ('Cannot parse elfread -lW output' )
57
57
elif count > 1 :
@@ -68,9 +68,9 @@ def check_ELF_NX(executable):
68
68
have_wx = False
69
69
have_gnu_stack = False
70
70
for (typ , flags ) in get_ELF_program_headers (executable ):
71
- if typ == b 'GNU_STACK' :
71
+ if typ == 'GNU_STACK' :
72
72
have_gnu_stack = True
73
- if b 'W' in flags and b 'E' in flags : # section is both writable and executable
73
+ if 'W' in flags and 'E' in flags : # section is both writable and executable
74
74
have_wx = True
75
75
return have_gnu_stack and not have_wx
76
76
@@ -87,31 +87,31 @@ def check_ELF_RELRO(executable):
87
87
# However, the dynamic linker need to write to this area so these are RW.
88
88
# Glibc itself takes care of mprotecting this area R after relocations are finished.
89
89
# See also http://permalink.gmane.org/gmane.comp.gnu.binutils/71347
90
- if typ == b 'GNU_RELRO' :
90
+ if typ == 'GNU_RELRO' :
91
91
have_gnu_relro = True
92
92
93
93
have_bindnow = False
94
- p = subprocess .Popen ([READELF_CMD , '-d' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE )
94
+ p = subprocess .Popen ([READELF_CMD , '-d' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
95
95
(stdout , stderr ) = p .communicate ()
96
96
if p .returncode :
97
97
raise IOError ('Error opening file' )
98
- for line in stdout .split ( b' \n ' ):
98
+ for line in stdout .splitlines ( ):
99
99
tokens = line .split ()
100
- if len (tokens )> 1 and tokens [1 ] == b '(BIND_NOW)' or (len (tokens )> 2 and tokens [1 ] == b '(FLAGS)' and b 'BIND_NOW' in tokens [2 ]):
100
+ if len (tokens )> 1 and tokens [1 ] == '(BIND_NOW)' or (len (tokens )> 2 and tokens [1 ] == '(FLAGS)' and 'BIND_NOW' in tokens [2 ]):
101
101
have_bindnow = True
102
102
return have_gnu_relro and have_bindnow
103
103
104
104
def check_ELF_Canary (executable ):
105
105
'''
106
106
Check for use of stack canary
107
107
'''
108
- p = subprocess .Popen ([READELF_CMD , '--dyn-syms' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE )
108
+ p = subprocess .Popen ([READELF_CMD , '--dyn-syms' , '-W' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
109
109
(stdout , stderr ) = p .communicate ()
110
110
if p .returncode :
111
111
raise IOError ('Error opening file' )
112
112
ok = False
113
- for line in stdout .split ( b' \n ' ):
114
- if b '__stack_chk_fail' in line :
113
+ for line in stdout .splitlines ( ):
114
+ if '__stack_chk_fail' in line :
115
115
ok = True
116
116
return ok
117
117
@@ -121,13 +121,13 @@ def get_PE_dll_characteristics(executable):
121
121
Returns a tuple (arch,bits) where arch is 'i386:x86-64' or 'i386'
122
122
and bits is the DllCharacteristics value.
123
123
'''
124
- p = subprocess .Popen ([OBJDUMP_CMD , '-x' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE )
124
+ p = subprocess .Popen ([OBJDUMP_CMD , '-x' , executable ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , stdin = subprocess .PIPE , universal_newlines = True )
125
125
(stdout , stderr ) = p .communicate ()
126
126
if p .returncode :
127
127
raise IOError ('Error opening file' )
128
128
arch = ''
129
129
bits = 0
130
- for line in stdout .split ( ' \n ' ):
130
+ for line in stdout .splitlines ( ):
131
131
tokens = line .split ()
132
132
if len (tokens )>= 2 and tokens [0 ] == 'architecture:' :
133
133
arch = tokens [1 ].rstrip (',' )
0 commit comments