user1473508
user1473508

Reputation: 195

String to binary python

This is probably a dummy question for most of you, but i'm facing this problem;

I'm capturing a mac address on the network with scapy,I convert this mac address to "\x00\x21\x23\x24\x25" this way :

...
  b = p.sprintf("%Dot11.addr2%")
  print "Sending to this MAC Address",b
  #Here, the mac address looks like 00:00:00:00:00:00
  dstmac = str('\\x'+b.replace(':','\\x'))
  #Now, the mac address looks like this:
  "\x00\x00\x00\x00\x00\x00"
  sendp(dst=dstmac,blablablabla)
...

When this script send the mac on the wire, it send the actual \x (and all followed number)encoded in hex instead of sending 00 00 00 00 00 00.

Usually, you can use it this way and it's fine : "sendp(dst="\x00\x00\x00\x00\x00\x00",blablablabla)

I think i'm doing something wrong while doing this string manipulation, or encoding.

Any input on this will help !

Thanks !

Upvotes: 1

Views: 1335

Answers (2)

Andrew Cox
Andrew Cox

Reputation: 10998

When you do the replace(':','\\x') you aren't doing what you think you are doing. You are replacing the : with the characters \x not converting the numbers to their binary value.
If you did a print of dstmac you would get '\\x00\\x00\\x00\\x00\\x00\\x00'

you could get the effect you are looking for by doing the following

as specified in another answer to get what you are looking for you should

dstmac = b.replace(':','').decode('hex')

which is remove the : and decode the string as if it was hex

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799300

The \x notation is for string literals. Decode the string as hex instead.

>>> '123456789abc'.decode('hex')
'\x124Vx\x9a\xbc'

Upvotes: 6

Related Questions