Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 34

Thread: Beginners programming challenge #28

  1. #21
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners programming challenge #28

    Quote Originally Posted by debd View Post
    I've got a question, Is the arp packet capsuled in the ethernet frame as a payload or is it a stand alone packet?
    What do you mean by "payload"?
    「明後日の夕方には帰ってるからね。」


  2. #22
    Join Date
    Sep 2011
    Beans
    10

    Re: Beginners programming challenge #28

    Quote Originally Posted by Bachstelze View Post
    What do you mean by "payload"?
    The table showing the structure of an Ethernet frame includes a "payload" section, where in this case the ARP packet would be.

    To the initial question: Yes the provided example files include the Ethernet frame as well as the ARP packet payload.

  3. #23
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners programming challenge #28

    Quote Originally Posted by CoyleTrydan View Post
    The table showing the structure of an Ethernet frame includes a "payload" section, where in this case the ARP packet would be.

    To the initial question: Yes the provided example files include the Ethernet frame as well as the ARP packet payload.
    The initial question doesn't mention the example files, why do you think I asked for clarification? Also, I can read, thank you very much.
    「明後日の夕方には帰ってるからね。」


  4. #24
    Join Date
    Sep 2011
    Beans
    10

    Re: Beginners programming challenge #28

    Just trying to help answer a question. Didn't mean to step on any toes...

  5. #25
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners programming challenge #28

    Quote Originally Posted by CoyleTrydan View Post
    Just trying to help answer a question. Didn't mean to step on any toes...
    With an implication that I hadn't read the Wikipedia page I myself gave a link to... Anyway, what debd meant may or may not be what you assumed it to be, so let's wait for him to clarify, shall we?
    「明後日の夕方には帰ってるからね。」


  6. #26
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners programming challenge #28

    And about your program, Python is not my forte, but it looks very good. One odd thing, instead of importing __future__, why not use Python 3 altogether?

    One thing you could do to improve address formatting is to make separate functions that take the "raw" address as argument and return it as a formatted string.

    Quote Originally Posted by CoyleTrydan View Post
    Alright, it took long enough, but I think I've got it. It isn't the most elegant solution perhaps (especially formatting the MAC and IP addresses...) but it seems to work.

    I'd appreciate any feedback! I've learned a lot through this exercise, and I'm sure I can learn plenty more from critiques on how I did it.

    PHP Code:
    #!/usr/bin/python
    from __future__ import print_function
    import argparse
    import binascii


    def getBits
    (file):
        
    packet open(file"r")
        
    data binascii.hexlify(packet.read())    
        
    packet.close()
        return(
    data)

    def processPacket(data):
        
    #First 14 octets of ethernet frame are ethernet header
        #Realized that Preamble and Start of Frame Delimiter aren't seen on OSI Level 2
        
    ether0x data[0:28]

        
    macDest ether0x[0:12]
        
    macSrc ether0x[12:24]
        
    type ether0x[24:28]

        
    macDestination str(macDest[0:2]) + ':' str(macDest[2:4]) + ':' str(macDest[4:6]) + ':' str(macDest[6:8]) + ':' str(macDest[8:10]) + ':' +  str(macDest[10:12])
        
    macSource str(macSrc[0:2]) + ':' str(macSrc[2:4]) + ':' str(macSrc[4:6]) + ':' str(macSrc[6:8]) + ':' str(macSrc[8:10]) + ':' +  str(macSrc[10:12])

        print()
        print(
    "[+] Ethernet Header")
        print(
    " * Destination MAC Address: " str(macDestination))
        print(
    " * Source MAC Address: " str(macSource))
        print(
    " * EtherType: 0x" str(type))

        
    #Next is the ARP packet, 28 octets
        
    arp0x data[28:84]

        
    htype arp0x[0:4]
        
    ptype arp0x[4:8]
        
    hlen arp0x[8:10]
        
    plen arp0x[10:12]
        
    oper arp0x[12:16]
        
    sha arp0x[16:28]
        
    spa arp0x[28:36]
        
    tha arp0x[36:48]
        
    tpa arp0x[48:56]

        if 
    int(oper16) == 1:
            
    oper "REQUEST"
        
    elif int(oper16) == 2:
            
    oper "REPLY"

        
    senderProtocol str(int(spa[0:2], 16)) + '.' str(int(spa[2:4], 16)) + '.' str(int(spa[4:6], 16)) + '.' str(int(spa[6:8], 16))
        
    targetProtocol str(int(tpa[0:2], 16)) + '.' str(int(tpa[2:4], 16)) + '.' str(int(tpa[4:6], 16)) + '.' str(int(tpa[6:8], 16))

        
    senderHardware str(sha[0:2]) + ':' str(sha[2:4]) + ':' str(sha[4:6]) + ':' str(sha[6:8]) + ':' str(sha[8:10]) + ':' +  str(sha[10:12])
        
    targetHardware str(tha[0:2]) + ':' str(tha[2:4]) + ':' str(tha[4:6]) + ':' str(tha[6:8]) + ':' str(tha[8:10]) + ':' +  str(tha[10:12])
        
        print()
        print(
    "[+] ARP Packet")
        print(
    " * Hardware Type: " str(int((htype), 16)))
        print(
    " * Protocol Type: 0x" str(ptype))
        print(
    " * Hardware Length: " str(int((hlen), 16)))
        print(
    " * Protocol Length: " str(int((plen), 16)))
        print(
    " * Operation: " str(oper))
        print(
    " * Sender Hardware Address: " str(senderHardware))
        print(
    " * Sender Protocol Address: " str(senderProtocol))
        print(
    " * Target Hardware Address: " str(targetHardware))
        print(
    " * Target Protocol Address: " str(targetProtocol))

        return

    def main():
        
    parser argparse.ArgumentParser()
        
    parser.add_argument("file"help="Path to packet file")
        
    args parser.parse_args()

        
    file args.file
        data 
    getBits(file)
        
    processPacket(data)

    main() 
    「明後日の夕方には帰ってるからね。」


  7. #27
    Join Date
    Sep 2011
    Beans
    10

    Re: Beginners programming challenge #28

    Can we back up here please? I really didn't mean to imply that you hadn't read anything, I'm sorry if it came across that way. I had wondered the same thing (regarding the Ethernet frame) at first myself, so I thought I'd be able to help debd out.
    It really isn't necessary to be so harsh with me, I'm new to the community and trying to be helpful in the tiny ways I'm able - I don't see how my post was offensive. However, your response has really discouraged me from offering input.

    Thanks for the feedback about my program, I appreciate it. Yes, I'm planning on making the jump to Python 3 before long, the reason I don't is I'm so new to the language, and most tutorials and discussions so far seem to still use 2.6 or 2.7. I have heard one of the biggest annoyances when switching over to 3 is the print_function, so I figured I'd go ahead and learn it with that in place. I'm not familiar with all the changes though, so I don't want to make the jump and then have tutorials not work.

    Functions to format the addresses is a good idea, I may change that. Are there any overarching programming philosophies regarding when a job is too small to warrant its own function, or is that more of a stylistic decision?

  8. #28
    Join Date
    Jul 2012
    Beans
    14

    Re: Beginners programming challenge #28

    Quote Originally Posted by CoyleTrydan View Post
    Can we back up here please? I really didn't mean to imply that you hadn't read anything, I'm sorry if it came across that way. I had wondered the same thing (regarding the Ethernet frame) at first myself, so I thought I'd be able to help debd out.
    It really isn't necessary to be so harsh with me, I'm new to the community and trying to be helpful in the tiny ways I'm able - I don't see how my post was offensive. However, your response has really discouraged me from offering input.

    Thanks for the feedback about my program, I appreciate it. Yes, I'm planning on making the jump to Python 3 before long, the reason I don't is I'm so new to the language, and most tutorials and discussions so far seem to still use 2.6 or 2.7. I have heard one of the biggest annoyances when switching over to 3 is the print_function, so I figured I'd go ahead and learn it with that in place. I'm not familiar with all the changes though, so I don't want to make the jump and then have tutorials not work.

    Functions to format the addresses is a good idea, I may change that. Are there any overarching programming philosophies regarding when a job is too small to warrant its own function, or is that more of a stylistic decision?
    From what I can tell it's almost always better to use a function unless you're writing a program that literally does one thing. (i.e. the beer song example from challenge 1)

  9. #29
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Beginners programming challenge #28

    Quote Originally Posted by CoyleTrydan View Post
    Functions to format the addresses is a good idea, I may change that. Are there any overarching programming philosophies regarding when a job is too small to warrant its own function, or is that more of a stylistic decision?
    A good rule of thumb is: if you have to write the same code more than once in your program, make a function for it. Writing the same code several times is called "code duplication", and the reasons why it's a bad thing are amply documented (just Google it).
    「明後日の夕方には帰ってるからね。」


  10. #30
    Join Date
    Feb 2009
    Beans
    1,468

    Re: Beginners programming challenge #28

    Another good rule of thumb is that most functions should fit in a screenful of text. If you have to page back and forth multiple times to view the entire function, you very easily lose track of the big picture. Breaking a small chunk of code off into its own subroutine is cheap; maintaining a function that's 1000 lines long is not.

Page 3 of 4 FirstFirst 1234 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •