summaryrefslogtreecommitdiff
path: root/socketfuzzer/honggfuzz_socketclient.py
blob: 9c37b23ec00ee0bf1dc5b139bdea07c673d24c92 (plain)
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
#!/usr/bin/python
# Python3

import socket
import sys
import time
import random


class HonggfuzzSocket:
    def __init__(self):
        self.sock = None


    def connect(self, file):
        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

        server_address = file
        print( 'connecting to %s' % server_address)

        try:
            self.sock.connect(server_address)
        except socket.error as msg:
            print ("Error connecting to honggfuzz socket: " + str(msg))
            sys.exit(1)


    def send(self, data):
        self.sock.sendall( str.encode(data) )


    def recv(self):
        return self.sock.recv(4).decode()


    def disconnect(self):
        self.sock.close()


class TargetSocket:
    def __init__(self):
        self.sock = None

    def testServerConnectionTcp(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = ('localhost', self.targetPort)

        try:
            sock.connect(server_address)
        except socket.error as exc:
            return False

        sock.close()

        return True


    def sendToSocket(self, data):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(1)

        host = 'localhost'
        port = 5001

        isOpen = False

        n = 0
        while isOpen is False:
            try:
                s.connect((host, port))
                isOpen = True
            except Exception as e:
                time.sleep(0.1)
                n += 1
                isOpen = False

            if n == 10:
                return False

        try:
            s.send( str.encode(data) )
        except Exception as e:
            print( "B: " + str(e))

        s.close()
        return True


    def sendFuzz(self, n):
        data = ""
        if n == 1:
            data = "AAAAAA"
        if n == 2:
            data = "BBBBBB"
        if n == 3:
            data = "CCCCCC"
        if n == 4:
            data = "DDDDDD"
        if n == 5:
            data = "EEEEEE"
        if n == 6:
            # stack buffer overflow
            data = "B" * 128
        if n == 7:
            # heap buffer overflow
            data = "C" * 128

        #print "  Send: " + str(data)
        return self.sendToSocket(data)



def sendResp(targetSocketRes, hfSocket):
    if not targetSocketRes:
        print "  ! Server down. Send: bad!"
        hfSocket.send("bad!")
    else:
        hfSocket.send("okay")



def auto():
    print "Auto"

    hfSocket = HonggfuzzSocket()
    targetSocket = TargetSocket()

    hfSocket.connect("/tmp/honggfuzz_socket")


    print ""
    print "Test: 0 - initial"
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return


    print ""
    print "Test: 1 - first new BB"
    ret = targetSocket.sendFuzz(1)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "New!" or ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return


    print ""
    print "Test: 2 - second new BB"
    targetSocket.sendFuzz(2)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "New!":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return


    print ""
    print "Test: 3 - repeat second msg, no new BB"
    targetSocket.sendFuzz(2)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return

    print ""
    print "Test: 4 - crash stack"
    targetSocket.sendFuzz(6)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "Cras":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return

    print ""
    print "Test: 5 - resend second, no new BB"
    targetSocket.sendFuzz(2)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return

    print ""
    print "Test: 6 - send three, new BB"
    targetSocket.sendFuzz(3)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "New!":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return


    print ""
    print "Test: 7 - send four, new BB"
    targetSocket.sendFuzz(4)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "New!":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return


    print ""
    print "Test: 8 - send four again, no new BB"
    targetSocket.sendFuzz(4)
    sendResp(ret, hfSocket)
    ret = hfSocket.recv()
    if ret == "Fuzz":
        print "  ok: " + ret
    else:
        print "  nok: " + ret
        return


def interactive():
    hfSocket = HonggfuzzSocket()
    targetSocket = TargetSocket()

    hfSocket.connect("/tmp/honggfuzz_socket")

    while(True):
        try:
            recv = hfSocket.recv()

            if recv == "Fuzz":
                # Send the bad data to the target
                i = input("--[ Send Msg #: ")
                #i = random.randint(0, 3)
                #sendFuzz(int(i))
                print "Send to target: " + str(i)
                if not targetSocket.sendFuzz(i):
                    print "Server down. Send: bad!"
                    hfSocket.send("bad!")
                else:
                    hfSocket.send("okay")

            elif recv == "New!":
                print ("--[ R Adding file to corpus...")
                # add the data you sent to the target to your input
                # corpus, as it reached new basic blocks

            elif recv == "Cras":
                print ("--[ R Target crashed")
                # target crashed, store the things you sent to the target

            elif recv == "":
                print("Hongfuzz quit, exiting too\n")
                break

            else:
                print ("--[ Unknown: " + str(recv))

        except Exception as e:
            print("Exception: " + str(e))



def main():
    if len(sys.argv) == 2:
        if sys.argv[1] == "auto":
            auto()
        elif sys.argv[1] == "interactive":
            interactive()
    else:
        print "honggfuzz_socketclient.py [auto/interactive]"


main()