Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for forge frames and send them. #954

Merged
merged 11 commits into from
Nov 25, 2021
Merged

Add support for forge frames and send them. #954

merged 11 commits into from
Nov 25, 2021

Conversation

jjnicola
Copy link
Member

@jjnicola jjnicola commented Nov 18, 2021

What:
Add support for forge frames and send them

Jira: SC-383

Why:
New feature

How:

Example usage:
Run the following script with openvas-nasl

    if(description) {
      script_oid("1.2.3");
      exit(0);
    }
    
    include("misc_func.inc");
    
    myhost = this_host();
    port = 22022;
    srcport= 35000;
    dstip = get_host_ip();
    src_mac = raw_string (0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb); # get_local_mac_address_from_ip can be used
    dst_mac = raw_string (0x54, 0xe1, 0xad, 0xd4, 0xed, 0x74); # send_arp_request() can be used
    
    ether_proto = 0x0800;
    
    filter = string("ether dst ", hexstr(dst_mac), " and src port ", 35000);
    
    ip = forge_ip_packet(ip_v   : 4,
                         ip_hl  : 5,
                         ip_tos : 0,
                         ip_len : 20,
                         ip_id  : rand(),
                         ip_p   : IPPROTO_TCP,
                         ip_ttl : 255,
                         ip_off : 0,
                         ip_src : myhost,
                         ip_dst: dstip);
    
    display (IPPROTO_TCP);
    
    tcp = forge_tcp_packet(ip       : ip,
                           th_ack   : 0,
                           th_dport : port,
                           th_flags : TH_SYN,
                           #th_seq   : tcp_seq + 1024,
                           th_sport : srcport,
                           th_x2    : 0,
                           th_off   : 5,
                           th_win   : 1024,
                           th_urp   : 0,
                           data: "hola mundo");
    
    frame = forge_frame (src_haddr: src_mac, dst_haddr: dst_mac, ether_proto: ether_proto, payload: tcp);
    
    f = send_frame (frame: frame, pcap_filter: filter, timeout: 10);
    
    dump_frame (frame:f);

If you want to try something at lower level, you can try the following script, which sends an arp request as raw string, without need to forge the frame. Just be sure to use the right local MAC address and ip address (in the example, 0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb and 0xc0, 0xa8, 0x00, 0x01 , respectively )

f(description) {
  script_oid("1.2.3");
  exit(0);
}

include("misc_func.inc");

dstip = get_host_ip();

