| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#include "librist/librist.h" |
| 47 |
#include <stdio.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <string.h> |
| 50 |
#include <stdbool.h> |
| 51 |
#include "pthread-shim.h" |
| 52 |
|
| 53 |
#ifdef _WIN32 |
| 54 |
#include <windows.h> |
| 55 |
#define usleep(x) Sleep((x)/1000) |
| 56 |
#else |
| 57 |
#include <unistd.h> |
| 58 |
#endif |
| 59 |
|
| 60 |
#define LISTEN_PORT 19900 |
| 61 |
#define VICTIM_PORT 20011 |
| 62 |
#define ATTACKER_PORT 20012 |
| 63 |
#define CNAME "stream-1" |
| 64 |
#define PSK "sharedgroupkey4321" |
| 65 |
|
| 66 |
static struct rist_logging_settings *log_settings = NULL; |
| 67 |
|
| 68 |
static pthread_mutex_t tracker_lock; |
| 69 |
#define MAX_TRACKED_PEERS 16 |
| 70 |
static struct { |
| 71 |
struct rist_peer *seen[MAX_TRACKED_PEERS]; |
| 72 |
int count; |
| 73 |
} tracker; |
| 74 |
|
| 75 |
static volatile int victim_pkts = 0; |
| 76 |
static volatile int attacker_pkts = 0; |
| 77 |
static volatile bool sender_run = true; |
| 78 |
|
| 79 |
static int log_cb(void *arg, enum rist_log_level level, const char *msg) { |
| 80 |
(void)arg; |
| 81 |
const char *tag = level <= RIST_LOG_ERROR ? "E" : |
| 82 |
level <= RIST_LOG_WARN ? "W" : |
| 83 |
level <= RIST_LOG_INFO ? "I" : "D"; |
| 84 |
fprintf(stderr, "%s| %s", tag, msg); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static void sender_status_cb(void *arg, struct rist_peer *peer, |
| 89 |
enum rist_connection_status status) { |
| 90 |
(void)arg; |
| 91 |
if (status == RIST_CONNECTION_ESTABLISHED || |
| 92 |
status == RIST_CLIENT_CONNECTED) { |
| 93 |
pthread_mutex_lock(&tracker_lock); |
| 94 |
bool seen = false; |
| 95 |
for (int i = 0; i < tracker.count; i++) |
| 96 |
if (tracker.seen[i] == peer) { seen = true; break; } |
| 97 |
if (!seen && tracker.count < MAX_TRACKED_PEERS) { |
| 98 |
tracker.seen[tracker.count++] = peer; |
| 99 |
fprintf(stderr, ">> sender CONNECTED (peer=%p, distinct_callers=%d)\n", |
| 100 |
(void *)peer, tracker.count); |
| 101 |
} |
| 102 |
pthread_mutex_unlock(&tracker_lock); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
static int victim_data_cb(void *arg, struct rist_data_block *b) { |
| 107 |
(void)arg; |
| 108 |
victim_pkts++; |
| 109 |
rist_receiver_data_block_free2(&b); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
static int attacker_data_cb(void *arg, struct rist_data_block *b) { |
| 113 |
(void)arg; |
| 114 |
attacker_pkts++; |
| 115 |
rist_receiver_data_block_free2(&b); |
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
static struct rist_ctx *start_receiver(const char *url, receiver_data_callback2_t cb) { |
| 120 |
struct rist_ctx *rx = NULL; |
| 121 |
if (rist_receiver_create(&rx, RIST_PROFILE_MAIN, log_settings) != 0) |
| 122 |
return NULL; |
| 123 |
struct rist_peer_config *pcfg = NULL; |
| 124 |
if (rist_parse_address2(url, (void *)&pcfg) != 0) { rist_destroy(rx); return NULL; } |
| 125 |
struct rist_peer *peer = NULL; |
| 126 |
if (rist_peer_create(rx, &peer, pcfg) != 0) { free(pcfg); rist_destroy(rx); return NULL; } |
| 127 |
free(pcfg); |
| 128 |
if (rist_receiver_data_callback_set2(rx, cb, NULL) != 0) { rist_destroy(rx); return NULL; } |
| 129 |
if (rist_start(rx) != 0) { rist_destroy(rx); return NULL; } |
| 130 |
return rx; |
| 131 |
} |
| 132 |
|
| 133 |
static PTHREAD_START_FUNC(sender_feed, arg) { |
| 134 |
struct rist_ctx *tx = arg; |
| 135 |
uint32_t counter = 0; |
| 136 |
while (sender_run) { |
| 137 |
struct rist_data_block blk; |
| 138 |
memset(&blk, 0, sizeof(blk)); |
| 139 |
blk.payload = &counter; |
| 140 |
blk.payload_len = sizeof(counter); |
| 141 |
blk.virt_src_port = 1968; |
| 142 |
rist_sender_data_write(tx, &blk); |
| 143 |
counter++; |
| 144 |
usleep(20000); |
| 145 |
} |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
int main(int argc, char *argv[]) { |
| 150 |
char url[320]; |
| 151 |
|
| 152 |
|
| 153 |
bool control = (argc > 1 && strcmp(argv[1], "control") == 0); |
| 154 |
const char *attacker_cname = control ? "other-stream" : CNAME; |
| 155 |
|
| 156 |
if (rist_logging_set(&log_settings, RIST_LOG_INFO, log_cb, NULL, NULL, stderr) != 0) |
| 157 |
return 99; |
| 158 |
|
| 159 |
memset(&tracker, 0, sizeof(tracker)); |
| 160 |
pthread_mutex_init(&tracker_lock, NULL); |
| 161 |
|
| 162 |
|
| 163 |
struct rist_ctx *tx = NULL; |
| 164 |
if (rist_sender_create(&tx, RIST_PROFILE_MAIN, 0, log_settings) != 0) return 99; |
| 165 |
rist_connection_status_callback_set(tx, sender_status_cb, NULL); |
| 166 |
snprintf(url, sizeof(url), |
| 167 |
"rist://@127.0.0.1:%d?session-timeout=30000&keepalive-interval=1000" |
| 168 |
"&secret=%s&aes-type=128", LISTEN_PORT, PSK); |
| 169 |
struct rist_peer_config *tx_pcfg = NULL; |
| 170 |
if (rist_parse_address2(url, (void *)&tx_pcfg) != 0) { rist_destroy(tx); return 99; } |
| 171 |
struct rist_peer *tx_peer = NULL; |
| 172 |
if (rist_peer_create(tx, &tx_peer, tx_pcfg) != 0) { free(tx_pcfg); rist_destroy(tx); return 99; } |
| 173 |
free(tx_pcfg); |
| 174 |
if (rist_start(tx) != 0) { rist_destroy(tx); return 99; } |
| 175 |
|
| 176 |
pthread_t feeder; |
| 177 |
pthread_create(&feeder, NULL, sender_feed, tx); |
| 178 |
usleep(200000); |
| 179 |
|
| 180 |
|
| 181 |
fprintf(stderr, "\n== round 1: VICTIM connects (port %d, cname %s) ==\n", |
| 182 |
VICTIM_PORT, CNAME); |
| 183 |
snprintf(url, sizeof(url), |
| 184 |
"rist://127.0.0.1:%d?local-port=%d&cname=%s" |
| 185 |
"&session-timeout=30000&keepalive-interval=1000&secret=%s&aes-type=128", |
| 186 |
LISTEN_PORT, VICTIM_PORT, CNAME, PSK); |
| 187 |
struct rist_ctx *victim = start_receiver(url, victim_data_cb); |
| 188 |
if (!victim) { sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99; } |
| 189 |
usleep(3000000); |
| 190 |
|
| 191 |
pthread_mutex_lock(&tracker_lock); |
| 192 |
int callers_after_victim = tracker.count; |
| 193 |
pthread_mutex_unlock(&tracker_lock); |
| 194 |
int victim_before = victim_pkts; |
| 195 |
fprintf(stderr, "after victim: distinct_callers=%d, victim_pkts=%d\n", |
| 196 |
callers_after_victim, victim_before); |
| 197 |
|
| 198 |
|
| 199 |
fprintf(stderr, "\n== victim goes silent ==\n"); |
| 200 |
rist_destroy(victim); |
| 201 |
usleep(3000000); |
| 202 |
|
| 203 |
|
| 204 |
fprintf(stderr, "\n== round 2: ATTACKER connects (port %d, cname=%s, same PSK)%s ==\n", |
| 205 |
ATTACKER_PORT, attacker_cname, control ? " [CONTROL: different cname]" : ""); |
| 206 |
snprintf(url, sizeof(url), |
| 207 |
"rist://127.0.0.1:%d?local-port=%d&cname=%s" |
| 208 |
"&session-timeout=30000&keepalive-interval=1000&secret=%s&aes-type=128", |
| 209 |
LISTEN_PORT, ATTACKER_PORT, attacker_cname, PSK); |
| 210 |
int attacker_start = attacker_pkts; |
| 211 |
struct rist_ctx *attacker = start_receiver(url, attacker_data_cb); |
| 212 |
if (!attacker) { sender_run = false; pthread_join(feeder, NULL); rist_destroy(tx); return 99; } |
| 213 |
usleep(4000000); |
| 214 |
|
| 215 |
pthread_mutex_lock(&tracker_lock); |
| 216 |
int callers_after_attacker = tracker.count; |
| 217 |
pthread_mutex_unlock(&tracker_lock); |
| 218 |
int attacker_got = attacker_pkts - attacker_start; |
| 219 |
fprintf(stderr, "\n== after attacker: distinct_callers=%d, attacker_pkts=%d ==\n", |
| 220 |
callers_after_attacker, attacker_got); |
| 221 |
|
| 222 |
sender_run = false; |
| 223 |
pthread_join(feeder, NULL); |
| 224 |
rist_destroy(attacker); |
| 225 |
rist_destroy(tx); |
| 226 |
free(log_settings); |
| 227 |
|
| 228 |
if (victim_before == 0) { |
| 229 |
fprintf(stderr, "INDETERMINATE: victim never received the stream.\n"); |
| 230 |
return 2; |
| 231 |
} |
| 232 |
|
| 233 |
bool absorbed = (callers_after_attacker == callers_after_victim); |
| 234 |
bool attacker_received = (attacker_got > 0); |
| 235 |
|
| 236 |
if (control) { |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
fprintf(stderr, "\nCONTROL (different cname): absorbed=%d " |
| 241 |
"(distinct_callers %d->%d), attacker_pkts=%d\n", |
| 242 |
absorbed, callers_after_victim, callers_after_attacker, attacker_got); |
| 243 |
if (!absorbed) { |
| 244 |
fprintf(stderr, "CONTROL OK: different-cname caller stayed " |
| 245 |
"separate.\n"); |
| 246 |
return 0; |
| 247 |
} |
| 248 |
fprintf(stderr, "CONTROL UNEXPECTED: different cname was absorbed.\n"); |
| 249 |
return 1; |
| 250 |
} |
| 251 |
|
| 252 |
if (absorbed) { |
| 253 |
fprintf(stderr, "\nREGRESSION: independent attacker holding only the " |
| 254 |
"shared PSK + cname \"%s\" was ABSORBED into the " |
| 255 |
"victim's peer record (distinct_callers stayed %d). " |
| 256 |
"Finding 1 (PSK cname identity takeover) has been " |
| 257 |
"reintroduced.\n", |
| 258 |
CNAME, callers_after_attacker); |
| 259 |
return 1; |
| 260 |
} |
| 261 |
fprintf(stderr, "\nFIX HOLDS: PSK attacker was NOT absorbed; it became a " |
| 262 |
"separate caller (distinct_callers %d->%d). The SRP-only " |
| 263 |
"gate refused the cname migration. (attacker_received=%d is " |
| 264 |
"expected PSK broadcast behaviour, not the defect.)\n", |
| 265 |
callers_after_victim, callers_after_attacker, attacker_received); |
| 266 |
return 0; |
| 267 |
} |