manipulating hex values

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 1 20:28:13 EDT 2008


En Tue, 01 Apr 2008 16:09:09 -0300, Stephen Cattaneo  
<stephen.cattaneo at u4eatech.com> escribió:
> Gabriel Genellina wrote:
>> <div class="moz-text-flowed" style="font-family: -moz-fixed">En Tue,
>> 01 Apr 2008 14:11:31 -0300, Stephen Cattaneo
>> <stephen.cattaneo at u4eatech.com> escribió:
>>
>>> 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
> (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)

It seems you are looking for NAT - have you considered the existing NAT  
solutions? I'm afraid I can't provide further details but it's a  
well-known topic.

> 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.  (And yes, I have tried the proof-of-concept.  It
> works correctly.  It is not my code.)
> dstAddr = "\x01\x02\x03\x04\x05\x06"
> dstAddr1 = "0x010203040506"
> dstAddr != dstAddr1

As others have pointed out, you're confusing numbers, strings, and how  
they're represented. As an example, here you have 10 different ways to get  
the integer "two hundreds and fifty eight":

py> 258, 0x0102, int("0x0102",16)
(258, 258, 258)
py> int("0102",16), ord("\x02\x01".decode("utf-16-le"))
(258, 258)
py> struct.unpack("<H", "\x02\x01")[0]
258
py> struct.unpack(">H", "\x01\x02")[0]
258
py> 0402, int("0402",8), int("100000010",2)
(258, 258, 258)

In all cases you get an *integer* object; it doesn't matter how you wrote  
it, you always get the same integer, the same as writting 258 alone.

> Follow up question:  What is the best to store my bytes up until sending
> the packets?  Perhaps I should use lists of decimal numbers

"list of numbers"

> and then
> before sending convert to hex.

"convert to string of bytes"

> I.E. dstAddr = [ 1, 2, 3, 4, 5, 6]
> dstAddr.prepareToSend()
> txFrame = struct.pack("!6s6sh",dstAddr,srcAddr,proto) + ethData

I thought you were working with IP packets, not ethernet frames.
If you have the MACs as a list of 6 numbers, 0-255:

txFrame = struct.pack("!6B", *dstAddr) + \
           struct.pack("!6B", *srcAddr) + \
           struct.pack("!h", proto) + ethData


> proto = 0x55aa
> s = socket(AF_PACKET, SOCK_RAW, proto)
> s.bind(("eth0",proto))
>
> ifName,ifProto,pktType,hwType,hwAddr = s.getsockname()
> srcAddr = hwAddr
> # send packet to ethernet MAC: 01-02-03-04-05-06
> dstAddr = "\x01\x02\x03\x04\x05\x06"
> ethData = "here is some data for an ethernet packet"
> print "ethData length is: " + str(len(ethData))
>
> txFrame = struct.pack("!6s6sh",dstAddr,srcAddr,proto) + ethData
> s.send(txFrame)

That's wrong. Your raw socket is working with PACKETS (layer 3, network),  
not FRAMES (layer 2).

-- 
Gabriel Genellina




More information about the Python-list mailing list