manipulating hex values
Stephen Cattaneo
Stephen.Cattaneo at u4eatech.com
Thu Apr 3 10:31:06 EDT 2008
Thanks to everyone ( Grant, Cliff, and Gabriel) for responding and
helping me.
Cheers,
Steve
-----Original Message-----
From: Grant Edwards [mailto:grante at visi.com]
Sent: Tuesday, April 01, 2008 7:46 PM
To: python-list at python.org
Subject: Re: manipulating hex values
On 2008-04-01, Stephen Cattaneo <stephen.cattaneo at u4eatech.com> wrote:
>>> I am relatively new to socket programming. I am attempting to
>>> use raw sockets to spoof my IP address.
>>
>> Don't bother to try...
>
> Is there a better solution to spoofing my IP. then using raw
> sockets
You'll have to define "spoofing my IP", but I suspect that what
you're trying can't be done by using raw sockets.
> (I'm working on a machine with multiple interfaces and need to
> be able to some how specify which interface that traffic needs
> to be sent/recieved to/from)
That's what routing tables are for.
If you want to send packets using a particular IP, then
configure an interface so that it has that adrress, and then
bind your socket to that address.
> The source of my confusion is that I need to keep my bytes formated
> correctly. I am using the below 'raw socket example'
proof-of-concept
> code as my example.
OK.
> (And yes, I have tried the proof-of-concept. It works
> correctly. It is not my code.)
I know. It's my code. :) I wrote Python's raw socket support
code and the example code that is floating around the 'net.
> dstAddr = "\x01\x02\x03\x04\x05\x06"
> dstAddr1 = "0x010203040506"
> dstAddr != dstAddr1
Right.
> Follow up question: What is the best to store my bytes up
> until sending the packets?
That depends on what you want to do with them. Ultimately,
they need to be strings when they're sent out the socket (in
Python a "string" is really just an array of 8-bit bytes). The
best way to store them is entirely dependent on how you want to
manipulate them before they're sent.
> Perhaps I should use lists of decimal numbers and then
> before sending convert to hex.
Again: you're not converting them to hex. You're converting
them to a python "string" object which is really just an array
of bytes. Stop saying "hex" when you're talking about a string
(array of bytes). "hex" is a way to _represent_ a value
textually. It's simply a format used when print a value on
paper or a screen. The value itself isn't hex any more than a
particular instance of Canis lupus familiaris is "English"
because somebody spells it "dog" instead of "chien" or "Hund".
> I.E. dstAddr = [ 1, 2, 3, 4, 5, 6]
> dstAddr.prepareToSend()
I presume you know that list objects don't have a method called
prepareToSend().
> txFrame = struct.pack("!6s6sh",dstAddr,srcAddr,proto) + ethData
>
> Is there a better way to do this?
It's not at all clear what "this" is. If you want to convert
from a list (or any sequence, really) of integer objects to a
string where each integer is converted into a single byte
within the string, then here are a few ways:
import operator
import struct
intlist = [1,2,3,4,5,6]
s1 = ''.join(chr(b) for b in intlist)
s2 = reduce(operator.add,[chr(b) for b in intlist])
s3 = struct.pack("6B",*tuple(intlist))
print repr(s1)
print repr(s2)
print repr(s3)
print s1==s2
print s2==s3
When run you get this:
'\x01\x02\x03\x04\x05\x06'
'\x01\x02\x03\x04\x05\x06'
'\x01\x02\x03\x04\x05\x06'
True
True
I think the third alternative using struct.pack() is the most
readable, but others will no doubt disagree.
--
Grant Edwards grante Yow! I'm GLAD I
at remembered to XEROX
all
visi.com my UNDERSHIRTS!!
More information about the Python-list
mailing list