• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Gallopsled / pwntools / 5436493948

pending completion
5436493948

push

github-actions

jasperla
add "retguard" property and display it with checksec

quoting http://undeadly.org/cgi?action=article;sid=20180606064444,
retguard is an "anti-ROP security mechanism, which uses random
per-function cookies to protect return addresses on the stack."

3968 of 6663 branches covered (59.55%)

6 of 6 new or added lines in 1 file covered. (100.0%)

12137 of 16984 relevant lines covered (71.46%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.36
/pwnlib/tubes/buffer.py
1
from __future__ import absolute_import
1✔
2
from __future__ import division
1✔
3

4
from pwnlib.context import context
1✔
5

6

7
class Buffer(object):
1✔
8
    """
9
    List of strings with some helper routines.
10

11
    Example:
12

13
        >>> b = Buffer()
14
        >>> b.add(b"A" * 10)
15
        >>> b.add(b"B" * 10)
16
        >>> len(b)
17
        20
18
        >>> b.get(1)
19
        b'A'
20
        >>> len(b)
21
        19
22
        >>> b.get(9999)
23
        b'AAAAAAAAABBBBBBBBBB'
24
        >>> len(b)
25
        0
26
        >>> b.get(1)
27
        b''
28

29
    Implementation Details:
30

31
        Implemented as a list.  Strings are added onto the end.
32
        The ``0th`` item in the buffer is the oldest item, and
33
        will be received first.
34
    """
35
    def __init__(self, buffer_fill_size = None):
1✔
36
        self.data = [] # Buffer
1✔
37
        self.size = 0  # Length
1✔
38
        self.buffer_fill_size = buffer_fill_size
1✔
39

40
    def __len__(self):
1✔
41
        """
42
        >>> b = Buffer()
43
        >>> b.add(b'lol')
44
        >>> len(b) == 3
45
        True
46
        >>> b.add(b'foobar')
47
        >>> len(b) == 9
48
        True
49
        """
50
        return self.size
1✔
51

52
    def __nonzero__(self):
1✔
53
        return len(self) > 0
×
54

55
    def __contains__(self, x):
1✔
56
        """
57
        >>> b = Buffer()
58
        >>> b.add(b'asdf')
59
        >>> b'x' in b
60
        False
61
        >>> b.add(b'x')
62
        >>> b'x' in b
63
        True
64
        """
65
        for b in self.data:
1✔
66
            if x in b:
1✔
67
                return True
1✔
68
        return False
1✔
69

70
    def index(self, x):
1✔
71
        """
72
        >>> b = Buffer()
73
        >>> b.add(b'asdf')
74
        >>> b.add(b'qwert')
75
        >>> b.index(b't') == len(b) - 1
76
        True
77
        """
78
        sofar = 0
1✔
79
        for b in self.data:
1!
80
            if x in b:
1✔
81
                return sofar + b.index(x)
1✔
82
            sofar += len(b)
1✔
83
        raise IndexError()
×
84

85
    def add(self, data):
1✔
86
        """
87
        Adds data to the buffer.
88

89
        Arguments:
90
            data(str,Buffer): Data to add
91
        """
92
        # Fast path for ''
93
        if not data: return
1!
94

95
        if isinstance(data, Buffer):
1!
96
            self.size += data.size
×
97
            self.data += data.data
×
98
        else:
99
            self.size += len(data)
1✔
100
            self.data.append(data)
1✔
101

102
    def unget(self, data):
1✔
103
        """
104
        Places data at the front of the buffer.
105

106
        Arguments:
107
            data(str,Buffer): Data to place at the beginning of the buffer.
108

109
        Example:
110

111
            >>> b = Buffer()
112
            >>> b.add(b"hello")
113
            >>> b.add(b"world")
114
            >>> b.get(5)
115
            b'hello'
116
            >>> b.unget(b"goodbye")
117
            >>> b.get()
118
            b'goodbyeworld'
119
        """
120
        if isinstance(data, Buffer):
1✔
121
            self.data = data.data + self.data
1✔
122
            self.size += data.size
1✔
123
        else:
124
            self.data.insert(0, data)
1✔
125
            self.size += len(data)
1✔
126

127
    def get(self, want=float('inf')):
1✔
128
        """
129
        Retrieves bytes from the buffer.
130

131
        Arguments:
132
            want(int): Maximum number of bytes to fetch
133

134
        Returns:
135
            Data as string
136

137
        Example:
138

139
            >>> b = Buffer()
140
            >>> b.add(b'hello')
141
            >>> b.add(b'world')
142
            >>> b.get(1)
143
            b'h'
144
            >>> b.get()
145
            b'elloworld'
146
        """
147
        # Fast path, get all of the data
148
        if want >= self.size:
1✔
149
            data   = b''.join(self.data)
1✔
150
            self.size = 0
1✔
151
            self.data = []
1✔
152
            return data
1✔
153

154
        # Slow path, find the correct-index chunk
155
        have = 0
1✔
156
        i    = 0
1✔
157
        while want >= have:
1✔
158
            have += len(self.data[i])
1✔
159
            i    += 1
1✔
160

161
        # Join the chunks, evict from the buffer
162
        data   = b''.join(self.data[:i])
1✔
163
        self.data = self.data[i:]
1✔
164

165
        # If the last chunk puts us over the limit,
166
        # stick the extra back at the beginning.
167
        if have > want:
1!
168
            extra = data[want:]
1✔
169
            data  = data[:want]
1✔
170
            self.data.insert(0, extra)
1✔
171

172
        # Size update
173
        self.size -= len(data)
1✔
174

175
        return data
1✔
176

177
    def get_fill_size(self, size=None):
1✔
178
        """
179
        Retrieves the default fill size for this buffer class.
180

181
        Arguments:
182
            size (int): (Optional) If set and not None, returns the size variable back.
183

184
        Returns:
185
            Fill size as integer if size is None, else size.
186
        """
187
        if size is None:
1✔
188
            size = self.buffer_fill_size
1✔
189

190
        with context.local(buffer_size=size):
1✔
191
            return context.buffer_size
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc