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

ddos_detector.py to monitor DDOS attacks #2140

Merged
merged 9 commits into from
Jan 22, 2019

Conversation

jugurthab
Copy link
Contributor

Written as a basic networking example of using ePBF to detect a potential DDOS attack against a system

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

// use perf buffer (avoid using /sys/kernel/debug/tracing/trace_pipe)
BPF_PERF_OUTPUT(events);

int detect_ddos(struct pt_regs *ctx, struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For unused parameters like "pt", you can just do "void *pt" to avoid compilation warning.

Copy link
Collaborator

@yonghong-song yonghong-song left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you correct some format issues? removing unnecessary empty lines, make sure each line at most having 80 characters, removing any trailing spaces, etc? This will make reviews easier. Thanks!

@yonghong-song
Copy link
Collaborator

also could you change ddos_detector.py to be an executable?

@jugurthab
Copy link
Contributor Author

I have made the changes, thank's a lot for your help.

@@ -0,0 +1,87 @@
#!/usr/bin/env python
#
# ddos_detector.py DDOS dectection system.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls chmod +x ddos_detector.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed permissions and uploaded the file using "upload file option" but seems like this does not keep the permissions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, we can have a followup patch for this as there are a couple of scripts which need change as well.


/* If We receive more than 100 succesive packets with a difference of */
/* timestamp between each one of them is less than 1000000ns */
/* Trigger ALERT */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give more explanation when this could be a real case or a false positive? I am assuming detecting a real ddos is more complex than this.

struct detectionTimestamp detectionTs = {};
// Counts number of received packets
u64 rcv_packets_nb = 0, rcv_packets_nb_inter=1, *rcv_packets_nb_ptr;
// Measures elapsed time between 2 successive received packets
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you give more description of algorithms here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments were added!

3 - ddos_detector.py triggers alerts and reports a DDOS attack:
DDOS detector started ... Hit Ctrl-C to end!
TIME(s) MESSAGE
6714099756390 Attack detected
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you can somehow convert this TIME to a human understandable time, it would be great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, done!

u64 ts;
};

// use perf buffer (avoid using /sys/kernel/debug/tracing/trace_pipe)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, removed!

@jugurthab
Copy link
Contributor Author

Thank's a lot for your review

@@ -0,0 +1,97 @@
#!/usr/bin/env python
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you change this to #!/usr/bin/python? Sorry if this is what I suggested. We just formalized that all python script shebang should be #!/usr/bin/python instead of #!/usr/bin/env python.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, done!

@@ -0,0 +1,87 @@
#!/usr/bin/env python
#
# ddos_detector.py DDOS dectection system.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, we can have a followup patch for this as there are a couple of scripts which need change as well.


BPF_HASH(rcv_packets);

// define C structure
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank's, I have removed the comment!

import datetime
prog = """
#include <linux/skbuff.h>
#include <uapi/linux/ip.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line here for readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, done!

BPF_PERF_OUTPUT(events);

int detect_ddos(struct pt_regs *ctx, void *skb){
struct detectionPackets detectionPacket = {};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty line here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line added!

/* (which is not like regular applications behaviour). */
/* This script looks for this difference in time and if it sees */
/* more than 1000 succesive packets with a difference */
/* of timestamp between each one of them less than 1000000ns, */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please the above 1000 and 1000000 with macro names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done!

rcv_packets_ts_inter = bpf_ktime_get_ns();
rcv_packets.update(&rcv_packets_nb, &rcv_packets_nb_inter);
rcv_packets.update(&rcv_packets_ts_index, &rcv_packets_ts_inter);
return 0; // always return 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment // always return 0 is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment removed!


def trigger_alert_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(DetectionTimestamp)).contents
print("%-26s %s %ld" % (datetime.datetime.now(), \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most other python scripts in bcc does not use /?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just noticed that, thank you. "" removed!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean backslash removed!

@@ -0,0 +1,44 @@
Demonstrations of ddos_detector.py, the Linux eBPF/bcc version.

This tracks ip_rcv function (using kprobe) and elapsed time between received packets to detect potential DDOS attacks.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Break this into two lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, done!

TIME(s) MESSAGE



Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One extra line is enough. The same for below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra lines removed!

@brendangregg
Copy link
Member

Minor comment: if one day someone ever considers moving this from examples/tracing to /tools, then I'd ask to shorten the name to fit with the other /tools. Could call it dddos: short for detect ddos.

@jugurthab
Copy link
Contributor Author

Thank's for your code review. I have made the changes as you have recommended.

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@yonghong-song
Copy link
Collaborator

Looks good. Thanks!

@yonghong-song yonghong-song merged commit c77b158 into iovisor:master Jan 22, 2019
navytux added a commit to navytux/bcc that referenced this pull request Feb 7, 2019
* master: (609 commits)
  docs: references_guide.md: add/fix search examples/tools links (iovisor#2186)
  Fix misc file permissions (iovisor#2185)
  sync with latest bpf (iovisor#2184)
  sync with latest libbpf repo (iovisor#2183)
  docs: fix broken link of bpf_log2l(iovisor#2176)
  examples/tracing: some minor fixes
  Fix tools/syscount -l (iovisor#2180)
  examples/tracing/bitehist.py: add example of linear histogram (iovisor#2177)
  cachestat: bring back HITRATIO column
  Fix debuginfo search on Ubuntu
  Add installation instructions for Amazon Linux 1 AMI Sign-Off-By Travis Davies <trdavies@amazon.com>
  [iovisor/bcc] trace: Incorrect symbol offsets when using build_id (iovisor#2161) (iovisor#2162)
  profile: exclude CPU idle stacks by default (iovisor#2166)
  fix cpuunclaimed.py with cfs_rq structure change (iovisor#2164)
  tools: rename "deadlock_detector" to "deadlock" (iovisor#2152) (iovisor#2160)
  use libbpf api in bpf_attach_xdp (iovisor#2158)
  support symbol resolution of short-lived process.  (iovisor#2144)
  profile.py: return kernel annotations for folded stacks
  use libbpf APIs from libbpf.c (iovisor#2156)
  ddos_detector.py to monitor DDOS attacks (iovisor#2140)
  ...
palexster pushed a commit to palexster/bcc that referenced this pull request Jul 7, 2019
 ddos_detector.py to monitor DDOS attacks
CrackerCat pushed a commit to CrackerCat/bcc that referenced this pull request Jul 31, 2024
 ddos_detector.py to monitor DDOS attacks
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.

3 participants