diff options
| author | Maciej Borowiec <81316152+Mosder@users.noreply.github.com> | 2026-06-14 01:54:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-06-14 01:54:07 +0200 |
| commit | 2e3f7fe39791cd671a68330f7ba7a5bb5fc6f9ae (patch) | |
| tree | bf2f679387bff19637e1183400c0b0290d2d1834 | |
| parent | 35dc15a03ba5cf2dec10d63a1fdc7ab1fb082899 (diff) | |
| parent | c2568d72efca8fae3c610072cb055f9c8de3ca5f (diff) | |
Merge pull request #4 from Mosder/2-simple-capturing-mode
Simple capturing mode
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | Makefile | 16 | ||||
| -rw-r--r-- | README.md | 104 | ||||
| -rw-r--r-- | analyzer/capture.c | 28 | ||||
| -rw-r--r-- | analyzer/capture.h | 8 | ||||
| -rw-r--r-- | analyzer/headers/interface.h | 1 | ||||
| -rw-r--r-- | analyzer/utils/fs.c | 43 | ||||
| -rw-r--r-- | analyzer/utils/fs.h | 31 | ||||
| -rw-r--r-- | analyzer/utils/time.c | 10 | ||||
| -rw-r--r-- | analyzer/utils/time.h | 7 | ||||
| -rw-r--r-- | main.c | 61 |
11 files changed, 308 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc2bf7f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +compile_commands.json +sample_data/ +analyzer/headers/server.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a85df64 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +global: compile + sudo mv branal /usr/bin + +local: compile + mv branal ${HOME}/.local/bin + +compile: utils capture + gcc *.o main.c -o branal + rm *.o + +utils: + gcc -c analyzer/utils/fs.c + gcc -c analyzer/utils/time.c + +capture: + gcc -c analyzer/capture.c @@ -1,6 +1,102 @@ -# A (to be) bare-bones analyzer for the game Broken Ranks +# A bare-bones analyzer for the game Broken Ranks -Yet to be implemented, analyzer for the game Broken Ranks. -Inspired by [BrokenHUD](https://github.com/Brok3nSign/BrokenTools/releases). -Will definitely be way more primitive than the one it's inspired on, because I just can't be bothered to add all those features. +Inspired by [BrokenHUD](https://github.com/Brok3nSign/BrokenTools/releases). +Way more primitive than the one it's inspired by, because I just can't be bothered to add all those features. Unless you value FOSS or have troubles setting up BrokenHUD on your linux system, then you should use [it](https://github.com/Brok3nSign/BrokenTools/releases) instead. + +## Features +- Capturing packets sent by the game and saving them to a file for later use + +### Planned features +- Analyzer for the `.pcap` files containing captured packets: + - List of fight results in the given file with rewards gained for each player + - Total gained rewards in all fights in the given file +- Live analyzer parsing packets in real time: + - Results of the last fight + - Total gained rewards, since starting the analyzer + +## Getting started + +### Prerequisites +- Base utils: `git`, `gcc`, `make` +- [tshark](https://tshark.dev/setup/install/#installing-tshark-only) + +### Installation +**1. Clone and enter the repository:** +``` +git clone https://github.com/Mosder/branal +cd branal +``` + +**2. Create the server header file:** +You will need to find out the IP address of the game server. +To do so, use [termshark](https://github.com/gcla/termshark) or any other tool you'd prefer. +Then, create the file (replace `<ip>` with the IP address you found): +``` +echo "#define SERVER_IP \"<ip>\"" > analyzer/headers/server.h +``` +It's not included in the repository to avoid it being scraped by bots or something. + +**3. Modify the interface header file:** +Right now the used interface is `enp8s0`, which likely isn't the name of your interface. +To find the name of your interface, use: +``` +ip address +``` +Then, modify the file (replace `<interface>` with your interface name): +``` +echo "#define INTERFACE \"<interface>\"" > analyzer/headers/interface.h +``` + +**4. Compile the code**: +Finally, compile the code with `make`. +To install it into `/usr/bin`: +``` +make +``` +that will use sudo and require password. + +You can instead install it into `~/.local/bin`: +``` +make local +``` + +You can also just compile it in place: +``` +make compile +``` +and just move the `branal` binary to your preferred directory. + +## Usage + +### Live analyzer (NOT IMPLEMENTED) +To start live analyzer, use: +``` +branal +``` +or +``` +branal (l | live) +``` +It will open the live analyzer in your terminal. + +### File analyzer (NOT IMPLEMENTED) +To analize a `.pcap` file, use: +``` +branal (f | file) <file> +``` +It will perform analysis of the specified `<file>`. + +### Capturing packets +To just capture packets from the server, use: +``` +branal (c | capture) [file] +``` +It will start capturing packets to the specified `[file]`. +If `[file]` is not specified, it will capture them to the default location (`~/.local/share/branal/captures`). + +### Help +You can display help using: +``` +branal (h | help) +``` diff --git a/analyzer/capture.c b/analyzer/capture.c new file mode 100644 index 0000000..0a75e09 --- /dev/null +++ b/analyzer/capture.c @@ -0,0 +1,28 @@ +#include "capture.h" + +#include <stdio.h> +#include <stdlib.h> + +#include "headers/interface.h" +#include "headers/server.h" +#include "utils/fs.h" +#include "utils/time.h" + +void capture_to_file(char *path) { + char command[128]; + sprintf(command, "tshark -i %s -f \"src host %s\" -w %s", INTERFACE, SERVER_IP, path); + system(command); +} + +void capturing_mode(char *path) { + if (path != NULL) { + capture_to_file(path); + return; + } + + char capture_dir[256], new_path[256], date_time[128]; + get_capture_dir(capture_dir); + get_date_time_string(date_time); + sprintf(new_path, "%s/capture-%s.pcap", capture_dir, date_time); + capture_to_file(new_path); +} diff --git a/analyzer/capture.h b/analyzer/capture.h new file mode 100644 index 0000000..648e5ad --- /dev/null +++ b/analyzer/capture.h @@ -0,0 +1,8 @@ +// capture.h +// functions for capturing packets using tshark + +// starts capturing mode +// params: +// - path - file path to capture the .pcap file to, +// if NULL, saves it to default capture directory as capture-<date>_<time>.pcap +extern void capturing_mode(char *path); diff --git a/analyzer/headers/interface.h b/analyzer/headers/interface.h new file mode 100644 index 0000000..8b407c4 --- /dev/null +++ b/analyzer/headers/interface.h @@ -0,0 +1 @@ +#define INTERFACE "enp8s0" diff --git a/analyzer/utils/fs.c b/analyzer/utils/fs.c new file mode 100644 index 0000000..76fb581 --- /dev/null +++ b/analyzer/utils/fs.c @@ -0,0 +1,43 @@ +#include "utils.h" + +#include <fcntl.h> +#include <sys/stat.h> +#include <unistd.h> + +void get_home_dir(char *home_dir) { + strcpy(home_dir, getenv("HOME")); +} + +void get_main_dir(char *main_dir) { + char home_dir[256]; + get_home_dir(home_dir); + sprintf(main_dir, "%s/.local/share/branal", home_dir); +} + +void get_capture_dir(char *capture_dir) { + char main_dir[256]; + get_main_dir(main_dir); + sprintf(capture_dir, "%s/captures", main_dir); +} + +void get_saves_dir(char *saves_dir) { + char main_dir[256]; + get_main_dir(main_dir); + sprintf(saves_dir, "%s/saves", main_dir); +} + +void create_necessary_directories() { + char main_dir[256], capture_dir[256], saves_dir[256]; + get_main_dir(main_dir); + get_capture_dir(capture_dir); + get_saves_dir(saves_dir); + mkdir(main_dir, 0755); + mkdir(capture_dir, 0755); + mkdir(saves_dir, 0755); +} + +void write_file(char *path, void *contents, int contents_len, int flags, mode_t permissions) { + int fd = open(path, flags, permissions); + write(fd, contents, contents_len); + close(fd); +} diff --git a/analyzer/utils/fs.h b/analyzer/utils/fs.h new file mode 100644 index 0000000..6335799 --- /dev/null +++ b/analyzer/utils/fs.h @@ -0,0 +1,31 @@ +// utils/fs.h +// functions for operating on the file system, i.e. creating directories, writing files, etc. + +#include <sys/types.h> + +// get main directory used by the program +// params: +// - main_dir - buffer to save the directory to +extern void get_main_dir(char *main_dir); + +// get directory to save the .pcap files to +// params: +// - capture_dir - buffer to save the directory to +extern void get_capture_dir(char *capture_dir); + +// get directory of save data of the program +// params: +// - saves_dir - buffer to save the directory to +extern void get_saves_dir(char *saves_dir); + +// create directories that will be used by the program +extern void create_necessary_directories(); + +// writes file to the specified path +// params: +// - path - path to write the file to +// - contents - contents of the file +// - contents_len - amount of bytes of contents to write +// - flags - flags to open the file with +// - permissions - permissions of the written file +extern void write_file(char *path, void *contents, int contents_len, int flags, mode_t permissions); diff --git a/analyzer/utils/time.c b/analyzer/utils/time.c new file mode 100644 index 0000000..72c0bc7 --- /dev/null +++ b/analyzer/utils/time.c @@ -0,0 +1,10 @@ +#include "time.h" + +#include <stdio.h> +#include <time.h> + +void get_date_time_string(char *date_time) { + time_t now = time(NULL); + struct tm *t = localtime(&now); + sprintf(date_time, "%02d%02d%02d_%02d%02d%02d", t->tm_year - 100, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); +} diff --git a/analyzer/utils/time.h b/analyzer/utils/time.h new file mode 100644 index 0000000..0bd0df1 --- /dev/null +++ b/analyzer/utils/time.h @@ -0,0 +1,7 @@ +// utils/time.h +// functions regarding date and time + +// gets a string <date>_<time> for current localtime +// params: +// date_time - buffer to save the datetime string to +extern void get_date_time_string(char *date_time); @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <string.h> + +#include "analyzer/capture.h" +#include "analyzer/utils/fs.h" + +// prints help +void print_help() { + printf("A bare-bones analyzer for the game Broken Ranks\n"); + printf("\nUsage:\n"); + printf(" branal - start live analysis\n"); + printf(" branal (l | live) - start live analysis (same as above)\n"); + printf(" branal (c | capture) [file] - start capturing packets to [file].\n"); + printf(" If [file] not given - captures to $HOME/.local/share/branal/captures\n"); + printf(" branal (f | file) <file> - analyze the specified <file>\n"); + printf(" branal (h | help) - display this help\n"); +} + +// starts live analyzer mode +void start_live_analyzer() { + // TODO: implement + printf("Not implemented\n"); +} + +// analyzes the specified file +// params: +// - file_path - path to the .pcap file to be analyzed +void analyze_file(char *file_path) { + // TODO: implement + printf("Not implemented\n"); +} + +int main(int argc, char *argv[]) { + create_necessary_directories(); + + // start live analysis if no arguments provided + if (argc < 2) { + start_live_analyzer(); + return 0; + } + + if (strcmp("l", argv[1]) == 0 || strcmp("live", argv[1]) == 0) { + start_live_analyzer(); + } + else if (strcmp("c", argv[1]) == 0 || strcmp("capture", argv[1]) == 0) { + capturing_mode(argc >= 3 ? argv[2] : NULL); + } + else if (strcmp("h", argv[1]) == 0 || strcmp("help", argv[1]) == 0) { + print_help(); + } + else if (argc >= 3 && (strcmp("f", argv[1]) == 0 || strcmp("file", argv[1]) == 0)) { + analyze_file(argv[2]); + } + else { + printf("Command not recognized\n"); + printf("Use \"branal (h | help)\" for help\n"); + return 1; + } + + return 0; +} |
