Book Image

Python Penetration Testing Essentials

By : Mohit
Book Image

Python Penetration Testing Essentials

By: Mohit

Overview of this book

Table of Contents (14 chapters)
Python Penetration Testing Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Implementing a network sniffer using Python


Before learning about the implementation of a network sniffer, let's learn about a particular struct method:

  • struct.pack(fmt, v1, v2, ...): This method returns a string that contains the values v1, v2, and so on, packed according to the given format

  • struct.unpack(fmt, string): This method unpacks the string according to the given format

Let's discuss the code:

import struct
ms=  struct.pack('hhl', 1, 2, 3)
print (ms)
k= struct.unpack('hhl',ms)
print k

The output for the preceding code is as follows:

G:\Python\Networking\network>python str1.py
☺ ☻ ♥
(1, 2, 3)

First, import the struct module, and then pack the integers 1, 2, and 3 in the hhl format. The packed values are like machine code. Values are unpacked using the same hhl format; here, h means a short integer and l means a long integer. More details are provided in the subsequent sections.

Consider the situation of the client server model; let's illustrate it by means of an example.

Run the struct1...