Skip to content

Commit

Permalink
Merge pull request #12 from rafael-santiago/pybind-refactor
Browse files Browse the repository at this point in the history
Refactor macgonuts_pybind
  • Loading branch information
rafael-santiago authored Sep 6, 2023
2 parents 47fac74 + b186b7f commit e811232
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
*.o/
coverage.info
**/binds/py/*.so
**/binds/py/macgonuts.c
**/binds/py/macgonuts_pybind.c
**/binds/py/build
28 changes: 14 additions & 14 deletions doc/BINDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Details about how to build is not discussed here, take a look at `doc/BUILD.md`.
## Topics

- [What is available until now](#what-is-available-until-now)
- [Using `macgonuts_pybind`](#using-macgonuts_pybind)
- [Using `macgonuts` from `Python`](#using-macgonuts-from-python)
- [Using `macgonuts` from `Golang`](#using-macgonuts-from-golang)

## What is available until now
Expand All @@ -26,23 +26,23 @@ Until now `macgonuts` features binds for `Python`.

[``Back``](#topics)

## Using `macgonuts_pybind`
## Using `macgonuts` from `Python`

Once it build and well-installed, it is fairly simple to use `macgonuts_pyind` module.
Once it build and well-installed, it is fairly simple to use `macgonuts` module.
The functions present in this module are:

- `macgonuts_spoof()`
- `macgonuts_undo_spoof()`
- `spoof()`
- `undo_spoof()`

The `macgonuts_spoof()` function can receive five arguments:
The `spoof()` function can receive five arguments:

- `lo_iface` is the name of the interface you are accessing the network.
- `target_addr` is the network address of the target, it can be a `IPv4` or `IPv6` address.
- `addr2spoof` is the address that will be spoofed at target, it can be a `IPv4` or `IPv6` address.
- `fake_pkts_amount` is the total of spoofed packets sent to target, it defaults to one.
- `timeout` is the timeout in `mss` between a spoofed packet and the next, it defauts to no timeout.

The `macgonuts_undo_spoof()` undoes a previous promoted spoof attack against a specific target.
The `undo_spoof()` undoes a previous promoted spoof attack against a specific target.
This function expects three arguments:

- `lo_iface` is the name of the interface you are accessing the network.
Expand All @@ -52,38 +52,38 @@ This function expects three arguments:
Follows the general idea when using `macgonuts` spoofing primitives from `Python`:

```python
import macgonuts_pybind
import macgonuts

(...)

# INFO(Rafael): Send one fake ARP packet to 192.168.5.142.
if macgonuts_pybind.macgonuts_spoof('eth0', '192.168.5.142', '192.168.5.1') != 0:
if macgonuts.spoof('eth0', '192.168.5.142', '192.168.5.1') != 0:
print('error when trying to spoof.\n');
(...)

(...)

# INFO(Rafael): Send 200 fake NDP packets to dead::beef:1 at each 500 mss.
if macgonuts_pybind.macgonuts_spoof('eth1',
'dead::beef::8e',
'dead::beef:1', 200, 500) != 0:
if macgonuts.spoof('eth1', 'dead::beef::8e', 'dead::beef:1', 200, 500) != 0:
print('error when trying to spoof.\n');
(...)

(...)

# INFO(Rafael): Now undoing all promoted spoofing attacks.
if macgonuts_undo_spoof('eth0', '192.168.5.142', '192.168.5.1') != 0:
if macgonuts.undo_spoof('eth0', '192.168.5.142', '192.168.5.1') != 0:
print('unable to undo spoof attack done from eth0')
(...)

if macgonuts_undo_spoof('eth1', 'dead::beef:8e', 'dead::beef:1') != 0:
if macgonuts.undo_spoof('eth1', 'dead::beef:8e', 'dead::beef:1') != 0:
print('unable to undo spoof attack done from eth1')
(...)

(...)
```

You also can check a more complete sample at `src/binds/py/sample.py`.

[``Back``](#topics)

## Using `macgonuts` from `Golang`
Expand Down
2 changes: 1 addition & 1 deletion src/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ all: setup build_accacia $(BINARY)
cd cmd && $(MAKE)
ifdef with-pybind
ar -r $(LIBDIR)/libmacgonutssock.a
cd binds/py && rm -rf build && rm -f macgonuts.c && python setup.py build_ext --inplace
cd binds/py && rm -rf build && rm -f macgonuts_pybind.c && python setup.py build_ext --inplace
endif
ifdef with-gobind
cd binds/go/$(CURR_GOBIND_VERSION) && go build
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/binds/py/macgonuts_pybind.h → src/binds/py/macgonuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifndef MACGONTUS_BINDS_PY_MACGONUTS_PYBIND_H
#define MACGONUTS_BINDS_PY_MACGONUTS_PYBIND_H 1
#ifndef MACGONTUS_BINDS_PY_MACGONUTS_H
#define MACGONUTS_BINDS_PY_MACGONUTS_H 1

int macgonuts_pybind_spoof(char *lo_iface, char *target_addr, char *addr2spoof,
int fake_pkts_amount, int timeout);

int macgonuts_pybind_undo_spoof(char *lo_iface, char *target_addr, char *addr2spoof);

#endif // MACGONUTS_BINDS_PY_MACGONUTS_PYBIND_H
#endif // MACGONUTS_BINDS_PY_MACGONUTS_H
31 changes: 21 additions & 10 deletions src/binds/py/macgonuts.pyx → src/binds/py/macgonuts_pybind.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

""" Macgonuts general spoofing utilities binds for Python """

cdef extern from "macgonuts_pybind.h":
import sys

cdef extern from "macgonuts.h":
int macgonuts_pybind_spoof(char *lo_iface, char *target_addr, char *addr2spoof,
int fake_pkts_amount, int timeout);

cdef extern from "macgonuts_pybind.h":
cdef extern from "macgonuts.h":
int macgonuts_pybind_undo_spoof(char *lo_iface, char *target_addr, char *addr2spoof);

def macgonuts_spoof(lo_iface, target_addr, addr2spoof, fake_pkts_amount = 1, timeout = 0):
def spoof(lo_iface, target_addr, addr2spoof, fake_pkts_amount = 1, timeout = 0):
"""The python wrapper for macgonuts_spoof() C function
By using this function you can easily promote a spoofing attack based on IPv4 or IPv6.
Expand All @@ -30,12 +32,17 @@ def macgonuts_spoof(lo_iface, target_addr, addr2spoof, fake_pkts_amount = 1, tim
It returns zero on success and non-zero value on failure, besides writing some error description to stderr.
"""
return macgonuts_pybind_spoof(bytes(lo_iface, 'ascii'),
bytes(target_addr, 'ascii'),
bytes(addr2spoof, 'ascii'),
if sys.version_info >= (3,):
return macgonuts_pybind_spoof(bytes(lo_iface, 'ascii'),
bytes(target_addr, 'ascii'),
bytes(addr2spoof, 'ascii'),
fake_pkts_amount, timeout)
return macgonuts_pybind_spoof(bytes(lo_iface),
bytes(target_addr),
bytes(addr2spoof),
fake_pkts_amount, timeout)

def macgonuts_undo_spoof(lo_iface, target_addr, addr2spoof):
def undo_spoof(lo_iface, target_addr, addr2spoof):
"""The python wrapper for macgonuts_undo_spoof() C function
By using this function you can easily undo a previous promoted spoofing attack based on IPv4 or IPV6.
Expand All @@ -48,6 +55,10 @@ def macgonuts_undo_spoof(lo_iface, target_addr, addr2spoof):
It returns zero on success and non-zero value on failure, besides writing some error description to stderr.
"""
return macgonuts_pybind_undo_spoof(bytes(lo_iface, 'ascii'),
bytes(target_addr, 'ascii'),
bytes(addr2spoof, 'ascii'))
if sys.version_info >= (3,):
return macgonuts_pybind_undo_spoof(bytes(lo_iface, 'ascii'),
bytes(target_addr, 'ascii'),
bytes(addr2spoof, 'ascii'))
return macgonuts_pybind_undo_spoof(bytes(lo_iface),
bytes(target_addr),
bytes(addr2spoof))
46 changes: 46 additions & 0 deletions src/binds/py/sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python
#
# Copyright (c) 2023, Rafael Santiago
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
# INFO(Rafael): In order to test this sample give it a try:
# $ python sample.py
# or
# $ ./sample.py
# and so follow the instructions.
#

import macgonuts
import sys

def main():
if len(sys.argv) < 4:
sys.stderr.write("use: " + sys.argv[0] + " <iface> <target-ip> <address-to-spoof> "
"[ <packets-total> <timeout in mss> ]\n")
sys.exit(1)

try:
packets_total = 1 if len(sys.argv) < 5 else int(sys.argv[4])
except:
sys.stderr.write("error: invalid packets-total.\n")
sys.exit(1)

try:
timeout_in_mss = 0 if len(sys.argv) < 6 else int(sys.argv[5])
except:
sys.stderr.write("error: invalid timeout-in-mss.\n");
sys.exit(1)

if macgonuts.spoof(sys.argv[1], sys.argv[2], sys.argv[3], packets_total, timeout_in_mss) != 0:
sys.stderr.write("error: while trying to spoof.\n")
sys.exit(1)

if macgonuts.undo_spoof(sys.argv[1], sys.argv[2], sys.argv[3]) != 0:
sys.stderr.write("error: while undoing spoof.\n")
sys.exit(1)

if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions src/binds/py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from Cython.Build import cythonize

setup(
name = 'macgonuts_pybind',
name = 'macgonuts',
ext_modules=cythonize([
Extension("macgonuts_pybind", ["macgonuts.pyx", "macgonuts_pybind.c", "../macgonuts_binds.c"],
Extension("macgonuts", ["macgonuts_pybind.pyx", "macgonuts.c", "../macgonuts_binds.c"],
include_dirs=['../..'],
library_dirs=['../../../lib','../../libs/accacia/lib'],
libraries=['macgonuts', 'macgonutssock','accacia']),
Expand Down
2 changes: 1 addition & 1 deletion src/build/toolsets.hsl
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ local function build_pybind() : result type int {
hefesto.sys.echo("*** Now building macgonuts_pybind...\n");

var err type int;
$err = hefesto.sys.run("rm -rf build && rm -f macgonuts.c && " +
$err = hefesto.sys.run("rm -rf build && rm -f macgonuts_pybind.c && " +
"python setup.py build_ext --inplace");

hefesto.sys.cd($oldcwd);
Expand Down

0 comments on commit e811232

Please sign in to comment.