filter = string ("arp and src host ", dstip);
frame = raw_string (
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb, 0x08, 0x06, 0x00, 0x01,
0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb, 0xc0, 0xa8, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x0a, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

dump_frame (frame:frame);

r = send_frame (frame: frame, pcap_filter: filter, timeout: 10);

dump_frame (frame:r);

Checklist:

  • Tests
  • PR merge commit message adjusted

@jjnicola jjnicola requested a review from a team as a code owner November 18, 2021 07:40
@jjnicola jjnicola marked this pull request as draft November 18, 2021 07:43
@jjnicola jjnicola marked this pull request as ready for review November 18, 2021 09:56
@jjnicola jjnicola enabled auto-merge (squash) November 18, 2021 09:56
This functions allows to get the interface's ID where a frame will be sent through, depending on the target's IP
It receives now a new int parameter to tell the function to return the frame only or to include the payload.
It returns now a char * instead of a struct ether_header.
Example usage:
Run the following script with openvas-nasl

```
if(description) {
  script_oid("1.2.3");
  exit(0);
}

include("misc_func.inc");

myhost = this_host();
port = 22022;
srcport= 35000;
dstip = get_host_ip();
src_mac = raw_string (0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb); # get_local_mac_address_from_ip can be used
dst_mac = raw_string (0x54, 0xe1, 0xad, 0xd4, 0xed, 0x74); # send_arp_request() can be used

ether_proto = 0x0800;

filter = string("ether dst ", hexstr(dst_mac), " and src port ", 35000);

ip = forge_ip_packet(ip_v   : 4,
                     ip_hl  : 5,
                     ip_tos : 0,
                     ip_len : 20,
                     ip_id  : rand(),
                     ip_p   : IPPROTO_TCP,
                     ip_ttl : 255,
                     ip_off : 0,
                     ip_src : myhost,
                     ip_dst: dstip);

display (IPPROTO_TCP);

tcp = forge_tcp_packet(ip       : ip,
                       th_ack   : 0,
                       th_dport : port,
		       th_flags : TH_SYN,
                       #th_seq   : tcp_seq + 1024,
		       th_sport : srcport,
                       th_x2    : 0,
                       th_off   : 5,
                       th_win   : 1024,
		       th_urp   : 0,
		       data: "hola mundo");

frame = forge_frame (src_haddr: src_mac, dst_haddr: dst_mac, ether_proto: ether_proto, payload: tcp);

f = send_frame (frame: frame, pcap_filter: filter, timeout: 10);

dump_frame (frame:f);

```
@jjnicola jjnicola merged commit 2550b26 into main Nov 25, 2021
@jjnicola jjnicola deleted the frame-forgery branch November 25, 2021 11:04
mergify bot pushed a commit that referenced this pull request Nov 25, 2021
* Add function get_iface_index()

This functions allows to get the interface's ID where a frame will be sent through, depending on the target's IP

* Extend capture_next_frame() with a new param.

It receives now a new int parameter to tell the function to return the frame only or to include the payload.
It returns now a char * instead of a struct ether_header.

* Add struct pseudo_frame

* Add function to forge a frame

* Add function to prepare a message which includes a frame as payload and prepares the socket for captures

* Add function to dump a frame

* Make new functions visibles

Example usage:
Run the following script with openvas-nasl

```
if(description) {
  script_oid("1.2.3");
  exit(0);
}

include("misc_func.inc");

myhost = this_host();
port = 22022;
srcport= 35000;
dstip = get_host_ip();
src_mac = raw_string (0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb); # get_local_mac_address_from_ip can be used
dst_mac = raw_string (0x54, 0xe1, 0xad, 0xd4, 0xed, 0x74); # send_arp_request() can be used

ether_proto = 0x0800;

filter = string("ether dst ", hexstr(dst_mac), " and src port ", 35000);

ip = forge_ip_packet(ip_v   : 4,
                     ip_hl  : 5,
                     ip_tos : 0,
                     ip_len : 20,
                     ip_id  : rand(),
                     ip_p   : IPPROTO_TCP,
                     ip_ttl : 255,
                     ip_off : 0,
                     ip_src : myhost,
                     ip_dst: dstip);

display (IPPROTO_TCP);

tcp = forge_tcp_packet(ip       : ip,
                       th_ack   : 0,
                       th_dport : port,
		       th_flags : TH_SYN,
                       #th_seq   : tcp_seq + 1024,
		       th_sport : srcport,
                       th_x2    : 0,
                       th_off   : 5,
                       th_win   : 1024,
		       th_urp   : 0,
		       data: "hola mundo");

frame = forge_frame (src_haddr: src_mac, dst_haddr: dst_mac, ether_proto: ether_proto, payload: tcp);

f = send_frame (frame: frame, pcap_filter: filter, timeout: 10);

dump_frame (frame:f);

```

* Fix invalid read/write reported by valgrind

(cherry picked from commit 2550b26)
jjnicola added a commit that referenced this pull request Nov 26, 2021
* Add function get_iface_index()

This functions allows to get the interface's ID where a frame will be sent through, depending on the target's IP

* Extend capture_next_frame() with a new param.

It receives now a new int parameter to tell the function to return the frame only or to include the payload.
It returns now a char * instead of a struct ether_header.

* Add struct pseudo_frame

* Add function to forge a frame

* Add function to prepare a message which includes a frame as payload and prepares the socket for captures

* Add function to dump a frame

* Make new functions visibles

Example usage:
Run the following script with openvas-nasl

```
if(description) {
  script_oid("1.2.3");
  exit(0);
}

include("misc_func.inc");

myhost = this_host();
port = 22022;
srcport= 35000;
dstip = get_host_ip();
src_mac = raw_string (0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb); # get_local_mac_address_from_ip can be used
dst_mac = raw_string (0x54, 0xe1, 0xad, 0xd4, 0xed, 0x74); # send_arp_request() can be used

ether_proto = 0x0800;

filter = string("ether dst ", hexstr(dst_mac), " and src port ", 35000);

ip = forge_ip_packet(ip_v   : 4,
                     ip_hl  : 5,
                     ip_tos : 0,
                     ip_len : 20,
                     ip_id  : rand(),
                     ip_p   : IPPROTO_TCP,
                     ip_ttl : 255,
                     ip_off : 0,
                     ip_src : myhost,
                     ip_dst: dstip);

display (IPPROTO_TCP);

tcp = forge_tcp_packet(ip       : ip,
                       th_ack   : 0,
                       th_dport : port,
		       th_flags : TH_SYN,
                       #th_seq   : tcp_seq + 1024,
		       th_sport : srcport,
                       th_x2    : 0,
                       th_off   : 5,
                       th_win   : 1024,
		       th_urp   : 0,
		       data: "hola mundo");

frame = forge_frame (src_haddr: src_mac, dst_haddr: dst_mac, ether_proto: ether_proto, payload: tcp);

f = send_frame (frame: frame, pcap_filter: filter, timeout: 10);

dump_frame (frame:f);

```

* Fix invalid read/write reported by valgrind

(cherry picked from commit 2550b26)

Co-authored-by: Juan José Nicola <jjnicola@gmail.com>
mergify bot pushed a commit that referenced this pull request Nov 29, 2021
* Add function get_iface_index()

This functions allows to get the interface's ID where a frame will be sent through, depending on the target's IP

* Extend capture_next_frame() with a new param.

It receives now a new int parameter to tell the function to return the frame only or to include the payload.
It returns now a char * instead of a struct ether_header.

* Add struct pseudo_frame

* Add function to forge a frame

* Add function to prepare a message which includes a frame as payload and prepares the socket for captures

* Add function to dump a frame

* Make new functions visibles

Example usage:
Run the following script with openvas-nasl

```
if(description) {
  script_oid("1.2.3");
  exit(0);
}

include("misc_func.inc");

myhost = this_host();
port = 22022;
srcport= 35000;
dstip = get_host_ip();
src_mac = raw_string (0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb); # get_local_mac_address_from_ip can be used
dst_mac = raw_string (0x54, 0xe1, 0xad, 0xd4, 0xed, 0x74); # send_arp_request() can be used

ether_proto = 0x0800;

filter = string("ether dst ", hexstr(dst_mac), " and src port ", 35000);

ip = forge_ip_packet(ip_v   : 4,
                     ip_hl  : 5,
                     ip_tos : 0,
                     ip_len : 20,
                     ip_id  : rand(),
                     ip_p   : IPPROTO_TCP,
                     ip_ttl : 255,
                     ip_off : 0,
                     ip_src : myhost,
                     ip_dst: dstip);

display (IPPROTO_TCP);

tcp = forge_tcp_packet(ip       : ip,
                       th_ack   : 0,
                       th_dport : port,
		       th_flags : TH_SYN,
                       #th_seq   : tcp_seq + 1024,
		       th_sport : srcport,
                       th_x2    : 0,
                       th_off   : 5,
                       th_win   : 1024,
		       th_urp   : 0,
		       data: "hola mundo");

frame = forge_frame (src_haddr: src_mac, dst_haddr: dst_mac, ether_proto: ether_proto, payload: tcp);

f = send_frame (frame: frame, pcap_filter: filter, timeout: 10);

dump_frame (frame:f);

```

* Fix invalid read/write reported by valgrind

(cherry picked from commit 2550b26)
jjnicola added a commit that referenced this pull request Nov 29, 2021
* Add function get_iface_index()

This functions allows to get the interface's ID where a frame will be sent through, depending on the target's IP

* Extend capture_next_frame() with a new param.

It receives now a new int parameter to tell the function to return the frame only or to include the payload.
It returns now a char * instead of a struct ether_header.

* Add struct pseudo_frame

* Add function to forge a frame

* Add function to prepare a message which includes a frame as payload and prepares the socket for captures

* Add function to dump a frame

* Make new functions visibles

Example usage:
Run the following script with openvas-nasl

```
if(description) {
  script_oid("1.2.3");
  exit(0);
}

include("misc_func.inc");

myhost = this_host();
port = 22022;
srcport= 35000;
dstip = get_host_ip();
src_mac = raw_string (0x08, 0x00, 0x27, 0x8f, 0x69, 0xbb); # get_local_mac_address_from_ip can be used
dst_mac = raw_string (0x54, 0xe1, 0xad, 0xd4, 0xed, 0x74); # send_arp_request() can be used

ether_proto = 0x0800;

filter = string("ether dst ", hexstr(dst_mac), " and src port ", 35000);

ip = forge_ip_packet(ip_v   : 4,
                     ip_hl  : 5,
                     ip_tos : 0,
                     ip_len : 20,
                     ip_id  : rand(),
                     ip_p   : IPPROTO_TCP,
                     ip_ttl : 255,
                     ip_off : 0,
                     ip_src : myhost,
                     ip_dst: dstip);

display (IPPROTO_TCP);

tcp = forge_tcp_packet(ip       : ip,
                       th_ack   : 0,
                       th_dport : port,
		       th_flags : TH_SYN,
                       #th_seq   : tcp_seq + 1024,
		       th_sport : srcport,
                       th_x2    : 0,
                       th_off   : 5,
                       th_win   : 1024,
		       th_urp   : 0,
		       data: "hola mundo");

frame = forge_frame (src_haddr: src_mac, dst_haddr: dst_mac, ether_proto: ether_proto, payload: tcp);

f = send_frame (frame: frame, pcap_filter: filter, timeout: 10);

dump_frame (frame:f);

```

* Fix invalid read/write reported by valgrind

(cherry picked from commit 2550b26)

Co-authored-by: Juan José Nicola <jjnicola@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants