Repository: secdev/scapy

Issue:
getattr() in volatile.py throws an AttributeError when str() or hexdump() is used on fuzz packet
Hello, 
I have recently installed Scapy to use it to fuzz a CoAP server. I found a few threads related to this by @bsmelo  and tried to recreate the results by building the fuzz packets the exact same way. But when I try to convert it into a string using str(), I get an AttributeError when the getattr() function in volatile.py is called. 
I have installed the latest version of scapy from the secdev/scapy repository.
I have not modified any of the files in scapy (coap.py , utils.py , volatile.py etc) Please help me identify the source of this error.

$ sudo python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
>>> from scapy.contrib.coap import *
>>> 
>>> 
>>> 
>>> conf.L3socket = L3RawSocket
>>> 
>>> 
>>> fuzz_pattern = fuzz(CoAP(ver=1L, type=RandNum(0, 1), code=RandNum(0, 4), token=RandBin(RandNum(0, 8)), options=[(11L, 'core')], paymark='\xff'+str(RandBin())))
>>> 
>>> s = str(fuzz_pattern)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/scapy/packet.py", line 350, in __str__
    return str(self.build())
  File "/usr/local/lib/python2.7/dist-packages/scapy/packet.py", line 460, in build
    p = self.do_build()
  File "/usr/local/lib/python2.7/dist-packages/scapy/packet.py", line 441, in do_build
    self = next(iter(self))
  File "/usr/local/lib/python2.7/dist-packages/scapy/packet.py", line 828, in loop
    for x in loop(todo[:], done):
  File "/usr/local/lib/python2.7/dist-packages/scapy/packet.py", line 820, in loop
    elt = self.getfieldval(eltname)
  File "/usr/local/lib/python2.7/dist-packages/scapy/contrib/coap.py", line 240, in getfieldval
    if v:
  File "/usr/local/lib/python2.7/dist-packages/scapy/volatile.py", line 127, in __nonzero__
    return bool(self.value)
  File "/usr/local/lib/python2.7/dist-packages/scapy/volatile.py", line 89, in __getattr__
    return getattr(self._fix(), attr)
AttributeError: 'int' object has no attribute 'value'
>>> 



Discussion:
@bsmelo @guedou Marking you on this issue since I have been following the issues related to fuzzing CoAP server using scapy that you have resolved previously. Thanks in advance :) 
Please upgrade to the latest Scaly version. It seems that you are using an old one.

Thanks for the prompt response. I have installed scapy using the pip2 command. Therefore I am assuming it is the latest development version that is being installed. I have also upgraded it just to be sure using git pull. I get the same error as before. 
![attributeerror](https://user-images.githubusercontent.com/33404977/38570604-428facb2-3cbc-11e8-9d03-66d5621503af.png)

I can't reproduce your issue either, could you please:
- Check your scapy version?
```
 $ scapy
Welcome to Scapy (2.3.3.dev493)
>>>
```
- Try from outside the scapy folder?
```
 $ python
Python 2.7.6 (default, Nov 23 2017, 15:49:48) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from scapy.all import *
>>> from scapy.contrib.coap import *
>>> conf.L3socket = L3RawSocket
>>> fuzz_pattern = fuzz(CoAP(ver=1L, type=RandNum(0, 1), code=RandNum(0, 4), token=RandBin(RandNum(0, 8)), options=[(11L, 'core')], paymark='\xff'+str(RandBin())))
>>> s = str(fuzz_pattern)
>>> 
```
Hey, 
Scapy version : 2.4.0
I have tried running it from outside the scapy folder. Same error occurs. 
I am not sure if there are any support modules that need to be installed along with scapy. Do you think I am missing any python packages needed ?
Was able to reproduce. Will investigate
```
>>> a = "True" if RandNum(False, True) else "False"
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-60-1f6d6688b98f> in <module>()
----> 1 a = "True" if RandNum(False, True) else "False"

C:\Python27\lib\site-packages\scapy\volatile.pyc in __nonzero__(self)
    113         return int(self)
    114     def __nonzero__(self):
--> 115         return bool(self.value)
    116     __bool__ = __nonzero__
    117     def __add__(self, other):

C:\Python27\lib\site-packages\scapy\volatile.pyc in __getattr__(self, attr)
     83         if attr in ["__setstate__", "__getstate__"]:
     84             raise AttributeError(attr)
---> 85         return getattr(self._fix(),attr)
     86     def __str__(self):
     87         return str(self._fix())

AttributeError: 'int' object has no attribute 'value'
```

Resolving patch:
diff --git a/scapy/volatile.py b/scapy/volatile.py
--- a/scapy/volatile.py
+++ b/scapy/volatile.py
@@ -124,7 +124,7 @@ def __index__(self):
         return int(self)
 
     def __nonzero__(self):
-        return bool(self.value)
+        return bool(self._fix())
     __bool__ = __nonzero__
 
     def __add__(self, other):


Test patch:
diff --git a/test/regression.uts b/test/regression.uts
--- a/test/regression.uts
+++ b/test/regression.uts
@@ -9087,6 +9087,9 @@ random.seed(0x2807)
 rts = RandTermString(4, "scapy")
 assert(sane(raw(rts)) in ["...Zscapy", "$#..scapy"])
 
+= RandInt (test __bool__)
+a = "True" if RandNum(False, True) else "False"
+assert a in ["True", "False"]
 
 ############
 ############