/scapy/modules/voip.py

https://github.com/levigross/Scapy · Python · 150 lines · 111 code · 20 blank · 19 comment · 37 complexity · f3df0d1d5986041b39e1c4d2b7b542fa MD5 · raw file

  1. ## This file is part of Scapy
  2. ## See http://www.secdev.org/projects/scapy for more informations
  3. ## Copyright (C) Philippe Biondi <phil@secdev.org>
  4. ## This program is published under a GPLv2 license
  5. """
  6. VoIP (Voice over IP) related functions
  7. """
  8. import os
  9. ###################
  10. ## Testing stuff ##
  11. ###################
  12. from fcntl import fcntl
  13. from scapy.sendrecv import sniff
  14. from scapy.packet import Raw
  15. from scapy.layers.inet import IP,UDP
  16. from scapy.layers.rtp import RTP
  17. from scapy.utils import get_temp_file
  18. def merge(x,y,sample_size=2):
  19. if len(x) > len(y):
  20. y += "\x00"*(len(x)-len(y))
  21. elif len(x) < len(y):
  22. x += "\x00"*(len(y)-len(x))
  23. m = ""
  24. ss=sample_size
  25. for i in range(len(x)/ss):
  26. m += x[ss*i:ss*(i+1)]+y[ss*i:ss*(i+1)]
  27. return m
  28. # return "".join(map(str.__add__, x, y))
  29. def voip_play(s1,list=None,**kargs):
  30. FIFO=get_temp_file()
  31. FIFO1=FIFO % 1
  32. FIFO2=FIFO % 2
  33. os.mkfifo(FIFO1)
  34. os.mkfifo(FIFO2)
  35. try:
  36. os.system("soxmix -t .ul %s -t .ul %s -t ossdsp /dev/dsp &" % (FIFO1,FIFO2))
  37. c1=open(FIFO1,"w", 4096)
  38. c2=open(FIFO2,"w", 4096)
  39. fcntl.fcntl(c1.fileno(),fcntl.F_SETFL, os.O_NONBLOCK)
  40. fcntl.fcntl(c2.fileno(),fcntl.F_SETFL, os.O_NONBLOCK)
  41. # dsp,rd = os.popen2("sox -t .ul -c 2 - -t ossdsp /dev/dsp")
  42. def play(pkt,last=[]):
  43. if not pkt:
  44. return
  45. if not pkt.haslayer(UDP):
  46. return
  47. ip=pkt.getlayer(IP)
  48. if s1 in [ip.src, ip.dst]:
  49. if not last:
  50. last.append(pkt)
  51. return
  52. load=last.pop()
  53. # x1 = load.load[12:]
  54. c1.write(load.load[12:])
  55. if load.getlayer(IP).src == ip.src:
  56. # x2 = ""
  57. c2.write("\x00"*len(load.load[12:]))
  58. last.append(pkt)
  59. else:
  60. # x2 = pkt.load[:12]
  61. c2.write(pkt.load[12:])
  62. # dsp.write(merge(x1,x2))
  63. if list is None:
  64. sniff(store=0, prn=play, **kargs)
  65. else:
  66. for p in list:
  67. play(p)
  68. finally:
  69. os.unlink(FIFO1)
  70. os.unlink(FIFO2)
  71. def voip_play1(s1,list=None,**kargs):
  72. dsp,rd = os.popen2("sox -t .ul - -t ossdsp /dev/dsp")
  73. def play(pkt):
  74. if not pkt:
  75. return
  76. if not pkt.haslayer(UDP):
  77. return
  78. ip=pkt.getlayer(IP)
  79. if s1 in [ip.src, ip.dst]:
  80. dsp.write(pkt.getlayer(Raw).load[12:])
  81. try:
  82. if list is None:
  83. sniff(store=0, prn=play, **kargs)
  84. else:
  85. for p in list:
  86. play(p)
  87. finally:
  88. dsp.close()
  89. rd.close()
  90. def voip_play2(s1,**kargs):
  91. dsp,rd = os.popen2("sox -t .ul -c 2 - -t ossdsp /dev/dsp")
  92. def play(pkt,last=[]):
  93. if not pkt:
  94. return
  95. if not pkt.haslayer(UDP):
  96. return
  97. ip=pkt.getlayer(IP)
  98. if s1 in [ip.src, ip.dst]:
  99. if not last:
  100. last.append(pkt)
  101. return
  102. load=last.pop()
  103. x1 = load.load[12:]
  104. # c1.write(load.load[12:])
  105. if load.getlayer(IP).src == ip.src:
  106. x2 = ""
  107. # c2.write("\x00"*len(load.load[12:]))
  108. last.append(pkt)
  109. else:
  110. x2 = pkt.load[:12]
  111. # c2.write(pkt.load[12:])
  112. dsp.write(merge(x1,x2))
  113. sniff(store=0, prn=play, **kargs)
  114. def voip_play3(lst=None,**kargs):
  115. dsp,rd = os.popen2("sox -t .ul - -t ossdsp /dev/dsp")
  116. try:
  117. def play(pkt, dsp=dsp):
  118. if pkt and pkt.haslayer(UDP) and pkt.haslayer(Raw):
  119. dsp.write(pkt.getlayer(RTP).load)
  120. if lst is None:
  121. sniff(store=0, prn=play, **kargs)
  122. else:
  123. for p in lst:
  124. play(p)
  125. finally:
  126. try:
  127. dsp.close()
  128. rd.close()
  129. except:
  130. pass