├── .github └── workflows │ └── build.yml ├── .gitignore ├── CHANGES ├── LICENSE ├── README.md ├── bin └── .hold └── src ├── Makefile ├── cap2hccapx.c ├── cleanup-rules.c ├── combinator.c ├── combinator3.c ├── combinatorX.c ├── combipow.c ├── cpu_rules.c ├── cpu_rules.h ├── ct3_to_ntlm.c ├── cutb.c ├── deskey_to_ntlm.pl ├── expander.c ├── export_potfile.py ├── gate.c ├── generate-rules.c ├── hcstat2gen.c ├── hcstatgen.c ├── keyspace.c ├── len.c ├── mli2.c ├── morph.c ├── permute.c ├── permute_exist.c ├── prepare.c ├── remaining.pl ├── req-exclude.c ├── req-include.c ├── rli.c ├── rli2.c ├── rp_cpu.h ├── rules_optimize.c ├── seprule.pl ├── splitlen.c ├── strip-bsn.c ├── strip-bsr.c ├── tmesis-dynamic.pl ├── tmesis.pl ├── topmorph.pl └── utils.c /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build Hashcat Utils 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | tags: 8 | - v* 9 | paths: 10 | - 'src/**' 11 | - '.github/workflows/build.yml' 12 | pull_request: 13 | branches: 14 | - master 15 | paths: 16 | - 'src/**' 17 | - '.github/workflows/build.yml' 18 | 19 | jobs: 20 | build-linux: 21 | strategy: 22 | fail-fast: false 23 | name: Build Linux 24 | runs-on: ubuntu-latest 25 | steps: 26 | - uses: actions/checkout@v3 27 | - name: Build 28 | run: cd src/ && make 29 | - name: Generate artifacts 30 | uses: actions/upload-artifact@v3 31 | with: 32 | name: hashcat-utils-linux 33 | path: src/*.bin 34 | 35 | build-macos: 36 | strategy: 37 | fail-fast: false 38 | name: Build macOS 39 | runs-on: macos-latest 40 | steps: 41 | - uses: actions/checkout@v3 42 | - name: Build 43 | run: cd src/ && make 44 | - name: Generate artifacts 45 | uses: actions/upload-artifact@v3 46 | with: 47 | name: hashcat-utils-macos 48 | path: src/*.bin 49 | 50 | build-windows: 51 | strategy: 52 | fail-fast: false 53 | name: Build Windows 54 | runs-on: windows-latest 55 | steps: 56 | - name: Install MSys2 57 | uses: msys2/setup-msys2@v2 58 | with: 59 | update: true 60 | install: | 61 | make 62 | mingw-w64-x86_64-gcc 63 | - uses: actions/checkout@v3 64 | # Paths aren't correct in MSys2 for mingw, copy to the correct path for the Makefile 65 | - name: Copy CRT_glob.o 66 | shell: msys2 {0} 67 | run: mkdir -p D:/a/_temp/msys64/usr/x86_64-w64-mingw32/lib && cp D:/a/_temp/msys64/mingw64/lib/CRT_glob.o D:/a/_temp/msys64/usr/x86_64-w64-mingw32/lib/CRT_glob.o 68 | - name: Build 69 | shell: msys2 {0} 70 | run: cd src/ && make windows 71 | - name: Generate artifacts 72 | uses: actions/upload-artifact@v3 73 | with: 74 | name: hashcat-utils-windows 75 | path: src/*.exe 76 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.bin 2 | *.exe 3 | *.dSYM 4 | bin/ 5 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | * v1.8 -> v1.9 2 | 3 | - Switched C standard from c99 to gnu99 4 | - Removed unused -lm requirement from hcstatgen 5 | - Increased maximum password length for hcstatgen from 64 to 256 6 | - Switched some datatypes and cleaned up in hcstatgen.c 7 | - Switched some datatypes and cleaned up in utils.c 8 | - Added hcstat2gen.c which is like hcstatgen but supports a maximum password length up to 256 and header 9 | - Fixed prioritized bssid-to-essid database ordering 10 | 11 | * v1.7 -> v1.8 12 | 13 | - Fix radiotap header parsing in cap2hccapx.c 14 | - Check for zero timestamp and abort if detected in cap2hccapx.c 15 | - Decrease EAPOL timeout from 2 to 1 in cap2hccapx.c 16 | - Disable check for matching replay count in cap2hccap.c (can be reenabled by setting TEST_REPLAYCOUNT to 1) 17 | - Set 0x80 to hccapx.message_pair in case TEST_REPLAYCOUNT is set to 1 in cap2hccapx.c 18 | - Add support for ppi header in cap2hccapx.c 19 | - Add support for extracting essid from assoc and reassoc requests in cap2hccapx.c 20 | - Prioritized bssid-to-essid database in cap2hccapx.c, see #27 21 | 22 | * v1.6 -> v1.7 23 | 24 | - Skip essid if it's length 0 or if first by is zero-byte (hidden network) 25 | - Allow reading broken .cap files even it has been cut short in the middle of a packet 26 | - Allow Message-Pair 1+2, 1+4, 2+3 and 3+4 in cap2hccapx.c 27 | - Update cap2hccapx format to version 4 28 | 29 | * v1.5 -> v1.6 30 | 31 | - Added support to read prism header with cap2hccapx 32 | - Fixed timestamp handling in cap2hccapx 33 | 34 | * v1.4 -> v1.5 35 | 36 | - Added support to read radiotap header with cap2hccapx 37 | - Fixed packet handling for cap2hccapx on windows 38 | 39 | * v1.3 -> v1.4 40 | 41 | - Add pragma to cap2hccapx.c to workaround broken packed handling on mingw 42 | 43 | * v1.2 -> v1.3 44 | 45 | - Added cap2hccapx.c 46 | - Added ct3_to_ntlm.c 47 | - Added deskey_to_ntlm.pl 48 | - Added tmesis.pl 49 | - Added release makefile target: move binaries from src folder into bin folder 50 | - cleanup-rules: fixes #12, allow @X (purge) rule for the rule-engine on GPU 51 | 52 | * v1.1 -> v1.2 53 | 54 | - Open Source the project 55 | - License is MIT 56 | - Moved repository to github: https://github.com/hashcat/hashcat-utils 57 | - Added CHANGES 58 | - Added LICENSE 59 | - Added README.md 60 | 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2018 Jens Steube 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | hashcat-utils 2 | ============== 3 | 4 | Hashcat-utils are a set of small utilities that are useful in advanced password cracking 5 | 6 | Brief description 7 | -------------- 8 | 9 | They all are packed into multiple stand-alone binaries. 10 | 11 | All of these utils are designed to execute only one specific function. 12 | 13 | Since they all work with STDIN and STDOUT you can group them into chains. 14 | 15 | Detailed description 16 | -------------- 17 | 18 | See the hashcat wiki page of hashcat-utils: https://hashcat.net/wiki/doku.php?id=hashcat_utils 19 | 20 | Compile 21 | -------------- 22 | 23 | Simply run make 24 | 25 | Binary distribution 26 | -------------- 27 | 28 | Binaries for Linux, Windows and OSX: https://github.com/hashcat/hashcat-utils/releases 29 | -------------------------------------------------------------------------------- /bin/.hold: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hashcat/hashcat-utils/a814cebc1c4226099f3e5cb7b08116dfd4322a6b/bin/.hold -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile 3 | ## 4 | 5 | DEBUG := 0 6 | 7 | CFLAGS += -Wall -W -pipe -std=gnu99 8 | 9 | ifeq ($(DEBUG),0) 10 | CFLAGS += -O2 11 | ifneq ($(UNAME),Darwin) 12 | LFLAGS += -s 13 | endif 14 | else 15 | ifeq ($(DEBUG),1) 16 | ifneq ($(UNAME),Darwin) 17 | CFLAGS += -DDEBUG -Og -ggdb 18 | else 19 | CFLAGS += -DDEBUG -O0 -ggdb 20 | endif 21 | else 22 | ifeq ($(DEBUG),2) 23 | ifneq ($(UNAME),Darwin) 24 | CFLAGS += -DDEBUG -Og -ggdb 25 | else 26 | CFLAGS += -DDEBUG -O0 -ggdb 27 | endif 28 | CFLAGS += -fsanitize=address -fno-omit-frame-pointer 29 | endif 30 | endif 31 | endif 32 | 33 | all: clean native 34 | 35 | release: native windows 36 | $(STRIP_NATIVE) *.bin 37 | $(STRIP_WINDOWS) *.exe 38 | mv *.bin ../bin 39 | mv *.exe ../bin 40 | cp -a *.pl ../bin 41 | 42 | clean: 43 | rm -f ../bin/* 44 | rm -f *.bin *.exe 45 | rm -rf *.dSYM 46 | 47 | ## 48 | ## native 49 | ## 50 | 51 | CC ?= gcc 52 | CC_NATIVE = $(CC) 53 | STRIP_NATIVE = strip 54 | CFLAGS_NATIVE = $(CFLAGS) 55 | LDFLAGS_NATIVE = $(LDFLAGS) 56 | 57 | native: cap2hccapx.bin cleanup-rules.bin combinator.bin combinator3.bin combinatorX.bin combipow.bin ct3_to_ntlm.bin cutb.bin expander.bin gate.bin generate-rules.bin hcstatgen.bin hcstat2gen.bin keyspace.bin len.bin mli2.bin morph.bin permute.bin permute_exist.bin prepare.bin req-include.bin req-exclude.bin rli.bin rli2.bin rules_optimize.bin splitlen.bin strip-bsr.bin strip-bsn.bin 58 | 59 | cap2hccapx.bin: cap2hccapx.c 60 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 61 | 62 | cleanup-rules.bin: cleanup-rules.c 63 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 64 | 65 | combinator.bin: combinator.c 66 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 67 | 68 | combinator3.bin: combinator3.c 69 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 70 | 71 | combinatorX.bin: combinatorX.c 72 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 73 | 74 | combipow.bin: combipow.c 75 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 76 | 77 | ct3_to_ntlm.bin: ct3_to_ntlm.c 78 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 79 | 80 | cutb.bin: cutb.c 81 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 82 | 83 | expander.bin: expander.c 84 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 85 | 86 | gate.bin: gate.c 87 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 88 | 89 | generate-rules.bin: generate-rules.c 90 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 91 | 92 | hcstatgen.bin: hcstatgen.c 93 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 94 | 95 | hcstat2gen.bin: hcstat2gen.c 96 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 97 | 98 | keyspace.bin: keyspace.c 99 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 100 | 101 | len.bin: len.c 102 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 103 | 104 | mli2.bin: mli2.c 105 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 106 | 107 | morph.bin: morph.c 108 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 109 | 110 | permute.bin: permute.c 111 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 112 | 113 | permute_exist.bin: permute_exist.c 114 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 115 | 116 | prepare.bin: prepare.c 117 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 118 | 119 | req-include.bin: req-include.c 120 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 121 | 122 | req-exclude.bin: req-exclude.c 123 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 124 | 125 | rli.bin: rli.c 126 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 127 | 128 | rli2.bin: rli2.c 129 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 130 | 131 | rules_optimize.bin: rules_optimize.c cpu_rules.c 132 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ rules_optimize.c cpu_rules.c 133 | 134 | splitlen.bin: splitlen.c 135 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 136 | 137 | strip-bsr.bin: strip-bsr.c 138 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 139 | 140 | strip-bsn.bin: strip-bsn.c 141 | ${CC_NATIVE} ${CFLAGS_NATIVE} ${LDFLAGS_NATIVE} -o $@ $< 142 | 143 | ## 144 | ## WINDOWS 145 | ## 146 | 147 | CC_WINDOWS = x86_64-w64-mingw32-gcc 148 | STRIP_WINDOWS = x86_64-w64-mingw32-strip 149 | CFLAGS_WINDOWS = $(CFLAGS) -D_WINDOWS 150 | GLOB_WINDOWS = /usr/x86_64-w64-mingw32/lib/CRT_glob.o 151 | 152 | windows: cap2hccapx.exe cleanup-rules.exe combinator.exe combinator3.exe combinatorX.exe combipow.exe ct3_to_ntlm.exe cutb.exe expander.exe gate.exe generate-rules.exe hcstatgen.exe hcstat2gen.exe keyspace.exe len.exe mli2.exe morph.exe permute.exe permute_exist.exe prepare.exe req-include.exe req-exclude.exe rli.exe rli2.exe rules_optimize.exe splitlen.exe strip-bsr.exe strip-bsn.exe 153 | 154 | cap2hccapx.exe: cap2hccapx.c 155 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 156 | 157 | cleanup-rules.exe: cleanup-rules.c 158 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 159 | 160 | combinator.exe: combinator.c 161 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 162 | 163 | combinator3.exe: combinator3.c 164 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 165 | 166 | combinatorX.exe: combinatorX.c 167 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 168 | 169 | combipow.exe: combipow.c 170 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 171 | 172 | ct3_to_ntlm.exe: ct3_to_ntlm.c 173 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 174 | 175 | cutb.exe: cutb.c 176 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 177 | 178 | expander.exe: expander.c 179 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 180 | 181 | gate.exe: gate.c 182 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 183 | 184 | generate-rules.exe: generate-rules.c 185 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 186 | 187 | hcstatgen.exe: hcstatgen.c 188 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 189 | 190 | hcstat2gen.exe: hcstat2gen.c 191 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 192 | 193 | keyspace.exe: keyspace.c 194 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 195 | 196 | len.exe: len.c 197 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 198 | 199 | mli2.exe: mli2.c 200 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 201 | 202 | morph.exe: morph.c 203 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 204 | 205 | permute.exe: permute.c 206 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 207 | 208 | permute_exist.exe: permute_exist.c 209 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 210 | 211 | prepare.exe: prepare.c 212 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 213 | 214 | req-include.exe: req-include.c 215 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 216 | 217 | req-exclude.exe: req-exclude.c 218 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 219 | 220 | rli.exe: rli.c 221 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ rli.c ${GLOB_WINDOWS} 222 | 223 | rli2.exe: rli2.c 224 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 225 | 226 | rules_optimize.exe: rules_optimize.c cpu_rules.c 227 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ rules_optimize.c cpu_rules.c 228 | 229 | splitlen.exe: splitlen.c 230 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 231 | 232 | strip-bsr.exe: strip-bsr.c 233 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 234 | 235 | strip-bsn.exe: strip-bsn.c 236 | ${CC_WINDOWS} ${CFLAGS_WINDOWS} -o $@ $< 237 | -------------------------------------------------------------------------------- /src/cleanup-rules.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: cleanup-rules 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | */ 19 | 20 | #define RULE_OP_MANGLE_NOOP ':' 21 | #define RULE_OP_MANGLE_LREST 'l' 22 | #define RULE_OP_MANGLE_UREST 'u' 23 | #define RULE_OP_MANGLE_LREST_UFIRST 'c' 24 | #define RULE_OP_MANGLE_UREST_LFIRST 'C' 25 | #define RULE_OP_MANGLE_TREST 't' 26 | #define RULE_OP_MANGLE_TOGGLE_AT 'T' 27 | #define RULE_OP_MANGLE_REVERSE 'r' 28 | #define RULE_OP_MANGLE_DUPEWORD 'd' 29 | #define RULE_OP_MANGLE_DUPEWORD_TIMES 'p' 30 | #define RULE_OP_MANGLE_REFLECT 'f' 31 | #define RULE_OP_MANGLE_ROTATE_LEFT '{' 32 | #define RULE_OP_MANGLE_ROTATE_RIGHT '}' 33 | #define RULE_OP_MANGLE_APPEND '$' 34 | #define RULE_OP_MANGLE_PREPEND '^' 35 | #define RULE_OP_MANGLE_DELETE_FIRST '[' 36 | #define RULE_OP_MANGLE_DELETE_LAST ']' 37 | #define RULE_OP_MANGLE_DELETE_AT 'D' 38 | #define RULE_OP_MANGLE_EXTRACT 'x' 39 | #define RULE_OP_MANGLE_INSERT 'i' 40 | #define RULE_OP_MANGLE_OVERSTRIKE 'o' 41 | #define RULE_OP_MANGLE_TRUNCATE_AT '\'' 42 | #define RULE_OP_MANGLE_REPLACE 's' 43 | #define RULE_OP_MANGLE_PURGECHAR '@' 44 | #define RULE_OP_MANGLE_TOGGLECASE_REC 'a' 45 | #define RULE_OP_MANGLE_DUPECHAR_FIRST 'z' 46 | #define RULE_OP_MANGLE_DUPECHAR_LAST 'Z' 47 | #define RULE_OP_MANGLE_DUPECHAR_ALL 'q' 48 | #define RULE_OP_MANGLE_EXTRACT_MEMORY 'X' 49 | #define RULE_OP_MANGLE_APPEND_MEMORY '4' 50 | #define RULE_OP_MANGLE_PREPEND_MEMORY '6' 51 | 52 | #define RULE_OP_MEMORIZE_WORD 'M' 53 | 54 | #define RULE_OP_REJECT_LESS '<' 55 | #define RULE_OP_REJECT_GREATER '>' 56 | #define RULE_OP_REJECT_CONTAIN '!' 57 | #define RULE_OP_REJECT_NOT_CONTAIN '/' 58 | #define RULE_OP_REJECT_EQUAL_FIRST '(' 59 | #define RULE_OP_REJECT_EQUAL_LAST ')' 60 | #define RULE_OP_REJECT_EQUAL_AT '=' 61 | #define RULE_OP_REJECT_CONTAINS '%' 62 | #define RULE_OP_REJECT_MEMORY 'Q' 63 | 64 | /* hashcat only */ 65 | #define RULE_OP_MANGLE_SWITCH_FIRST 'k' 66 | #define RULE_OP_MANGLE_SWITCH_LAST 'K' 67 | #define RULE_OP_MANGLE_SWITCH_AT '*' 68 | #define RULE_OP_MANGLE_CHR_SHIFTL 'L' 69 | #define RULE_OP_MANGLE_CHR_SHIFTR 'R' 70 | #define RULE_OP_MANGLE_CHR_INCR '+' 71 | #define RULE_OP_MANGLE_CHR_DECR '-' 72 | #define RULE_OP_MANGLE_REPLACE_NP1 '.' 73 | #define RULE_OP_MANGLE_REPLACE_NM1 ',' 74 | #define RULE_OP_MANGLE_DUPEBLOCK_FIRST 'y' 75 | #define RULE_OP_MANGLE_DUPEBLOCK_LAST 'Y' 76 | #define RULE_OP_MANGLE_TITLE 'E' 77 | 78 | #define ATTACK_EXEC_ON_CPU 1 79 | #define ATTACK_EXEC_ON_GPU 2 80 | 81 | #define MAX_CPU_RULES 255 // this is defined in include/types.h (hashcat) 82 | #define MAX_GPU_RULES 255 83 | 84 | static int class_num (const char c) 85 | { 86 | return ((c >= '0') && (c <= '9')); 87 | } 88 | 89 | static int class_upper (const char c) 90 | { 91 | return ((c >= 'A') && (c <= 'Z')); 92 | } 93 | 94 | static char conv_ctoi (const char c) 95 | { 96 | if (class_num (c)) 97 | { 98 | return (char)(c - '0'); 99 | } 100 | else if (class_upper (c)) 101 | { 102 | return (char)(c - 'A' + 10); 103 | } 104 | 105 | return -1; 106 | } 107 | 108 | int main (int argc, char *argv[]) 109 | { 110 | if (argc != 2) 111 | { 112 | fprintf (stderr, "usage: %s mode\n", argv[0]); 113 | 114 | return (-1); 115 | } 116 | 117 | int num = atoi (argv[1]); 118 | 119 | if ((num != ATTACK_EXEC_ON_CPU) && (num != ATTACK_EXEC_ON_GPU)) 120 | { 121 | fprintf (stderr, "mode: 1 = CPU, 2 = GPU\n"); 122 | 123 | return (-1); 124 | } 125 | 126 | #ifdef _WINDOWS 127 | _setmode (_fileno (stdin), _O_BINARY); 128 | #endif 129 | 130 | char line_buf[BUFSIZ]; 131 | 132 | int line_len; 133 | 134 | #define NEXT_RULEPOS if (++pos == line_len) rc = -1; 135 | #define NEXT_RPTOI if (conv_ctoi (line_buf[pos]) == -1) rc = -1; 136 | #define DENY_GPU if (num == ATTACK_EXEC_ON_GPU) rc = -1; 137 | 138 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 139 | { 140 | int rc = 0; 141 | 142 | int cnt = 0; 143 | 144 | int pos; 145 | 146 | for (pos = 0; pos < line_len; pos++) 147 | { 148 | switch (line_buf[pos]) 149 | { 150 | case ' ': 151 | continue; // just skip all spaces around rules 152 | 153 | case RULE_OP_MANGLE_NOOP: 154 | break; 155 | 156 | case RULE_OP_MANGLE_LREST: 157 | break; 158 | 159 | case RULE_OP_MANGLE_UREST: 160 | break; 161 | 162 | case RULE_OP_MANGLE_LREST_UFIRST: 163 | break; 164 | 165 | case RULE_OP_MANGLE_UREST_LFIRST: 166 | break; 167 | 168 | case RULE_OP_MANGLE_TREST: 169 | break; 170 | 171 | case RULE_OP_MANGLE_TOGGLE_AT: 172 | NEXT_RULEPOS; 173 | NEXT_RPTOI; 174 | break; 175 | 176 | case RULE_OP_MANGLE_REVERSE: 177 | break; 178 | 179 | case RULE_OP_MANGLE_DUPEWORD: 180 | break; 181 | 182 | case RULE_OP_MANGLE_DUPEWORD_TIMES: 183 | NEXT_RULEPOS; 184 | NEXT_RPTOI; 185 | break; 186 | 187 | case RULE_OP_MANGLE_REFLECT: 188 | break; 189 | 190 | case RULE_OP_MANGLE_ROTATE_LEFT: 191 | break; 192 | 193 | case RULE_OP_MANGLE_ROTATE_RIGHT: 194 | break; 195 | 196 | case RULE_OP_MANGLE_APPEND: 197 | NEXT_RULEPOS; 198 | break; 199 | 200 | case RULE_OP_MANGLE_PREPEND: 201 | NEXT_RULEPOS; 202 | break; 203 | 204 | case RULE_OP_MANGLE_DELETE_FIRST: 205 | break; 206 | 207 | case RULE_OP_MANGLE_DELETE_LAST: 208 | break; 209 | 210 | case RULE_OP_MANGLE_DELETE_AT: 211 | NEXT_RULEPOS; 212 | NEXT_RPTOI; 213 | break; 214 | 215 | case RULE_OP_MANGLE_EXTRACT: 216 | NEXT_RULEPOS; 217 | NEXT_RPTOI; 218 | NEXT_RULEPOS; 219 | NEXT_RPTOI; 220 | break; 221 | 222 | case RULE_OP_MANGLE_INSERT: 223 | NEXT_RULEPOS; 224 | NEXT_RPTOI; 225 | NEXT_RULEPOS; 226 | break; 227 | 228 | case RULE_OP_MANGLE_OVERSTRIKE: 229 | NEXT_RULEPOS; 230 | NEXT_RPTOI; 231 | NEXT_RULEPOS; 232 | break; 233 | 234 | case RULE_OP_MANGLE_TRUNCATE_AT: 235 | NEXT_RULEPOS; 236 | NEXT_RPTOI; 237 | break; 238 | 239 | case RULE_OP_MANGLE_REPLACE: 240 | NEXT_RULEPOS; 241 | NEXT_RULEPOS; 242 | break; 243 | 244 | case RULE_OP_MANGLE_PURGECHAR: 245 | NEXT_RULEPOS; 246 | break; 247 | 248 | case RULE_OP_MANGLE_TOGGLECASE_REC: 249 | break; 250 | 251 | case RULE_OP_MANGLE_DUPECHAR_FIRST: 252 | NEXT_RULEPOS; 253 | NEXT_RPTOI; 254 | break; 255 | 256 | case RULE_OP_MANGLE_DUPECHAR_LAST: 257 | NEXT_RULEPOS; 258 | NEXT_RPTOI; 259 | break; 260 | 261 | case RULE_OP_MANGLE_DUPECHAR_ALL: 262 | break; 263 | 264 | case RULE_OP_MANGLE_DUPEBLOCK_FIRST: 265 | NEXT_RULEPOS; 266 | NEXT_RPTOI; 267 | break; 268 | 269 | case RULE_OP_MANGLE_DUPEBLOCK_LAST: 270 | NEXT_RULEPOS; 271 | NEXT_RPTOI; 272 | break; 273 | 274 | case RULE_OP_MANGLE_SWITCH_FIRST: 275 | break; 276 | 277 | case RULE_OP_MANGLE_SWITCH_LAST: 278 | break; 279 | 280 | case RULE_OP_MANGLE_SWITCH_AT: 281 | NEXT_RULEPOS; 282 | NEXT_RPTOI; 283 | NEXT_RULEPOS; 284 | NEXT_RPTOI; 285 | break; 286 | 287 | case RULE_OP_MANGLE_CHR_SHIFTL: 288 | NEXT_RULEPOS; 289 | NEXT_RPTOI; 290 | break; 291 | 292 | case RULE_OP_MANGLE_CHR_SHIFTR: 293 | NEXT_RULEPOS; 294 | NEXT_RPTOI; 295 | break; 296 | 297 | case RULE_OP_MANGLE_CHR_INCR: 298 | NEXT_RULEPOS; 299 | NEXT_RPTOI; 300 | break; 301 | 302 | case RULE_OP_MANGLE_CHR_DECR: 303 | NEXT_RULEPOS; 304 | NEXT_RPTOI; 305 | break; 306 | 307 | case RULE_OP_MANGLE_REPLACE_NP1: 308 | NEXT_RULEPOS; 309 | NEXT_RPTOI; 310 | break; 311 | 312 | case RULE_OP_MANGLE_REPLACE_NM1: 313 | NEXT_RULEPOS; 314 | NEXT_RPTOI; 315 | break; 316 | 317 | case RULE_OP_MANGLE_TITLE: 318 | break; 319 | 320 | case RULE_OP_MANGLE_EXTRACT_MEMORY: 321 | NEXT_RULEPOS; 322 | NEXT_RPTOI; 323 | NEXT_RULEPOS; 324 | NEXT_RPTOI; 325 | NEXT_RULEPOS; 326 | NEXT_RPTOI; 327 | DENY_GPU; 328 | break; 329 | 330 | case RULE_OP_MANGLE_APPEND_MEMORY: 331 | DENY_GPU; 332 | break; 333 | 334 | case RULE_OP_MANGLE_PREPEND_MEMORY: 335 | DENY_GPU; 336 | break; 337 | 338 | case RULE_OP_MEMORIZE_WORD: 339 | DENY_GPU; 340 | break; 341 | 342 | case RULE_OP_REJECT_LESS: 343 | NEXT_RULEPOS; 344 | NEXT_RPTOI; 345 | DENY_GPU; 346 | break; 347 | 348 | case RULE_OP_REJECT_GREATER: 349 | NEXT_RULEPOS; 350 | NEXT_RPTOI; 351 | DENY_GPU; 352 | break; 353 | 354 | case RULE_OP_REJECT_CONTAIN: 355 | NEXT_RULEPOS; 356 | DENY_GPU; 357 | break; 358 | 359 | case RULE_OP_REJECT_NOT_CONTAIN: 360 | NEXT_RULEPOS; 361 | DENY_GPU; 362 | break; 363 | 364 | case RULE_OP_REJECT_EQUAL_FIRST: 365 | NEXT_RULEPOS; 366 | DENY_GPU; 367 | break; 368 | 369 | case RULE_OP_REJECT_EQUAL_LAST: 370 | NEXT_RULEPOS; 371 | DENY_GPU; 372 | break; 373 | 374 | case RULE_OP_REJECT_EQUAL_AT: 375 | NEXT_RULEPOS; 376 | NEXT_RPTOI; 377 | NEXT_RULEPOS; 378 | DENY_GPU; 379 | break; 380 | 381 | case RULE_OP_REJECT_CONTAINS: 382 | NEXT_RULEPOS; 383 | NEXT_RPTOI; 384 | NEXT_RULEPOS; 385 | DENY_GPU; 386 | break; 387 | 388 | case RULE_OP_REJECT_MEMORY: 389 | DENY_GPU; 390 | break; 391 | 392 | default: 393 | rc = -1; 394 | break; 395 | } 396 | 397 | if (rc == -1) break; 398 | 399 | cnt++; 400 | 401 | if ((num == ATTACK_EXEC_ON_CPU) && (cnt > MAX_CPU_RULES)) 402 | { 403 | rc = -1; 404 | 405 | break; 406 | } 407 | 408 | if ((num == ATTACK_EXEC_ON_GPU) && (cnt > MAX_GPU_RULES)) 409 | { 410 | rc = -1; 411 | 412 | break; 413 | } 414 | } 415 | 416 | if (rc == -1) continue; 417 | 418 | puts (line_buf); 419 | } 420 | 421 | return 0; 422 | } 423 | -------------------------------------------------------------------------------- /src/combinator.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define LEN_MAX 32 15 | 16 | #define SEGMENT_SIZE (32 * 1024 * 1024) 17 | #define SEGMENT_ALIGN ( 8 * 1024) 18 | 19 | /** 20 | * Name........: combinator 21 | * Autor.......: Jens Steube 22 | * License.....: MIT 23 | */ 24 | 25 | static size_t read_segment (char *buf, FILE *fd) 26 | { 27 | size_t read_sz = SEGMENT_SIZE - SEGMENT_ALIGN; 28 | 29 | size_t real_sz = fread (buf, 1, read_sz, fd); 30 | 31 | if (real_sz == 0) return (0); 32 | 33 | if (real_sz != read_sz) 34 | { 35 | if (buf[real_sz - 1] != '\n') 36 | { 37 | real_sz++; 38 | 39 | buf[real_sz - 1] = '\n'; 40 | } 41 | 42 | return (real_sz); 43 | } 44 | 45 | size_t extra; 46 | 47 | for (extra = 0; extra < SEGMENT_ALIGN; extra++) 48 | { 49 | if (fread (buf + real_sz, 1, 1, fd) == 0) break; 50 | 51 | real_sz++; 52 | 53 | if (buf[real_sz - 1] == '\n') break; 54 | } 55 | 56 | return (real_sz); 57 | } 58 | 59 | static size_t get_line_len (char *pos, char *max) 60 | { 61 | char *cur = pos; 62 | 63 | for (cur = pos; cur < max; cur++) 64 | { 65 | if (*cur == '\n') break; 66 | } 67 | 68 | size_t len = cur - pos; 69 | 70 | return (len); 71 | } 72 | 73 | static void add (char *ptr_out, char *ptr_in1, size_t len_in1, char *ptr_in2, size_t len_in2) 74 | { 75 | memcpy (ptr_out, ptr_in1, len_in1); 76 | 77 | ptr_out += len_in1; 78 | 79 | memcpy (ptr_out, ptr_in2, len_in2); 80 | 81 | ptr_out += len_in2; 82 | 83 | *ptr_out = '\n'; 84 | } 85 | 86 | int main (int argc, char *argv[]) 87 | { 88 | if (argc != 3) 89 | { 90 | fprintf (stderr, "usage: %s file1 file2\nfile1 can be \"-\" for stdin.", argv[0]); 91 | 92 | return (-1); 93 | } 94 | 95 | size_t sz_buf = SEGMENT_SIZE + SEGMENT_ALIGN; 96 | 97 | char *buf_in1 = (char *) malloc (sz_buf); 98 | char *buf_in2 = (char *) malloc (sz_buf); 99 | 100 | char *buf_out = (char *) malloc (sz_buf); 101 | 102 | char *ptr_out = buf_out; 103 | 104 | FILE *fd1; 105 | FILE *fd2; 106 | 107 | if (!strcmp (argv[1], "-")) 108 | { 109 | fd1 = stdin; 110 | } 111 | else 112 | { 113 | if ((fd1 = fopen (argv[1], "rb")) == NULL) 114 | { 115 | fprintf (stderr, "%s: %s\n", argv[1], strerror (errno)); 116 | 117 | free (buf_in1); 118 | free (buf_in2); 119 | free (buf_out); 120 | 121 | return (-1); 122 | } 123 | } 124 | 125 | if ((fd2 = fopen (argv[2], "rb")) == NULL) 126 | { 127 | fprintf (stderr, "%s: %s\n", argv[2], strerror (errno)); 128 | 129 | free (buf_in1); 130 | free (buf_in2); 131 | free (buf_out); 132 | 133 | fclose (fd1); 134 | 135 | return (-1); 136 | } 137 | 138 | while (!feof (fd1)) 139 | { 140 | size_t real_sz1 = read_segment (buf_in1, fd1); 141 | 142 | char *max_in1 = buf_in1 + real_sz1; 143 | 144 | char *ptr_in1; 145 | 146 | size_t len_in1; 147 | 148 | for (ptr_in1 = buf_in1; ptr_in1 < max_in1; ptr_in1 += len_in1 + 1) 149 | { 150 | len_in1 = get_line_len (ptr_in1, max_in1); 151 | 152 | size_t vir_in1 = len_in1; 153 | 154 | while (vir_in1) 155 | { 156 | if (ptr_in1[vir_in1 - 1] != '\r') break; 157 | 158 | vir_in1--; 159 | } 160 | 161 | if (vir_in1 > LEN_MAX) continue; 162 | 163 | while (!feof (fd2)) 164 | { 165 | size_t real_sz2 = read_segment (buf_in2, fd2); 166 | 167 | char *max_in2 = buf_in2 + real_sz2; 168 | 169 | char *ptr_in2; 170 | 171 | size_t len_in2; 172 | 173 | for (ptr_in2 = buf_in2; ptr_in2 < max_in2; ptr_in2 += len_in2 + 1) 174 | { 175 | len_in2 = get_line_len (ptr_in2, max_in2); 176 | 177 | size_t vir_in2 = len_in2; 178 | 179 | while (vir_in2) 180 | { 181 | if (ptr_in2[vir_in2 - 1] != '\r') break; 182 | 183 | vir_in2--; 184 | } 185 | 186 | if (vir_in2 > LEN_MAX) continue; 187 | 188 | /** 189 | * add to output buffer 190 | */ 191 | 192 | size_t len_out = ptr_out - buf_out; 193 | 194 | size_t len_add = vir_in1 + vir_in2 + 1; 195 | 196 | if ((len_out + len_add) < SEGMENT_SIZE) 197 | { 198 | add (ptr_out, ptr_in1, vir_in1, ptr_in2, vir_in2); 199 | 200 | ptr_out += len_add; 201 | } 202 | else 203 | { 204 | size_t len_out = ptr_out - buf_out; 205 | 206 | fwrite (buf_out, 1, len_out, stdout); 207 | 208 | ptr_out = buf_out; 209 | 210 | add (ptr_out, ptr_in1, vir_in1, ptr_in2, vir_in2); 211 | 212 | ptr_out += len_add; 213 | } 214 | } 215 | } 216 | 217 | rewind (fd2); 218 | } 219 | } 220 | 221 | size_t len_out = ptr_out - buf_out; 222 | 223 | fwrite (buf_out, 1, len_out, stdout); 224 | 225 | fclose (fd2); 226 | fclose (fd1); 227 | 228 | free (buf_out); 229 | 230 | free (buf_in2); 231 | free (buf_in1); 232 | 233 | return 0; 234 | } 235 | -------------------------------------------------------------------------------- /src/combinator3.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define LEN_MAX 32 15 | 16 | #define SEGMENT_SIZE (32 * 1024 * 1024) 17 | #define SEGMENT_ALIGN ( 8 * 1024) 18 | 19 | /** 20 | * Name........: combinator3 21 | * Autor.......: Jens Steube 22 | * License.....: MIT 23 | */ 24 | 25 | static size_t read_segment (char *buf, FILE *fd) 26 | { 27 | size_t read_sz = SEGMENT_SIZE - SEGMENT_ALIGN; 28 | 29 | size_t real_sz = fread (buf, 1, read_sz, fd); 30 | 31 | if (real_sz == 0) return (0); 32 | 33 | if (real_sz != read_sz) 34 | { 35 | if (buf[real_sz - 1] != '\n') 36 | { 37 | real_sz++; 38 | 39 | buf[real_sz - 1] = '\n'; 40 | } 41 | 42 | return (real_sz); 43 | } 44 | 45 | size_t extra; 46 | 47 | for (extra = 0; extra < SEGMENT_ALIGN; extra++) 48 | { 49 | if (fread (buf + real_sz, 1, 1, fd) == 0) break; 50 | 51 | real_sz++; 52 | 53 | if (buf[real_sz - 1] == '\n') break; 54 | } 55 | 56 | return (real_sz); 57 | } 58 | 59 | static size_t get_line_len (char *pos, char *max) 60 | { 61 | char *cur = pos; 62 | 63 | for (cur = pos; cur < max; cur++) 64 | { 65 | if (*cur == '\n') break; 66 | } 67 | 68 | size_t len = cur - pos; 69 | 70 | return (len); 71 | } 72 | 73 | static void add (char *ptr_out, char *ptr_in1, size_t len_in1, char *ptr_in2, size_t len_in2, char *ptr_in3, size_t len_in3) 74 | { 75 | memcpy (ptr_out, ptr_in1, len_in1); 76 | 77 | ptr_out += len_in1; 78 | 79 | memcpy (ptr_out, ptr_in2, len_in2); 80 | 81 | ptr_out += len_in2; 82 | 83 | memcpy (ptr_out, ptr_in3, len_in3); 84 | 85 | ptr_out += len_in3; 86 | 87 | *ptr_out = '\n'; 88 | } 89 | 90 | int main (int argc, char *argv[]) 91 | { 92 | if (argc != 4) 93 | { 94 | fprintf (stderr, "usage: %s file1 file2 file3\n", argv[0]); 95 | 96 | return (-1); 97 | } 98 | 99 | size_t sz_buf = SEGMENT_SIZE + SEGMENT_ALIGN; 100 | 101 | char *buf_in1 = (char *) malloc (sz_buf); 102 | char *buf_in2 = (char *) malloc (sz_buf); 103 | char *buf_in3 = (char *) malloc (sz_buf); 104 | 105 | char *buf_out = (char *) malloc (sz_buf); 106 | 107 | char *ptr_out = buf_out; 108 | 109 | FILE *fd1; 110 | FILE *fd2; 111 | FILE *fd3; 112 | 113 | if ((fd1 = fopen (argv[1], "rb")) == NULL) 114 | { 115 | fprintf (stderr, "%s: %s\n", argv[1], strerror (errno)); 116 | 117 | free (buf_in1); 118 | free (buf_in2); 119 | free (buf_in3); 120 | 121 | free (buf_out); 122 | 123 | return (-1); 124 | } 125 | 126 | if ((fd2 = fopen (argv[2], "rb")) == NULL) 127 | { 128 | fprintf (stderr, "%s: %s\n", argv[2], strerror (errno)); 129 | 130 | free (buf_in1); 131 | free (buf_in2); 132 | free (buf_in3); 133 | 134 | free (buf_out); 135 | 136 | fclose (fd1); 137 | 138 | return (-1); 139 | } 140 | 141 | if ((fd3 = fopen (argv[3], "rb")) == NULL) 142 | { 143 | fprintf (stderr, "%s: %s\n", argv[3], strerror (errno)); 144 | 145 | free (buf_in1); 146 | free (buf_in2); 147 | free (buf_in3); 148 | 149 | free (buf_out); 150 | 151 | fclose (fd1); 152 | fclose (fd2); 153 | 154 | return (-1); 155 | } 156 | 157 | while (!feof (fd1)) 158 | { 159 | size_t real_sz1 = read_segment (buf_in1, fd1); 160 | 161 | char *max_in1 = buf_in1 + real_sz1; 162 | 163 | char *ptr_in1; 164 | 165 | size_t len_in1; 166 | 167 | for (ptr_in1 = buf_in1; ptr_in1 < max_in1; ptr_in1 += len_in1 + 1) 168 | { 169 | len_in1 = get_line_len (ptr_in1, max_in1); 170 | 171 | size_t vir_in1 = len_in1; 172 | 173 | while (vir_in1) 174 | { 175 | if (ptr_in1[vir_in1 - 1] != '\r') break; 176 | 177 | vir_in1--; 178 | } 179 | 180 | if (vir_in1 > LEN_MAX) continue; 181 | 182 | while (!feof (fd2)) 183 | { 184 | size_t real_sz2 = read_segment (buf_in2, fd2); 185 | 186 | char *max_in2 = buf_in2 + real_sz2; 187 | 188 | char *ptr_in2; 189 | 190 | size_t len_in2; 191 | 192 | for (ptr_in2 = buf_in2; ptr_in2 < max_in2; ptr_in2 += len_in2 + 1) 193 | { 194 | len_in2 = get_line_len (ptr_in2, max_in2); 195 | 196 | size_t vir_in2 = len_in2; 197 | 198 | while (vir_in2) 199 | { 200 | if (ptr_in2[vir_in2 - 1] != '\r') break; 201 | 202 | vir_in2--; 203 | } 204 | 205 | if (vir_in2 > LEN_MAX) continue; 206 | 207 | while (!feof (fd3)) 208 | { 209 | size_t real_sz3 = read_segment (buf_in3, fd3); 210 | 211 | char *max_in3 = buf_in3 + real_sz3; 212 | 213 | char *ptr_in3; 214 | 215 | size_t len_in3; 216 | 217 | for (ptr_in3 = buf_in3; ptr_in3 < max_in3; ptr_in3 += len_in3 + 1) 218 | { 219 | len_in3 = get_line_len (ptr_in3, max_in3); 220 | 221 | size_t vir_in3 = len_in3; 222 | 223 | while (vir_in3) 224 | { 225 | if (ptr_in3[vir_in3 - 1] != '\r') break; 226 | 227 | vir_in3--; 228 | } 229 | 230 | if (vir_in3 > LEN_MAX) continue; 231 | 232 | /** 233 | * add to output buffer 234 | */ 235 | 236 | size_t len_out = ptr_out - buf_out; 237 | 238 | size_t len_add = vir_in1 + vir_in2 + vir_in3 + 1; 239 | 240 | if ((len_out + len_add) < SEGMENT_SIZE) 241 | { 242 | add (ptr_out, ptr_in1, vir_in1, ptr_in2, vir_in2, ptr_in3, vir_in3); 243 | 244 | ptr_out += len_add; 245 | } 246 | else 247 | { 248 | size_t len_out = ptr_out - buf_out; 249 | 250 | fwrite (buf_out, 1, len_out, stdout); 251 | 252 | ptr_out = buf_out; 253 | 254 | add (ptr_out, ptr_in1, vir_in1, ptr_in2, vir_in2, ptr_in3, vir_in3); 255 | 256 | ptr_out += len_add; 257 | } 258 | } 259 | } 260 | 261 | rewind (fd3); 262 | } 263 | } 264 | 265 | rewind (fd2); 266 | } 267 | } 268 | 269 | size_t len_out = ptr_out - buf_out; 270 | 271 | fwrite (buf_out, 1, len_out, stdout); 272 | 273 | fclose (fd3); 274 | fclose (fd2); 275 | fclose (fd1); 276 | 277 | free (buf_out); 278 | 279 | free (buf_in3); 280 | free (buf_in2); 281 | free (buf_in1); 282 | 283 | return 0; 284 | } 285 | -------------------------------------------------------------------------------- /src/combipow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /** 8 | * Name........: Combined-Power Utility 9 | * Autor.......: Unix-Ninja 10 | */ 11 | 12 | /* 13 | * This utility is meant to created "unique combinations" of a given input file's lines. 14 | * Please note, this will NOT create permutations of the input. 15 | * 16 | */ 17 | 18 | typedef uint32_t bool; 19 | typedef uint32_t u32; 20 | typedef uint64_t u64; 21 | 22 | #define false 0 23 | #define true 1 24 | #define LINE_SIZE 64 25 | #define LINE_LIMIT 15 + 1 /* we add one to the limit for the null terminator */ 26 | #define MAX_LINES 63 /* this is the limit of using a single unsigned 64-bit integer */ 27 | /* exceeding this count will cause the counters to wrap */ 28 | 29 | int usage (char *progname) 30 | { 31 | fprintf (stderr, "%s - utility to create \"unique combinations\" of given input\n", progname); 32 | fprintf (stderr, "\n"); 33 | fprintf (stderr, "Usage: %s [options] file1\n", progname); 34 | fprintf (stderr, "\n"); 35 | fprintf (stderr, " Please note, you can not use more then 64 lines of input for this utility. In\n"); 36 | fprintf (stderr, " all honesty, you don't want to. 64 lines would generate approximately 187 EB\n"); 37 | fprintf (stderr, " (or 187,660,921,384 GB) worth of data (supposing each line was about 1 byte).\n"); 38 | fprintf (stderr, " If you are trying to generate that much data, you're probably doing it wrong.\n"); 39 | fprintf (stderr, "\n"); 40 | fprintf (stderr, "=======\n"); 41 | fprintf (stderr, "Options\n"); 42 | fprintf (stderr, "=======\n"); 43 | fprintf (stderr, "\n"); 44 | fprintf (stderr, " -h display this message\n"); 45 | fprintf (stderr, " -s use space separator in output\n"); 46 | fprintf (stderr, " -l limit lines to 15 chars (useful for hashcat rules)\n"); 47 | return (-1); 48 | } 49 | 50 | int main (int argc, char *argv[]) 51 | { 52 | bool op_space = false; 53 | bool op_limit = false; 54 | char *f1, *f2; 55 | 56 | f1 = f2 = "\0"; 57 | u64 i; 58 | u32 lines; 59 | 60 | char *progname = argv[0]; 61 | 62 | for (i = 1; i < (u64) argc; i++) 63 | { 64 | if (*(argv[i] + 0) == '-') 65 | { 66 | if (!strcmp (argv[i], "-h")) 67 | { 68 | return usage (progname); 69 | } 70 | else if (!strcmp (argv[i], "-l")) 71 | { 72 | op_limit = true; 73 | } 74 | else if (!strcmp (argv[i], "-s")) 75 | { 76 | op_space = true; 77 | } 78 | } 79 | else if (!strcmp (f1, "\0")) 80 | { 81 | f1 = argv[i]; 82 | } 83 | else 84 | { 85 | return usage (progname); 86 | } 87 | } 88 | 89 | if (!strcmp (f1, "\0")) 90 | { 91 | return usage (progname); 92 | } 93 | 94 | FILE *fd1; 95 | 96 | if ((fd1 = fopen (f1, "rb")) == NULL) 97 | { 98 | fprintf (stderr, "%s: %s\n", f1, strerror (errno)); 99 | return (-1); 100 | } 101 | 102 | lines = 0; 103 | char line[LINE_SIZE]; 104 | 105 | while (fgets (line, LINE_SIZE, fd1)) 106 | { 107 | if (strlen (line) > LINE_LIMIT && op_limit) 108 | { 109 | fprintf (stderr, "Line length exceeded in input. Skipping...\n"); 110 | continue; 111 | } 112 | lines++; 113 | } 114 | 115 | rewind (fd1); 116 | 117 | /* we can't exceed the max line count. err if we do. */ 118 | if (lines > MAX_LINES) 119 | { 120 | fprintf (stderr, "You can not exceed %d lines in your input file! Unable to continue.\n", MAX_LINES); 121 | return (-1); 122 | } 123 | 124 | /* allocate mem for buffer and check for success */ 125 | char **buf = calloc (lines, sizeof (char *)); 126 | 127 | if (buf == NULL) 128 | { 129 | fprintf (stderr, "Unable to allocate memory!"); 130 | return (-1); 131 | } 132 | 133 | i = 0; 134 | 135 | while (fgets (line, LINE_SIZE, fd1)) 136 | { 137 | /* skip empty lines and remove them from line count */ 138 | if (line[0] == '\r' || line[0] == '\n') 139 | { 140 | lines--; 141 | continue; 142 | } 143 | /* skip long lines */ 144 | if (strlen (line) > LINE_LIMIT && op_limit) 145 | { 146 | continue; 147 | } 148 | 149 | u32 length = strlen (line); 150 | 151 | // copy line... 152 | // but without newline char(s) 153 | if (line[length - 2] == '\r') length--; 154 | 155 | buf[i] = calloc (length, sizeof (char)); 156 | 157 | if (buf[i] == NULL) 158 | { 159 | fprintf (stderr, "Unable to allocate memory!"); 160 | return (-1); 161 | } 162 | 163 | strncpy ((char *) buf[i], line, length - 1); 164 | 165 | i++; 166 | } 167 | 168 | fclose (fd1); 169 | 170 | /* printf ("%d lines found.\n", lines); */ 171 | 172 | /* find combinations */ 173 | u64 j; 174 | u32 pad_size = op_limit ? 1 : 0; 175 | 176 | bool pad; 177 | char lb[LINE_LIMIT]; 178 | 179 | for (i = 1; i < (((u64) 1u) << lines); i++) 180 | { 181 | pad = false; 182 | memset (lb, '\0', LINE_LIMIT); /* initialize the line buffer */ 183 | 184 | for (j = 0; j < lines; j++) 185 | { 186 | if (i & (((u64) 1u) << j)) 187 | { 188 | if (op_limit) 189 | { 190 | if ((strlen ((char *) buf[j]) + strlen (lb) + pad_size) > (LINE_LIMIT - 1)) 191 | { 192 | fprintf (stderr, "Line length exceeded in output. Skipping...\n"); 193 | continue; 194 | } 195 | 196 | if (op_space && pad) strcat (lb, " "); 197 | 198 | strcat (lb, (char *) buf[j]); 199 | } 200 | else 201 | { 202 | if (op_space && pad) printf (" "); 203 | 204 | printf ("%s", (char *) buf[j]); 205 | } 206 | pad = true; 207 | } 208 | } 209 | 210 | /* print buffer if not empty */ 211 | 212 | if (strlen (lb)) 213 | { 214 | printf ("%s", lb); 215 | } 216 | 217 | printf ("\n"); 218 | } 219 | 220 | return 0; 221 | } 222 | -------------------------------------------------------------------------------- /src/cpu_rules.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Author......: Jens Steube 3 | * License.....: MIT 4 | */ 5 | 6 | #include "cpu_rules.h" 7 | 8 | extern int max_len; 9 | 10 | /** 11 | * GPU rules 12 | */ 13 | 14 | #define INCR_POS if (++rule_pos == rule_len) return (-1) 15 | #define SET_NAME(rule,val) (rule)->cmds[rule_cnt] = ((val) & 0xff) << 0 16 | #define SET_P0(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 8 17 | #define SET_P1(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((val) & 0xff) << 16 18 | #define MAX_GPU_RULES 31 19 | #define GET_NAME(rule) rule_cmd = (((rule)->cmds[rule_cnt] >> 0) & 0xff) 20 | #define GET_P0(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 8) & 0xff) 21 | #define GET_P1(rule) INCR_POS; rule_buf[rule_pos] = (((rule)->cmds[rule_cnt] >> 16) & 0xff) 22 | 23 | #define SET_P0_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 8 24 | #define SET_P1_CONV(rule,val) INCR_POS; (rule)->cmds[rule_cnt] |= ((conv_ctoi (val)) & 0xff) << 16 25 | #define GET_P0_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 8) & 0xff) 26 | #define GET_P1_CONV(rule) INCR_POS; rule_buf[rule_pos] = conv_itoc (((rule)->cmds[rule_cnt] >> 16) & 0xff) 27 | 28 | void gen_cmask (const uint8_t *word, uint8_t *cmask, const uint len) 29 | { 30 | uint i; 31 | 32 | for (i = 0; i < len; i++) 33 | { 34 | if (class_alpha (word[i]) == 0) 35 | { 36 | cmask[i] = 0; 37 | } 38 | else 39 | { 40 | cmask[i] = 0xff; 41 | } 42 | } 43 | } 44 | 45 | /** 46 | * CPU rules 47 | */ 48 | 49 | #define NEXT_RULEPOS(rp) if (++(rp) == rule_len) return (RULE_RC_SYNTAX_ERROR) 50 | #define NEXT_RPTOI(r,rp,up) if (((up) = conv_ctoi ((r)[(rp)])) == -1) return (RULE_RC_SYNTAX_ERROR) 51 | 52 | #define MANGLE_TOGGLE_AT(a,p) if (class_alpha ((a)[(p)])) (a)[(p)] ^= 0x20 53 | #define MANGLE_LOWER_AT(a,p) if (class_upper ((a)[(p)])) (a)[(p)] ^= 0x20 54 | #define MANGLE_UPPER_AT(a,p) if (class_lower ((a)[(p)])) (a)[(p)] ^= 0x20 55 | 56 | /* #define MANGLE_SWITCH(a,l,r) { char c = (l); arr[(r)] = arr[(l)]; arr[(l)] = c; } */ 57 | /* #define MANGLE_SWITCH(a,l,r) { char c = (l); (a)[(r)] = (a)[(l)]; (a)[(l)] = c; } */ 58 | #define MANGLE_SWITCH(a,l,r) { char c = (a)[(r)]; (a)[(r)] = (a)[(l)]; (a)[(l)] = c; } 59 | 60 | int mangle_lrest (char arr[BLOCK_SIZE], int arr_len) 61 | { 62 | int pos; 63 | 64 | for (pos = 0; pos < arr_len; pos++) MANGLE_LOWER_AT (arr, pos); 65 | 66 | return (arr_len); 67 | } 68 | 69 | int mangle_urest (char arr[BLOCK_SIZE], int arr_len) 70 | { 71 | int pos; 72 | 73 | for (pos = 0; pos < arr_len; pos++) MANGLE_UPPER_AT (arr, pos); 74 | 75 | return (arr_len); 76 | } 77 | 78 | int mangle_trest (char arr[BLOCK_SIZE], int arr_len) 79 | { 80 | int pos; 81 | 82 | for (pos = 0; pos < arr_len; pos++) MANGLE_TOGGLE_AT (arr, pos); 83 | 84 | return (arr_len); 85 | } 86 | 87 | int mangle_reverse (char arr[BLOCK_SIZE], int arr_len) 88 | { 89 | int l; 90 | int r; 91 | 92 | for (l = 0; l < arr_len; l++) 93 | { 94 | r = arr_len - 1 - l; 95 | 96 | if (l >= r) break; 97 | 98 | MANGLE_SWITCH (arr, l, r); 99 | } 100 | 101 | return (arr_len); 102 | } 103 | 104 | int mangle_double (char arr[BLOCK_SIZE], int arr_len) 105 | { 106 | if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len); 107 | 108 | memcpy (&arr[arr_len], arr, (size_t) arr_len); 109 | 110 | return (arr_len * 2); 111 | } 112 | 113 | int mangle_double_times (char arr[BLOCK_SIZE], int arr_len, int times) 114 | { 115 | if (((arr_len * times) + arr_len) >= BLOCK_SIZE) return (arr_len); 116 | 117 | int orig_len = arr_len; 118 | 119 | int i; 120 | 121 | for (i = 0; i < times; i++) 122 | { 123 | memcpy (&arr[arr_len], arr, orig_len); 124 | 125 | arr_len += orig_len; 126 | } 127 | 128 | return (arr_len); 129 | } 130 | 131 | int mangle_reflect (char arr[BLOCK_SIZE], int arr_len) 132 | { 133 | if ((arr_len * 2) >= BLOCK_SIZE) return (arr_len); 134 | 135 | mangle_double (arr, arr_len); 136 | 137 | mangle_reverse (arr + arr_len, arr_len); 138 | 139 | return (arr_len * 2); 140 | } 141 | 142 | int mangle_rotate_left (char arr[BLOCK_SIZE], int arr_len) 143 | { 144 | int l; 145 | int r; 146 | 147 | for (l = 0, r = arr_len - 1; r > 0; r--) 148 | { 149 | MANGLE_SWITCH (arr, l, r); 150 | } 151 | 152 | return (arr_len); 153 | } 154 | 155 | int mangle_rotate_right (char arr[BLOCK_SIZE], int arr_len) 156 | { 157 | int l; 158 | int r; 159 | 160 | for (l = 0, r = arr_len - 1; l < r; l++) 161 | { 162 | MANGLE_SWITCH (arr, l, r); 163 | } 164 | 165 | return (arr_len); 166 | } 167 | 168 | int mangle_append (char arr[BLOCK_SIZE], int arr_len, char c) 169 | { 170 | if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len); 171 | 172 | arr[arr_len] = c; 173 | 174 | return (arr_len + 1); 175 | } 176 | 177 | int mangle_prepend (char arr[BLOCK_SIZE], int arr_len, char c) 178 | { 179 | if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len); 180 | 181 | int arr_pos; 182 | 183 | for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--) 184 | { 185 | arr[arr_pos + 1] = arr[arr_pos]; 186 | } 187 | 188 | arr[0] = c; 189 | 190 | return (arr_len + 1); 191 | } 192 | 193 | int mangle_delete_at (char arr[BLOCK_SIZE], int arr_len, int upos) 194 | { 195 | if (upos >= arr_len) return (arr_len); 196 | 197 | int arr_pos; 198 | 199 | for (arr_pos = upos; arr_pos < arr_len - 1; arr_pos++) 200 | { 201 | arr[arr_pos] = arr[arr_pos + 1]; 202 | } 203 | 204 | return (arr_len - 1); 205 | } 206 | 207 | int mangle_extract (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen) 208 | { 209 | if (upos >= arr_len) return (arr_len); 210 | 211 | if ((upos + ulen) > arr_len) return (arr_len); 212 | 213 | int arr_pos; 214 | 215 | for (arr_pos = 0; arr_pos < ulen; arr_pos++) 216 | { 217 | arr[arr_pos] = arr[upos + arr_pos]; 218 | } 219 | 220 | return (ulen); 221 | } 222 | 223 | int mangle_omit (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen) 224 | { 225 | if (upos >= arr_len) return (arr_len); 226 | 227 | if ((upos + ulen) > arr_len) return (arr_len); 228 | 229 | int arr_pos; 230 | 231 | for (arr_pos = upos; arr_pos < arr_len - ulen; arr_pos++) 232 | { 233 | arr[arr_pos] = arr[arr_pos + ulen]; 234 | } 235 | 236 | return (arr_len - ulen); 237 | } 238 | 239 | int mangle_insert (char arr[BLOCK_SIZE], int arr_len, int upos, char c) 240 | { 241 | if (upos > arr_len) return (arr_len); 242 | 243 | if ((arr_len + 1) >= BLOCK_SIZE) return (arr_len); 244 | 245 | int arr_pos; 246 | 247 | for (arr_pos = arr_len - 1; arr_pos > upos - 1; arr_pos--) 248 | { 249 | arr[arr_pos + 1] = arr[arr_pos]; 250 | } 251 | 252 | arr[upos] = c; 253 | 254 | return (arr_len + 1); 255 | } 256 | 257 | int mangle_insert_multi (char arr[BLOCK_SIZE], int arr_len, int arr_pos, char arr2[BLOCK_SIZE], int arr2_len, int arr2_pos, int arr2_cpy) 258 | { 259 | if ((arr_len + arr2_cpy) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR); 260 | 261 | if (arr_pos > arr_len) return (RULE_RC_REJECT_ERROR); 262 | 263 | if (arr2_pos > arr2_len) return (RULE_RC_REJECT_ERROR); 264 | 265 | if ((arr2_pos + arr2_cpy) > arr2_len) return (RULE_RC_REJECT_ERROR); 266 | 267 | if (arr2_cpy < 1) return (RULE_RC_SYNTAX_ERROR); 268 | 269 | memcpy (arr2, arr2 + arr2_pos, arr2_len - arr2_pos); 270 | 271 | memcpy (arr2 + arr2_cpy, arr + arr_pos, arr_len - arr_pos); 272 | 273 | memcpy (arr + arr_pos, arr2, arr_len - arr_pos + arr2_cpy); 274 | 275 | return (arr_len + arr2_cpy); 276 | } 277 | 278 | int mangle_overstrike (char arr[BLOCK_SIZE], int arr_len, int upos, char c) 279 | { 280 | if (upos >= arr_len) return (arr_len); 281 | 282 | arr[upos] = c; 283 | 284 | return (arr_len); 285 | } 286 | 287 | int mangle_truncate_at (char arr[BLOCK_SIZE], int arr_len, int upos) 288 | { 289 | if (upos >= arr_len) return (arr_len); 290 | 291 | memset (arr + upos, 0, arr_len - upos); 292 | 293 | return (upos); 294 | } 295 | 296 | int mangle_replace (char arr[BLOCK_SIZE], int arr_len, char oldc, char newc) 297 | { 298 | int arr_pos; 299 | 300 | for (arr_pos = 0; arr_pos < arr_len; arr_pos++) 301 | { 302 | if (arr[arr_pos] != oldc) continue; 303 | 304 | arr[arr_pos] = newc; 305 | } 306 | 307 | return (arr_len); 308 | } 309 | 310 | int mangle_purgechar (char arr[BLOCK_SIZE], int arr_len, char c) 311 | { 312 | int arr_pos; 313 | 314 | int ret_len; 315 | 316 | for (ret_len = 0, arr_pos = 0; arr_pos < arr_len; arr_pos++) 317 | { 318 | if (arr[arr_pos] == c) continue; 319 | 320 | arr[ret_len] = arr[arr_pos]; 321 | 322 | ret_len++; 323 | } 324 | 325 | return (ret_len); 326 | } 327 | 328 | int mangle_dupeblock_prepend (char arr[BLOCK_SIZE], int arr_len, int ulen) 329 | { 330 | if (ulen > arr_len) return (arr_len); 331 | 332 | if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len); 333 | 334 | char cs[100]; 335 | 336 | memcpy (cs, arr, ulen); 337 | 338 | int i; 339 | 340 | for (i = 0; i < ulen; i++) 341 | { 342 | char c = cs[i]; 343 | 344 | arr_len = mangle_insert (arr, arr_len, i, c); 345 | } 346 | 347 | return (arr_len); 348 | } 349 | 350 | int mangle_dupeblock_append (char arr[BLOCK_SIZE], int arr_len, int ulen) 351 | { 352 | if (ulen > arr_len) return (arr_len); 353 | 354 | if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len); 355 | 356 | int upos = arr_len - ulen; 357 | 358 | int i; 359 | 360 | for (i = 0; i < ulen; i++) 361 | { 362 | char c = arr[upos + i]; 363 | 364 | arr_len = mangle_append (arr, arr_len, c); 365 | } 366 | 367 | return (arr_len); 368 | } 369 | 370 | int mangle_dupechar_at (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen) 371 | { 372 | if ( arr_len == 0) return (arr_len); 373 | if ((arr_len + ulen) >= BLOCK_SIZE) return (arr_len); 374 | 375 | char c = arr[upos]; 376 | 377 | int i; 378 | 379 | for (i = 0; i < ulen; i++) 380 | { 381 | arr_len = mangle_insert (arr, arr_len, upos, c); 382 | } 383 | 384 | return (arr_len); 385 | } 386 | 387 | int mangle_dupechar (char arr[BLOCK_SIZE], int arr_len) 388 | { 389 | if ( arr_len == 0) return (arr_len); 390 | if ((arr_len + arr_len) >= BLOCK_SIZE) return (arr_len); 391 | 392 | int arr_pos; 393 | 394 | for (arr_pos = arr_len - 1; arr_pos > -1; arr_pos--) 395 | { 396 | int new_pos = arr_pos * 2; 397 | 398 | arr[new_pos] = arr[arr_pos]; 399 | 400 | arr[new_pos + 1] = arr[arr_pos]; 401 | } 402 | 403 | return (arr_len * 2); 404 | } 405 | 406 | int mangle_switch_at_check (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2) 407 | { 408 | if (upos >= arr_len) return (arr_len); 409 | if (upos2 >= arr_len) return (arr_len); 410 | 411 | MANGLE_SWITCH (arr, upos, upos2); 412 | 413 | return (arr_len); 414 | } 415 | 416 | int mangle_switch_at (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2) 417 | { 418 | MANGLE_SWITCH (arr, upos, upos2); 419 | 420 | return (arr_len); 421 | } 422 | 423 | int mangle_chr_shiftl (uint8_t arr[BLOCK_SIZE], int arr_len, int upos) 424 | { 425 | if (upos >= arr_len) return (arr_len); 426 | 427 | arr[upos] <<= 1; 428 | 429 | return (arr_len); 430 | } 431 | 432 | int mangle_chr_shiftr (uint8_t arr[BLOCK_SIZE], int arr_len, int upos) 433 | { 434 | if (upos >= arr_len) return (arr_len); 435 | 436 | arr[upos] >>= 1; 437 | 438 | return (arr_len); 439 | } 440 | 441 | int mangle_chr_incr (uint8_t arr[BLOCK_SIZE], int arr_len, int upos) 442 | { 443 | if (upos >= arr_len) return (arr_len); 444 | 445 | arr[upos] += 1; 446 | 447 | return (arr_len); 448 | } 449 | 450 | int mangle_chr_decr (uint8_t arr[BLOCK_SIZE], int arr_len, int upos) 451 | { 452 | if (upos >= arr_len) return (arr_len); 453 | 454 | arr[upos] -= 1; 455 | 456 | return (arr_len); 457 | } 458 | 459 | int mangle_title (char arr[BLOCK_SIZE], int arr_len) 460 | { 461 | int upper_next = 1; 462 | 463 | int pos; 464 | 465 | for (pos = 0; pos < arr_len; pos++) 466 | { 467 | if (arr[pos] == ' ') 468 | { 469 | upper_next = 1; 470 | 471 | continue; 472 | } 473 | 474 | if (upper_next) 475 | { 476 | upper_next = 0; 477 | 478 | MANGLE_UPPER_AT (arr, pos); 479 | } 480 | else 481 | { 482 | MANGLE_LOWER_AT (arr, pos); 483 | } 484 | } 485 | 486 | return (arr_len); 487 | } 488 | 489 | int generate_random_rule (char rule_buf[RP_RULE_BUFSIZ], uint32_t rp_gen_func_min, uint32_t rp_gen_func_max) 490 | { 491 | uint32_t rp_gen_num = get_random_num (rp_gen_func_min, rp_gen_func_max); 492 | 493 | uint32_t j; 494 | 495 | uint32_t rule_pos = 0; 496 | 497 | for (j = 0; j < rp_gen_num; j++) 498 | { 499 | uint32_t r = 0; 500 | uint32_t p1 = 0; 501 | uint32_t p2 = 0; 502 | uint32_t p3 = 0; 503 | 504 | switch ((char) get_random_num (0, 9)) 505 | { 506 | case 0: 507 | r = get_random_num (0, sizeof (grp_op_nop)); 508 | rule_buf[rule_pos++] = grp_op_nop[r]; 509 | break; 510 | 511 | case 1: 512 | r = get_random_num (0, sizeof (grp_op_pos_p0)); 513 | rule_buf[rule_pos++] = grp_op_pos_p0[r]; 514 | p1 = get_random_num (0, sizeof (grp_pos)); 515 | rule_buf[rule_pos++] = grp_pos[p1]; 516 | break; 517 | 518 | case 2: 519 | r = get_random_num (0, sizeof (grp_op_pos_p1)); 520 | rule_buf[rule_pos++] = grp_op_pos_p1[r]; 521 | p1 = get_random_num (1, 6); 522 | rule_buf[rule_pos++] = grp_pos[p1]; 523 | break; 524 | 525 | case 3: 526 | r = get_random_num (0, sizeof (grp_op_chr)); 527 | rule_buf[rule_pos++] = grp_op_chr[r]; 528 | p1 = get_random_num (0x20, 0x7e); 529 | rule_buf[rule_pos++] = (char) p1; 530 | break; 531 | 532 | case 4: 533 | r = get_random_num (0, sizeof (grp_op_chr_chr)); 534 | rule_buf[rule_pos++] = grp_op_chr_chr[r]; 535 | p1 = get_random_num (0x20, 0x7e); 536 | rule_buf[rule_pos++] = (char) p1; 537 | p2 = get_random_num (0x20, 0x7e); 538 | while (p1 == p2) 539 | p2 = get_random_num (0x20, 0x7e); 540 | rule_buf[rule_pos++] = (char) p2; 541 | break; 542 | 543 | case 5: 544 | r = get_random_num (0, sizeof (grp_op_pos_chr)); 545 | rule_buf[rule_pos++] = grp_op_pos_chr[r]; 546 | p1 = get_random_num (0, sizeof (grp_pos)); 547 | rule_buf[rule_pos++] = grp_pos[p1]; 548 | p2 = get_random_num (0x20, 0x7e); 549 | rule_buf[rule_pos++] = (char) p2; 550 | break; 551 | 552 | case 6: 553 | r = get_random_num (0, sizeof (grp_op_pos_pos0)); 554 | rule_buf[rule_pos++] = grp_op_pos_pos0[r]; 555 | p1 = get_random_num (0, sizeof (grp_pos)); 556 | rule_buf[rule_pos++] = grp_pos[p1]; 557 | p2 = get_random_num (0, sizeof (grp_pos)); 558 | while (p1 == p2) 559 | p2 = get_random_num (0, sizeof (grp_pos)); 560 | rule_buf[rule_pos++] = grp_pos[p2]; 561 | break; 562 | 563 | case 7: 564 | r = get_random_num (0, sizeof (grp_op_pos_pos1)); 565 | rule_buf[rule_pos++] = grp_op_pos_pos1[r]; 566 | p1 = get_random_num (0, sizeof (grp_pos)); 567 | rule_buf[rule_pos++] = grp_pos[p1]; 568 | p2 = get_random_num (1, sizeof (grp_pos)); 569 | while (p1 == p2) 570 | p2 = get_random_num (1, sizeof (grp_pos)); 571 | rule_buf[rule_pos++] = grp_pos[p2]; 572 | break; 573 | 574 | case 8: 575 | r = get_random_num (0, sizeof (grp_op_pos1_pos2_pos3)); 576 | rule_buf[rule_pos++] = grp_op_pos1_pos2_pos3[r]; 577 | p1 = get_random_num (0, sizeof (grp_pos)); 578 | rule_buf[rule_pos++] = grp_pos[p1]; 579 | p2 = get_random_num (1, sizeof (grp_pos)); 580 | rule_buf[rule_pos++] = grp_pos[p2]; 581 | p3 = get_random_num (0, sizeof (grp_pos)); 582 | rule_buf[rule_pos++] = grp_pos[p3]; 583 | break; 584 | } 585 | } 586 | 587 | return (rule_pos); 588 | } 589 | 590 | int apply_rule_cpu (char *rule, int rule_len, char in[BLOCK_SIZE], int in_len, char out[BLOCK_SIZE]) 591 | { 592 | char mem[BLOCK_SIZE]; 593 | 594 | if (in == NULL) return (RULE_RC_REJECT_ERROR); 595 | 596 | if (out == NULL) return (RULE_RC_REJECT_ERROR); 597 | 598 | if (in_len < 1) return (RULE_RC_REJECT_ERROR); 599 | 600 | if (rule_len < 1) return (RULE_RC_REJECT_ERROR); 601 | 602 | int out_len = in_len; 603 | int mem_len = in_len; 604 | 605 | memcpy (out, in, out_len); 606 | 607 | int rule_pos; 608 | 609 | for (rule_pos = 0; rule_pos < rule_len; rule_pos++) 610 | { 611 | int upos; int upos2; 612 | int ulen; 613 | 614 | switch (rule[rule_pos]) 615 | { 616 | case ' ': 617 | break; 618 | 619 | case RULE_OP_MANGLE_NOOP: 620 | break; 621 | 622 | case RULE_OP_MANGLE_LREST: 623 | out_len = mangle_lrest (out, out_len); 624 | break; 625 | 626 | case RULE_OP_MANGLE_UREST: 627 | out_len = mangle_urest (out, out_len); 628 | break; 629 | 630 | case RULE_OP_MANGLE_LREST_UFIRST: 631 | out_len = mangle_lrest (out, out_len); 632 | if (out_len) MANGLE_UPPER_AT (out, 0); 633 | break; 634 | 635 | case RULE_OP_MANGLE_UREST_LFIRST: 636 | out_len = mangle_urest (out, out_len); 637 | if (out_len) MANGLE_LOWER_AT (out, 0); 638 | break; 639 | 640 | case RULE_OP_MANGLE_TREST: 641 | out_len = mangle_trest (out, out_len); 642 | break; 643 | 644 | case RULE_OP_MANGLE_TOGGLE_AT: 645 | NEXT_RULEPOS (rule_pos); 646 | NEXT_RPTOI (rule, rule_pos, upos); 647 | if (upos < out_len) MANGLE_TOGGLE_AT (out, upos); 648 | break; 649 | 650 | case RULE_OP_MANGLE_REVERSE: 651 | out_len = mangle_reverse (out, out_len); 652 | break; 653 | 654 | case RULE_OP_MANGLE_DUPEWORD: 655 | out_len = mangle_double (out, out_len); 656 | break; 657 | 658 | case RULE_OP_MANGLE_DUPEWORD_TIMES: 659 | NEXT_RULEPOS (rule_pos); 660 | NEXT_RPTOI (rule, rule_pos, ulen); 661 | out_len = mangle_double_times (out, out_len, ulen); 662 | break; 663 | 664 | case RULE_OP_MANGLE_REFLECT: 665 | out_len = mangle_reflect (out, out_len); 666 | break; 667 | 668 | case RULE_OP_MANGLE_ROTATE_LEFT: 669 | mangle_rotate_left (out, out_len); 670 | break; 671 | 672 | case RULE_OP_MANGLE_ROTATE_RIGHT: 673 | mangle_rotate_right (out, out_len); 674 | break; 675 | 676 | case RULE_OP_MANGLE_APPEND: 677 | NEXT_RULEPOS (rule_pos); 678 | out_len = mangle_append (out, out_len, rule[rule_pos]); 679 | break; 680 | 681 | case RULE_OP_MANGLE_PREPEND: 682 | NEXT_RULEPOS (rule_pos); 683 | out_len = mangle_prepend (out, out_len, rule[rule_pos]); 684 | break; 685 | 686 | case RULE_OP_MANGLE_DELETE_FIRST: 687 | out_len = mangle_delete_at (out, out_len, 0); 688 | break; 689 | 690 | case RULE_OP_MANGLE_DELETE_LAST: 691 | out_len = mangle_delete_at (out, out_len, (out_len) ? out_len - 1 : 0); 692 | break; 693 | 694 | case RULE_OP_MANGLE_DELETE_AT: 695 | NEXT_RULEPOS (rule_pos); 696 | NEXT_RPTOI (rule, rule_pos, upos); 697 | out_len = mangle_delete_at (out, out_len, upos); 698 | break; 699 | 700 | case RULE_OP_MANGLE_EXTRACT: 701 | NEXT_RULEPOS (rule_pos); 702 | NEXT_RPTOI (rule, rule_pos, upos); 703 | NEXT_RULEPOS (rule_pos); 704 | NEXT_RPTOI (rule, rule_pos, ulen); 705 | out_len = mangle_extract (out, out_len, upos, ulen); 706 | break; 707 | 708 | case RULE_OP_MANGLE_OMIT: 709 | NEXT_RULEPOS (rule_pos); 710 | NEXT_RPTOI (rule, rule_pos, upos); 711 | NEXT_RULEPOS (rule_pos); 712 | NEXT_RPTOI (rule, rule_pos, ulen); 713 | out_len = mangle_omit (out, out_len, upos, ulen); 714 | break; 715 | 716 | case RULE_OP_MANGLE_INSERT: 717 | NEXT_RULEPOS (rule_pos); 718 | NEXT_RPTOI (rule, rule_pos, upos); 719 | NEXT_RULEPOS (rule_pos); 720 | out_len = mangle_insert (out, out_len, upos, rule[rule_pos]); 721 | break; 722 | 723 | case RULE_OP_MANGLE_OVERSTRIKE: 724 | NEXT_RULEPOS (rule_pos); 725 | NEXT_RPTOI (rule, rule_pos, upos); 726 | NEXT_RULEPOS (rule_pos); 727 | out_len = mangle_overstrike (out, out_len, upos, rule[rule_pos]); 728 | break; 729 | 730 | case RULE_OP_MANGLE_TRUNCATE_AT: 731 | NEXT_RULEPOS (rule_pos); 732 | NEXT_RPTOI (rule, rule_pos, upos); 733 | out_len = mangle_truncate_at (out, out_len, upos); 734 | break; 735 | 736 | case RULE_OP_MANGLE_REPLACE: 737 | NEXT_RULEPOS (rule_pos); 738 | NEXT_RULEPOS (rule_pos); 739 | out_len = mangle_replace (out, out_len, rule[rule_pos - 1], rule[rule_pos]); 740 | break; 741 | 742 | case RULE_OP_MANGLE_PURGECHAR: 743 | NEXT_RULEPOS (rule_pos); 744 | out_len = mangle_purgechar (out, out_len, rule[rule_pos]); 745 | break; 746 | 747 | case RULE_OP_MANGLE_TOGGLECASE_REC: 748 | /* todo */ 749 | break; 750 | 751 | case RULE_OP_MANGLE_DUPECHAR_FIRST: 752 | NEXT_RULEPOS (rule_pos); 753 | NEXT_RPTOI (rule, rule_pos, ulen); 754 | out_len = mangle_dupechar_at (out, out_len, 0, ulen); 755 | break; 756 | 757 | case RULE_OP_MANGLE_DUPECHAR_LAST: 758 | NEXT_RULEPOS (rule_pos); 759 | NEXT_RPTOI (rule, rule_pos, ulen); 760 | out_len = mangle_dupechar_at (out, out_len, out_len - 1, ulen); 761 | break; 762 | 763 | case RULE_OP_MANGLE_DUPECHAR_ALL: 764 | out_len = mangle_dupechar (out, out_len); 765 | break; 766 | 767 | case RULE_OP_MANGLE_DUPEBLOCK_FIRST: 768 | NEXT_RULEPOS (rule_pos); 769 | NEXT_RPTOI (rule, rule_pos, ulen); 770 | out_len = mangle_dupeblock_prepend (out, out_len, ulen); 771 | break; 772 | 773 | case RULE_OP_MANGLE_DUPEBLOCK_LAST: 774 | NEXT_RULEPOS (rule_pos); 775 | NEXT_RPTOI (rule, rule_pos, ulen); 776 | out_len = mangle_dupeblock_append (out, out_len, ulen); 777 | break; 778 | 779 | case RULE_OP_MANGLE_SWITCH_FIRST: 780 | if (out_len >= 2) mangle_switch_at (out, out_len, 0, 1); 781 | break; 782 | 783 | case RULE_OP_MANGLE_SWITCH_LAST: 784 | if (out_len >= 2) mangle_switch_at (out, out_len, out_len - 1, out_len - 2); 785 | break; 786 | 787 | case RULE_OP_MANGLE_SWITCH_AT: 788 | NEXT_RULEPOS (rule_pos); 789 | NEXT_RPTOI (rule, rule_pos, upos); 790 | NEXT_RULEPOS (rule_pos); 791 | NEXT_RPTOI (rule, rule_pos, upos2); 792 | out_len = mangle_switch_at_check (out, out_len, upos, upos2); 793 | break; 794 | 795 | case RULE_OP_MANGLE_CHR_SHIFTL: 796 | NEXT_RULEPOS (rule_pos); 797 | NEXT_RPTOI (rule, rule_pos, upos); 798 | mangle_chr_shiftl ((uint8_t *) out, out_len, upos); 799 | break; 800 | 801 | case RULE_OP_MANGLE_CHR_SHIFTR: 802 | NEXT_RULEPOS (rule_pos); 803 | NEXT_RPTOI (rule, rule_pos, upos); 804 | mangle_chr_shiftr ((uint8_t *) out, out_len, upos); 805 | break; 806 | 807 | case RULE_OP_MANGLE_CHR_INCR: 808 | NEXT_RULEPOS (rule_pos); 809 | NEXT_RPTOI (rule, rule_pos, upos); 810 | mangle_chr_incr ((uint8_t *) out, out_len, upos); 811 | break; 812 | 813 | case RULE_OP_MANGLE_CHR_DECR: 814 | NEXT_RULEPOS (rule_pos); 815 | NEXT_RPTOI (rule, rule_pos, upos); 816 | mangle_chr_decr ((uint8_t *) out, out_len, upos); 817 | break; 818 | 819 | case RULE_OP_MANGLE_REPLACE_NP1: 820 | NEXT_RULEPOS (rule_pos); 821 | NEXT_RPTOI (rule, rule_pos, upos); 822 | if ((upos >= 0) && ((upos + 1) < out_len)) mangle_overstrike (out, out_len, upos, out[upos + 1]); 823 | break; 824 | 825 | case RULE_OP_MANGLE_REPLACE_NM1: 826 | NEXT_RULEPOS (rule_pos); 827 | NEXT_RPTOI (rule, rule_pos, upos); 828 | if ((upos >= 1) && ((upos + 0) < out_len)) mangle_overstrike (out, out_len, upos, out[upos - 1]); 829 | break; 830 | 831 | case RULE_OP_MANGLE_TITLE: 832 | mangle_title (out, out_len); 833 | break; 834 | 835 | case RULE_OP_MANGLE_EXTRACT_MEMORY: 836 | if (mem_len < 1) return (RULE_RC_REJECT_ERROR); 837 | NEXT_RULEPOS (rule_pos); 838 | NEXT_RPTOI (rule, rule_pos, upos); 839 | NEXT_RULEPOS (rule_pos); 840 | NEXT_RPTOI (rule, rule_pos, ulen); 841 | NEXT_RULEPOS (rule_pos); 842 | NEXT_RPTOI (rule, rule_pos, upos2); 843 | if ((out_len = mangle_insert_multi (out, out_len, upos2, mem, mem_len, upos, ulen)) < 1) return (out_len); 844 | break; 845 | 846 | case RULE_OP_MANGLE_APPEND_MEMORY: 847 | if (mem_len < 1) return (RULE_RC_REJECT_ERROR); 848 | if ((out_len + mem_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR); 849 | memcpy (out + out_len, mem, mem_len); 850 | out_len += mem_len; 851 | break; 852 | 853 | case RULE_OP_MANGLE_PREPEND_MEMORY: 854 | if (mem_len < 1) return (RULE_RC_REJECT_ERROR); 855 | if ((mem_len + out_len) > BLOCK_SIZE) return (RULE_RC_REJECT_ERROR); 856 | memcpy (mem + mem_len, out, out_len); 857 | out_len += mem_len; 858 | memcpy (out, mem, out_len); 859 | break; 860 | 861 | case RULE_OP_MEMORIZE_WORD: 862 | memcpy (mem, out, out_len); 863 | mem_len = out_len; 864 | break; 865 | 866 | case RULE_OP_REJECT_LESS: 867 | NEXT_RULEPOS (rule_pos); 868 | NEXT_RPTOI (rule, rule_pos, upos); 869 | if (out_len > upos) return (RULE_RC_REJECT_ERROR); 870 | break; 871 | 872 | case RULE_OP_REJECT_GREATER: 873 | NEXT_RULEPOS (rule_pos); 874 | NEXT_RPTOI (rule, rule_pos, upos); 875 | if (out_len < upos) return (RULE_RC_REJECT_ERROR); 876 | break; 877 | 878 | case RULE_OP_REJECT_CONTAIN: 879 | NEXT_RULEPOS (rule_pos); 880 | if (strchr (out, rule[rule_pos]) != NULL) return (RULE_RC_REJECT_ERROR); 881 | break; 882 | 883 | case RULE_OP_REJECT_NOT_CONTAIN: 884 | NEXT_RULEPOS (rule_pos); 885 | if (strchr (out, rule[rule_pos]) == NULL) return (RULE_RC_REJECT_ERROR); 886 | break; 887 | 888 | case RULE_OP_REJECT_EQUAL_FIRST: 889 | NEXT_RULEPOS (rule_pos); 890 | if (out[0] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR); 891 | break; 892 | 893 | case RULE_OP_REJECT_EQUAL_LAST: 894 | NEXT_RULEPOS (rule_pos); 895 | if (out[out_len - 1] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR); 896 | break; 897 | 898 | case RULE_OP_REJECT_EQUAL_AT: 899 | NEXT_RULEPOS (rule_pos); 900 | NEXT_RPTOI (rule, rule_pos, upos); 901 | if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR); 902 | NEXT_RULEPOS (rule_pos); 903 | if (out[upos] != rule[rule_pos]) return (RULE_RC_REJECT_ERROR); 904 | break; 905 | 906 | case RULE_OP_REJECT_CONTAINS: 907 | NEXT_RULEPOS (rule_pos); 908 | NEXT_RPTOI (rule, rule_pos, upos); 909 | if ((upos + 1) > out_len) return (RULE_RC_REJECT_ERROR); 910 | NEXT_RULEPOS (rule_pos); 911 | int c; int cnt; for (c = 0, cnt = 0; c < out_len; c++) if (out[c] == rule[rule_pos]) cnt++; 912 | if (cnt < upos) return (RULE_RC_REJECT_ERROR); 913 | break; 914 | 915 | case RULE_OP_REJECT_MEMORY: 916 | if ((out_len == mem_len) && (memcmp (out, mem, out_len) == 0)) return (RULE_RC_REJECT_ERROR); 917 | break; 918 | 919 | default: 920 | return (RULE_RC_SYNTAX_ERROR); 921 | break; 922 | } 923 | 924 | max_len = (out_len > max_len) ? out_len : max_len; 925 | } 926 | 927 | memset (out + out_len, 0, BLOCK_SIZE - out_len); 928 | 929 | return (out_len); 930 | } 931 | 932 | int cpu_rule_to_gpu_rule (char rule_buf[BUFSIZ], uint rule_len, gpu_rule_t *rule) 933 | { 934 | uint rule_pos; 935 | uint rule_cnt; 936 | 937 | for (rule_pos = 0, rule_cnt = 0; rule_pos < rule_len && rule_cnt < MAX_GPU_RULES; rule_pos++, rule_cnt++) 938 | { 939 | switch (rule_buf[rule_pos]) 940 | { 941 | case ' ': 942 | rule_cnt--; 943 | break; 944 | 945 | case RULE_OP_MANGLE_NOOP: 946 | SET_NAME (rule, rule_buf[rule_pos]); 947 | break; 948 | 949 | case RULE_OP_MANGLE_LREST: 950 | SET_NAME (rule, rule_buf[rule_pos]); 951 | break; 952 | 953 | case RULE_OP_MANGLE_UREST: 954 | SET_NAME (rule, rule_buf[rule_pos]); 955 | break; 956 | 957 | case RULE_OP_MANGLE_LREST_UFIRST: 958 | SET_NAME (rule, rule_buf[rule_pos]); 959 | break; 960 | 961 | case RULE_OP_MANGLE_UREST_LFIRST: 962 | SET_NAME (rule, rule_buf[rule_pos]); 963 | break; 964 | 965 | case RULE_OP_MANGLE_TREST: 966 | SET_NAME (rule, rule_buf[rule_pos]); 967 | break; 968 | 969 | case RULE_OP_MANGLE_TOGGLE_AT: 970 | SET_NAME (rule, rule_buf[rule_pos]); 971 | SET_P0_CONV (rule, rule_buf[rule_pos]); 972 | break; 973 | 974 | case RULE_OP_MANGLE_REVERSE: 975 | SET_NAME (rule, rule_buf[rule_pos]); 976 | break; 977 | 978 | case RULE_OP_MANGLE_DUPEWORD: 979 | SET_NAME (rule, rule_buf[rule_pos]); 980 | break; 981 | 982 | case RULE_OP_MANGLE_DUPEWORD_TIMES: 983 | SET_NAME (rule, rule_buf[rule_pos]); 984 | SET_P0_CONV (rule, rule_buf[rule_pos]); 985 | break; 986 | 987 | case RULE_OP_MANGLE_REFLECT: 988 | SET_NAME (rule, rule_buf[rule_pos]); 989 | break; 990 | 991 | case RULE_OP_MANGLE_ROTATE_LEFT: 992 | SET_NAME (rule, rule_buf[rule_pos]); 993 | break; 994 | 995 | case RULE_OP_MANGLE_ROTATE_RIGHT: 996 | SET_NAME (rule, rule_buf[rule_pos]); 997 | break; 998 | 999 | case RULE_OP_MANGLE_APPEND: 1000 | SET_NAME (rule, rule_buf[rule_pos]); 1001 | SET_P0 (rule, rule_buf[rule_pos]); 1002 | break; 1003 | 1004 | case RULE_OP_MANGLE_PREPEND: 1005 | SET_NAME (rule, rule_buf[rule_pos]); 1006 | SET_P0 (rule, rule_buf[rule_pos]); 1007 | break; 1008 | 1009 | case RULE_OP_MANGLE_DELETE_FIRST: 1010 | SET_NAME (rule, rule_buf[rule_pos]); 1011 | break; 1012 | 1013 | case RULE_OP_MANGLE_DELETE_LAST: 1014 | SET_NAME (rule, rule_buf[rule_pos]); 1015 | break; 1016 | 1017 | case RULE_OP_MANGLE_DELETE_AT: 1018 | SET_NAME (rule, rule_buf[rule_pos]); 1019 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1020 | break; 1021 | 1022 | case RULE_OP_MANGLE_EXTRACT: 1023 | SET_NAME (rule, rule_buf[rule_pos]); 1024 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1025 | SET_P1_CONV (rule, rule_buf[rule_pos]); 1026 | break; 1027 | 1028 | case RULE_OP_MANGLE_OMIT: 1029 | SET_NAME (rule, rule_buf[rule_pos]); 1030 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1031 | SET_P1_CONV (rule, rule_buf[rule_pos]); 1032 | break; 1033 | 1034 | case RULE_OP_MANGLE_INSERT: 1035 | SET_NAME (rule, rule_buf[rule_pos]); 1036 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1037 | SET_P1 (rule, rule_buf[rule_pos]); 1038 | break; 1039 | 1040 | case RULE_OP_MANGLE_OVERSTRIKE: 1041 | SET_NAME (rule, rule_buf[rule_pos]); 1042 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1043 | SET_P1 (rule, rule_buf[rule_pos]); 1044 | break; 1045 | 1046 | case RULE_OP_MANGLE_TRUNCATE_AT: 1047 | SET_NAME (rule, rule_buf[rule_pos]); 1048 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1049 | break; 1050 | 1051 | case RULE_OP_MANGLE_REPLACE: 1052 | SET_NAME (rule, rule_buf[rule_pos]); 1053 | SET_P0 (rule, rule_buf[rule_pos]); 1054 | SET_P1 (rule, rule_buf[rule_pos]); 1055 | break; 1056 | 1057 | case RULE_OP_MANGLE_PURGECHAR: 1058 | return (-1); 1059 | break; 1060 | 1061 | case RULE_OP_MANGLE_TOGGLECASE_REC: 1062 | return (-1); 1063 | break; 1064 | 1065 | case RULE_OP_MANGLE_DUPECHAR_FIRST: 1066 | SET_NAME (rule, rule_buf[rule_pos]); 1067 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1068 | break; 1069 | 1070 | case RULE_OP_MANGLE_DUPECHAR_LAST: 1071 | SET_NAME (rule, rule_buf[rule_pos]); 1072 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1073 | break; 1074 | 1075 | case RULE_OP_MANGLE_DUPECHAR_ALL: 1076 | SET_NAME (rule, rule_buf[rule_pos]); 1077 | break; 1078 | 1079 | case RULE_OP_MANGLE_SWITCH_FIRST: 1080 | SET_NAME (rule, rule_buf[rule_pos]); 1081 | break; 1082 | 1083 | case RULE_OP_MANGLE_SWITCH_LAST: 1084 | SET_NAME (rule, rule_buf[rule_pos]); 1085 | break; 1086 | 1087 | case RULE_OP_MANGLE_SWITCH_AT: 1088 | SET_NAME (rule, rule_buf[rule_pos]); 1089 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1090 | SET_P1_CONV (rule, rule_buf[rule_pos]); 1091 | break; 1092 | 1093 | case RULE_OP_MANGLE_CHR_SHIFTL: 1094 | SET_NAME (rule, rule_buf[rule_pos]); 1095 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1096 | break; 1097 | 1098 | case RULE_OP_MANGLE_CHR_SHIFTR: 1099 | SET_NAME (rule, rule_buf[rule_pos]); 1100 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1101 | break; 1102 | 1103 | case RULE_OP_MANGLE_CHR_INCR: 1104 | SET_NAME (rule, rule_buf[rule_pos]); 1105 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1106 | break; 1107 | 1108 | case RULE_OP_MANGLE_CHR_DECR: 1109 | SET_NAME (rule, rule_buf[rule_pos]); 1110 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1111 | break; 1112 | 1113 | case RULE_OP_MANGLE_REPLACE_NP1: 1114 | SET_NAME (rule, rule_buf[rule_pos]); 1115 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1116 | break; 1117 | 1118 | case RULE_OP_MANGLE_REPLACE_NM1: 1119 | SET_NAME (rule, rule_buf[rule_pos]); 1120 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1121 | break; 1122 | 1123 | case RULE_OP_MANGLE_DUPEBLOCK_FIRST: 1124 | SET_NAME (rule, rule_buf[rule_pos]); 1125 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1126 | break; 1127 | 1128 | case RULE_OP_MANGLE_DUPEBLOCK_LAST: 1129 | SET_NAME (rule, rule_buf[rule_pos]); 1130 | SET_P0_CONV (rule, rule_buf[rule_pos]); 1131 | break; 1132 | 1133 | case RULE_OP_MANGLE_TITLE: 1134 | SET_NAME (rule, rule_buf[rule_pos]); 1135 | break; 1136 | 1137 | default: 1138 | return (-1); 1139 | break; 1140 | } 1141 | } 1142 | 1143 | if (rule_pos < rule_len) return (-1); 1144 | 1145 | return (0); 1146 | } 1147 | 1148 | /** 1149 | * rules common 1150 | */ 1151 | 1152 | bool class_num (char c) 1153 | { 1154 | return ((c >= '0') && (c <= '9')); 1155 | } 1156 | 1157 | bool class_lower (char c) 1158 | { 1159 | return ((c >= 'a') && (c <= 'z')); 1160 | } 1161 | 1162 | bool class_upper (char c) 1163 | { 1164 | return ((c >= 'A') && (c <= 'Z')); 1165 | } 1166 | 1167 | bool class_alpha (char c) 1168 | { 1169 | return (class_lower (c) || class_upper (c)); 1170 | } 1171 | 1172 | char conv_ctoi (char c) 1173 | { 1174 | if (class_num (c)) 1175 | { 1176 | return (char)(c - '0'); 1177 | } 1178 | else if (class_upper (c)) 1179 | { 1180 | return (char)(c - 'A' + (char) 10); 1181 | } 1182 | 1183 | return (char) (-1); 1184 | } 1185 | 1186 | char conv_itoc (char c) 1187 | { 1188 | if (c < 10) 1189 | { 1190 | return (char)(c + (char)'0'); 1191 | } 1192 | else if (c < 37) 1193 | { 1194 | return (char)(c + (char)'A' - (char) 10); 1195 | } 1196 | 1197 | return (char) (-1); 1198 | } 1199 | 1200 | uint get_random_num (const uint min, const uint max) 1201 | { 1202 | if (min == max) return (min); 1203 | 1204 | const uint low = max - min; 1205 | 1206 | if (low == 0) return (0); 1207 | 1208 | uint data; 1209 | 1210 | FILE *fp = fopen("/dev/urandom", "rb"); 1211 | 1212 | if (fp == NULL) exit (1); 1213 | 1214 | if ((fread (&data, 1, sizeof (uint), fp)) != sizeof (uint)) 1215 | { 1216 | exit (-1); 1217 | } 1218 | 1219 | fclose (fp); 1220 | 1221 | uint64_t r = data % low; 1222 | 1223 | r += min; 1224 | 1225 | if (r > 0xffffffff) 1226 | { 1227 | exit (-1); 1228 | } 1229 | 1230 | return (uint) r; 1231 | } 1232 | -------------------------------------------------------------------------------- /src/cpu_rules.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Author......: Jens Steube 3 | * License.....: MIT 4 | */ 5 | 6 | #ifndef CPU_RULES_H 7 | #define CPU_RULES_H 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "rp_cpu.h" 14 | 15 | #define BLOCK_SIZE 64 16 | #define RULE_RC_REJECT_ERROR -2 17 | #define RP_RULE_BUFSIZ 0x100 18 | #define RULE_RC_SYNTAX_ERROR -1 19 | 20 | typedef unsigned int uint; 21 | 22 | typedef struct 23 | { 24 | uint cmds[32]; 25 | 26 | } gpu_rule_t; 27 | 28 | int mangle_lrest (char arr[BLOCK_SIZE], int arr_len); 29 | int mangle_urest (char arr[BLOCK_SIZE], int arr_len); 30 | int mangle_trest (char arr[BLOCK_SIZE], int arr_len); 31 | int mangle_reverse (char arr[BLOCK_SIZE], int arr_len); 32 | int mangle_double (char arr[BLOCK_SIZE], int arr_len); 33 | int mangle_double_times (char arr[BLOCK_SIZE], int arr_len, int times); 34 | int mangle_reflect (char arr[BLOCK_SIZE], int arr_len); 35 | int mangle_rotate_left (char arr[BLOCK_SIZE], int arr_len); 36 | int mangle_rotate_right (char arr[BLOCK_SIZE], int arr_len); 37 | int mangle_append (char arr[BLOCK_SIZE], int arr_len, char c); 38 | int mangle_prepend (char arr[BLOCK_SIZE], int arr_len, char c); 39 | int mangle_delete_at (char arr[BLOCK_SIZE], int arr_len, int upos); 40 | int mangle_extract (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen); 41 | int mangle_omit (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen); 42 | int mangle_insert (char arr[BLOCK_SIZE], int arr_len, int upos, char c); 43 | int mangle_insert_multi (char arr[BLOCK_SIZE], int arr_len, int arr_pos, char arr2[BLOCK_SIZE], int arr2_len, int arr2_pos, int arr2_cpy); 44 | int mangle_overstrike (char arr[BLOCK_SIZE], int arr_len, int upos, char c); 45 | int mangle_truncate_at (char arr[BLOCK_SIZE], int arr_len, int upos); 46 | int mangle_replace (char arr[BLOCK_SIZE], int arr_len, char oldc, char newc); 47 | int mangle_purgechar (char arr[BLOCK_SIZE], int arr_len, char c); 48 | int mangle_dupeblock_prepend (char arr[BLOCK_SIZE], int arr_len, int ulen); 49 | int mangle_dupeblock_append (char arr[BLOCK_SIZE], int arr_len, int ulen); 50 | int mangle_dupechar_at (char arr[BLOCK_SIZE], int arr_len, int upos, int ulen); 51 | int mangle_dupechar (char arr[BLOCK_SIZE], int arr_len); 52 | int mangle_switch_at_check (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2); 53 | int mangle_switch_at (char arr[BLOCK_SIZE], int arr_len, int upos, int upos2); 54 | int mangle_chr_shiftl (uint8_t arr[BLOCK_SIZE], int arr_len, int upos); 55 | int mangle_chr_shiftr (uint8_t arr[BLOCK_SIZE], int arr_len, int upos); 56 | int mangle_chr_incr (uint8_t arr[BLOCK_SIZE], int arr_len, int upos); 57 | int mangle_chr_decr (uint8_t arr[BLOCK_SIZE], int arr_len, int upos); 58 | int mangle_title (char arr[BLOCK_SIZE], int arr_len); 59 | int generate_random_rule (char rule_buf[RP_RULE_BUFSIZ], uint32_t rp_gen_func_min, uint32_t rp_gen_func_max); 60 | int apply_rule_cpu (char *rule, int rule_len, char in[BLOCK_SIZE], int in_len, char out[BLOCK_SIZE]); 61 | int cpu_rule_to_gpu_rule (char rule_buf[BUFSIZ], uint rule_len, gpu_rule_t *rule); 62 | 63 | typedef int bool; 64 | 65 | bool class_num (char c); 66 | bool class_lower (char c); 67 | bool class_upper (char c); 68 | bool class_alpha (char c); 69 | char conv_ctoi (char c); 70 | char conv_itoc (char c); 71 | 72 | uint get_random_num (uint min, uint max); 73 | 74 | void gen_cmask (const uint8_t *word, uint8_t *cmask, const uint len); 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /src/ct3_to_ntlm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | * Name........: deskey-to-ntlm.pl 6 | * Autor.......: Jens Steube 7 | * License.....: MIT 8 | * 9 | * Most of the code taken from hashcat 10 | */ 11 | 12 | typedef uint8_t u8; 13 | typedef uint32_t u32; 14 | 15 | #define BOX(v,i,S) (S)[(i)][(v)] 16 | 17 | #define PERM_OP(a,b,tt,n,m) \ 18 | { \ 19 | tt = a >> n; \ 20 | tt = tt ^ b; \ 21 | tt = tt & m; \ 22 | b = b ^ tt; \ 23 | tt = tt << n; \ 24 | a = a ^ tt; \ 25 | } 26 | 27 | #define HPERM_OP(a,tt,n,m) \ 28 | { \ 29 | tt = a << (16 + n); \ 30 | tt = tt ^ a; \ 31 | tt = tt & m; \ 32 | a = a ^ tt; \ 33 | tt = tt >> (16 + n); \ 34 | a = a ^ tt; \ 35 | } 36 | 37 | #define IP(l,r,tt) \ 38 | { \ 39 | PERM_OP (r, l, tt, 4, 0x0f0f0f0f); \ 40 | PERM_OP (l, r, tt, 16, 0x0000ffff); \ 41 | PERM_OP (r, l, tt, 2, 0x33333333); \ 42 | PERM_OP (l, r, tt, 8, 0x00ff00ff); \ 43 | PERM_OP (r, l, tt, 1, 0x55555555); \ 44 | } 45 | 46 | #define FP(l,r,tt) \ 47 | { \ 48 | PERM_OP (l, r, tt, 1, 0x55555555); \ 49 | PERM_OP (r, l, tt, 8, 0x00ff00ff); \ 50 | PERM_OP (l, r, tt, 2, 0x33333333); \ 51 | PERM_OP (r, l, tt, 16, 0x0000ffff); \ 52 | PERM_OP (l, r, tt, 4, 0x0f0f0f0f); \ 53 | } 54 | 55 | #define MD5_F(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) 56 | #define MD5_G(x,y,z) ((y) ^ ((z) & ((x) ^ (y)))) 57 | #define MD5_H(x,y,z) ((x) ^ (y) ^ (z)) 58 | #define MD5_I(x,y,z) ((y) ^ ((x) | ~(z))) 59 | #define MD5_Fo(x,y,z) (MD5_F((x), (y), (z))) 60 | #define MD5_Go(x,y,z) (MD5_G((x), (y), (z))) 61 | 62 | #define MD5_STEP(f,a,b,c,d,x,K,s) \ 63 | { \ 64 | a += K; \ 65 | a += x; \ 66 | a += f (b, c, d); \ 67 | a = rotl32 (a, s); \ 68 | a += b; \ 69 | } 70 | 71 | typedef enum md5_constants 72 | { 73 | MD5M_A=0x67452301, 74 | MD5M_B=0xefcdab89, 75 | MD5M_C=0x98badcfe, 76 | MD5M_D=0x10325476, 77 | 78 | MD5S00=7, 79 | MD5S01=12, 80 | MD5S02=17, 81 | MD5S03=22, 82 | MD5S10=5, 83 | MD5S11=9, 84 | MD5S12=14, 85 | MD5S13=20, 86 | MD5S20=4, 87 | MD5S21=11, 88 | MD5S22=16, 89 | MD5S23=23, 90 | MD5S30=6, 91 | MD5S31=10, 92 | MD5S32=15, 93 | MD5S33=21, 94 | 95 | MD5C00=0xd76aa478, 96 | MD5C01=0xe8c7b756, 97 | MD5C02=0x242070db, 98 | MD5C03=0xc1bdceee, 99 | MD5C04=0xf57c0faf, 100 | MD5C05=0x4787c62a, 101 | MD5C06=0xa8304613, 102 | MD5C07=0xfd469501, 103 | MD5C08=0x698098d8, 104 | MD5C09=0x8b44f7af, 105 | MD5C0a=0xffff5bb1, 106 | MD5C0b=0x895cd7be, 107 | MD5C0c=0x6b901122, 108 | MD5C0d=0xfd987193, 109 | MD5C0e=0xa679438e, 110 | MD5C0f=0x49b40821, 111 | MD5C10=0xf61e2562, 112 | MD5C11=0xc040b340, 113 | MD5C12=0x265e5a51, 114 | MD5C13=0xe9b6c7aa, 115 | MD5C14=0xd62f105d, 116 | MD5C15=0x02441453, 117 | MD5C16=0xd8a1e681, 118 | MD5C17=0xe7d3fbc8, 119 | MD5C18=0x21e1cde6, 120 | MD5C19=0xc33707d6, 121 | MD5C1a=0xf4d50d87, 122 | MD5C1b=0x455a14ed, 123 | MD5C1c=0xa9e3e905, 124 | MD5C1d=0xfcefa3f8, 125 | MD5C1e=0x676f02d9, 126 | MD5C1f=0x8d2a4c8a, 127 | MD5C20=0xfffa3942, 128 | MD5C21=0x8771f681, 129 | MD5C22=0x6d9d6122, 130 | MD5C23=0xfde5380c, 131 | MD5C24=0xa4beea44, 132 | MD5C25=0x4bdecfa9, 133 | MD5C26=0xf6bb4b60, 134 | MD5C27=0xbebfbc70, 135 | MD5C28=0x289b7ec6, 136 | MD5C29=0xeaa127fa, 137 | MD5C2a=0xd4ef3085, 138 | MD5C2b=0x04881d05, 139 | MD5C2c=0xd9d4d039, 140 | MD5C2d=0xe6db99e5, 141 | MD5C2e=0x1fa27cf8, 142 | MD5C2f=0xc4ac5665, 143 | MD5C30=0xf4292244, 144 | MD5C31=0x432aff97, 145 | MD5C32=0xab9423a7, 146 | MD5C33=0xfc93a039, 147 | MD5C34=0x655b59c3, 148 | MD5C35=0x8f0ccc92, 149 | MD5C36=0xffeff47d, 150 | MD5C37=0x85845dd1, 151 | MD5C38=0x6fa87e4f, 152 | MD5C39=0xfe2ce6e0, 153 | MD5C3a=0xa3014314, 154 | MD5C3b=0x4e0811a1, 155 | MD5C3c=0xf7537e82, 156 | MD5C3d=0xbd3af235, 157 | MD5C3e=0x2ad7d2bb, 158 | MD5C3f=0xeb86d391u 159 | 160 | } md5_constants_t; 161 | 162 | static const u32 c_SPtrans[8][64] = 163 | { 164 | { 165 | /* nibble 0 */ 166 | 0x02080800, 0x00080000, 0x02000002, 0x02080802, 167 | 0x02000000, 0x00080802, 0x00080002, 0x02000002, 168 | 0x00080802, 0x02080800, 0x02080000, 0x00000802, 169 | 0x02000802, 0x02000000, 0x00000000, 0x00080002, 170 | 0x00080000, 0x00000002, 0x02000800, 0x00080800, 171 | 0x02080802, 0x02080000, 0x00000802, 0x02000800, 172 | 0x00000002, 0x00000800, 0x00080800, 0x02080002, 173 | 0x00000800, 0x02000802, 0x02080002, 0x00000000, 174 | 0x00000000, 0x02080802, 0x02000800, 0x00080002, 175 | 0x02080800, 0x00080000, 0x00000802, 0x02000800, 176 | 0x02080002, 0x00000800, 0x00080800, 0x02000002, 177 | 0x00080802, 0x00000002, 0x02000002, 0x02080000, 178 | 0x02080802, 0x00080800, 0x02080000, 0x02000802, 179 | 0x02000000, 0x00000802, 0x00080002, 0x00000000, 180 | 0x00080000, 0x02000000, 0x02000802, 0x02080800, 181 | 0x00000002, 0x02080002, 0x00000800, 0x00080802, 182 | }, 183 | { 184 | /* nibble 1 */ 185 | 0x40108010, 0x00000000, 0x00108000, 0x40100000, 186 | 0x40000010, 0x00008010, 0x40008000, 0x00108000, 187 | 0x00008000, 0x40100010, 0x00000010, 0x40008000, 188 | 0x00100010, 0x40108000, 0x40100000, 0x00000010, 189 | 0x00100000, 0x40008010, 0x40100010, 0x00008000, 190 | 0x00108010, 0x40000000, 0x00000000, 0x00100010, 191 | 0x40008010, 0x00108010, 0x40108000, 0x40000010, 192 | 0x40000000, 0x00100000, 0x00008010, 0x40108010, 193 | 0x00100010, 0x40108000, 0x40008000, 0x00108010, 194 | 0x40108010, 0x00100010, 0x40000010, 0x00000000, 195 | 0x40000000, 0x00008010, 0x00100000, 0x40100010, 196 | 0x00008000, 0x40000000, 0x00108010, 0x40008010, 197 | 0x40108000, 0x00008000, 0x00000000, 0x40000010, 198 | 0x00000010, 0x40108010, 0x00108000, 0x40100000, 199 | 0x40100010, 0x00100000, 0x00008010, 0x40008000, 200 | 0x40008010, 0x00000010, 0x40100000, 0x00108000, 201 | }, 202 | { 203 | /* nibble 2 */ 204 | 0x04000001, 0x04040100, 0x00000100, 0x04000101, 205 | 0x00040001, 0x04000000, 0x04000101, 0x00040100, 206 | 0x04000100, 0x00040000, 0x04040000, 0x00000001, 207 | 0x04040101, 0x00000101, 0x00000001, 0x04040001, 208 | 0x00000000, 0x00040001, 0x04040100, 0x00000100, 209 | 0x00000101, 0x04040101, 0x00040000, 0x04000001, 210 | 0x04040001, 0x04000100, 0x00040101, 0x04040000, 211 | 0x00040100, 0x00000000, 0x04000000, 0x00040101, 212 | 0x04040100, 0x00000100, 0x00000001, 0x00040000, 213 | 0x00000101, 0x00040001, 0x04040000, 0x04000101, 214 | 0x00000000, 0x04040100, 0x00040100, 0x04040001, 215 | 0x00040001, 0x04000000, 0x04040101, 0x00000001, 216 | 0x00040101, 0x04000001, 0x04000000, 0x04040101, 217 | 0x00040000, 0x04000100, 0x04000101, 0x00040100, 218 | 0x04000100, 0x00000000, 0x04040001, 0x00000101, 219 | 0x04000001, 0x00040101, 0x00000100, 0x04040000, 220 | }, 221 | { 222 | /* nibble 3 */ 223 | 0x00401008, 0x10001000, 0x00000008, 0x10401008, 224 | 0x00000000, 0x10400000, 0x10001008, 0x00400008, 225 | 0x10401000, 0x10000008, 0x10000000, 0x00001008, 226 | 0x10000008, 0x00401008, 0x00400000, 0x10000000, 227 | 0x10400008, 0x00401000, 0x00001000, 0x00000008, 228 | 0x00401000, 0x10001008, 0x10400000, 0x00001000, 229 | 0x00001008, 0x00000000, 0x00400008, 0x10401000, 230 | 0x10001000, 0x10400008, 0x10401008, 0x00400000, 231 | 0x10400008, 0x00001008, 0x00400000, 0x10000008, 232 | 0x00401000, 0x10001000, 0x00000008, 0x10400000, 233 | 0x10001008, 0x00000000, 0x00001000, 0x00400008, 234 | 0x00000000, 0x10400008, 0x10401000, 0x00001000, 235 | 0x10000000, 0x10401008, 0x00401008, 0x00400000, 236 | 0x10401008, 0x00000008, 0x10001000, 0x00401008, 237 | 0x00400008, 0x00401000, 0x10400000, 0x10001008, 238 | 0x00001008, 0x10000000, 0x10000008, 0x10401000, 239 | }, 240 | { 241 | /* nibble 4 */ 242 | 0x08000000, 0x00010000, 0x00000400, 0x08010420, 243 | 0x08010020, 0x08000400, 0x00010420, 0x08010000, 244 | 0x00010000, 0x00000020, 0x08000020, 0x00010400, 245 | 0x08000420, 0x08010020, 0x08010400, 0x00000000, 246 | 0x00010400, 0x08000000, 0x00010020, 0x00000420, 247 | 0x08000400, 0x00010420, 0x00000000, 0x08000020, 248 | 0x00000020, 0x08000420, 0x08010420, 0x00010020, 249 | 0x08010000, 0x00000400, 0x00000420, 0x08010400, 250 | 0x08010400, 0x08000420, 0x00010020, 0x08010000, 251 | 0x00010000, 0x00000020, 0x08000020, 0x08000400, 252 | 0x08000000, 0x00010400, 0x08010420, 0x00000000, 253 | 0x00010420, 0x08000000, 0x00000400, 0x00010020, 254 | 0x08000420, 0x00000400, 0x00000000, 0x08010420, 255 | 0x08010020, 0x08010400, 0x00000420, 0x00010000, 256 | 0x00010400, 0x08010020, 0x08000400, 0x00000420, 257 | 0x00000020, 0x00010420, 0x08010000, 0x08000020, 258 | }, 259 | { 260 | /* nibble 5 */ 261 | 0x80000040, 0x00200040, 0x00000000, 0x80202000, 262 | 0x00200040, 0x00002000, 0x80002040, 0x00200000, 263 | 0x00002040, 0x80202040, 0x00202000, 0x80000000, 264 | 0x80002000, 0x80000040, 0x80200000, 0x00202040, 265 | 0x00200000, 0x80002040, 0x80200040, 0x00000000, 266 | 0x00002000, 0x00000040, 0x80202000, 0x80200040, 267 | 0x80202040, 0x80200000, 0x80000000, 0x00002040, 268 | 0x00000040, 0x00202000, 0x00202040, 0x80002000, 269 | 0x00002040, 0x80000000, 0x80002000, 0x00202040, 270 | 0x80202000, 0x00200040, 0x00000000, 0x80002000, 271 | 0x80000000, 0x00002000, 0x80200040, 0x00200000, 272 | 0x00200040, 0x80202040, 0x00202000, 0x00000040, 273 | 0x80202040, 0x00202000, 0x00200000, 0x80002040, 274 | 0x80000040, 0x80200000, 0x00202040, 0x00000000, 275 | 0x00002000, 0x80000040, 0x80002040, 0x80202000, 276 | 0x80200000, 0x00002040, 0x00000040, 0x80200040, 277 | }, 278 | { 279 | /* nibble 6 */ 280 | 0x00004000, 0x00000200, 0x01000200, 0x01000004, 281 | 0x01004204, 0x00004004, 0x00004200, 0x00000000, 282 | 0x01000000, 0x01000204, 0x00000204, 0x01004000, 283 | 0x00000004, 0x01004200, 0x01004000, 0x00000204, 284 | 0x01000204, 0x00004000, 0x00004004, 0x01004204, 285 | 0x00000000, 0x01000200, 0x01000004, 0x00004200, 286 | 0x01004004, 0x00004204, 0x01004200, 0x00000004, 287 | 0x00004204, 0x01004004, 0x00000200, 0x01000000, 288 | 0x00004204, 0x01004000, 0x01004004, 0x00000204, 289 | 0x00004000, 0x00000200, 0x01000000, 0x01004004, 290 | 0x01000204, 0x00004204, 0x00004200, 0x00000000, 291 | 0x00000200, 0x01000004, 0x00000004, 0x01000200, 292 | 0x00000000, 0x01000204, 0x01000200, 0x00004200, 293 | 0x00000204, 0x00004000, 0x01004204, 0x01000000, 294 | 0x01004200, 0x00000004, 0x00004004, 0x01004204, 295 | 0x01000004, 0x01004200, 0x01004000, 0x00004004, 296 | }, 297 | { 298 | /* nibble 7 */ 299 | 0x20800080, 0x20820000, 0x00020080, 0x00000000, 300 | 0x20020000, 0x00800080, 0x20800000, 0x20820080, 301 | 0x00000080, 0x20000000, 0x00820000, 0x00020080, 302 | 0x00820080, 0x20020080, 0x20000080, 0x20800000, 303 | 0x00020000, 0x00820080, 0x00800080, 0x20020000, 304 | 0x20820080, 0x20000080, 0x00000000, 0x00820000, 305 | 0x20000000, 0x00800000, 0x20020080, 0x20800080, 306 | 0x00800000, 0x00020000, 0x20820000, 0x00000080, 307 | 0x00800000, 0x00020000, 0x20000080, 0x20820080, 308 | 0x00020080, 0x20000000, 0x00000000, 0x00820000, 309 | 0x20800080, 0x20020080, 0x20020000, 0x00800080, 310 | 0x20820000, 0x00000080, 0x00800080, 0x20020000, 311 | 0x20820080, 0x00800000, 0x20800000, 0x20000080, 312 | 0x00820000, 0x00020080, 0x20020080, 0x20800000, 313 | 0x00000080, 0x20820000, 0x00820080, 0x00000000, 314 | 0x20000000, 0x20800080, 0x00020000, 0x00820080, 315 | }, 316 | }; 317 | 318 | static const u32 c_skb[8][64] = 319 | { 320 | { 321 | /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ 322 | 0x00000000, 0x00000010, 0x20000000, 0x20000010, 323 | 0x00010000, 0x00010010, 0x20010000, 0x20010010, 324 | 0x00000800, 0x00000810, 0x20000800, 0x20000810, 325 | 0x00010800, 0x00010810, 0x20010800, 0x20010810, 326 | 0x00000020, 0x00000030, 0x20000020, 0x20000030, 327 | 0x00010020, 0x00010030, 0x20010020, 0x20010030, 328 | 0x00000820, 0x00000830, 0x20000820, 0x20000830, 329 | 0x00010820, 0x00010830, 0x20010820, 0x20010830, 330 | 0x00080000, 0x00080010, 0x20080000, 0x20080010, 331 | 0x00090000, 0x00090010, 0x20090000, 0x20090010, 332 | 0x00080800, 0x00080810, 0x20080800, 0x20080810, 333 | 0x00090800, 0x00090810, 0x20090800, 0x20090810, 334 | 0x00080020, 0x00080030, 0x20080020, 0x20080030, 335 | 0x00090020, 0x00090030, 0x20090020, 0x20090030, 336 | 0x00080820, 0x00080830, 0x20080820, 0x20080830, 337 | 0x00090820, 0x00090830, 0x20090820, 0x20090830, 338 | }, 339 | { 340 | /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ 341 | 0x00000000, 0x02000000, 0x00002000, 0x02002000, 342 | 0x00200000, 0x02200000, 0x00202000, 0x02202000, 343 | 0x00000004, 0x02000004, 0x00002004, 0x02002004, 344 | 0x00200004, 0x02200004, 0x00202004, 0x02202004, 345 | 0x00000400, 0x02000400, 0x00002400, 0x02002400, 346 | 0x00200400, 0x02200400, 0x00202400, 0x02202400, 347 | 0x00000404, 0x02000404, 0x00002404, 0x02002404, 348 | 0x00200404, 0x02200404, 0x00202404, 0x02202404, 349 | 0x10000000, 0x12000000, 0x10002000, 0x12002000, 350 | 0x10200000, 0x12200000, 0x10202000, 0x12202000, 351 | 0x10000004, 0x12000004, 0x10002004, 0x12002004, 352 | 0x10200004, 0x12200004, 0x10202004, 0x12202004, 353 | 0x10000400, 0x12000400, 0x10002400, 0x12002400, 354 | 0x10200400, 0x12200400, 0x10202400, 0x12202400, 355 | 0x10000404, 0x12000404, 0x10002404, 0x12002404, 356 | 0x10200404, 0x12200404, 0x10202404, 0x12202404, 357 | }, 358 | { 359 | /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ 360 | 0x00000000, 0x00000001, 0x00040000, 0x00040001, 361 | 0x01000000, 0x01000001, 0x01040000, 0x01040001, 362 | 0x00000002, 0x00000003, 0x00040002, 0x00040003, 363 | 0x01000002, 0x01000003, 0x01040002, 0x01040003, 364 | 0x00000200, 0x00000201, 0x00040200, 0x00040201, 365 | 0x01000200, 0x01000201, 0x01040200, 0x01040201, 366 | 0x00000202, 0x00000203, 0x00040202, 0x00040203, 367 | 0x01000202, 0x01000203, 0x01040202, 0x01040203, 368 | 0x08000000, 0x08000001, 0x08040000, 0x08040001, 369 | 0x09000000, 0x09000001, 0x09040000, 0x09040001, 370 | 0x08000002, 0x08000003, 0x08040002, 0x08040003, 371 | 0x09000002, 0x09000003, 0x09040002, 0x09040003, 372 | 0x08000200, 0x08000201, 0x08040200, 0x08040201, 373 | 0x09000200, 0x09000201, 0x09040200, 0x09040201, 374 | 0x08000202, 0x08000203, 0x08040202, 0x08040203, 375 | 0x09000202, 0x09000203, 0x09040202, 0x09040203, 376 | }, 377 | { 378 | /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ 379 | 0x00000000, 0x00100000, 0x00000100, 0x00100100, 380 | 0x00000008, 0x00100008, 0x00000108, 0x00100108, 381 | 0x00001000, 0x00101000, 0x00001100, 0x00101100, 382 | 0x00001008, 0x00101008, 0x00001108, 0x00101108, 383 | 0x04000000, 0x04100000, 0x04000100, 0x04100100, 384 | 0x04000008, 0x04100008, 0x04000108, 0x04100108, 385 | 0x04001000, 0x04101000, 0x04001100, 0x04101100, 386 | 0x04001008, 0x04101008, 0x04001108, 0x04101108, 387 | 0x00020000, 0x00120000, 0x00020100, 0x00120100, 388 | 0x00020008, 0x00120008, 0x00020108, 0x00120108, 389 | 0x00021000, 0x00121000, 0x00021100, 0x00121100, 390 | 0x00021008, 0x00121008, 0x00021108, 0x00121108, 391 | 0x04020000, 0x04120000, 0x04020100, 0x04120100, 392 | 0x04020008, 0x04120008, 0x04020108, 0x04120108, 393 | 0x04021000, 0x04121000, 0x04021100, 0x04121100, 394 | 0x04021008, 0x04121008, 0x04021108, 0x04121108, 395 | }, 396 | { 397 | /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ 398 | 0x00000000, 0x10000000, 0x00010000, 0x10010000, 399 | 0x00000004, 0x10000004, 0x00010004, 0x10010004, 400 | 0x20000000, 0x30000000, 0x20010000, 0x30010000, 401 | 0x20000004, 0x30000004, 0x20010004, 0x30010004, 402 | 0x00100000, 0x10100000, 0x00110000, 0x10110000, 403 | 0x00100004, 0x10100004, 0x00110004, 0x10110004, 404 | 0x20100000, 0x30100000, 0x20110000, 0x30110000, 405 | 0x20100004, 0x30100004, 0x20110004, 0x30110004, 406 | 0x00001000, 0x10001000, 0x00011000, 0x10011000, 407 | 0x00001004, 0x10001004, 0x00011004, 0x10011004, 408 | 0x20001000, 0x30001000, 0x20011000, 0x30011000, 409 | 0x20001004, 0x30001004, 0x20011004, 0x30011004, 410 | 0x00101000, 0x10101000, 0x00111000, 0x10111000, 411 | 0x00101004, 0x10101004, 0x00111004, 0x10111004, 412 | 0x20101000, 0x30101000, 0x20111000, 0x30111000, 413 | 0x20101004, 0x30101004, 0x20111004, 0x30111004, 414 | }, 415 | { 416 | /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ 417 | 0x00000000, 0x08000000, 0x00000008, 0x08000008, 418 | 0x00000400, 0x08000400, 0x00000408, 0x08000408, 419 | 0x00020000, 0x08020000, 0x00020008, 0x08020008, 420 | 0x00020400, 0x08020400, 0x00020408, 0x08020408, 421 | 0x00000001, 0x08000001, 0x00000009, 0x08000009, 422 | 0x00000401, 0x08000401, 0x00000409, 0x08000409, 423 | 0x00020001, 0x08020001, 0x00020009, 0x08020009, 424 | 0x00020401, 0x08020401, 0x00020409, 0x08020409, 425 | 0x02000000, 0x0A000000, 0x02000008, 0x0A000008, 426 | 0x02000400, 0x0A000400, 0x02000408, 0x0A000408, 427 | 0x02020000, 0x0A020000, 0x02020008, 0x0A020008, 428 | 0x02020400, 0x0A020400, 0x02020408, 0x0A020408, 429 | 0x02000001, 0x0A000001, 0x02000009, 0x0A000009, 430 | 0x02000401, 0x0A000401, 0x02000409, 0x0A000409, 431 | 0x02020001, 0x0A020001, 0x02020009, 0x0A020009, 432 | 0x02020401, 0x0A020401, 0x02020409, 0x0A020409, 433 | }, 434 | { 435 | /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ 436 | 0x00000000, 0x00000100, 0x00080000, 0x00080100, 437 | 0x01000000, 0x01000100, 0x01080000, 0x01080100, 438 | 0x00000010, 0x00000110, 0x00080010, 0x00080110, 439 | 0x01000010, 0x01000110, 0x01080010, 0x01080110, 440 | 0x00200000, 0x00200100, 0x00280000, 0x00280100, 441 | 0x01200000, 0x01200100, 0x01280000, 0x01280100, 442 | 0x00200010, 0x00200110, 0x00280010, 0x00280110, 443 | 0x01200010, 0x01200110, 0x01280010, 0x01280110, 444 | 0x00000200, 0x00000300, 0x00080200, 0x00080300, 445 | 0x01000200, 0x01000300, 0x01080200, 0x01080300, 446 | 0x00000210, 0x00000310, 0x00080210, 0x00080310, 447 | 0x01000210, 0x01000310, 0x01080210, 0x01080310, 448 | 0x00200200, 0x00200300, 0x00280200, 0x00280300, 449 | 0x01200200, 0x01200300, 0x01280200, 0x01280300, 450 | 0x00200210, 0x00200310, 0x00280210, 0x00280310, 451 | 0x01200210, 0x01200310, 0x01280210, 0x01280310, 452 | }, 453 | { 454 | /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ 455 | 0x00000000, 0x04000000, 0x00040000, 0x04040000, 456 | 0x00000002, 0x04000002, 0x00040002, 0x04040002, 457 | 0x00002000, 0x04002000, 0x00042000, 0x04042000, 458 | 0x00002002, 0x04002002, 0x00042002, 0x04042002, 459 | 0x00000020, 0x04000020, 0x00040020, 0x04040020, 460 | 0x00000022, 0x04000022, 0x00040022, 0x04040022, 461 | 0x00002020, 0x04002020, 0x00042020, 0x04042020, 462 | 0x00002022, 0x04002022, 0x00042022, 0x04042022, 463 | 0x00000800, 0x04000800, 0x00040800, 0x04040800, 464 | 0x00000802, 0x04000802, 0x00040802, 0x04040802, 465 | 0x00002800, 0x04002800, 0x00042800, 0x04042800, 466 | 0x00002802, 0x04002802, 0x00042802, 0x04042802, 467 | 0x00000820, 0x04000820, 0x00040820, 0x04040820, 468 | 0x00000822, 0x04000822, 0x00040822, 0x04040822, 469 | 0x00002820, 0x04002820, 0x00042820, 0x04042820, 470 | 0x00002822, 0x04002822, 0x00042822, 0x04042822 471 | } 472 | }; 473 | 474 | static u32 byte_swap_32 (const u32 n) 475 | { 476 | return (n & 0xff000000) >> 24 477 | | (n & 0x00ff0000) >> 8 478 | | (n & 0x0000ff00) << 8 479 | | (n & 0x000000ff) << 24; 480 | } 481 | 482 | static u32 rotl32 (const u32 a, const u32 n) 483 | { 484 | return ((a << n) | (a >> (32 - n))); 485 | } 486 | 487 | static void md5_64 (u32 block[16], u32 digest[4]) 488 | { 489 | u32 w0[4]; 490 | u32 w1[4]; 491 | u32 w2[4]; 492 | u32 w3[4]; 493 | 494 | w0[0] = block[ 0]; 495 | w0[1] = block[ 1]; 496 | w0[2] = block[ 2]; 497 | w0[3] = block[ 3]; 498 | w1[0] = block[ 4]; 499 | w1[1] = block[ 5]; 500 | w1[2] = block[ 6]; 501 | w1[3] = block[ 7]; 502 | w2[0] = block[ 8]; 503 | w2[1] = block[ 9]; 504 | w2[2] = block[10]; 505 | w2[3] = block[11]; 506 | w3[0] = block[12]; 507 | w3[1] = block[13]; 508 | w3[2] = block[14]; 509 | w3[3] = block[15]; 510 | 511 | u32 a = digest[0]; 512 | u32 b = digest[1]; 513 | u32 c = digest[2]; 514 | u32 d = digest[3]; 515 | 516 | MD5_STEP (MD5_Fo, a, b, c, d, w0[0], MD5C00, MD5S00); 517 | MD5_STEP (MD5_Fo, d, a, b, c, w0[1], MD5C01, MD5S01); 518 | MD5_STEP (MD5_Fo, c, d, a, b, w0[2], MD5C02, MD5S02); 519 | MD5_STEP (MD5_Fo, b, c, d, a, w0[3], MD5C03, MD5S03); 520 | MD5_STEP (MD5_Fo, a, b, c, d, w1[0], MD5C04, MD5S00); 521 | MD5_STEP (MD5_Fo, d, a, b, c, w1[1], MD5C05, MD5S01); 522 | MD5_STEP (MD5_Fo, c, d, a, b, w1[2], MD5C06, MD5S02); 523 | MD5_STEP (MD5_Fo, b, c, d, a, w1[3], MD5C07, MD5S03); 524 | MD5_STEP (MD5_Fo, a, b, c, d, w2[0], MD5C08, MD5S00); 525 | MD5_STEP (MD5_Fo, d, a, b, c, w2[1], MD5C09, MD5S01); 526 | MD5_STEP (MD5_Fo, c, d, a, b, w2[2], MD5C0a, MD5S02); 527 | MD5_STEP (MD5_Fo, b, c, d, a, w2[3], MD5C0b, MD5S03); 528 | MD5_STEP (MD5_Fo, a, b, c, d, w3[0], MD5C0c, MD5S00); 529 | MD5_STEP (MD5_Fo, d, a, b, c, w3[1], MD5C0d, MD5S01); 530 | MD5_STEP (MD5_Fo, c, d, a, b, w3[2], MD5C0e, MD5S02); 531 | MD5_STEP (MD5_Fo, b, c, d, a, w3[3], MD5C0f, MD5S03); 532 | 533 | MD5_STEP (MD5_Go, a, b, c, d, w0[1], MD5C10, MD5S10); 534 | MD5_STEP (MD5_Go, d, a, b, c, w1[2], MD5C11, MD5S11); 535 | MD5_STEP (MD5_Go, c, d, a, b, w2[3], MD5C12, MD5S12); 536 | MD5_STEP (MD5_Go, b, c, d, a, w0[0], MD5C13, MD5S13); 537 | MD5_STEP (MD5_Go, a, b, c, d, w1[1], MD5C14, MD5S10); 538 | MD5_STEP (MD5_Go, d, a, b, c, w2[2], MD5C15, MD5S11); 539 | MD5_STEP (MD5_Go, c, d, a, b, w3[3], MD5C16, MD5S12); 540 | MD5_STEP (MD5_Go, b, c, d, a, w1[0], MD5C17, MD5S13); 541 | MD5_STEP (MD5_Go, a, b, c, d, w2[1], MD5C18, MD5S10); 542 | MD5_STEP (MD5_Go, d, a, b, c, w3[2], MD5C19, MD5S11); 543 | MD5_STEP (MD5_Go, c, d, a, b, w0[3], MD5C1a, MD5S12); 544 | MD5_STEP (MD5_Go, b, c, d, a, w2[0], MD5C1b, MD5S13); 545 | MD5_STEP (MD5_Go, a, b, c, d, w3[1], MD5C1c, MD5S10); 546 | MD5_STEP (MD5_Go, d, a, b, c, w0[2], MD5C1d, MD5S11); 547 | MD5_STEP (MD5_Go, c, d, a, b, w1[3], MD5C1e, MD5S12); 548 | MD5_STEP (MD5_Go, b, c, d, a, w3[0], MD5C1f, MD5S13); 549 | 550 | MD5_STEP (MD5_H , a, b, c, d, w1[1], MD5C20, MD5S20); 551 | MD5_STEP (MD5_H , d, a, b, c, w2[0], MD5C21, MD5S21); 552 | MD5_STEP (MD5_H , c, d, a, b, w2[3], MD5C22, MD5S22); 553 | MD5_STEP (MD5_H , b, c, d, a, w3[2], MD5C23, MD5S23); 554 | MD5_STEP (MD5_H , a, b, c, d, w0[1], MD5C24, MD5S20); 555 | MD5_STEP (MD5_H , d, a, b, c, w1[0], MD5C25, MD5S21); 556 | MD5_STEP (MD5_H , c, d, a, b, w1[3], MD5C26, MD5S22); 557 | MD5_STEP (MD5_H , b, c, d, a, w2[2], MD5C27, MD5S23); 558 | MD5_STEP (MD5_H , a, b, c, d, w3[1], MD5C28, MD5S20); 559 | MD5_STEP (MD5_H , d, a, b, c, w0[0], MD5C29, MD5S21); 560 | MD5_STEP (MD5_H , c, d, a, b, w0[3], MD5C2a, MD5S22); 561 | MD5_STEP (MD5_H , b, c, d, a, w1[2], MD5C2b, MD5S23); 562 | MD5_STEP (MD5_H , a, b, c, d, w2[1], MD5C2c, MD5S20); 563 | MD5_STEP (MD5_H , d, a, b, c, w3[0], MD5C2d, MD5S21); 564 | MD5_STEP (MD5_H , c, d, a, b, w3[3], MD5C2e, MD5S22); 565 | MD5_STEP (MD5_H , b, c, d, a, w0[2], MD5C2f, MD5S23); 566 | 567 | MD5_STEP (MD5_I , a, b, c, d, w0[0], MD5C30, MD5S30); 568 | MD5_STEP (MD5_I , d, a, b, c, w1[3], MD5C31, MD5S31); 569 | MD5_STEP (MD5_I , c, d, a, b, w3[2], MD5C32, MD5S32); 570 | MD5_STEP (MD5_I , b, c, d, a, w1[1], MD5C33, MD5S33); 571 | MD5_STEP (MD5_I , a, b, c, d, w3[0], MD5C34, MD5S30); 572 | MD5_STEP (MD5_I , d, a, b, c, w0[3], MD5C35, MD5S31); 573 | MD5_STEP (MD5_I , c, d, a, b, w2[2], MD5C36, MD5S32); 574 | MD5_STEP (MD5_I , b, c, d, a, w0[1], MD5C37, MD5S33); 575 | MD5_STEP (MD5_I , a, b, c, d, w2[0], MD5C38, MD5S30); 576 | MD5_STEP (MD5_I , d, a, b, c, w3[3], MD5C39, MD5S31); 577 | MD5_STEP (MD5_I , c, d, a, b, w1[2], MD5C3a, MD5S32); 578 | MD5_STEP (MD5_I , b, c, d, a, w3[1], MD5C3b, MD5S33); 579 | MD5_STEP (MD5_I , a, b, c, d, w1[0], MD5C3c, MD5S30); 580 | MD5_STEP (MD5_I , d, a, b, c, w2[3], MD5C3d, MD5S31); 581 | MD5_STEP (MD5_I , c, d, a, b, w0[2], MD5C3e, MD5S32); 582 | MD5_STEP (MD5_I , b, c, d, a, w2[1], MD5C3f, MD5S33); 583 | 584 | digest[0] += a; 585 | digest[1] += b; 586 | digest[2] += c; 587 | digest[3] += d; 588 | } 589 | 590 | static void _des_keysetup (u32 data[2], u32 Kc[16], u32 Kd[16]) 591 | { 592 | u32 c = data[0]; 593 | u32 d = data[1]; 594 | 595 | u32 tt; 596 | 597 | PERM_OP (d, c, tt, 4, 0x0f0f0f0f); 598 | HPERM_OP (c, tt, 2, 0xcccc0000); 599 | HPERM_OP (d, tt, 2, 0xcccc0000); 600 | PERM_OP (d, c, tt, 1, 0x55555555); 601 | PERM_OP (c, d, tt, 8, 0x00ff00ff); 602 | PERM_OP (d, c, tt, 1, 0x55555555); 603 | 604 | d = ((d & 0x000000ff) << 16) 605 | | ((d & 0x0000ff00) << 0) 606 | | ((d & 0x00ff0000) >> 16) 607 | | ((c & 0xf0000000) >> 4); 608 | 609 | c = c & 0x0fffffff; 610 | 611 | int i; 612 | 613 | for (i = 0; i < 16; i++) 614 | { 615 | const u32 shifts3s0[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; 616 | const u32 shifts3s1[16] = { 27, 27, 26, 26, 26, 26, 26, 26, 27, 26, 26, 26, 26, 26, 26, 27 }; 617 | 618 | c = c >> shifts3s0[i] | c << shifts3s1[i]; 619 | d = d >> shifts3s0[i] | d << shifts3s1[i]; 620 | 621 | c = c & 0x0fffffff; 622 | d = d & 0x0fffffff; 623 | 624 | u32 s = BOX ((( c >> 0) & 0x3f), 0, c_skb) 625 | | BOX ((((c >> 6) & 0x03) 626 | | ((c >> 7) & 0x3c)), 1, c_skb) 627 | | BOX ((((c >> 13) & 0x0f) 628 | | ((c >> 14) & 0x30)), 2, c_skb) 629 | | BOX ((((c >> 20) & 0x01) 630 | | ((c >> 21) & 0x06) 631 | | ((c >> 22) & 0x38)), 3, c_skb); 632 | 633 | u32 t = BOX ((( d >> 0) & 0x3f), 4, c_skb) 634 | | BOX ((((d >> 7) & 0x03) 635 | | ((d >> 8) & 0x3c)), 5, c_skb) 636 | | BOX ((((d >> 15) & 0x3f)), 6, c_skb) 637 | | BOX ((((d >> 21) & 0x0f) 638 | | ((d >> 22) & 0x30)), 7, c_skb); 639 | 640 | Kc[i] = ((t << 16) | (s & 0x0000ffff)); 641 | Kd[i] = ((s >> 16) | (t & 0xffff0000)); 642 | 643 | Kc[i] = rotl32 (Kc[i], 2u); 644 | Kd[i] = rotl32 (Kd[i], 2u); 645 | } 646 | } 647 | 648 | static void _des_encrypt (u32 data[2], u32 Kc[16], u32 Kd[16]) 649 | { 650 | u32 r = data[0]; 651 | u32 l = data[1]; 652 | 653 | u32 tt; 654 | 655 | IP (r, l, tt); 656 | 657 | r = rotl32 (r, 3u); 658 | l = rotl32 (l, 3u); 659 | 660 | int i; 661 | 662 | for (i = 0; i < 16; i++) 663 | { 664 | u32 u = Kc[i] ^ r; 665 | u32 t = Kd[i] ^ rotl32 (r, 28u); 666 | 667 | l ^= BOX (((u >> 2) & 0x3f), 0, c_SPtrans) 668 | | BOX (((u >> 10) & 0x3f), 2, c_SPtrans) 669 | | BOX (((u >> 18) & 0x3f), 4, c_SPtrans) 670 | | BOX (((u >> 26) & 0x3f), 6, c_SPtrans) 671 | | BOX (((t >> 2) & 0x3f), 1, c_SPtrans) 672 | | BOX (((t >> 10) & 0x3f), 3, c_SPtrans) 673 | | BOX (((t >> 18) & 0x3f), 5, c_SPtrans) 674 | | BOX (((t >> 26) & 0x3f), 7, c_SPtrans); 675 | 676 | tt = l; 677 | l = r; 678 | r = tt; 679 | } 680 | 681 | l = rotl32 (l, 29u); 682 | r = rotl32 (r, 29u); 683 | 684 | FP (r, l, tt); 685 | 686 | data[0] = l; 687 | data[1] = r; 688 | } 689 | 690 | static void transform_netntlmv1_key (const u8 *nthash, u8 *key) 691 | { 692 | key[0] = (nthash[0] >> 0); 693 | key[1] = (nthash[0] << 7) | (nthash[1] >> 1); 694 | key[2] = (nthash[1] << 6) | (nthash[2] >> 2); 695 | key[3] = (nthash[2] << 5) | (nthash[3] >> 3); 696 | key[4] = (nthash[3] << 4) | (nthash[4] >> 4); 697 | key[5] = (nthash[4] << 3) | (nthash[5] >> 5); 698 | key[6] = (nthash[5] << 2) | (nthash[6] >> 6); 699 | key[7] = (nthash[6] << 1); 700 | 701 | key[0] |= 0x01; 702 | key[1] |= 0x01; 703 | key[2] |= 0x01; 704 | key[3] |= 0x01; 705 | key[4] |= 0x01; 706 | key[5] |= 0x01; 707 | key[6] |= 0x01; 708 | key[7] |= 0x01; 709 | } 710 | 711 | int main (int argc, char *argv[]) 712 | { 713 | u32 ct3_buf[2]; 714 | u32 salt_buf[2]; 715 | u32 chall_buf[6]; 716 | 717 | if ((argc != 3) && (argc != 4)) 718 | { 719 | fprintf (stderr, "usage: %s 8-byte-ct3-in-hex 8-byte-salt-in-hex [24-byte-ESS-in-hex]\n", argv[0]); 720 | 721 | return -1; 722 | } 723 | 724 | if (sscanf (argv[1], "%08x%08x", &ct3_buf[0], &ct3_buf[1]) != 2) 725 | { 726 | fprintf (stderr, "Invalid data '%s'\n", argv[1]); 727 | 728 | return -1; 729 | } 730 | 731 | ct3_buf[0] = byte_swap_32 (ct3_buf[0]); 732 | ct3_buf[1] = byte_swap_32 (ct3_buf[1]); 733 | 734 | if (sscanf (argv[2], "%08x%08x", &salt_buf[0], &salt_buf[1]) != 2) 735 | { 736 | fprintf (stderr, "Invalid data '%s'\n", argv[2]); 737 | 738 | return -1; 739 | } 740 | 741 | salt_buf[0] = byte_swap_32 (salt_buf[0]); 742 | salt_buf[1] = byte_swap_32 (salt_buf[1]); 743 | 744 | if (argc == 4) 745 | { 746 | if (sscanf (argv[3], "%08x%08x%08x%08x%08x%08x", &chall_buf[0], &chall_buf[1], &chall_buf[2], &chall_buf[3], &chall_buf[4], &chall_buf[5]) != 6) 747 | { 748 | fprintf (stderr, "Invalid data '%s'\n", argv[3]); 749 | 750 | return -1; 751 | } 752 | 753 | chall_buf[0] = byte_swap_32 (chall_buf[0]); 754 | chall_buf[1] = byte_swap_32 (chall_buf[1]); 755 | chall_buf[2] = byte_swap_32 (chall_buf[2]); 756 | chall_buf[3] = byte_swap_32 (chall_buf[3]); 757 | chall_buf[4] = byte_swap_32 (chall_buf[4]); 758 | chall_buf[5] = byte_swap_32 (chall_buf[5]); 759 | 760 | if ((chall_buf[2] == 0) && (chall_buf[3] == 0) && (chall_buf[4] == 0) && (chall_buf[5] == 0)) 761 | { 762 | u32 w[16] = { 0 }; 763 | 764 | w[ 0] = salt_buf[0]; 765 | w[ 1] = salt_buf[1]; 766 | w[ 2] = chall_buf[0]; 767 | w[ 3] = chall_buf[1]; 768 | w[ 4] = 0x80; 769 | w[14] = 16 * 8; 770 | 771 | u32 dgst[4] = { 0 }; 772 | 773 | dgst[0] = MD5M_A; 774 | dgst[1] = MD5M_B; 775 | dgst[2] = MD5M_C; 776 | dgst[3] = MD5M_D; 777 | 778 | md5_64 (w, dgst); 779 | 780 | salt_buf[0] = dgst[0]; 781 | salt_buf[1] = dgst[1]; 782 | } 783 | } 784 | 785 | for (u32 i = 0; i < 0x10000; i++) 786 | { 787 | u32 key_md4[2] = { i, 0 }; 788 | u32 key_des[2] = { 0, 0 }; 789 | 790 | transform_netntlmv1_key ((u8 *) key_md4, (u8 *) key_des); 791 | 792 | u32 Kc[16] = { 0 }; 793 | u32 Kd[16] = { 0 }; 794 | 795 | _des_keysetup (key_des, Kc, Kd); 796 | 797 | u32 pt3_buf[2] = { salt_buf[0], salt_buf[1] }; 798 | 799 | _des_encrypt (pt3_buf, Kc, Kd); 800 | 801 | if (pt3_buf[0] != ct3_buf[0]) continue; 802 | if (pt3_buf[1] != ct3_buf[1]) continue; 803 | 804 | printf ("%02x%02x\n", (i >> 0) & 0xff, (i >> 8) & 0xff); 805 | 806 | return 0; 807 | } 808 | 809 | fprintf (stderr, "Key not found\n"); 810 | 811 | return -1; 812 | } 813 | -------------------------------------------------------------------------------- /src/cutb.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: cutb 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | */ 19 | 20 | int main (int argc, char *argv[]) 21 | { 22 | if ((argc != 2) && (argc != 3)) 23 | { 24 | fprintf (stderr, "usage: %s offset [length] < infile > outfile\n", argv[0]); 25 | 26 | return (-1); 27 | } 28 | 29 | #ifdef _WINDOWS 30 | _setmode (_fileno (stdin), _O_BINARY); 31 | #endif 32 | 33 | int offset = atoi (argv[1]); 34 | 35 | int length = 1000; 36 | 37 | if (argc == 3) 38 | { 39 | length = atoi (argv[2]); 40 | } 41 | 42 | char line_buf[BUFSIZ]; 43 | 44 | int line_len; 45 | 46 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 47 | { 48 | char *ptr = line_buf; 49 | 50 | if (offset > 0) 51 | { 52 | ptr += offset; 53 | 54 | line_len -= offset; 55 | } 56 | else if (offset < 0) 57 | { 58 | ptr += line_len + offset; 59 | 60 | line_len += offset; 61 | } 62 | 63 | if (line_len > length) line_len = length; 64 | 65 | ptr[line_len] = 0; 66 | 67 | puts (ptr); 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /src/deskey_to_ntlm.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | ## Name........: deskey-to-ntlm.pl 4 | ## Autor.......: Jens Steube 5 | ## License.....: MIT 6 | 7 | use strict; 8 | use warnings; 9 | 10 | my $des_key = $ARGV[0] or die ("usage: $0 8-byte-key-in-hex\n"); 11 | 12 | my $des_key_bin = pack ("H16", $des_key); 13 | 14 | my @des_keys = split "", $des_key_bin; 15 | 16 | my @ntlm; 17 | 18 | $ntlm[0] = (((ord ($des_keys[0]) << 0) & ~0x01) | (ord ($des_keys[1]) >> 7)) & 0xff; 19 | $ntlm[1] = (((ord ($des_keys[1]) << 1) & ~0x03) | (ord ($des_keys[2]) >> 6)) & 0xff; 20 | $ntlm[2] = (((ord ($des_keys[2]) << 2) & ~0x07) | (ord ($des_keys[3]) >> 5)) & 0xff; 21 | $ntlm[3] = (((ord ($des_keys[3]) << 3) & ~0x0f) | (ord ($des_keys[4]) >> 4)) & 0xff; 22 | $ntlm[4] = (((ord ($des_keys[4]) << 4) & ~0x1f) | (ord ($des_keys[5]) >> 3)) & 0xff; 23 | $ntlm[5] = (((ord ($des_keys[5]) << 5) & ~0x3f) | (ord ($des_keys[6]) >> 2)) & 0xff; 24 | $ntlm[6] = (((ord ($des_keys[6]) << 6) & ~0x7f) | (ord ($des_keys[7]) >> 1)) & 0xff; 25 | 26 | printf "%02x%02x%02x%02x%02x%02x%02x\n", 27 | $ntlm[0], 28 | $ntlm[1], 29 | $ntlm[2], 30 | $ntlm[3], 31 | $ntlm[4], 32 | $ntlm[5], 33 | $ntlm[6]; 34 | -------------------------------------------------------------------------------- /src/expander.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define LEN_MIN 1 15 | #define LEN_MAX 4 16 | 17 | /** 18 | * Name........: expander 19 | * Autor.......: Jens Steube 20 | * License.....: MIT 21 | */ 22 | 23 | void strrotl (char *s, int len) 24 | { 25 | char *s1 = s; 26 | char *s2 = s; 27 | 28 | for (s2 += len - 1; s1 < s2; s2--) 29 | { 30 | *s1 ^= *s2; 31 | *s2 ^= *s1; 32 | *s1 ^= *s2; 33 | } 34 | } 35 | 36 | void strrotr (char *s, int len) 37 | { 38 | char *s1 = s; 39 | char *s2 = s; 40 | 41 | for (s2++; s2 < s1 + len; s2++) 42 | { 43 | *s1 ^= *s2; 44 | *s2 ^= *s1; 45 | *s1 ^= *s2; 46 | } 47 | } 48 | 49 | int main (int argc, char *argv[]) 50 | { 51 | if (argc != 1) 52 | { 53 | fprintf (stderr, "usage: %s < infile > outfile\n", argv[0]); 54 | 55 | return (-1); 56 | } 57 | 58 | #ifdef _WINDOWS 59 | _setmode (_fileno (stdin), _O_BINARY); 60 | #endif 61 | 62 | char line_buf[BUFSIZ]; 63 | 64 | int line_len; 65 | 66 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 67 | { 68 | if (line_len == 0) continue; 69 | 70 | int n; 71 | 72 | for (n = LEN_MIN; n <= LEN_MAX; n++) 73 | { 74 | if (n > line_len) break; 75 | 76 | char tmp2_buf[BUFSIZ]; 77 | 78 | memcpy (tmp2_buf, line_buf, line_len); 79 | 80 | int i; 81 | 82 | /* rotate to the left */ 83 | 84 | for (i = 0; i < n; i++) 85 | { 86 | int j; 87 | 88 | for (j = 0; j + n <= line_len; j += n) 89 | { 90 | int out_len = (int) strlen (tmp2_buf + j); 91 | 92 | if (out_len > n) out_len = n; 93 | 94 | char out_buf[BUFSIZ]; 95 | 96 | memcpy (out_buf, tmp2_buf + j, out_len); 97 | 98 | out_buf[out_len] = 0; 99 | 100 | puts (out_buf); 101 | } 102 | 103 | strrotl (tmp2_buf, line_len); 104 | } 105 | 106 | /* rotate to the right */ 107 | 108 | for (i = 0; i < n; i++) 109 | { 110 | int j; 111 | 112 | for (j = 0; j + n <= line_len; j += n) 113 | { 114 | int out_len = (int) strlen (tmp2_buf + j); 115 | 116 | if (out_len > n) out_len = n; 117 | 118 | char out_buf[BUFSIZ]; 119 | 120 | memcpy (out_buf, tmp2_buf + j, out_len); 121 | 122 | out_buf[out_len] = 0; 123 | 124 | puts (out_buf); 125 | } 126 | 127 | strrotr (tmp2_buf, line_len); 128 | } 129 | } 130 | } 131 | 132 | return 0; 133 | } 134 | -------------------------------------------------------------------------------- /src/export_potfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import sys 4 | import re 5 | 6 | 7 | for line in sys.stdin: 8 | if ':' in line: 9 | passw = line.split(':')[1].rstrip() 10 | if passw.startswith('$HEX['): 11 | hexpassw = re.search('\$HEX\[([a-zA-Z0-9]+)\]', passw).group(1) 12 | passw = bytearray.fromhex(hexpassw).decode('latin-1') 13 | rep = repr(passw) 14 | if rep.startswith('u'): 15 | print(rep[2:][:-1]) 16 | else: 17 | print(rep[1:][:-1]) 18 | -------------------------------------------------------------------------------- /src/gate.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: gate 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | */ 19 | 20 | int main (int argc, char *argv[]) 21 | { 22 | if (argc != 3) 23 | { 24 | fprintf (stderr, "usage: %s mod offset < infile > outfile\n", argv[0]); 25 | 26 | return (-1); 27 | } 28 | 29 | #ifdef _WINDOWS 30 | _setmode (_fileno (stdin), _O_BINARY); 31 | #endif 32 | 33 | const int mod = atoi (argv[1]); 34 | 35 | if (mod < 1) 36 | { 37 | fprintf (stderr, "mod < 1\n"); 38 | 39 | return (-1); 40 | } 41 | 42 | const int offset = atoi (argv[2]); 43 | 44 | if (offset >= mod) 45 | { 46 | fprintf (stderr, "offset >= mod\n"); 47 | 48 | return (-1); 49 | } 50 | 51 | int pos = 0; 52 | 53 | char line_buf[BUFSIZ]; 54 | 55 | int line_len; 56 | 57 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 58 | { 59 | if (line_len == 0) continue; 60 | 61 | if (pos == mod) pos = 0; 62 | 63 | if ((pos++ % mod) != offset) continue; 64 | 65 | puts (line_buf); 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /src/generate-rules.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "utils.c" 11 | #include "rp_cpu.h" 12 | 13 | /** 14 | * Name........: generate-rules 15 | * Autor.......: Jens Steube 16 | * License.....: MIT 17 | */ 18 | 19 | #define RP_GEN_FUNC_MIN 1 20 | #define RP_GEN_FUNC_MAX 4 21 | 22 | static void generate_random_rule (char rule_buf[BUFSIZ], const uint32_t rp_gen_func_min, const uint32_t rp_gen_func_max) 23 | { 24 | const uint32_t rp_gen_num = get_random_num (rp_gen_func_min, rp_gen_func_max); 25 | 26 | uint32_t j; 27 | 28 | uint32_t rule_pos = 0; 29 | 30 | for (j = 0; j < rp_gen_num; j++) 31 | { 32 | uint32_t r = 0; 33 | uint32_t p1 = 0; 34 | uint32_t p2 = 0; 35 | uint32_t p3 = 0; 36 | 37 | switch ((char) get_random_num (0, 9)) 38 | { 39 | case 0: 40 | r = get_random_num (0, sizeof (grp_op_nop)); 41 | rule_buf[rule_pos++] = grp_op_nop[r]; 42 | break; 43 | 44 | case 1: 45 | r = get_random_num (0, sizeof (grp_op_pos_p0)); 46 | rule_buf[rule_pos++] = grp_op_pos_p0[r]; 47 | p1 = get_random_num (0, sizeof (grp_pos)); 48 | rule_buf[rule_pos++] = grp_pos[p1]; 49 | break; 50 | 51 | case 2: 52 | r = get_random_num (0, sizeof (grp_op_pos_p1)); 53 | rule_buf[rule_pos++] = grp_op_pos_p1[r]; 54 | p1 = get_random_num (1, 6); 55 | rule_buf[rule_pos++] = grp_pos[p1]; 56 | break; 57 | 58 | case 3: 59 | r = get_random_num (0, sizeof (grp_op_chr)); 60 | rule_buf[rule_pos++] = grp_op_chr[r]; 61 | p1 = get_random_num (0x20, 0x7e); 62 | rule_buf[rule_pos++] = (char) p1; 63 | break; 64 | 65 | case 4: 66 | r = get_random_num (0, sizeof (grp_op_chr_chr)); 67 | rule_buf[rule_pos++] = grp_op_chr_chr[r]; 68 | p1 = get_random_num (0x20, 0x7e); 69 | rule_buf[rule_pos++] = (char) p1; 70 | p2 = get_random_num (0x20, 0x7e); 71 | while (p1 == p2) 72 | p2 = get_random_num (0x20, 0x7e); 73 | rule_buf[rule_pos++] = (char) p2; 74 | break; 75 | 76 | case 5: 77 | r = get_random_num (0, sizeof (grp_op_pos_chr)); 78 | rule_buf[rule_pos++] = grp_op_pos_chr[r]; 79 | p1 = get_random_num (0, sizeof (grp_pos)); 80 | rule_buf[rule_pos++] = grp_pos[p1]; 81 | p2 = get_random_num (0x20, 0x7e); 82 | rule_buf[rule_pos++] = (char) p2; 83 | break; 84 | 85 | case 6: 86 | r = get_random_num (0, sizeof (grp_op_pos_pos0)); 87 | rule_buf[rule_pos++] = grp_op_pos_pos0[r]; 88 | p1 = get_random_num (0, sizeof (grp_pos)); 89 | rule_buf[rule_pos++] = grp_pos[p1]; 90 | p2 = get_random_num (0, sizeof (grp_pos)); 91 | while (p1 == p2) 92 | p2 = get_random_num (0, sizeof (grp_pos)); 93 | rule_buf[rule_pos++] = grp_pos[p2]; 94 | break; 95 | 96 | case 7: 97 | r = get_random_num (0, sizeof (grp_op_pos_pos1)); 98 | rule_buf[rule_pos++] = grp_op_pos_pos1[r]; 99 | p1 = get_random_num (0, sizeof (grp_pos)); 100 | rule_buf[rule_pos++] = grp_pos[p1]; 101 | p2 = get_random_num (1, sizeof (grp_pos)); 102 | while (p1 == p2) 103 | p2 = get_random_num (1, sizeof (grp_pos)); 104 | rule_buf[rule_pos++] = grp_pos[p2]; 105 | break; 106 | 107 | case 8: 108 | r = get_random_num (0, sizeof (grp_op_pos1_pos2_pos3)); 109 | rule_buf[rule_pos++] = grp_op_pos1_pos2_pos3[r]; 110 | p1 = get_random_num (0, sizeof (grp_pos)); 111 | rule_buf[rule_pos++] = grp_pos[p1]; 112 | p2 = get_random_num (1, sizeof (grp_pos)); 113 | rule_buf[rule_pos++] = grp_pos[p1]; 114 | p3 = get_random_num (0, sizeof (grp_pos)); 115 | rule_buf[rule_pos++] = grp_pos[p3]; 116 | break; 117 | } 118 | 119 | rule_buf[rule_pos++] = ' '; 120 | } 121 | } 122 | 123 | int main (int argc, char *argv[]) 124 | { 125 | if ((argc != 2) && (argc != 3)) 126 | { 127 | fprintf (stderr, "usage: %s number [seed]\n", argv[0]); 128 | 129 | return (-1); 130 | } 131 | 132 | const int num = atoi (argv[1]); 133 | 134 | if ((num < 1) || (num > 1000000000)) 135 | { 136 | fprintf (stderr, "invalid rule count\n"); 137 | 138 | return (-1); 139 | } 140 | 141 | time_t seed; 142 | 143 | if (argc == 3) 144 | { 145 | seed = atoi (argv[2]); 146 | } 147 | else 148 | { 149 | time (&seed); 150 | } 151 | 152 | srand (seed); 153 | 154 | int i; 155 | 156 | for (i = 0; i < num; i++) 157 | { 158 | char rule_buf[BUFSIZ]; 159 | 160 | memset (rule_buf, 0, sizeof (rule_buf)); 161 | 162 | generate_random_rule (rule_buf, RP_GEN_FUNC_MIN, RP_GEN_FUNC_MAX); 163 | 164 | puts (rule_buf); 165 | } 166 | 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /src/hcstat2gen.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "utils.c" 14 | 15 | /** 16 | * Name........: hcstat2gen 17 | * Autor.......: Jens Steube 18 | * License.....: MIT 19 | */ 20 | 21 | // hashcat reads the file as lzma compress file, therefore we have to postprocess the output file: 22 | // lzma --compress --format=raw --stdout -9e thisfile > hashcat.hcstat2 23 | 24 | #define CHARSIZ 0x100 25 | 26 | #define PW_MAX 256 27 | 28 | #define ROOT_CNT (PW_MAX * CHARSIZ) 29 | #define MARKOV_CNT (PW_MAX * CHARSIZ * CHARSIZ) 30 | 31 | #define FGETSBUFSZ 0x100000 32 | 33 | #define HEX 0 34 | 35 | #define VERSION (0x6863737461740000 | 0x0002) 36 | 37 | /** 38 | * Outfile structure: 39 | * 40 | * - PW_MAX pw_pos 41 | * - CHARSIZ root 42 | * - PW_MAX pw_pos 43 | * - CHARSIZ key0 44 | * - CHARSIZ key1 45 | */ 46 | 47 | typedef uint8_t u8; 48 | typedef uint32_t u32; 49 | typedef uint64_t u64; 50 | 51 | static u64 byte_swap_64 (const u64 n) 52 | { 53 | #if defined (_MSC_VER) 54 | return _byteswap_uint64 (n); 55 | #elif defined (__clang__) || defined (__GNUC__) 56 | return __builtin_bswap64 (n); 57 | #else 58 | return (n & 0xff00000000000000ULL) >> 56 59 | | (n & 0x00ff000000000000ULL) >> 40 60 | | (n & 0x0000ff0000000000ULL) >> 24 61 | | (n & 0x000000ff00000000ULL) >> 8 62 | | (n & 0x00000000ff000000ULL) << 8 63 | | (n & 0x0000000000ff0000ULL) << 24 64 | | (n & 0x000000000000ff00ULL) << 40 65 | | (n & 0x00000000000000ffULL) << 56; 66 | #endif 67 | } 68 | 69 | #if HEX 70 | static u8 hex_convert (const u8 c) 71 | { 72 | return ((c & 15) + (c >> 6) * 9); 73 | } 74 | #endif 75 | 76 | int main (int argc, char *argv[]) 77 | { 78 | if (argc != 2) 79 | { 80 | fprintf (stderr, "usage: %s outfile < dictionary\n", argv[0]); 81 | 82 | return (-1); 83 | } 84 | 85 | char *outfile = argv[1]; 86 | 87 | /* allocate memory */ 88 | 89 | u64 *root_stats_buf = (u64 *) calloc (ROOT_CNT, sizeof (u64)); 90 | 91 | u64 *markov_stats_buf = (u64 *) calloc (MARKOV_CNT, sizeof (u64)); 92 | 93 | char *buf = (char *) calloc (FGETSBUFSZ, sizeof (char)); 94 | 95 | /* init pointer */ 96 | 97 | u64 *root_stats_ptr = root_stats_buf; 98 | 99 | u64 *root_stats_buf_by_pos[PW_MAX]; 100 | 101 | for (int i = 0; i < PW_MAX; i++) 102 | { 103 | root_stats_buf_by_pos[i] = root_stats_ptr; 104 | 105 | root_stats_ptr += CHARSIZ; 106 | } 107 | 108 | u64 *markov_stats_ptr = markov_stats_buf; 109 | 110 | u64 *markov_stats_buf_by_key[PW_MAX][CHARSIZ]; 111 | 112 | for (int i = 0; i < PW_MAX; i++) 113 | { 114 | for (int j = 0; j < CHARSIZ; j++) 115 | { 116 | markov_stats_buf_by_key[i][j] = markov_stats_ptr; 117 | 118 | markov_stats_ptr += CHARSIZ; 119 | } 120 | } 121 | 122 | /* parse dictionary */ 123 | 124 | printf ("Reading input...\n"); 125 | 126 | #if HEX 127 | while (!feof (stdin)) 128 | { 129 | const int len = fgetl (stdin, FGETSBUFSZ, buf); 130 | 131 | if (len == -1) continue; 132 | 133 | const int max = (len > PW_MAX * 2) ? PW_MAX * 2 : len; 134 | 135 | for (int pos = 0; pos < len; pos += 2) 136 | { 137 | const u8 c0 = (hex_convert ((const u8) buf[pos + 0]) << 4) 138 | | (hex_convert ((const u8) buf[pos + 1]) << 0); 139 | 140 | root_stats_buf_by_pos[pos / 2][c0]++; 141 | } 142 | 143 | for (int pos = 0; pos < len - 2; pos += 2) 144 | { 145 | const u8 c0 = (hex_convert ((const u8) buf[pos + 0]) << 4) 146 | | (hex_convert ((const u8) buf[pos + 1]) << 0); 147 | 148 | const u8 c1 = (hex_convert ((const u8) buf[pos + 2]) << 4) 149 | | (hex_convert ((const u8) buf[pos + 3]) << 0); 150 | 151 | markov_stats_buf_by_key[pos / 2][c0][c1]++; 152 | } 153 | } 154 | #else 155 | while (!feof (stdin)) 156 | { 157 | const int len = fgetl (stdin, FGETSBUFSZ, buf); 158 | 159 | if (len == -1) continue; 160 | 161 | const int max = (len > PW_MAX) ? PW_MAX : len; 162 | 163 | for (int pos = 0; pos < max; pos++) 164 | { 165 | const u8 c0 = (const u8) buf[pos]; 166 | 167 | root_stats_buf_by_pos[pos][c0]++; 168 | } 169 | 170 | for (int pos = 0; pos < max - 1; pos++) 171 | { 172 | const u8 c0 = (const u8) buf[pos + 0]; 173 | const u8 c1 = (const u8) buf[pos + 1]; 174 | 175 | markov_stats_buf_by_key[pos][c0][c1]++; 176 | } 177 | } 178 | #endif 179 | 180 | /* write results */ 181 | 182 | printf ("Writing stats...\n"); 183 | 184 | FILE *fd = fopen (outfile, "wb"); 185 | 186 | if (fd == NULL) 187 | { 188 | fprintf (stderr, "%s: %s", outfile, strerror (errno)); 189 | 190 | return (-1); 191 | } 192 | 193 | // swap the data, it's easier to read and analyze it with a hex editor 194 | const u64 v = byte_swap_64 (VERSION); 195 | const u64 z = 0; 196 | 197 | for (int i = 0; i < ROOT_CNT; i++) root_stats_buf[i] = byte_swap_64 (root_stats_buf[i]); 198 | for (int i = 0; i < MARKOV_CNT; i++) markov_stats_buf[i] = byte_swap_64 (markov_stats_buf[i]); 199 | 200 | fwrite (&v, sizeof (u64), 1, fd); 201 | fwrite (&z, sizeof (u64), 1, fd); 202 | 203 | fwrite (root_stats_buf, sizeof (u64), ROOT_CNT, fd); 204 | fwrite (markov_stats_buf, sizeof (u64), MARKOV_CNT, fd); 205 | 206 | fclose (fd); 207 | 208 | free (root_stats_buf); 209 | free (markov_stats_buf); 210 | free (buf); 211 | 212 | return 0; 213 | } 214 | -------------------------------------------------------------------------------- /src/hcstatgen.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "utils.c" 14 | 15 | /** 16 | * Name........: hcstatgen 17 | * Autor.......: Jens Steube 18 | * License.....: MIT 19 | */ 20 | 21 | #define CHARSIZ 0x100 22 | 23 | #define PW_MAX 64 24 | 25 | #define ROOT_CNT (PW_MAX * CHARSIZ) 26 | #define MARKOV_CNT (PW_MAX * CHARSIZ * CHARSIZ) 27 | 28 | #define FGETSBUFSZ 0x100000 29 | 30 | #define HEX 0 31 | 32 | /** 33 | * Outfile structure: 34 | * 35 | * - PW_MAX pw_pos 36 | * - CHARSIZ root 37 | * - PW_MAX pw_pos 38 | * - CHARSIZ key0 39 | * - CHARSIZ key1 40 | */ 41 | 42 | typedef uint8_t u8; 43 | typedef uint32_t u32; 44 | typedef uint64_t u64; 45 | 46 | #if HEX 47 | static u8 hex_convert (const u8 c) 48 | { 49 | return ((c & 15) + (c >> 6) * 9); 50 | } 51 | #endif 52 | 53 | int main (int argc, char *argv[]) 54 | { 55 | if (argc != 2) 56 | { 57 | fprintf (stderr, "usage: %s outfile < dictionary\n", argv[0]); 58 | 59 | return (-1); 60 | } 61 | 62 | char *outfile = argv[1]; 63 | 64 | /* allocate memory */ 65 | 66 | u64 *root_stats_buf = (u64 *) calloc (ROOT_CNT, sizeof (u64)); 67 | 68 | u64 *markov_stats_buf = (u64 *) calloc (MARKOV_CNT, sizeof (u64)); 69 | 70 | char *buf = (char *) calloc (FGETSBUFSZ, sizeof (char)); 71 | 72 | /* init pointer */ 73 | 74 | u64 *root_stats_ptr = root_stats_buf; 75 | 76 | u64 *root_stats_buf_by_pos[PW_MAX]; 77 | 78 | for (int i = 0; i < PW_MAX; i++) 79 | { 80 | root_stats_buf_by_pos[i] = root_stats_ptr; 81 | 82 | root_stats_ptr += CHARSIZ; 83 | } 84 | 85 | u64 *markov_stats_ptr = markov_stats_buf; 86 | 87 | u64 *markov_stats_buf_by_key[PW_MAX][CHARSIZ]; 88 | 89 | for (int i = 0; i < PW_MAX; i++) 90 | { 91 | for (int j = 0; j < CHARSIZ; j++) 92 | { 93 | markov_stats_buf_by_key[i][j] = markov_stats_ptr; 94 | 95 | markov_stats_ptr += CHARSIZ; 96 | } 97 | } 98 | 99 | /* parse dictionary */ 100 | 101 | printf ("Reading input...\n"); 102 | 103 | #if HEX 104 | while (!feof (stdin)) 105 | { 106 | const int len = fgetl (stdin, FGETSBUFSZ, buf); 107 | 108 | if (len == -1) continue; 109 | 110 | const int max = (len > PW_MAX * 2) ? PW_MAX * 2 : len; 111 | 112 | for (int pos = 0; pos < len; pos += 2) 113 | { 114 | const u8 c0 = (hex_convert ((const u8) buf[pos + 0]) << 4) 115 | | (hex_convert ((const u8) buf[pos + 1]) << 0); 116 | 117 | root_stats_buf_by_pos[pos / 2][c0]++; 118 | } 119 | 120 | for (int pos = 0; pos < len - 2; pos += 2) 121 | { 122 | const u8 c0 = (hex_convert ((const u8) buf[pos + 0]) << 4) 123 | | (hex_convert ((const u8) buf[pos + 1]) << 0); 124 | 125 | const u8 c1 = (hex_convert ((const u8) buf[pos + 2]) << 4) 126 | | (hex_convert ((const u8) buf[pos + 3]) << 0); 127 | 128 | markov_stats_buf_by_key[pos / 2][c0][c1]++; 129 | } 130 | } 131 | #else 132 | while (!feof (stdin)) 133 | { 134 | const int len = fgetl (stdin, FGETSBUFSZ, buf); 135 | 136 | if (len == -1) continue; 137 | 138 | const int max = (len > PW_MAX) ? PW_MAX : len; 139 | 140 | for (int pos = 0; pos < max; pos++) 141 | { 142 | const u8 c0 = (const u8) buf[pos]; 143 | 144 | root_stats_buf_by_pos[pos][c0]++; 145 | } 146 | 147 | for (int pos = 0; pos < max - 1; pos++) 148 | { 149 | const u8 c0 = (const u8) buf[pos + 0]; 150 | const u8 c1 = (const u8) buf[pos + 1]; 151 | 152 | markov_stats_buf_by_key[pos][c0][c1]++; 153 | } 154 | } 155 | #endif 156 | 157 | /* write results */ 158 | 159 | printf ("Writing stats...\n"); 160 | 161 | FILE *fd = fopen (outfile, "wb"); 162 | 163 | if (fd == NULL) 164 | { 165 | fprintf (stderr, "%s: %s", outfile, strerror (errno)); 166 | 167 | return (-1); 168 | } 169 | 170 | fwrite (root_stats_buf, sizeof (u64), ROOT_CNT, fd); 171 | fwrite (markov_stats_buf, sizeof (u64), MARKOV_CNT, fd); 172 | 173 | fclose (fd); 174 | 175 | free (root_stats_buf); 176 | free (markov_stats_buf); 177 | free (buf); 178 | 179 | return 0; 180 | } 181 | -------------------------------------------------------------------------------- /src/keyspace.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | #define __USE_MINGW_ANSI_STDIO 1 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | /** 16 | * Name........: keyspace 17 | * Autor.......: Jens Steube 18 | * License.....: MIT 19 | */ 20 | 21 | #define CHARSIZ 0x100 22 | 23 | #define SP_HCSTAT "hashcat.hcstat" 24 | #define SP_PW_MIN 2 25 | #define SP_PW_MAX 64 26 | #define SP_ROOT_CNT (SP_PW_MAX * CHARSIZ) 27 | #define SP_MARKOV_CNT (SP_PW_MAX * CHARSIZ * CHARSIZ) 28 | 29 | #define OPTS_TYPE_PT_UNICODE (1 << 0) 30 | #define OPTS_TYPE_ST_UNICODE (1 << 10) 31 | 32 | typedef struct 33 | { 34 | uint8_t key; 35 | uint64_t val; 36 | 37 | } hcstat_table_t; 38 | 39 | typedef struct 40 | { 41 | uint8_t cs_buf[CHARSIZ]; 42 | uint32_t cs_len; 43 | 44 | } cs_t; 45 | 46 | uint8_t hex_convert (const uint8_t c) 47 | { 48 | return (uint8_t)((c & 15) + (c >> 6) * 9); 49 | } 50 | 51 | void mp_css_to_uniq_tbl (const int css_cnt, cs_t *css_buf, int uniq_tbls[SP_PW_MAX][CHARSIZ]) 52 | { 53 | int css_pos; 54 | 55 | for (css_pos = 0; css_pos < css_cnt; css_pos++) 56 | { 57 | uint8_t *cs_buf = css_buf[css_pos].cs_buf; 58 | uint32_t cs_len = css_buf[css_pos].cs_len; 59 | 60 | int *uniq_tbl = uniq_tbls[css_pos]; 61 | 62 | uint32_t cs_pos; 63 | 64 | for (cs_pos = 0; cs_pos < cs_len; cs_pos++) 65 | { 66 | const uint8_t c = cs_buf[cs_pos]; 67 | 68 | uniq_tbl[c] = 1; 69 | } 70 | } 71 | } 72 | 73 | void mp_add_cs_buf (const uint32_t in_len, const uint8_t *in_buf, const int css_pos, cs_t *css_buf) 74 | { 75 | cs_t *cs = &css_buf[css_pos]; 76 | 77 | int *css_uniq = (int *) calloc (CHARSIZ, sizeof (int)); 78 | 79 | uint32_t i; 80 | 81 | for (i = 0; i < cs->cs_len; i++) 82 | { 83 | const uint8_t u = cs->cs_buf[i]; 84 | 85 | css_uniq[u] = 1; 86 | } 87 | 88 | for (i = 0; i < in_len; i++) 89 | { 90 | const uint8_t u = in_buf[i]; 91 | 92 | if (css_uniq[u] == 1) continue; 93 | 94 | css_uniq[u] = 1; 95 | 96 | cs->cs_buf[cs->cs_len] = u; 97 | 98 | cs->cs_len++; 99 | } 100 | 101 | free (css_uniq); 102 | } 103 | 104 | void mp_expand (const int in_len, const uint8_t *in_buf, cs_t *mp_sys, cs_t *mp_usr, const int css_pos, const int hex_charset) 105 | { 106 | int in_pos; 107 | 108 | for (in_pos = 0; in_pos < in_len; in_pos++) 109 | { 110 | const uint8_t p0 = in_buf[in_pos]; 111 | 112 | if (p0 == '?') 113 | { 114 | in_pos++; 115 | 116 | if (in_pos == in_len) break; 117 | 118 | const uint8_t p1 = in_buf[in_pos]; 119 | 120 | switch (p1) 121 | { 122 | case 'l': mp_add_cs_buf (mp_sys[0].cs_len, mp_sys[0].cs_buf, css_pos, mp_usr); 123 | break; 124 | case 'u': mp_add_cs_buf (mp_sys[1].cs_len, mp_sys[1].cs_buf, css_pos, mp_usr); 125 | break; 126 | case 'd': mp_add_cs_buf (mp_sys[2].cs_len, mp_sys[2].cs_buf, css_pos, mp_usr); 127 | break; 128 | case 's': mp_add_cs_buf (mp_sys[3].cs_len, mp_sys[3].cs_buf, css_pos, mp_usr); 129 | break; 130 | case 'a': mp_add_cs_buf (mp_sys[4].cs_len, mp_sys[4].cs_buf, css_pos, mp_usr); 131 | break; 132 | case 'b': mp_add_cs_buf (mp_sys[5].cs_len, mp_sys[5].cs_buf, css_pos, mp_usr); 133 | break; 134 | case '1': mp_add_cs_buf (mp_usr[0].cs_len, mp_usr[0].cs_buf, css_pos, mp_usr); 135 | break; 136 | case '2': mp_add_cs_buf (mp_usr[1].cs_len, mp_usr[1].cs_buf, css_pos, mp_usr); 137 | break; 138 | case '3': mp_add_cs_buf (mp_usr[2].cs_len, mp_usr[2].cs_buf, css_pos, mp_usr); 139 | break; 140 | case '4': mp_add_cs_buf (mp_usr[3].cs_len, mp_usr[3].cs_buf, css_pos, mp_usr); 141 | break; 142 | case '?': mp_add_cs_buf (1, &p1, css_pos, mp_usr); 143 | break; 144 | default: fprintf (stderr, "Syntax error: %s\n", in_buf); 145 | exit (-1); 146 | } 147 | } 148 | else 149 | { 150 | if (hex_charset) 151 | { 152 | in_pos++; 153 | 154 | if (in_pos == in_len) break; 155 | 156 | const uint8_t p1 = in_buf[in_pos]; 157 | 158 | const uint8_t chr = (uint8_t)( hex_convert (p1) << 0 159 | | hex_convert (p0) << 4); 160 | 161 | mp_add_cs_buf (1, &chr, css_pos, mp_usr); 162 | } 163 | else 164 | { 165 | const uint8_t chr = p0; 166 | 167 | mp_add_cs_buf (1, &chr, css_pos, mp_usr); 168 | } 169 | } 170 | } 171 | } 172 | 173 | cs_t *mp_gen_css (const int in_len, const uint8_t *in_buf, cs_t *mp_sys, cs_t *mp_usr, int *css_cnt, const int hex_charset) 174 | { 175 | cs_t *css_buf = (cs_t *) calloc (CHARSIZ, sizeof (cs_t)); 176 | 177 | int in_pos; 178 | int css_pos; 179 | 180 | for (in_pos = 0, css_pos = 0; in_pos < in_len; in_pos++, css_pos++) 181 | { 182 | const uint8_t p0 = in_buf[in_pos]; 183 | 184 | if (p0 == '?') 185 | { 186 | in_pos++; 187 | 188 | if (in_pos == in_len) break; 189 | 190 | const uint8_t p1 = in_buf[in_pos]; 191 | 192 | switch (p1) 193 | { 194 | case 'l': mp_add_cs_buf (mp_sys[0].cs_len, mp_sys[0].cs_buf, css_pos, css_buf); 195 | break; 196 | case 'u': mp_add_cs_buf (mp_sys[1].cs_len, mp_sys[1].cs_buf, css_pos, css_buf); 197 | break; 198 | case 'd': mp_add_cs_buf (mp_sys[2].cs_len, mp_sys[2].cs_buf, css_pos, css_buf); 199 | break; 200 | case 's': mp_add_cs_buf (mp_sys[3].cs_len, mp_sys[3].cs_buf, css_pos, css_buf); 201 | break; 202 | case 'a': mp_add_cs_buf (mp_sys[4].cs_len, mp_sys[4].cs_buf, css_pos, css_buf); 203 | break; 204 | case 'b': mp_add_cs_buf (mp_sys[5].cs_len, mp_sys[5].cs_buf, css_pos, css_buf); 205 | break; 206 | case '1': mp_add_cs_buf (mp_usr[0].cs_len, mp_usr[0].cs_buf, css_pos, css_buf); 207 | break; 208 | case '2': mp_add_cs_buf (mp_usr[1].cs_len, mp_usr[1].cs_buf, css_pos, css_buf); 209 | break; 210 | case '3': mp_add_cs_buf (mp_usr[2].cs_len, mp_usr[2].cs_buf, css_pos, css_buf); 211 | break; 212 | case '4': mp_add_cs_buf (mp_usr[3].cs_len, mp_usr[3].cs_buf, css_pos, css_buf); 213 | break; 214 | case '?': mp_add_cs_buf (1, &p1, css_pos, css_buf); 215 | break; 216 | default: fprintf (stderr, "ERROR: syntax error: %s\n", in_buf); 217 | exit (-1); 218 | } 219 | } 220 | else 221 | { 222 | if (hex_charset) 223 | { 224 | in_pos++; 225 | 226 | if (in_pos == in_len) break; 227 | 228 | const uint8_t p1 = in_buf[in_pos]; 229 | 230 | const uint8_t chr = (uint8_t)( hex_convert (p1) << 0 231 | | hex_convert (p0) << 4); 232 | 233 | mp_add_cs_buf (1, &chr, css_pos, css_buf); 234 | } 235 | else 236 | { 237 | const uint8_t chr = p0; 238 | 239 | mp_add_cs_buf (1, &chr, css_pos, css_buf); 240 | } 241 | } 242 | } 243 | 244 | *css_cnt = css_pos; 245 | 246 | return (css_buf); 247 | } 248 | 249 | void mp_setup_sys (cs_t *mp_sys) 250 | { 251 | int pos; 252 | int chr; 253 | 254 | int donec[CHARSIZ]; 255 | 256 | memset (donec, 0, sizeof (donec)); 257 | 258 | for (pos = 0, chr = 'a'; chr <= 'z'; chr++) { donec[chr] = 1; 259 | mp_sys[0].cs_buf[pos++] = (uint8_t)chr; 260 | mp_sys[0].cs_len = pos; } 261 | 262 | for (pos = 0, chr = 'A'; chr <= 'Z'; chr++) { donec[chr] = 1; 263 | mp_sys[1].cs_buf[pos++] = (uint8_t)chr; 264 | mp_sys[1].cs_len = pos; } 265 | 266 | for (pos = 0, chr = '0'; chr <= '9'; chr++) { donec[chr] = 1; 267 | mp_sys[2].cs_buf[pos++] = (uint8_t)chr; 268 | mp_sys[2].cs_len = pos; } 269 | 270 | for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { if (donec[chr]) continue; 271 | mp_sys[3].cs_buf[pos++] = (uint8_t)chr; 272 | mp_sys[3].cs_len = pos; } 273 | 274 | for (pos = 0, chr = 0x20; chr <= 0x7e; chr++) { mp_sys[4].cs_buf[pos++] = (uint8_t)chr; 275 | mp_sys[4].cs_len = pos; } 276 | 277 | for (pos = 0, chr = 0x00; chr <= 0xff; chr++) { mp_sys[5].cs_buf[pos++] = (uint8_t)chr; 278 | mp_sys[5].cs_len = pos; } 279 | } 280 | 281 | void mp_setup_usr (cs_t *mp_sys, cs_t *mp_usr, const int in_len, const uint8_t *in_buf, const int css_pos, const int hex_charset) 282 | { 283 | mp_expand (in_len, in_buf, mp_sys, mp_usr, css_pos, hex_charset); 284 | } 285 | 286 | uint64_t sp_get_sum (const int start, const int stop, const cs_t *root_css_buf) 287 | { 288 | uint64_t sum = 1; 289 | 290 | int i; 291 | 292 | for (i = start; i < stop; i++) 293 | { 294 | sum *= root_css_buf[i].cs_len; 295 | } 296 | 297 | return (sum); 298 | } 299 | 300 | int sp_comp_val (const void *p1, const void *p2) 301 | { 302 | hcstat_table_t *b1 = (hcstat_table_t *) p1; 303 | hcstat_table_t *b2 = (hcstat_table_t *) p2; 304 | 305 | return b2->val - b1->val; 306 | } 307 | 308 | void sp_setup_tbl (const char *markov_hcstat, hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf) 309 | { 310 | /** 311 | * Initialize hcstats 312 | */ 313 | 314 | uint64_t *root_stats_buf = (uint64_t *) calloc (SP_ROOT_CNT, sizeof (uint64_t)); 315 | 316 | uint64_t *root_stats_ptr = root_stats_buf; 317 | 318 | int i; 319 | 320 | for (i = 0; i < SP_PW_MAX; i++) 321 | { 322 | root_stats_ptr += CHARSIZ; 323 | } 324 | 325 | uint64_t *markov_stats_buf = (uint64_t *) calloc (SP_MARKOV_CNT, sizeof (uint64_t)); 326 | 327 | uint64_t *markov_stats_ptr = markov_stats_buf; 328 | 329 | for (i = 0; i < SP_PW_MAX; i++) 330 | { 331 | int j; 332 | 333 | for (j = 0; j < CHARSIZ; j++) 334 | { 335 | markov_stats_ptr += CHARSIZ; 336 | } 337 | } 338 | 339 | /** 340 | * Load hcstats File 341 | */ 342 | 343 | FILE *fd = fopen (markov_hcstat, "rb"); 344 | 345 | if (fd == NULL) 346 | { 347 | fprintf (stderr, "%s: %s\n", markov_hcstat, strerror (errno)); 348 | 349 | exit (-1); 350 | } 351 | 352 | if (fread (root_stats_buf, sizeof (uint64_t), SP_ROOT_CNT, fd) != SP_ROOT_CNT) 353 | { 354 | fprintf (stderr, "%s: Could not load data\n", markov_hcstat); 355 | 356 | exit (-1); 357 | } 358 | 359 | if (fread (markov_stats_buf, sizeof (uint64_t), SP_MARKOV_CNT, fd) != SP_MARKOV_CNT) 360 | { 361 | fprintf (stderr, "%s: Could not load data\n", markov_hcstat); 362 | 363 | exit (-1); 364 | } 365 | 366 | fclose (fd); 367 | 368 | /** 369 | * Initialize tables 370 | */ 371 | 372 | hcstat_table_t *root_table_ptr = root_table_buf; 373 | 374 | hcstat_table_t *root_table_buf_by_pos[SP_PW_MAX]; 375 | 376 | for (i = 0; i < SP_PW_MAX; i++) 377 | { 378 | root_table_buf_by_pos[i] = root_table_ptr; 379 | 380 | root_table_ptr += CHARSIZ; 381 | } 382 | 383 | hcstat_table_t *markov_table_ptr = markov_table_buf; 384 | 385 | hcstat_table_t *markov_table_buf_by_key[SP_PW_MAX][CHARSIZ]; 386 | 387 | for (i = 0; i < SP_PW_MAX; i++) 388 | { 389 | int j; 390 | 391 | for (j = 0; j < CHARSIZ; j++) 392 | { 393 | markov_table_buf_by_key[i][j] = markov_table_ptr; 394 | 395 | markov_table_ptr += CHARSIZ; 396 | } 397 | } 398 | 399 | /** 400 | * Convert hcstat to tables 401 | */ 402 | 403 | for (i = 0; i < SP_ROOT_CNT; i++) 404 | { 405 | const uint8_t key = (uint8_t)(i % CHARSIZ); 406 | 407 | root_table_buf[i].key = key; 408 | root_table_buf[i].val = root_stats_buf[i]; 409 | } 410 | 411 | for (i = 0; i < SP_MARKOV_CNT; i++) 412 | { 413 | const uint8_t key = (uint8_t)(i % CHARSIZ); 414 | 415 | markov_table_buf[i].key = key; 416 | markov_table_buf[i].val = markov_stats_buf[i]; 417 | } 418 | 419 | free (root_stats_buf); 420 | free (markov_stats_buf); 421 | 422 | /** 423 | * Finally sort them 424 | */ 425 | 426 | for (i = 0; i < SP_PW_MAX; i++) 427 | { 428 | qsort (root_table_buf_by_pos[i], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val); 429 | } 430 | 431 | for (i = 0; i < SP_PW_MAX; i++) 432 | { 433 | int j; 434 | 435 | for (j = 0; j < CHARSIZ; j++) 436 | { 437 | qsort (markov_table_buf_by_key[i][j], CHARSIZ, sizeof (hcstat_table_t), sp_comp_val); 438 | } 439 | } 440 | } 441 | 442 | void sp_tbl_to_css (hcstat_table_t *root_table_buf, hcstat_table_t *markov_table_buf, cs_t *root_css_buf, cs_t *markov_css_buf, const uint32_t markov_threshold, int uniq_tbls[SP_PW_MAX][CHARSIZ]) 443 | { 444 | int i; 445 | 446 | for (i = 0; i < SP_ROOT_CNT; i++) 447 | { 448 | const int pw_pos = i / CHARSIZ; 449 | 450 | cs_t *cs = &root_css_buf[pw_pos]; 451 | 452 | if (cs->cs_len == markov_threshold) continue; 453 | 454 | const uint8_t key = root_table_buf[i].key; 455 | 456 | if (uniq_tbls[pw_pos][key] == 0) continue; 457 | 458 | cs->cs_buf[cs->cs_len] = key; 459 | 460 | cs->cs_len++; 461 | } 462 | 463 | for (i = 0; i < SP_MARKOV_CNT; i++) 464 | { 465 | const int c = i / CHARSIZ; 466 | 467 | cs_t *cs = &markov_css_buf[c]; 468 | 469 | if (cs->cs_len == markov_threshold) continue; 470 | 471 | const int pw_pos = c / CHARSIZ; 472 | 473 | const uint8_t key = markov_table_buf[i].key; 474 | 475 | if ((pw_pos + 1) < SP_PW_MAX) if (uniq_tbls[pw_pos + 1][key] == 0) continue; 476 | 477 | cs->cs_buf[cs->cs_len] = key; 478 | 479 | cs->cs_len++; 480 | } 481 | } 482 | 483 | uint64_t keyspace (const int in_len, const uint8_t *in_buf, cs_t *mp_sys, cs_t *mp_usr, const char *markov_hcstat, const uint32_t markov_threshold, const int opts_type, const int hex_charset) 484 | { 485 | int css_cnt = 0; 486 | 487 | cs_t *css_buf = mp_gen_css (in_len, in_buf, mp_sys, mp_usr, &css_cnt, hex_charset); 488 | 489 | if (opts_type & OPTS_TYPE_PT_UNICODE) 490 | { 491 | int css_cnt_unicode = css_cnt * 2; 492 | 493 | cs_t *css_buf_unicode = (cs_t *) calloc (css_cnt_unicode, sizeof (cs_t)); 494 | 495 | int i; 496 | int j; 497 | 498 | for (i = 0, j = 0; i < css_cnt; i += 1, j += 2) 499 | { 500 | memcpy (&css_buf_unicode[j + 0], &css_buf[i], sizeof (cs_t)); 501 | memset (&css_buf_unicode[j + 1], 0, sizeof (cs_t)); 502 | 503 | css_buf_unicode[j + 1].cs_len = 1; 504 | } 505 | 506 | free (css_buf); 507 | 508 | css_buf = css_buf_unicode; 509 | css_cnt = css_cnt_unicode; 510 | } 511 | 512 | int uniq_tbls[SP_PW_MAX][CHARSIZ]; 513 | 514 | memset (uniq_tbls, 0, sizeof (uniq_tbls)); 515 | 516 | mp_css_to_uniq_tbl (css_cnt, css_buf, uniq_tbls); 517 | 518 | hcstat_table_t *root_table_buf = (hcstat_table_t *) calloc (SP_ROOT_CNT, sizeof (hcstat_table_t)); 519 | hcstat_table_t *markov_table_buf = (hcstat_table_t *) calloc (SP_MARKOV_CNT, sizeof (hcstat_table_t)); 520 | 521 | sp_setup_tbl (markov_hcstat, root_table_buf, markov_table_buf); 522 | 523 | cs_t *root_css_buf = (cs_t *) calloc (SP_PW_MAX, sizeof (cs_t)); 524 | cs_t *markov_css_buf = (cs_t *) calloc (SP_PW_MAX * CHARSIZ, sizeof (cs_t)); 525 | 526 | sp_tbl_to_css (root_table_buf, markov_table_buf, root_css_buf, markov_css_buf, markov_threshold, uniq_tbls); 527 | 528 | int css_cnt_r; 529 | 530 | if (css_cnt < 6) 531 | { 532 | css_cnt_r = 1; 533 | } 534 | else if (css_cnt == 6) 535 | { 536 | css_cnt_r = 2; 537 | } 538 | else 539 | { 540 | if (opts_type & OPTS_TYPE_PT_UNICODE) 541 | { 542 | if (css_cnt == 8 || css_cnt == 10) 543 | { 544 | css_cnt_r = 2; 545 | } 546 | else 547 | { 548 | css_cnt_r = 4; 549 | } 550 | } 551 | else 552 | { 553 | if ((css_buf[0].cs_len * css_buf[1].cs_len * css_buf[2].cs_len) > 256) 554 | { 555 | css_cnt_r = 3; 556 | } 557 | else 558 | { 559 | css_cnt_r = 4; 560 | } 561 | } 562 | } 563 | 564 | const uint64_t sum = sp_get_sum (css_cnt_r, css_cnt, root_css_buf); 565 | 566 | free (root_css_buf); 567 | free (markov_css_buf); 568 | 569 | free (root_table_buf); 570 | free (markov_table_buf); 571 | 572 | free (css_buf); 573 | 574 | return sum; 575 | } 576 | 577 | void usage (char *program) 578 | { 579 | const char *help_text[] = { 580 | "%s, keyspace utility for hashcat", 581 | "", 582 | "Usage: %s [options] mask", 583 | "", 584 | "=======", 585 | "Options", 586 | "=======", 587 | "", 588 | " -m, --hash-type=NUM Hash-type", 589 | " --hex-charset Assume charset is given in hex", 590 | " --markov-hcstat=FILE Specify hcstat file to use, default is hashcat.hcstat", 591 | " -t, --markov-threshold=NUM Threshold for markov-chains", 592 | " -1, --custom-charset1=CS User-defined charsets", 593 | " -2, --custom-charset2=CS Examples:", 594 | " -3, --custom-charset3=CS --custom-charset3=?dabcdef : sets charset ?3 to 0123456789abcdef", 595 | " -4, --custom-charset4=CS --custom-charset4=?l?u : sets charset ?4 to all lower and upper case letters", 596 | " -h, --help Print help", 597 | NULL 598 | }; 599 | 600 | int i; 601 | 602 | for (i = 0; help_text[i] != NULL; i++) 603 | { 604 | fprintf (stderr, help_text[i], program, program); 605 | 606 | fprintf (stderr, "\n"); 607 | } 608 | } 609 | 610 | int main (int argc, char *argv[]) 611 | { 612 | #define IDX_HASH_MODE 'm' 613 | #define IDX_HEX_CHARSET 0xff20 614 | #define IDX_MARKOV_THRESHOLD 't' 615 | #define IDX_MARKOV_HCSTAT 0xff24 616 | #define IDX_CUSTOM_CHARSET_1 '1' 617 | #define IDX_CUSTOM_CHARSET_2 '2' 618 | #define IDX_CUSTOM_CHARSET_3 '3' 619 | #define IDX_CUSTOM_CHARSET_4 '4' 620 | #define IDX_HELP 'h' 621 | 622 | int hash_mode = 0; 623 | int hex_charset = 0; 624 | int markov_threshold = CHARSIZ; 625 | char *markov_hcstat = SP_HCSTAT; 626 | char *custom_charset_1 = NULL; 627 | char *custom_charset_2 = NULL; 628 | char *custom_charset_3 = NULL; 629 | char *custom_charset_4 = NULL; 630 | 631 | char short_options[] = "hm:t:1:2:3:4:"; 632 | 633 | struct option long_options[] = 634 | { 635 | {"hash-type", required_argument, 0, IDX_HASH_MODE}, 636 | {"hex-charset", no_argument, 0, IDX_HEX_CHARSET}, 637 | {"markov-threshold", required_argument, 0, IDX_MARKOV_THRESHOLD}, 638 | {"markov-hcstat", required_argument, 0, IDX_MARKOV_HCSTAT}, 639 | {"custom-charset1", required_argument, 0, IDX_CUSTOM_CHARSET_1}, 640 | {"custom-charset2", required_argument, 0, IDX_CUSTOM_CHARSET_2}, 641 | {"custom-charset3", required_argument, 0, IDX_CUSTOM_CHARSET_3}, 642 | {"custom-charset4", required_argument, 0, IDX_CUSTOM_CHARSET_4}, 643 | {"help", no_argument, 0, IDX_HELP}, 644 | 645 | {NULL, 0, 0, 0} 646 | }; 647 | 648 | optind = 1; 649 | 650 | int option_index = 0; 651 | int help = 0; 652 | int c; 653 | 654 | while ((c = getopt_long (argc, argv, short_options, long_options, &option_index)) != -1) 655 | { 656 | switch (c) 657 | { 658 | case IDX_HASH_MODE: hash_mode = atoi (optarg); break; 659 | case IDX_HEX_CHARSET: hex_charset = 1; break; 660 | case IDX_MARKOV_THRESHOLD: markov_threshold = atoi (optarg); break; 661 | case IDX_MARKOV_HCSTAT: markov_hcstat = optarg; break; 662 | case IDX_CUSTOM_CHARSET_1: custom_charset_1 = optarg; break; 663 | case IDX_CUSTOM_CHARSET_2: custom_charset_2 = optarg; break; 664 | case IDX_CUSTOM_CHARSET_3: custom_charset_3 = optarg; break; 665 | case IDX_CUSTOM_CHARSET_4: custom_charset_4 = optarg; break; 666 | case IDX_HELP: help = 1; break; 667 | } 668 | } 669 | 670 | if (help == 1) 671 | { 672 | usage (argv[0]); 673 | 674 | exit (1); 675 | } 676 | 677 | if (optind < 1) 678 | { 679 | usage (argv[0]); 680 | 681 | return (-1); 682 | } 683 | 684 | char *mask = argv[optind]; 685 | 686 | if (mask == NULL) 687 | { 688 | usage (argv[0]); 689 | 690 | 691 | return (-1); 692 | } 693 | 694 | int opts_type = 0; 695 | 696 | switch (hash_mode) 697 | { 698 | case 30: opts_type |= OPTS_TYPE_PT_UNICODE; break; 699 | case 40: opts_type |= OPTS_TYPE_PT_UNICODE; break; 700 | case 130: opts_type |= OPTS_TYPE_PT_UNICODE; break; 701 | case 131: opts_type |= OPTS_TYPE_PT_UNICODE; break; 702 | case 132: opts_type |= OPTS_TYPE_PT_UNICODE; break; 703 | case 133: opts_type |= OPTS_TYPE_PT_UNICODE; break; 704 | case 140: opts_type |= OPTS_TYPE_PT_UNICODE; break; 705 | case 141: opts_type |= OPTS_TYPE_PT_UNICODE; break; 706 | case 1000: opts_type |= OPTS_TYPE_PT_UNICODE; break; 707 | case 1100: opts_type |= OPTS_TYPE_PT_UNICODE; 708 | opts_type |= OPTS_TYPE_ST_UNICODE; break; 709 | case 1430: opts_type |= OPTS_TYPE_PT_UNICODE; break; 710 | case 1440: opts_type |= OPTS_TYPE_PT_UNICODE; break; 711 | case 1441: opts_type |= OPTS_TYPE_PT_UNICODE; break; 712 | case 1730: opts_type |= OPTS_TYPE_PT_UNICODE; break; 713 | case 1731: opts_type |= OPTS_TYPE_PT_UNICODE; break; 714 | case 1740: opts_type |= OPTS_TYPE_PT_UNICODE; break; 715 | case 2100: opts_type |= OPTS_TYPE_PT_UNICODE; break; 716 | case 5500: opts_type |= OPTS_TYPE_PT_UNICODE; break; 717 | case 5600: opts_type |= OPTS_TYPE_PT_UNICODE; break; 718 | case 8000: opts_type |= OPTS_TYPE_PT_UNICODE; break; 719 | } 720 | 721 | cs_t mp_sys[6]; 722 | cs_t mp_usr[4]; 723 | 724 | memset (mp_sys, 0, sizeof (mp_sys)); 725 | memset (mp_usr, 0, sizeof (mp_usr)); 726 | 727 | mp_setup_sys (mp_sys); 728 | 729 | if (custom_charset_1) mp_setup_usr (mp_sys, mp_usr, strlen (custom_charset_1), (uint8_t *) custom_charset_1, 0, hex_charset); 730 | if (custom_charset_2) mp_setup_usr (mp_sys, mp_usr, strlen (custom_charset_2), (uint8_t *) custom_charset_2, 1, hex_charset); 731 | if (custom_charset_3) mp_setup_usr (mp_sys, mp_usr, strlen (custom_charset_3), (uint8_t *) custom_charset_3, 2, hex_charset); 732 | if (custom_charset_4) mp_setup_usr (mp_sys, mp_usr, strlen (custom_charset_4), (uint8_t *) custom_charset_4, 3, hex_charset); 733 | 734 | const uint64_t n = keyspace (strlen (mask), (uint8_t *) mask, mp_sys, mp_usr, markov_hcstat, markov_threshold, opts_type, hex_charset); 735 | 736 | printf ("%llu\n", (unsigned long long int) n); 737 | 738 | return 0; 739 | } 740 | -------------------------------------------------------------------------------- /src/len.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: len 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | */ 19 | 20 | int main (int argc, char *argv[]) 21 | { 22 | if (argc != 3) 23 | { 24 | fprintf (stderr, "usage: %s min max < infile > outfile\n", argv[0]); 25 | 26 | return (-1); 27 | } 28 | 29 | #ifdef _WINDOWS 30 | _setmode (_fileno (stdin), _O_BINARY); 31 | #endif 32 | 33 | int min = atoi (argv[1]); 34 | int max = atoi (argv[2]); 35 | 36 | if (min > max) 37 | { 38 | fprintf (stderr, "min > max\n"); 39 | 40 | return (-1); 41 | } 42 | 43 | char line_buf[BUFSIZ]; 44 | 45 | int line_len; 46 | 47 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 48 | { 49 | if (line_len == 0) continue; 50 | 51 | if (line_len < min) continue; 52 | if (line_len > max) continue; 53 | 54 | puts (line_buf); 55 | } 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /src/mli2.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "utils.c" 16 | 17 | /** 18 | * Name........: mli2 19 | * Autor.......: Jens Steube 20 | * License.....: MIT 21 | */ 22 | 23 | static int cmp_cache (const void *p1, const void *p2) 24 | { 25 | return strcmp (p1, p2); 26 | } 27 | 28 | int main (int argc, char *argv[]) 29 | { 30 | FILE *fd1; 31 | FILE *fd2; 32 | 33 | if (argc != 3) 34 | { 35 | fprintf (stderr, "usage: %s infile mergefile\n", argv[0]); 36 | 37 | return (-1); 38 | } 39 | 40 | char *infile = argv[1]; 41 | char *mergefile = argv[2]; 42 | 43 | if ((fd1 = fopen (infile, "rb")) == NULL) 44 | { 45 | fprintf (stderr, "%s: %s\n", infile, strerror (errno)); 46 | 47 | return (-1); 48 | } 49 | 50 | if ((fd2 = fopen (mergefile, "rb")) == NULL) 51 | { 52 | fprintf (stderr, "%s: %s\n", mergefile, strerror (errno)); 53 | 54 | fclose (fd1); 55 | 56 | return (-1); 57 | } 58 | 59 | char line_buf1[BUFSIZ]; 60 | char line_buf2[BUFSIZ]; 61 | 62 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) memset (line_buf1, 0, BUFSIZ); 63 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) memset (line_buf2, 0, BUFSIZ); 64 | 65 | int comp = 1; 66 | 67 | while (!feof (fd1) && !feof (fd2)) 68 | { 69 | comp = cmp_cache (line_buf1, line_buf2); 70 | 71 | if (comp == 0) 72 | { 73 | puts (line_buf1); 74 | 75 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) memset (line_buf1, 0, BUFSIZ); 76 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) memset (line_buf2, 0, BUFSIZ); 77 | } 78 | else if (comp > 0) 79 | { 80 | puts (line_buf2); 81 | 82 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) memset (line_buf2, 0, BUFSIZ); 83 | } 84 | else if (comp < 0) 85 | { 86 | puts (line_buf1); 87 | 88 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) memset (line_buf1, 0, BUFSIZ); 89 | } 90 | } 91 | 92 | if (!feof (fd1) && comp == 0) puts (line_buf1); 93 | if (!feof (fd2) && comp == 0) puts (line_buf2); 94 | 95 | if (comp > 0) puts (line_buf1); 96 | 97 | while (!feof (fd1)) 98 | { 99 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) 100 | { 101 | memset (line_buf1, 0, BUFSIZ); 102 | 103 | continue; 104 | } 105 | 106 | puts (line_buf1); 107 | } 108 | 109 | while (!feof (fd2)) 110 | { 111 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) 112 | { 113 | memset (line_buf2, 0, BUFSIZ); 114 | 115 | continue; 116 | } 117 | 118 | puts (line_buf2); 119 | } 120 | 121 | fclose (fd1); 122 | fclose (fd2); 123 | 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /src/morph.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "utils.c" 14 | 15 | #define CHR_MIN 0x20 16 | #define CHR_MAX 0x7f 17 | 18 | #define KEYS_CNT1 0xff 19 | #define KEYS_CNT2 0xffff 20 | #define KEYS_CNT3 0xffffff 21 | 22 | /** 23 | * Name........: morph 24 | * Autor.......: Jens Steube 25 | * License.....: MIT 26 | */ 27 | 28 | typedef struct 29 | { 30 | uint32_t key; 31 | uint32_t val; 32 | 33 | } node; 34 | 35 | int comp (const void *p1, const void *p2) 36 | { 37 | node *b1 = (node *) p1; 38 | node *b2 = (node *) p2; 39 | 40 | return b2->val - b1->val; 41 | } 42 | 43 | uint32_t count_keys (const uint32_t *c, const uint32_t n) 44 | { 45 | uint32_t v = 0; 46 | 47 | uint32_t i; 48 | 49 | for (i = 0; i < n; i++) 50 | { 51 | if (c[i] == 0) continue; 52 | 53 | v++; 54 | } 55 | 56 | return v; 57 | } 58 | 59 | void move_keys (const uint32_t *c, const uint32_t n, node *s) 60 | { 61 | uint32_t i; 62 | 63 | for (i = 0; i < n; i++) 64 | { 65 | if (c[i] == 0) continue; 66 | 67 | s->key = i; 68 | s->val = c[i]; 69 | 70 | s++; 71 | } 72 | } 73 | 74 | void output_rule (node *sort_buf, const uint32_t sort_cnt, const uint32_t pos, const uint32_t width, const uint32_t depth) 75 | { 76 | uint32_t i; 77 | 78 | for (i = 0; i < sort_cnt; i++, sort_buf++) 79 | { 80 | if (i > depth) break; 81 | 82 | char *key = (char *) &sort_buf->key; 83 | 84 | switch (width) 85 | { 86 | case 1: printf ("i%X%c\n", 87 | pos + 0, key[0] 88 | ); 89 | break; 90 | 91 | case 2: printf ("i%X%c i%X%c\n", 92 | pos + 0, key[0], 93 | pos + 1, key[1] 94 | ); 95 | break; 96 | 97 | case 3: printf ("i%X%c i%X%c i%X%c\n", 98 | pos + 0, key[0], 99 | pos + 1, key[1], 100 | pos + 2, key[2] 101 | ); 102 | break; 103 | } 104 | } 105 | } 106 | 107 | int main (int argc, char *argv[]) 108 | { 109 | if (argc != 6) 110 | { 111 | fprintf (stderr, "usage: %s dictionary depth width pos_min pos_max\n", argv[0]); 112 | 113 | return (-1); 114 | } 115 | 116 | const char *dictionary = argv[1]; 117 | 118 | const int depth = atoi (argv[2]); 119 | const int width = atoi (argv[3]); 120 | const int pos_min = atoi (argv[4]); 121 | const int pos_max = atoi (argv[5]); 122 | 123 | if ((width < 1) || (width > 3)) 124 | { 125 | fprintf (stderr, "invalid width\n"); 126 | 127 | return (-1); 128 | } 129 | 130 | if ((pos_min < 1) || (pos_min > 15)) 131 | { 132 | fprintf (stderr, "invalid pos_min\n"); 133 | 134 | return (-1); 135 | } 136 | 137 | if ((pos_max < 1) || (pos_max > 15)) 138 | { 139 | fprintf (stderr, "invalid pos_max\n"); 140 | 141 | return (-1); 142 | } 143 | 144 | if ((width + pos_max - 1) > 15) 145 | { 146 | fprintf (stderr, "(width + pos_max - 1) > 15\n"); 147 | 148 | return (-1); 149 | } 150 | 151 | /* who cares about RAM nowadays :-) */ 152 | 153 | const size_t keys_size1 = KEYS_CNT1 * sizeof (uint32_t); 154 | const size_t keys_size2 = KEYS_CNT2 * sizeof (uint32_t); 155 | const size_t keys_size3 = KEYS_CNT3 * sizeof (uint32_t); 156 | 157 | uint32_t *keys_buf1 = (uint32_t *) malloc (keys_size1); 158 | uint32_t *keys_buf2 = (uint32_t *) malloc (keys_size2); 159 | uint32_t *keys_buf3 = (uint32_t *) malloc (keys_size3); 160 | 161 | int pos; 162 | 163 | for (pos = pos_min; pos < pos_max; pos++) 164 | { 165 | memset (keys_buf1, 0, keys_size1); 166 | memset (keys_buf2, 0, keys_size2); 167 | memset (keys_buf3, 0, keys_size3); 168 | 169 | FILE *fd = fopen (dictionary, "rb"); 170 | 171 | if (fd == NULL) 172 | { 173 | fprintf (stderr, "%s: %s", dictionary, strerror (errno)); 174 | 175 | free (keys_buf1); 176 | free (keys_buf2); 177 | free (keys_buf3); 178 | 179 | return (-1); 180 | } 181 | 182 | char line_buf[BUFSIZ]; 183 | 184 | int line_len; 185 | 186 | while ((line_len = fgetl (fd, BUFSIZ, line_buf)) != -1) 187 | { 188 | if (line_len == 0) continue; 189 | 190 | unsigned char c = 0; 191 | 192 | uint32_t key = 0; 193 | 194 | if ((pos + 0) >= line_len) continue; 195 | 196 | c = line_buf[pos + 0]; 197 | 198 | if (c < CHR_MIN) continue; 199 | if (c > CHR_MAX) continue; 200 | 201 | key |= c << 0; 202 | 203 | keys_buf1[key]++; 204 | 205 | if ((pos + 1) >= line_len) continue; 206 | 207 | c = line_buf[pos + 1]; 208 | 209 | if (c < CHR_MIN) continue; 210 | if (c > CHR_MAX) continue; 211 | 212 | key |= c << 8; 213 | 214 | keys_buf2[key]++; 215 | 216 | if ((pos + 2) >= line_len) continue; 217 | 218 | c = line_buf[pos + 2]; 219 | 220 | if (c < CHR_MIN) continue; 221 | if (c > CHR_MAX) continue; 222 | 223 | key |= c << 16; 224 | 225 | keys_buf3[key]++; 226 | } 227 | 228 | fclose (fd); 229 | 230 | const uint32_t sort_cnt1 = count_keys (keys_buf1, KEYS_CNT1); 231 | const uint32_t sort_cnt2 = count_keys (keys_buf2, KEYS_CNT2); 232 | const uint32_t sort_cnt3 = count_keys (keys_buf3, KEYS_CNT3); 233 | 234 | node *sort_buf1 = (node *) calloc (sort_cnt1, sizeof (node)); 235 | node *sort_buf2 = (node *) calloc (sort_cnt2, sizeof (node)); 236 | node *sort_buf3 = (node *) calloc (sort_cnt3, sizeof (node)); 237 | 238 | move_keys (keys_buf1, KEYS_CNT1, sort_buf1); 239 | move_keys (keys_buf2, KEYS_CNT2, sort_buf2); 240 | move_keys (keys_buf3, KEYS_CNT3, sort_buf3); 241 | 242 | qsort (sort_buf1, sort_cnt1, sizeof (node), comp); 243 | qsort (sort_buf2, sort_cnt2, sizeof (node), comp); 244 | qsort (sort_buf3, sort_cnt3, sizeof (node), comp); 245 | 246 | if (width > 0) output_rule (sort_buf1, sort_cnt1, pos, 1, depth); 247 | if (width > 1) output_rule (sort_buf2, sort_cnt2, pos, 2, depth); 248 | if (width > 2) output_rule (sort_buf3, sort_cnt3, pos, 3, depth); 249 | 250 | free (sort_buf1); 251 | free (sort_buf2); 252 | free (sort_buf3); 253 | } 254 | 255 | free (keys_buf1); 256 | free (keys_buf2); 257 | free (keys_buf3); 258 | 259 | return 0; 260 | } 261 | -------------------------------------------------------------------------------- /src/permute.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: permute 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | * Credits.....: This program is using the awesome "Countdown QuickPerm Algorithm" developed by Phillip Paul Fuchs 19 | */ 20 | 21 | #define LINE_SIZE 8192 22 | #define OUT_SIZE 8192 23 | 24 | typedef struct out 25 | { 26 | FILE *fp; 27 | 28 | char buf[OUT_SIZE]; 29 | int len; 30 | 31 | } out_t; 32 | 33 | static void out_flush (out_t *out) 34 | { 35 | if (out->len == 0) return; 36 | 37 | fwrite (out->buf, 1, out->len, out->fp); 38 | 39 | out->len = 0; 40 | } 41 | 42 | static void out_push (out_t *out, const char *pw_buf, const int pw_len) 43 | { 44 | char *ptr = out->buf + out->len; 45 | 46 | memcpy (ptr, pw_buf, pw_len); 47 | 48 | #if defined (_WIN) 49 | 50 | ptr[pw_len + 0] = '\r'; 51 | ptr[pw_len + 1] = '\n'; 52 | 53 | out->len += pw_len + 2; 54 | 55 | #else 56 | 57 | ptr[pw_len] = '\n'; 58 | 59 | out->len += pw_len + 1; 60 | 61 | #endif 62 | 63 | if (out->len >= OUT_SIZE - 300) 64 | { 65 | out_flush (out); 66 | } 67 | } 68 | 69 | size_t next_permutation (char *word, int *p, int k) 70 | { 71 | p[k]--; 72 | 73 | int j = k % 2 * p[k]; 74 | 75 | char tmp = word[j]; 76 | 77 | word[j] = word[k]; 78 | 79 | word[k] = tmp; 80 | 81 | for (k = 1; p[k] == 0; k++) p[k] = k; 82 | 83 | return k; 84 | } 85 | 86 | int main (int argc, char *argv[]) 87 | { 88 | if (argc != 1) 89 | { 90 | fprintf (stderr, "usage: %s < infile > outfile\n", argv[0]); 91 | 92 | return (-1); 93 | } 94 | 95 | #ifdef _WINDOWS 96 | _setmode (_fileno (stdin), _O_BINARY); 97 | #endif 98 | 99 | out_t out; 100 | 101 | out.fp = stdout; 102 | out.len = 0; 103 | 104 | char line_buf[LINE_SIZE]; 105 | 106 | int line_len; 107 | 108 | while ((line_len = fgetl (stdin, LINE_SIZE, line_buf)) != -1) 109 | { 110 | if (line_len == 0) continue; 111 | 112 | /* init permutation */ 113 | 114 | int p[LINE_SIZE]; 115 | 116 | int k; 117 | 118 | for (k = 0; k < line_len + 1; k++) p[k] = k; 119 | 120 | k = 1; 121 | 122 | /* run permutation */ 123 | 124 | out_push (&out, line_buf, line_len); 125 | 126 | while ((k = next_permutation (line_buf, p, k)) != line_len) 127 | { 128 | out_push (&out, line_buf, line_len); 129 | } 130 | 131 | out_push (&out, line_buf, line_len); 132 | 133 | out_flush (&out); 134 | } 135 | 136 | return (0); 137 | } 138 | -------------------------------------------------------------------------------- /src/permute_exist.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: permute_exist 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | */ 19 | 20 | typedef struct 21 | { 22 | char chr; 23 | int occ; 24 | 25 | } db_t; 26 | 27 | int scan_word (char *line_buf, int line_len, db_t db_buf[256]) 28 | { 29 | int db_cnt = 0; 30 | 31 | int i; 32 | 33 | for (i = 0; i < line_len; i++) 34 | { 35 | const char chr = line_buf[i]; 36 | 37 | int j; 38 | 39 | for (j = 0; j < db_cnt; j++) 40 | { 41 | if (db_buf[j].chr == chr) 42 | { 43 | db_buf[j].occ++; 44 | 45 | break; 46 | } 47 | } 48 | 49 | if (j == db_cnt) 50 | { 51 | db_buf[j].chr = chr; 52 | db_buf[j].occ = 0; 53 | 54 | db_cnt++; 55 | } 56 | } 57 | 58 | return db_cnt; 59 | } 60 | 61 | int sort_by_key (const void *p1, const void *p2) 62 | { 63 | const db_t *db1 = (db_t *) p1; 64 | const db_t *db2 = (db_t *) p2; 65 | 66 | return db1->chr - db2->chr; 67 | } 68 | 69 | int main (int argc, char *argv[]) 70 | { 71 | if (argc != 2) 72 | { 73 | fprintf (stderr, "usage: %s word < infile > outfile\n", argv[0]); 74 | 75 | return (-1); 76 | } 77 | 78 | char *word_buf = argv[1]; 79 | 80 | #ifdef _WINDOWS 81 | _setmode (_fileno (stdin), _O_BINARY); 82 | #endif 83 | 84 | db_t db1_buf[256]; 85 | 86 | memset (db1_buf, 0, sizeof (db1_buf)); 87 | 88 | int db1_cnt = scan_word (word_buf, strlen (word_buf), db1_buf); 89 | 90 | qsort (db1_buf, db1_cnt, sizeof (db_t), sort_by_key); 91 | 92 | char line_buf[BUFSIZ]; 93 | 94 | int line_len; 95 | 96 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 97 | { 98 | if (line_len == 0) continue; 99 | 100 | db_t db2_buf[256]; 101 | 102 | memset (db2_buf, 0, sizeof (db2_buf)); 103 | 104 | int db2_cnt = scan_word (line_buf, line_len, db2_buf); 105 | 106 | if (db1_cnt != db2_cnt) continue; 107 | 108 | qsort (db2_buf, db2_cnt, sizeof (db_t), sort_by_key); 109 | 110 | int i; 111 | 112 | for (i = 0; i < db1_cnt; i++) 113 | { 114 | if (db1_buf[i].chr != db2_buf[i].chr) break; 115 | if (db1_buf[i].occ != db2_buf[i].occ) break; 116 | } 117 | 118 | if (i < db1_cnt) continue; 119 | 120 | puts (line_buf); 121 | } 122 | 123 | return (0); 124 | } 125 | -------------------------------------------------------------------------------- /src/prepare.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define TMPSIZ 0x100 15 | 16 | /** 17 | * Name........: prepare 18 | * Autor.......: Jens Steube 19 | * License.....: MIT 20 | */ 21 | 22 | int main (int argc, char *argv[]) 23 | { 24 | if (argc != 1) 25 | { 26 | fprintf (stderr, "usage: %s < infile > outfile\n", argv[0]); 27 | 28 | return (-1); 29 | } 30 | 31 | #ifdef _WINDOWS 32 | _setmode (_fileno (stdin), _O_BINARY); 33 | #endif 34 | 35 | char line_buf[BUFSIZ]; 36 | 37 | int line_len; 38 | 39 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 40 | { 41 | if (line_len == 0) continue; 42 | 43 | /* build prepare buffer */ 44 | 45 | char tmp_buf[TMPSIZ]; 46 | 47 | memset (tmp_buf, 0, TMPSIZ); 48 | 49 | int p; 50 | 51 | for (p = 0; p < line_len; p++) 52 | { 53 | int chr = (int) line_buf[p]; 54 | 55 | if (chr < 0) continue; 56 | if (chr >= TMPSIZ) continue; 57 | 58 | tmp_buf[chr]++; 59 | } 60 | 61 | /* output prepare buffer */ 62 | 63 | int line_pos; 64 | 65 | int tmp_pos; 66 | 67 | for (tmp_pos = 0, line_pos = 0; tmp_pos < TMPSIZ; tmp_pos++) 68 | { 69 | int j; 70 | 71 | for (j = 0; j < tmp_buf[tmp_pos]; j++) 72 | { 73 | line_buf[line_pos] = (uint8_t)tmp_pos; 74 | 75 | line_pos++; 76 | } 77 | } 78 | 79 | puts (line_buf); 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /src/remaining.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | ## Name........: tmesis-dynamic 4 | ## Autor.......: Jens Steube 5 | ## License.....: MIT 6 | 7 | use strict; 8 | use warnings; 9 | 10 | # This program takes 2 wordlists: 11 | # 12 | # - A wordlist (search): Each word is matched against the other wordlist. Don't make this too big, it's cached in memory. 13 | # - A wordlist (base): Prints what remains after above word was "subtracted". This is something like rockyou.txt or better 14 | # 15 | # There's high chances to create duplicates, you need to sort -u the result yourself 16 | # 17 | # Result is ideal for using in -a 1 combinator attack mode. You may want to do two attacks: 18 | # 19 | # - one for having the result on the left 20 | # - one for having the result on the right 21 | # 22 | # content wordlist (base): 23 | # 24 | # isajack3935 25 | # jackysch_5131 26 | # HBjackas5 27 | # mom1jackhopes 28 | # 29 | # content wordlist (search): 30 | # 31 | # jack 32 | # jacky 33 | # 34 | # produces candidates: 35 | # 36 | # isa 37 | # 3935 38 | # ysch_5131 39 | # HB 40 | # as5 41 | # mom1 42 | # hopes 43 | # sch_5131 44 | 45 | die "use: $0 wordlist_base.txt wordlist_search.txt\n" if scalar @ARGV != 2; 46 | 47 | my ($wordlist_base, $wordlist_search) = @ARGV; 48 | 49 | my @searches; 50 | 51 | open (IN, $wordlist_search) or die; 52 | 53 | while (my $word = ) 54 | { 55 | chomp $word; 56 | 57 | push @searches, $word; 58 | } 59 | 60 | close (IN); 61 | 62 | open (IN, $wordlist_base) or die; 63 | 64 | while (my $word = ) 65 | { 66 | chomp $word; 67 | 68 | for my $search (@searches) 69 | { 70 | my $prev = 0; 71 | 72 | next if index ($word, $search, $prev) == -1; 73 | 74 | my $total_len = length $word; 75 | 76 | while ($prev < $total_len) 77 | { 78 | my $pos = index ($word, $search, $prev); 79 | 80 | if ($pos == -1) 81 | { 82 | my $slice = substr ($word, $prev); 83 | 84 | printf "%s\n", $slice; 85 | 86 | last; 87 | } 88 | 89 | my $len = $pos - $prev; 90 | 91 | if ($len == 0) 92 | { 93 | $prev = $pos + length $search; 94 | 95 | next; 96 | } 97 | 98 | my $slice = substr ($word, $prev, $len); 99 | 100 | printf "%s\n", $slice; 101 | 102 | $prev = $pos + length $search; 103 | } 104 | } 105 | } 106 | 107 | close (IN); 108 | 109 | 110 | -------------------------------------------------------------------------------- /src/req-exclude.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define LOWER (1 << 0) 15 | #define UPPER (1 << 1) 16 | #define DIGIT (1 << 2) 17 | #define SYMBOL (1 << 3) 18 | #define OTHER (1 << 4) 19 | 20 | /** 21 | * Name........: req-exclude 22 | * Autor.......: Jens Steube 23 | * License.....: MIT 24 | */ 25 | 26 | int main (int argc, char *argv[]) 27 | { 28 | if (argc != 2) 29 | { 30 | fprintf (stderr, "usage: %s exc_mask < infile > outfile\n", argv[0]); 31 | fprintf (stderr, " exc_mask is the mask of prohibited (at least one character of ANY) types\n"); 32 | fprintf (stderr, " type masks: add together the numbers, i.e. lower + upper = 3\n"); 33 | fprintf (stderr, " LOWER 1\n"); 34 | fprintf (stderr, " UPPER 2\n"); 35 | fprintf (stderr, " DIGIT 4\n"); 36 | fprintf (stderr, " SYMBOL 8 (0x20 to 0x7e NOT IN lower, upper, digit)\n"); 37 | fprintf (stderr, " OTHER (tab, high ASCII, etc.) 16\n"); 38 | 39 | return (-1); 40 | } 41 | 42 | #ifdef _WINDOWS 43 | setmode (0, O_BINARY); 44 | #endif 45 | 46 | int exc_mask = atoi (argv[1]); 47 | 48 | char line_buf[BUFSIZ]; 49 | 50 | int line_len; 51 | 52 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 53 | { 54 | if (line_len == 0) continue; 55 | 56 | int cur_mask = 0; 57 | 58 | int p; 59 | 60 | for (p = 0; p < line_len; p++) 61 | { 62 | if ((line_buf[p] >= 'a') && (line_buf[p] <= 'z')) cur_mask |= LOWER; 63 | else if ((line_buf[p] >= 'A') && (line_buf[p] <= 'Z')) cur_mask |= UPPER; 64 | else if ((line_buf[p] >= '0') && (line_buf[p] <= '9')) cur_mask |= DIGIT; 65 | else if ((line_buf[p] >= 0x20) && (line_buf[p] <= 0x7e)) cur_mask |= SYMBOL; 66 | else cur_mask |= OTHER; 67 | } 68 | 69 | if (cur_mask & exc_mask) continue; 70 | 71 | puts (line_buf); 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /src/req-include.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define LOWER (1 << 0) 15 | #define UPPER (1 << 1) 16 | #define DIGIT (1 << 2) 17 | #define SYMBOL (1 << 3) 18 | #define OTHER (1 << 4) 19 | 20 | /** 21 | * Name........: req-include 22 | * Autor.......: Jens Steube 23 | * License.....: MIT 24 | */ 25 | 26 | int main (int argc, char *argv[]) 27 | { 28 | if (argc != 2) 29 | { 30 | fprintf (stderr, "usage: %s inc_mask < infile > outfile\n", argv[0]); 31 | fprintf (stderr, " inc_mask is the mask of required (at least one character of EACH) types\n"); 32 | fprintf (stderr, " type masks: add together the numbers, i.e. lower + upper = 3\n"); 33 | fprintf (stderr, " LOWER 1\n"); 34 | fprintf (stderr, " UPPER 2\n"); 35 | fprintf (stderr, " DIGIT 4\n"); 36 | fprintf (stderr, " SYMBOL 8 (0x20 to 0x7e NOT IN lower, upper, digit)\n"); 37 | fprintf (stderr, " OTHER (tab, high ASCII, etc.) 16\n"); 38 | 39 | return (-1); 40 | } 41 | 42 | #ifdef _WINDOWS 43 | _setmode (_fileno (stdin), _O_BINARY); 44 | #endif 45 | 46 | int req_mask = atoi (argv[1]); 47 | 48 | char line_buf[BUFSIZ]; 49 | 50 | int line_len; 51 | 52 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 53 | { 54 | if (line_len == 0) continue; 55 | 56 | int cur_mask = 0; 57 | 58 | int p; 59 | 60 | for (p = 0; p < line_len; p++) 61 | { 62 | if ((line_buf[p] >= 'a') && (line_buf[p] <= 'z')) cur_mask |= LOWER; 63 | else if ((line_buf[p] >= 'A') && (line_buf[p] <= 'Z')) cur_mask |= UPPER; 64 | else if ((line_buf[p] >= '0') && (line_buf[p] <= '9')) cur_mask |= DIGIT; 65 | else if ((line_buf[p] >= 0x20) && (line_buf[p] <= 0x7e)) cur_mask |= SYMBOL; 66 | else cur_mask |= OTHER; 67 | } 68 | 69 | if ((cur_mask & req_mask) != req_mask) continue; 70 | 71 | puts (line_buf); 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /src/rli.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "utils.c" 16 | 17 | #define STEPS 0x1000000 18 | 19 | /** 20 | * Name........: rli 21 | * Autor.......: Jens Steube 22 | * License.....: MIT 23 | */ 24 | 25 | typedef struct 26 | { 27 | char *buf; 28 | 29 | uint len; 30 | uint pos; 31 | 32 | } cache_t; 33 | 34 | static int cmp_cache (const cache_t *c1, const cache_t *c2) 35 | { 36 | return strcmp (c1->buf, c2->buf); 37 | } 38 | 39 | static int cmp_buf (const void *p1, const void *p2) 40 | { 41 | cache_t *c1 = (cache_t *) p1; 42 | cache_t *c2 = (cache_t *) p2; 43 | 44 | return cmp_cache (c1, c2); 45 | } 46 | 47 | static int cmp_pos (const void *p1, const void *p2) 48 | { 49 | cache_t *c1 = (cache_t *) p1; 50 | cache_t *c2 = (cache_t *) p2; 51 | 52 | return c1->pos - c2->pos; 53 | } 54 | 55 | int main (int argc, char *argv[]) 56 | { 57 | /* buffers */ 58 | 59 | FILE *fd; 60 | 61 | uint avail = 0; 62 | uint count = 0; 63 | 64 | cache_t *cache = NULL; 65 | 66 | /* stats */ 67 | 68 | uint removed = 0; 69 | 70 | /* arg */ 71 | 72 | if (argc < 4) 73 | { 74 | fprintf (stderr, "usage: %s infile outfile removefiles...\n", argv[0]); 75 | 76 | return (-1); 77 | } 78 | 79 | char *infile = argv[1]; 80 | char *outfile = argv[2]; 81 | 82 | /* cache */ 83 | 84 | printf ("Caching %s...\n", infile); 85 | 86 | if ((fd = fopen (infile, "rb")) == NULL) 87 | { 88 | fprintf (stderr, "%s: %s\n", infile, strerror (errno)); 89 | 90 | return (-1); 91 | } 92 | 93 | char line_buf[BUFSIZ]; 94 | 95 | int line_len; 96 | 97 | while ((line_len = fgetl (fd, BUFSIZ, line_buf)) != -1) 98 | { 99 | if (count == avail) 100 | { 101 | avail += STEPS; 102 | 103 | cache = (cache_t *) realloc (cache, avail * sizeof (cache_t)); 104 | 105 | if (cache == NULL) 106 | { 107 | fprintf (stderr, "Not enough memory\n"); 108 | 109 | fclose (fd); 110 | 111 | return (-1); 112 | } 113 | 114 | memset (&cache[count], 0, STEPS * sizeof (cache_t)); 115 | } 116 | 117 | char *new_buf = (char *) malloc (line_len + 1); 118 | 119 | if (new_buf == NULL) 120 | { 121 | fprintf (stderr, "Not enough memory\n"); 122 | 123 | fclose (fd); 124 | 125 | return (-1); 126 | } 127 | 128 | memcpy (new_buf, line_buf, line_len); 129 | 130 | new_buf[line_len] = 0; 131 | 132 | cache[count].buf = new_buf; 133 | cache[count].len = line_len; 134 | cache[count].pos = count; 135 | 136 | if ((count > 0) && ((count % 1000000) == 0)) 137 | { 138 | printf ("\rCached %u lines", count); 139 | 140 | fflush (stdout); 141 | } 142 | 143 | count++; 144 | } 145 | 146 | fclose (fd); 147 | 148 | printf ("\rCached %u lines\n", count); 149 | 150 | /* sort */ 151 | 152 | printf ("\nSorting...\n\n"); 153 | 154 | qsort (cache, count, sizeof (cache_t), cmp_buf); 155 | 156 | /* iterate through work */ 157 | 158 | uint i; 159 | 160 | for (i = 3; i < (uint) argc; i++) 161 | { 162 | uint removed_sav = removed; 163 | 164 | char *removefile = argv[i]; 165 | 166 | if (strcmp (removefile, infile) == 0) 167 | { 168 | fprintf (stderr, "Skipping check against infile %s\n\n", removefile); 169 | 170 | continue; 171 | } 172 | 173 | if (strcmp (removefile, outfile) == 0) 174 | { 175 | fprintf (stderr, "Skipping check against outfile %s\n\n", removefile); 176 | 177 | continue; 178 | } 179 | 180 | printf ("Checking %s against cache\n", removefile); 181 | 182 | if ((fd = fopen (removefile, "rb")) == NULL) 183 | { 184 | fprintf (stderr, "%s: %s\n", removefile, strerror (errno)); 185 | 186 | return (-1); 187 | } 188 | 189 | uint target_count = 0; 190 | 191 | while ((line_len = fgetl (fd, BUFSIZ, line_buf)) != -1) 192 | { 193 | target_count++; 194 | 195 | if ((target_count % 1000000) == 0) 196 | { 197 | printf ("\rLines compared %u", target_count); 198 | 199 | fflush (stdout); 200 | } 201 | 202 | cache_t target; 203 | 204 | target.buf = line_buf; 205 | target.len = line_len; 206 | 207 | cache_t *found = (cache_t *) bsearch (&target, cache, count, sizeof (cache_t), cmp_buf); 208 | 209 | if (found == NULL) continue; 210 | 211 | /* already found before */ 212 | 213 | if (found->len == 0) continue; 214 | 215 | found->len = 0; 216 | 217 | removed++; 218 | 219 | /* check possible duplicates */ 220 | 221 | cache_t *min = &cache[0]; 222 | cache_t *max = &cache[count - 1]; 223 | 224 | cache_t *tmp; 225 | 226 | tmp = found; 227 | 228 | for (tmp--; tmp >= min; tmp--) 229 | { 230 | if (cmp_cache (tmp, &target)) break; 231 | 232 | tmp->len = 0; 233 | 234 | removed++; 235 | } 236 | 237 | tmp = found; 238 | 239 | for (tmp++; tmp <= max; tmp++) 240 | { 241 | if (cmp_cache (tmp, &target)) break; 242 | 243 | tmp->len = 0; 244 | 245 | removed++; 246 | } 247 | } 248 | 249 | fclose (fd); 250 | 251 | printf ("\rLines compared %u\n", target_count); 252 | 253 | printf ("Removed %u lines from cache\n\n", removed - removed_sav); 254 | } 255 | 256 | /* unsort */ 257 | 258 | printf ("Sorting back to original positions...\n\n"); 259 | 260 | qsort (cache, count, sizeof (cache_t), cmp_pos); 261 | 262 | /* save result */ 263 | 264 | printf ("Finished!\n"); 265 | 266 | printf ("Removed %u lines from cache\n", removed); 267 | 268 | printf ("Writing %u lines to %s\n", count - removed, outfile); 269 | 270 | if ((fd = fopen (outfile, "wb")) == NULL) 271 | { 272 | fprintf (stderr, "%s: %s\n", outfile, strerror (errno)); 273 | 274 | return (-1); 275 | } 276 | 277 | for (i = 0; i < count; i++) 278 | { 279 | if (cache[i].len == 0) continue; 280 | 281 | fputs (cache[i].buf, fd); 282 | 283 | fputc ('\n', fd); 284 | } 285 | 286 | fclose (fd); 287 | 288 | return 0; 289 | } 290 | -------------------------------------------------------------------------------- /src/rli2.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "utils.c" 16 | 17 | /** 18 | * Name........: rli2 19 | * Autor.......: Jens Steube 20 | * License.....: MIT 21 | */ 22 | 23 | static int cmp_cache (const void *p1, const void *p2) 24 | { 25 | return strcmp (p1, p2); 26 | } 27 | 28 | int main (int argc, char *argv[]) 29 | { 30 | FILE *fd1; 31 | FILE *fd2; 32 | 33 | if (argc != 3) 34 | { 35 | fprintf (stderr, "usage: %s infile removefile\n", argv[0]); 36 | 37 | return (-1); 38 | } 39 | 40 | char *infile = argv[1]; 41 | char *removefile = argv[2]; 42 | 43 | if ((fd1 = fopen (infile, "rb")) == NULL) 44 | { 45 | fprintf (stderr, "%s: %s\n", infile, strerror (errno)); 46 | 47 | return (-1); 48 | } 49 | 50 | if ((fd2 = fopen (removefile, "rb")) == NULL) 51 | { 52 | fprintf (stderr, "%s: %s\n", removefile, strerror (errno)); 53 | 54 | fclose (fd1); 55 | 56 | return (-1); 57 | } 58 | 59 | char line_buf1[BUFSIZ]; 60 | char line_buf2[BUFSIZ]; 61 | 62 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) memset (line_buf1, 0, BUFSIZ); 63 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) memset (line_buf2, 0, BUFSIZ); 64 | 65 | int comp = 1; 66 | 67 | while (!feof (fd1) && !feof (fd2)) 68 | { 69 | comp = cmp_cache (line_buf1, line_buf2); 70 | 71 | if (comp == 0) 72 | { 73 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) memset (line_buf1, 0, BUFSIZ); 74 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) memset (line_buf2, 0, BUFSIZ); 75 | } 76 | else if (comp > 0) 77 | { 78 | if (fgetl (fd2, BUFSIZ, line_buf2) == -1) memset (line_buf2, 0, BUFSIZ); 79 | } 80 | else if (comp < 0) 81 | { 82 | puts (line_buf1); 83 | 84 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) memset (line_buf1, 0, BUFSIZ); 85 | } 86 | } 87 | 88 | if (!feof (fd1) && comp == 0) puts (line_buf1); 89 | 90 | if (comp > 0) puts (line_buf1); 91 | 92 | while (!feof (fd1)) 93 | { 94 | if (fgetl (fd1, BUFSIZ, line_buf1) == -1) 95 | { 96 | memset (line_buf1, 0, BUFSIZ); 97 | 98 | continue; 99 | } 100 | 101 | puts (line_buf1); 102 | } 103 | 104 | fclose (fd1); 105 | fclose (fd2); 106 | 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /src/rp_cpu.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Author......: Jens Steube 3 | * License.....: MIT 4 | */ 5 | 6 | #define RP_RULE_BUFSIZ 0x100 7 | 8 | #define RULE_RC_SYNTAX_ERROR -1 9 | #define RULE_RC_REJECT_ERROR -2 10 | 11 | #define RULE_OP_MANGLE_NOOP ':' 12 | #define RULE_OP_MANGLE_LREST 'l' 13 | #define RULE_OP_MANGLE_UREST 'u' 14 | #define RULE_OP_MANGLE_LREST_UFIRST 'c' 15 | #define RULE_OP_MANGLE_UREST_LFIRST 'C' 16 | #define RULE_OP_MANGLE_TREST 't' 17 | #define RULE_OP_MANGLE_TOGGLE_AT 'T' 18 | #define RULE_OP_MANGLE_REVERSE 'r' 19 | #define RULE_OP_MANGLE_DUPEWORD 'd' 20 | #define RULE_OP_MANGLE_DUPEWORD_TIMES 'p' 21 | #define RULE_OP_MANGLE_REFLECT 'f' 22 | #define RULE_OP_MANGLE_ROTATE_LEFT '{' 23 | #define RULE_OP_MANGLE_ROTATE_RIGHT '}' 24 | #define RULE_OP_MANGLE_APPEND '$' 25 | #define RULE_OP_MANGLE_PREPEND '^' 26 | #define RULE_OP_MANGLE_DELETE_FIRST '[' 27 | #define RULE_OP_MANGLE_DELETE_LAST ']' 28 | #define RULE_OP_MANGLE_DELETE_AT 'D' 29 | #define RULE_OP_MANGLE_EXTRACT 'x' 30 | #define RULE_OP_MANGLE_OMIT 'O' 31 | #define RULE_OP_MANGLE_INSERT 'i' 32 | #define RULE_OP_MANGLE_OVERSTRIKE 'o' 33 | #define RULE_OP_MANGLE_TRUNCATE_AT '\'' 34 | #define RULE_OP_MANGLE_REPLACE 's' 35 | #define RULE_OP_MANGLE_PURGECHAR '@' 36 | #define RULE_OP_MANGLE_TOGGLECASE_REC 'a' 37 | #define RULE_OP_MANGLE_DUPECHAR_FIRST 'z' 38 | #define RULE_OP_MANGLE_DUPECHAR_LAST 'Z' 39 | #define RULE_OP_MANGLE_DUPECHAR_ALL 'q' 40 | #define RULE_OP_MANGLE_EXTRACT_MEMORY 'X' 41 | #define RULE_OP_MANGLE_APPEND_MEMORY '4' 42 | #define RULE_OP_MANGLE_PREPEND_MEMORY '6' 43 | 44 | #define RULE_OP_MEMORIZE_WORD 'M' 45 | 46 | #define RULE_OP_REJECT_LESS '<' 47 | #define RULE_OP_REJECT_GREATER '>' 48 | #define RULE_OP_REJECT_CONTAIN '!' 49 | #define RULE_OP_REJECT_NOT_CONTAIN '/' 50 | #define RULE_OP_REJECT_EQUAL_FIRST '(' 51 | #define RULE_OP_REJECT_EQUAL_LAST ')' 52 | #define RULE_OP_REJECT_EQUAL_AT '=' 53 | #define RULE_OP_REJECT_CONTAINS '%' 54 | #define RULE_OP_REJECT_MEMORY 'Q' 55 | 56 | /* hashcat only */ 57 | #define RULE_OP_MANGLE_SWITCH_FIRST 'k' 58 | #define RULE_OP_MANGLE_SWITCH_LAST 'K' 59 | #define RULE_OP_MANGLE_SWITCH_AT '*' 60 | #define RULE_OP_MANGLE_CHR_SHIFTL 'L' 61 | #define RULE_OP_MANGLE_CHR_SHIFTR 'R' 62 | #define RULE_OP_MANGLE_CHR_INCR '+' 63 | #define RULE_OP_MANGLE_CHR_DECR '-' 64 | #define RULE_OP_MANGLE_REPLACE_NP1 '.' 65 | #define RULE_OP_MANGLE_REPLACE_NM1 ',' 66 | #define RULE_OP_MANGLE_DUPEBLOCK_FIRST 'y' 67 | #define RULE_OP_MANGLE_DUPEBLOCK_LAST 'Y' 68 | #define RULE_OP_MANGLE_TITLE 'E' 69 | 70 | static const char grp_op_nop[] = 71 | { 72 | RULE_OP_MANGLE_LREST, 73 | RULE_OP_MANGLE_UREST, 74 | RULE_OP_MANGLE_LREST_UFIRST, 75 | RULE_OP_MANGLE_UREST_LFIRST, 76 | RULE_OP_MANGLE_TREST, 77 | RULE_OP_MANGLE_REVERSE, 78 | RULE_OP_MANGLE_DUPEWORD, 79 | RULE_OP_MANGLE_REFLECT, 80 | RULE_OP_MANGLE_DELETE_FIRST, 81 | RULE_OP_MANGLE_DELETE_LAST, 82 | RULE_OP_MANGLE_ROTATE_LEFT, 83 | RULE_OP_MANGLE_ROTATE_RIGHT, 84 | RULE_OP_MANGLE_SWITCH_FIRST, 85 | RULE_OP_MANGLE_SWITCH_LAST, 86 | RULE_OP_MANGLE_DUPECHAR_ALL, 87 | RULE_OP_MANGLE_TITLE, 88 | RULE_OP_MANGLE_APPEND_MEMORY, 89 | RULE_OP_MANGLE_PREPEND_MEMORY, 90 | }; 91 | 92 | static const char grp_op_pos_p0[] = 93 | { 94 | RULE_OP_MANGLE_TOGGLE_AT, 95 | RULE_OP_MANGLE_DELETE_AT, 96 | RULE_OP_MANGLE_TRUNCATE_AT, 97 | RULE_OP_MANGLE_CHR_INCR, 98 | RULE_OP_MANGLE_CHR_DECR, 99 | RULE_OP_MANGLE_CHR_SHIFTL, 100 | RULE_OP_MANGLE_CHR_SHIFTR, 101 | RULE_OP_MANGLE_REPLACE_NP1, 102 | RULE_OP_MANGLE_REPLACE_NM1 103 | }; 104 | 105 | static const char grp_op_pos_p1[] = 106 | { 107 | RULE_OP_MANGLE_DUPEWORD_TIMES, 108 | RULE_OP_MANGLE_DUPECHAR_FIRST, 109 | RULE_OP_MANGLE_DUPECHAR_LAST, 110 | RULE_OP_MANGLE_DUPEBLOCK_FIRST, 111 | RULE_OP_MANGLE_DUPEBLOCK_LAST 112 | }; 113 | 114 | static const char grp_op_chr[] = 115 | { 116 | RULE_OP_MANGLE_APPEND, 117 | RULE_OP_MANGLE_PREPEND, 118 | RULE_OP_MANGLE_PURGECHAR 119 | }; 120 | 121 | static const char grp_op_chr_chr[] = 122 | { 123 | RULE_OP_MANGLE_REPLACE 124 | }; 125 | 126 | static const char grp_op_pos_chr[] = 127 | { 128 | RULE_OP_MANGLE_INSERT, 129 | RULE_OP_MANGLE_OVERSTRIKE 130 | }; 131 | 132 | static const char grp_op_pos_pos0[] = 133 | { 134 | RULE_OP_MANGLE_SWITCH_AT 135 | }; 136 | 137 | static const char grp_op_pos_pos1[] = 138 | { 139 | RULE_OP_MANGLE_EXTRACT, 140 | RULE_OP_MANGLE_OMIT 141 | }; 142 | 143 | static const char grp_op_pos1_pos2_pos3[] = 144 | { 145 | RULE_OP_MANGLE_EXTRACT_MEMORY 146 | }; 147 | 148 | static const char grp_pos[] = 149 | { 150 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B' 151 | }; 152 | -------------------------------------------------------------------------------- /src/rules_optimize.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "cpu_rules.h" 12 | 13 | /** 14 | * Name........: rules_optimize 15 | * Autor.......: Jens Steube 16 | * License.....: MIT 17 | */ 18 | 19 | #define MIN_FUNCTIONS 1 20 | #define MAX_FUNCTIONS 5 21 | 22 | int max_len = 0; 23 | 24 | typedef struct 25 | { 26 | char rule_buf[BLOCK_SIZE]; 27 | int rule_len; 28 | 29 | } rule_t; 30 | 31 | static int cmp (const void *p1, const void *p2) 32 | { 33 | rule_t *r1 = (rule_t *) p1; 34 | rule_t *r2 = (rule_t *) p2; 35 | 36 | return r1->rule_len - r2->rule_len; 37 | } 38 | 39 | int process_block (int o[BLOCK_SIZE], char *block_ptr[BLOCK_SIZE], int block_cnt, char *word_buf, char final_buf[BLOCK_SIZE], int final_len, char rule_buf[BLOCK_SIZE]) 40 | { 41 | int last_o = o[0]; 42 | 43 | for (int i = 1; i < block_cnt; i++) 44 | { 45 | if (o[i] < last_o) return (0); 46 | } 47 | 48 | memset (rule_buf, 0, BLOCK_SIZE); 49 | 50 | strcat (rule_buf, block_ptr[o[0]]); 51 | 52 | int i; 53 | 54 | for (i = 1; i < block_cnt; i++) 55 | { 56 | strcat (rule_buf, " "); 57 | 58 | strcat (rule_buf, block_ptr[o[i]]); 59 | } 60 | 61 | char out_buf[BLOCK_SIZE]; 62 | 63 | memset (out_buf, 0, sizeof (out_buf)); 64 | 65 | int out_len = apply_rule_cpu (rule_buf, strlen (rule_buf), word_buf, strlen (word_buf), out_buf); 66 | 67 | if (out_len == final_len) 68 | { 69 | if (memcmp (final_buf, out_buf, out_len) == 0) return (1); 70 | } 71 | 72 | return 0; 73 | } 74 | 75 | int next_permutation (int *o, int *p, int k) 76 | { 77 | p[k]--; 78 | 79 | int j = k % 2 * p[k]; 80 | 81 | int tmp = o[j]; 82 | 83 | o[j] = o[k]; 84 | 85 | o[k] = tmp; 86 | 87 | for (k = 1; p[k] == 0; k++) p[k] = k; 88 | 89 | return k; 90 | } 91 | 92 | int main () 93 | { 94 | FILE *fp = stdin; 95 | 96 | char line_buf[BUFSIZ]; 97 | 98 | while (!feof (fp)) 99 | { 100 | /* 101 | * line 102 | */ 103 | 104 | char *line_ptr = fgets (line_buf, BUFSIZ, fp); 105 | 106 | if (line_ptr == NULL) continue; 107 | 108 | int line_len = strlen (line_ptr); 109 | 110 | if (line_len && line_ptr[line_len - 1] == '\n') line_len--; 111 | if (line_len && line_ptr[line_len - 1] == '\r') line_len--; 112 | 113 | line_ptr[line_len] = 0; 114 | 115 | /* 116 | * split 117 | */ 118 | 119 | char *word_buf = line_ptr; 120 | 121 | char *sep = strchr (line_ptr, ':'); 122 | 123 | if (sep == NULL) continue; 124 | 125 | *sep = 0; 126 | 127 | int word_len = sep - word_buf; 128 | 129 | if (strstr (word_buf, "$HEX[")) continue; // not yet supported 130 | 131 | char *rule_buf = sep + 1; 132 | 133 | if (strchr (rule_buf, ':')) continue; // another one? ignore line 134 | 135 | /* 136 | * final 137 | */ 138 | 139 | char final_buf[BLOCK_SIZE]; 140 | 141 | memset (final_buf, 0, sizeof (final_buf)); 142 | 143 | int final_len = apply_rule_cpu (rule_buf, strlen (rule_buf), word_buf, strlen (word_buf), final_buf); 144 | 145 | if (final_len < 0) continue; 146 | 147 | if ((final_len == word_len) && (memcmp (word_buf, final_buf, final_len)) == 0) continue; 148 | 149 | /* 150 | * split into blocks 151 | */ 152 | 153 | char *block_ptr[BLOCK_SIZE]; 154 | int block_cnt = 0; 155 | 156 | char *ptr = rule_buf; 157 | 158 | for (char *next = NULL; (next = strchr (ptr, ' ')) != NULL; ptr = next + 1) 159 | { 160 | if (next[1] == ' ') next++; 161 | 162 | *next = 0; 163 | 164 | block_ptr[block_cnt] = ptr; 165 | 166 | block_cnt++; 167 | } 168 | 169 | block_ptr[block_cnt] = ptr; 170 | 171 | block_cnt++; 172 | 173 | //if (block_cnt < MIN_FUNCTIONS) continue; // to many 174 | if (block_cnt > MAX_FUNCTIONS) continue; // to many 175 | 176 | /* 177 | * permute blocks, this where the real work starts.. 178 | */ 179 | 180 | int o[BLOCK_SIZE]; 181 | int p[BLOCK_SIZE]; 182 | 183 | for (int i = 0; i < block_cnt + 1; i++) 184 | { 185 | o[i] = i; 186 | p[i] = i; 187 | } 188 | 189 | int k = 1; 190 | 191 | rule_t *rules_buf = (rule_t *) calloc (120 * MAX_FUNCTIONS, sizeof (rule_t)); // 5! = 120, so its guaranteed 192 | int rules_cnt = 0; 193 | 194 | char rule_out_buf[BLOCK_SIZE]; 195 | 196 | for (int i0 = 0, i1 = 1; i0 < block_cnt; i0++, i1++) 197 | { 198 | if (process_block (o, block_ptr, i1, word_buf, final_buf, final_len, rule_out_buf) == 1) 199 | { 200 | memcpy (rules_buf[rules_cnt].rule_buf, rule_out_buf, BLOCK_SIZE); 201 | 202 | rules_buf[rules_cnt].rule_len = i1; 203 | 204 | rules_cnt++; 205 | } 206 | } 207 | 208 | if (block_cnt >= 2) 209 | { 210 | while ((k = next_permutation (o, p, k)) != block_cnt) 211 | { 212 | for (int i0 = 0, i1 = 1; i0 < block_cnt; i0++, i1++) 213 | { 214 | if (process_block (o, block_ptr, i1, word_buf, final_buf, final_len, rule_out_buf) == 1) 215 | { 216 | memcpy (rules_buf[rules_cnt].rule_buf, rule_out_buf, BLOCK_SIZE); 217 | 218 | rules_buf[rules_cnt].rule_len = i1; 219 | 220 | rules_cnt++; 221 | } 222 | } 223 | } 224 | 225 | for (int i0 = 0, i1 = 1; i0 < block_cnt; i0++, i1++) 226 | { 227 | if (process_block (o, block_ptr, i1, word_buf, final_buf, final_len, rule_out_buf) == 1) 228 | { 229 | memcpy (rules_buf[rules_cnt].rule_buf, rule_out_buf, BLOCK_SIZE); 230 | 231 | rules_buf[rules_cnt].rule_len = i1; 232 | 233 | rules_cnt++; 234 | } 235 | } 236 | } 237 | 238 | /** 239 | * sort and output the ones with the less length 240 | */ 241 | 242 | qsort (rules_buf, rules_cnt, sizeof (rule_t), cmp); 243 | 244 | int first_len = rules_buf[0].rule_len; 245 | 246 | for (int i = 0; i < rules_cnt; i++) 247 | { 248 | rule_t *rule_buf = &rules_buf[i]; 249 | 250 | if (rule_buf->rule_len > first_len) break; 251 | 252 | puts (rule_buf->rule_buf); 253 | } 254 | 255 | free (rules_buf); 256 | } 257 | 258 | return 0; 259 | } 260 | -------------------------------------------------------------------------------- /src/seprule.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | ## Name........: seprule 4 | ## Autor.......: Jens Steube 5 | ## License.....: MIT 6 | 7 | use strict; 8 | use warnings; 9 | 10 | ## 11 | ## configuration 12 | ## 13 | 14 | my @rp = ('0'..'9', 'A'..'Z'); 15 | 16 | my $width = 3; 17 | my $rule = "i"; 18 | my $sep = " "; 19 | 20 | ## 21 | ## code 22 | ## 23 | 24 | my $rp_size = scalar @rp; 25 | 26 | my $total = $rp_size ** $width; 27 | 28 | my $db; 29 | 30 | for (my $i = 0; $i < $total; $i++) 31 | { 32 | my $left = $i; 33 | 34 | my @out; 35 | 36 | for (my $c = 0; $c < $width; $c++) 37 | { 38 | my $m = $left % $rp_size; 39 | my $d = $left / $rp_size; 40 | 41 | push (@out, $m); 42 | 43 | $left = $d; 44 | } 45 | 46 | @out = sort { $a <=> $b } @out; 47 | 48 | my $val = join ("", @out); 49 | 50 | next if (exists $db->{$val}); 51 | 52 | $db->{$val} = undef; 53 | 54 | my @final; 55 | 56 | for (my $c = 0; $c < $width; $c++) 57 | { 58 | my $s = sprintf ("T%s", $rp[$out[$c]]); 59 | 60 | push (@final, $s); 61 | } 62 | 63 | for (my $c = 0; $c < $width; $c++) 64 | { 65 | my $s = sprintf ("%s%s%s", $rule, $rp[$out[$c]], $sep); 66 | 67 | push (@final, $s); 68 | } 69 | 70 | print join (" ", "l", @final), "\n"; 71 | } 72 | -------------------------------------------------------------------------------- /src/splitlen.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | #define LEN_MIN 1 15 | #define LEN_MAX 64 16 | 17 | /** 18 | * Name........: splitlen 19 | * Autor.......: Jens Steube 20 | * License.....: MIT 21 | */ 22 | 23 | int main (int argc, char *argv[]) 24 | { 25 | if (argc != 2) 26 | { 27 | fprintf (stderr, "usage: %s outdir < infile\n", argv[0]); 28 | 29 | return (-1); 30 | } 31 | 32 | #ifdef _WINDOWS 33 | _setmode (_fileno (stdin), _O_BINARY); 34 | #endif 35 | 36 | FILE *fps[LEN_MAX + 1]; 37 | 38 | int i; 39 | 40 | for (i = LEN_MIN; i <= LEN_MAX; i++) 41 | { 42 | char name[BUFSIZ]; 43 | 44 | snprintf (name, BUFSIZ, "%s/%02d", argv[1], i); 45 | 46 | fps[i] = fopen (name, "wb"); 47 | 48 | if (fps[i] == NULL) 49 | { 50 | fprintf (stderr, "%s: %s\n", name, strerror (errno)); 51 | 52 | return (-1); 53 | } 54 | } 55 | 56 | char line_buf[BUFSIZ]; 57 | 58 | int line_len; 59 | 60 | while ((line_len = fgetl (stdin, BUFSIZ, line_buf)) != -1) 61 | { 62 | if (line_len < LEN_MIN) continue; 63 | if (line_len > LEN_MAX) continue; 64 | 65 | fputs (line_buf, fps[line_len]); 66 | 67 | fputc ('\n', fps[line_len]); 68 | } 69 | 70 | for (i = LEN_MIN; i < LEN_MAX; i++) fclose (fps[i]); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /src/strip-bsn.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: strip-bsn 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | * Description.: strip all \0 bytes 19 | */ 20 | 21 | int main (int argc, char *argv[]) 22 | { 23 | if (argc != 1) 24 | { 25 | fprintf (stderr, "usage: %s < infile > outfile\n", argv[0]); 26 | 27 | return (-1); 28 | } 29 | 30 | #ifdef _WINDOWS 31 | _setmode (_fileno (stdin), _O_BINARY); 32 | #endif 33 | 34 | char buf_i[BUFSIZ]; 35 | char buf_o[BUFSIZ]; 36 | 37 | while (!feof (stdin)) 38 | { 39 | size_t len_i = fread (buf_i, 1, sizeof (buf_i), stdin); 40 | 41 | if (len_i <= 0) break; 42 | 43 | char *tmp_i = buf_i; 44 | char *tmp_o = buf_o; 45 | 46 | size_t i; 47 | 48 | for (i = 0; i < len_i; i++) 49 | { 50 | const char c = *tmp_i++; 51 | 52 | if (c == '\0') continue; 53 | 54 | *tmp_o++ = c; 55 | } 56 | 57 | size_t len_o = tmp_o - buf_o; 58 | 59 | fwrite (buf_o, 1, len_o, stdout); 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /src/strip-bsr.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #define _FILE_OFFSET_BITS 64 3 | #define __MSVCRT_VERSION__ 0x0700 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "utils.c" 13 | 14 | /** 15 | * Name........: strip-bsr 16 | * Autor.......: Jens Steube 17 | * License.....: MIT 18 | * Description.: strip all \r bytes 19 | */ 20 | 21 | int main (int argc, char *argv[]) 22 | { 23 | if (argc != 1) 24 | { 25 | fprintf (stderr, "usage: %s < infile > outfile\n", argv[0]); 26 | 27 | return (-1); 28 | } 29 | 30 | #ifdef _WINDOWS 31 | _setmode (_fileno (stdin), _O_BINARY); 32 | #endif 33 | 34 | char buf_i[BUFSIZ]; 35 | char buf_o[BUFSIZ]; 36 | 37 | while (!feof (stdin)) 38 | { 39 | size_t len_i = fread (buf_i, 1, sizeof (buf_i), stdin); 40 | 41 | if (len_i <= 0) break; 42 | 43 | char *tmp_i = buf_i; 44 | char *tmp_o = buf_o; 45 | 46 | size_t i; 47 | 48 | for (i = 0; i < len_i; i++) 49 | { 50 | const char c = *tmp_i++; 51 | 52 | if (c == '\r') continue; 53 | 54 | *tmp_o++ = c; 55 | } 56 | 57 | size_t len_o = tmp_o - buf_o; 58 | 59 | fwrite (buf_o, 1, len_o, stdout); 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /src/tmesis-dynamic.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | ## Name........: tmesis-dynamic 4 | ## Autor.......: Jens Steube 5 | ## License.....: MIT 6 | 7 | use strict; 8 | use warnings; 9 | use Fcntl; 10 | 11 | # tmesis-dynamic will take 2 wordlists and produces a new one 12 | # 13 | # each word of wordlist 1 which matches a user-defined substring substitutes 14 | # that substring with each word of wordlist 2 15 | # 16 | # content wordlist 1: 17 | # 18 | # isajack3935 19 | # jackysch_5131 20 | # HBjackas5 21 | # mom1jackhopes 22 | # 23 | # content wordlist 2: 24 | # 25 | # 123456 26 | # password 27 | # jill 28 | # hashcat 29 | # 30 | # produces candidates with key "jack": 31 | # 32 | # isa1234563935 33 | # isapassword3935 34 | # isajill3935 35 | # isahashcat3935 36 | # 123456ysch_5131 37 | # passwordysch_5131 38 | # jillysch_5131 39 | # hashcatysch_5131 40 | # HB123456as5 41 | # HBpasswordas5 42 | # HBjillas5 43 | # HBhashcatas5 44 | # mom1123456hopes 45 | # mom1passwordhopes 46 | # mom1jillhopes 47 | # mom1hashcathopes 48 | 49 | die "use: $0 substring wordlist1.txt wordlist2.txt\n" if scalar @ARGV != 3; 50 | 51 | my ($substring, $wordlist1, $wordlist2) = @ARGV; 52 | 53 | open (IN1, $wordlist1) or die; 54 | 55 | while (my $word1 = ) 56 | { 57 | chomp $word1; 58 | 59 | my @slices = split (/$substring/i, $word1); 60 | 61 | next if scalar @slices == 1; 62 | 63 | open (IN2, $wordlist2) or die; 64 | 65 | while (my $word2 = ) 66 | { 67 | chomp $word2; 68 | 69 | print join ($word2, @slices), "\n"; 70 | } 71 | 72 | close (IN2); 73 | } 74 | 75 | close (IN1); 76 | -------------------------------------------------------------------------------- /src/tmesis.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | ## Name........: tmesis 4 | ## Autor.......: Jens Steube 5 | ## License.....: MIT 6 | 7 | use strict; 8 | use warnings; 9 | 10 | #tmesis will take a wordlist and produce insertion rules that would insert each word of the wordlist to preset positions. 11 | #For example: 12 | #Word 'password' will create insertion rules that would insert 'password' from position 0 to position F (15), and it will mutate the string '123456' as follows. 13 | #password123456 14 | #1password23456 15 | #12password3456 16 | #123password456 17 | #1234password56 18 | #12345password6 19 | #123456password 20 | # 21 | #Hints: 22 | #*Use tmesis to create rules to attack hashlists the came from the source. Run initial analysis on the cracked passwords , collect the top 10 - 20 words appear on the passwords and use tmesis to generate rules. 23 | #*use tmesis generated rules in combination with best64.rules 24 | # 25 | # inspired by T0XlC 26 | 27 | my $min_rule_pos = 0; 28 | my $max_rule_pos = 15; 29 | 30 | my $db; 31 | 32 | my @intpos_to_rulepos = ('0'..'9', 'A'..'Z'); 33 | 34 | my $function = "i"; 35 | #my $function = "o"; 36 | 37 | while (my $word = <>) 38 | { 39 | chomp $word; 40 | 41 | my $word_len = length $word; 42 | 43 | my @word_buf = split "", $word; 44 | 45 | for (my $rule_pos = $min_rule_pos; $rule_pos < $max_rule_pos - $word_len; $rule_pos++) 46 | { 47 | my @rule; 48 | 49 | for (my $word_pos = 0; $word_pos < $word_len; $word_pos++) 50 | { 51 | my $charspec = ''; 52 | 53 | # If the byte is not 7-bit printable ASCII ... 54 | if ($word_buf[$word_pos] =~ m/[\x00-\x19\x7f-\xff]/) 55 | { 56 | # ... escape it in rule syntax. 57 | $charspec = '\x' . unpack("H*", $word_buf[$word_pos]); 58 | } 59 | else 60 | { 61 | $charspec = $word_buf[$word_pos]; 62 | } 63 | 64 | my $function_full = $function . $intpos_to_rulepos[$rule_pos + $word_pos] . $charspec; 65 | 66 | $charspec && push @rule, $function_full; 67 | } 68 | 69 | print join (" ", @rule), "\n"; 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/topmorph.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | ## Name........: topmorph 4 | ## Autor.......: Jens Steube 5 | ## License.....: MIT 6 | 7 | use strict; 8 | use warnings; 9 | 10 | my @intpos_to_rulepos = ('0'..'9', 'A'..'Z'); 11 | 12 | my $function = "i"; 13 | #my $function = "o"; 14 | 15 | if (scalar @ARGV != 5) 16 | { 17 | print "usage: $0 dictionary depth width pos_min pos_max\n"; 18 | 19 | exit -1; 20 | } 21 | 22 | my ($dictionary, $depth, $width, $pos_min, $pos_max) = @ARGV; 23 | 24 | if ($width > 20) 25 | { 26 | print "width > 20\n"; 27 | 28 | exit -1; 29 | } 30 | 31 | for (my $pos = $pos_min; $pos <= $pos_max; $pos++) 32 | { 33 | my $db; 34 | 35 | open (IN, $dictionary) or die "$dictionary: $!\n"; 36 | 37 | while (my $line = ) 38 | { 39 | chomp $line; 40 | 41 | my $len = length $line; 42 | 43 | next if (($len - $pos) < $width); 44 | 45 | my $word = substr ($line, $pos, $width); 46 | 47 | next unless defined $word; 48 | 49 | $db->{$word}++; 50 | } 51 | 52 | close (IN); 53 | 54 | my @keys = sort { $db->{$b} <=> $db->{$a} } keys %{$db}; 55 | 56 | for (my $i = 0; $i < $depth; $i++) 57 | { 58 | my @chars = split "", $keys[$i]; 59 | 60 | my @rule; 61 | 62 | for (my $j = 0; $j < $width; $j++) 63 | { 64 | my $function_full = join "", $function, $intpos_to_rulepos[$pos + $j], $chars[$j]; 65 | 66 | push @rule, $function_full; 67 | } 68 | 69 | print join (" ", @rule), "\n"; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/utils.c: -------------------------------------------------------------------------------- 1 | typedef unsigned int uint; 2 | 3 | int super_chop (char *s, const int len_orig) 4 | { 5 | int len = len_orig; 6 | 7 | char *p = s + len - 1; 8 | 9 | while (len) 10 | { 11 | if (*p != '\n') break; 12 | 13 | *p-- = 0; 14 | 15 | len--; 16 | } 17 | 18 | while (len) 19 | { 20 | if (*p != '\r') break; 21 | 22 | *p-- = 0; 23 | 24 | len--; 25 | } 26 | 27 | return len; 28 | } 29 | 30 | int fgetl (FILE *fd, const size_t sz, char *buf) 31 | { 32 | if (feof (fd)) return -1; 33 | 34 | char *s = fgets (buf, sz - 1, fd); 35 | 36 | if (s == NULL) return -1; 37 | 38 | const int len = (const int) strlen (s); 39 | 40 | return super_chop (s, len); 41 | } 42 | 43 | #ifdef _WINDOWS 44 | 45 | uint get_random_num (const uint min, const uint max) 46 | { 47 | if (min == max) return (min); 48 | 49 | const uint low = max - min; 50 | 51 | if (low == 0) return (0); 52 | 53 | uint64_t r = rand () % low; 54 | 55 | r += min; 56 | 57 | if (r > 0xffffffff) 58 | { 59 | exit (-1); 60 | } 61 | 62 | return (uint) r; 63 | } 64 | 65 | #else 66 | 67 | uint get_random_num (const uint min, const uint max) 68 | { 69 | if (min == max) return (min); 70 | 71 | const uint low = max - min; 72 | 73 | if (low == 0) return (0); 74 | 75 | uint data; 76 | 77 | FILE *fp = fopen("/dev/urandom", "rb"); 78 | 79 | if (fp == NULL) exit (1); 80 | 81 | if ((fread (&data, 1, sizeof (uint), fp)) != sizeof (uint)) 82 | { 83 | exit (-1); 84 | } 85 | 86 | fclose (fp); 87 | 88 | uint64_t r = data % low; 89 | 90 | r += min; 91 | 92 | if (r > 0xffffffff) 93 | { 94 | exit (-1); 95 | } 96 | 97 | return (uint) r; 98 | } 99 | 100 | #endif 101 | --------------------------------------------------------------------------------