0

I'm writing an addon for scapy, and encountered a problem. I had to slightly modify the original scapy code (every class is inheriting from object) The modified code can be found here: http://pastebin.com/pjcL1KJv

The code I wrote is the following:

class Foo():
   array=[ BitField("foo",0x0,2),
           BitField("foo1",0x0,2),
           BitField("bar",0x0,2),
           BitField("blub",None,2)
 ]
def returnArr(a):  
      for i in a.array:
           print type(i.default)


if __name__ == "__main__":
    a=Foo()
    a.blub=0x23
    returnArr(a)

The output:

< type 'int'>

< type 'int'>

< type 'int'>

< type 'NoneType'>

My question: Is it possible to detect if the second paremeter of BitField("foo",0x0,2) is 0x0 or something else? If it is possible, how would I do that? If not, why?

Steve
  • 549
  • 1
  • 5
  • 8

2 Answers2

1

The second parameter is called default, and it's stored as an attribute also called default.

b = BitField("foo",0x0,2)
b.default   # 0
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • Okay, is it also possible to detect if that value has been changed? I mean, if I understand correctly `b.default` returns the value defined in `fields_desc`. If I change the value of `BitField("foo",0x0,2)` the following way: `a = BitField("foo",0x0,2); a = 0x23` can I find out that the default != the new value? How would I reach that behaviour? Thx for your help :) – Steve Jul 14 '11 at 13:45
  • maybe I can use: `def getfield(self, pkt, s)`? could you show me an example if this is a correct idea? – Steve Jul 14 '11 at 13:57
  • @Steve: That just sets a to 0x23, replacing the BitField object you created before. – Thomas K Jul 14 '11 at 16:34
  • yes, I know, but I want to check the actual value, not the default value. I set the default to 0x0, but tehn I use the Scapy CLI to change 0x0 to 0x23. Now how can I find out that the value is now 0x23? I guess it simply overwrites the default value. Using the code above I only get the default, not the actual value of the field. any hint on this? – Steve Jul 14 '11 at 20:26
  • @Steve: How do you change the value? – Thomas K Jul 14 '11 at 20:43
  • in the examplefrom the first post I would do: `a=Foo(); a.foo1=0x23`. This sets the value to 0x23. – Steve Jul 14 '11 at 20:49
  • But before you do that, `a.foo1` gives `AttributeError: Foo instance has no attribute 'foo1'`. You've just set a separate value. – Thomas K Jul 14 '11 at 20:54
  • Hi Thomas K, I fixed te code above to make it work. Now the problem should be clear. – Steve Jul 15 '11 at 07:48
  • To summarize, what I want is to get the current value of a.blub, not the default one. – Steve Jul 15 '11 at 07:54
  • I could do: `print type(a.blub)` and would get ``. Okay and get the name of the field using the name function. Thx I solved the problem :-) – Steve Jul 15 '11 at 08:01
0

Try .default attribute for BitField instances.

Michał Bentkowski
  • 2,115
  • 16
  • 10