DISQUS

Matasano Chargen: Ruby for Pentesters #1: Use Modules For Lists Of Constants

  • Lee Hinman · 1 year ago
    Your github link is broken, it links to http://www.matasano.com/log/1084/ruby-for-pente...

    Just wanted to let you know :)

    I <3 Ruby
  • kowsik · 1 year ago
    I use this all the time. In general, I'm just a big fan of self-enumerating/annotating code. Saves a lot of time. To do this in C is a little bit of work, but saves a ton of mistakes and also propagates new additions to the enum list automatically. See:

    http://labs.mudynamics.com/2007/01/03/enums-str...
  • Thomas Ptacek · 1 year ago
    Nice catch, thanks!
  • Person · 1 year ago
    Python and Perl have equivalent mechanisms.
  • Thomas Ptacek · 1 year ago
    What's the equivalent mechanism in Python? You get no credit for using a dict.
  • snake in the grass · 1 year ago
    class Flags(type):
    def __new__(cls, clsname, clsbases, clsdict):
    clsdict['to_value_hash'] = clsdict
    clsdict['to_name_hash'] = dict([ (y, x) for x, y in clsdict.iteritems() if x.isupper() ])
    return type.__new__(cls, clsname, clsbases, clsdict)

    class EFlags:
    __metaclass__ = Flags

    CARRY = (1<< 0)
    X0 = (1<< 1)
    PARITY = (1<< 2)
    # ...
    VINT = (1<< 19)
    VINTPENDING = (1<< 20)
    CPUID = (1<< 21)

    if __name__ == '__main__':
    print 'CARRY', EFlags.CARRY
    print 'CPUID', EFlags.to_value_hash['CPUID']
    print EFlags.to_name_hash[1 << 19]
  • hidden_pythonista · 1 year ago
    When you want to do equivalent stuff in Python, use a class and a metaclass.

    ---8<---
    class MetaEnum(type):
    def __new__(cls, name, bases, classdict):
    classdict['to_name_hash'] = \
    dict([(enum, val) for enum, val in classdict.iteritems()
    if not enum.startswith('__')])
    classdict['to_value_hash'] = \
    dict([(val, enum) for enum, val in classdict['to_name_hash'].iteritems()])
    return type.__new__(cls, name, bases, classdict)

    class EFlags(object):
    __metaclass__ = MetaEnum
    CARRY = (1<< 0)
    X0 = (1<< 1)
    PARITY = (1<< 2)
    # ...
    VINT = (1<< 19)
    VINTPENDING = (1<< 20)
    CPUID = (1<< 21)

    EFlags.to_name_hash['VINT'] # 524288
    EFlags.to_value_hash[1 << 19] # 'VINT'
    ---8<---

    Oh, I used 2 dicts. So I got 2x0 = 0 credit :'(
  • Thomas · 1 year ago
    in python all class data is stored in a dict :(

    class C:
    pass

    C.a = 0
    C.b = 1
    C.__dict__

    {'a': 0, '__module__': '__main__', 'b': 1, '__doc__': None}
  • Florian Gross · 1 year ago
    This code I wrote ages ago might also be somewhat interesting:

    http://flgr.0x42.net/code/enum.rb

    The main benefit being that inspection of an enum member (wherever it appears) will show you the name instead of the value.
  • Matt · 1 year ago
    What's wrong with dicts?
  • Thomas Ptacek · 1 year ago
    Nothing. Even C has this feature. Just use a hash table library to store the enum values!
  • Phill · 1 year ago
    Syntactic sugar causes cancer of the semicolon.
  • Stephen Reese · 1 year ago
    Great, I can't wait to see more in this series!
  • Asted Habibbi · 1 year ago
    Don't get the argument -- I'm not a computer scientist. I'm just a hacker (the good definition) that needs to exploit something.

    Perl has Net::RawIP to help
    Python has scapy
    Ruby has scruby
    C has libnet and libdnet

    Python is prob the best IDA Pro automation language (IdaRub has not been updated in forever). Python also is handy for the Immunity debugger. And a bunch of other good RE tools are in python.

    Python and Ruby do not have libwhisker so perl has to stick around.


    Just use what works and learn them all like i had to.....

    peace out.