| |
| #include "../kernel.h" |
|
|
| |
| |
| |
| |
| |
| #define QSTATE_SZ 256 |
| typedef struct { u32 states[QSTATE_SZ]; u32 collapsed; u32 entropy; } QuantumState; |
|
|
| static inline u32 logistic_map(u32 x) { |
| |
| u64 y = (u64)x * ((1ULL<<32)-x); |
| return (u32)((y * 4) >> 32); |
| } |
|
|
| void quantum_crack_init(QuantumState* qs) { |
| u32 seed = 12345; |
| for(u32 i=0;i<QSTATE_SZ;i++) { qs->states[i] = seed; seed = logistic_map(seed); } |
| qs->collapsed = 0; qs->entropy = 0; |
| } |
|
|
| u32 quantum_crack_superpose(QuantumState* qs, u32 target_hash) { |
| |
| u32 best = 0xFFFFFFFF; u32 best_idx = 0; |
| for(u32 i=0;i<QSTATE_SZ;i++) { |
| u32 h = qs->states[i]; |
| |
| h ^= (h >> 13); h *= 0x5bd1e995; h ^= (h >> 15); |
| u32 d = (h > target_hash) ? h - target_hash : target_hash - h; |
| if(d < best) { best = d; best_idx = i; } |
| |
| qs->states[i] = logistic_map(qs->states[i]); |
| } |
| qs->collapsed = best_idx; |
| qs->entropy = best; |
| return best_idx; |
| } |
|
|
| |
| |
| |
| |
| |
| #define ENTROPY_WINDOW 256 |
| typedef struct { u32 values[ENTROPY_WINDOW]; u32 pos; double entropy; } EntropySniffer; |
|
|
| void entropy_sniffer_init(EntropySniffer* es) { es->pos=0;es->entropy=0;for(u32 i=0;i<ENTROPY_WINDOW;i++)es->values[i]=0; } |
|
|
| double entropy_sniffer_feed(EntropySniffer* es, u8 byte) { |
| es->values[es->pos % ENTROPY_WINDOW] = byte; |
| es->pos++; |
| |
| u32 counts[256] = {0}; |
| for(u32 i=0;i<ENTROPY_WINDOW;i++) counts[es->values[i]]++; |
| double h = 0; u32 n = ENTROPY_WINDOW; |
| for(u32 i=0;i<256;i++) if(counts[i]) { double p=(double)counts[i]/n; h-=p*(u32)(p*1000)/1000.0; } |
| es->entropy = h; |
| return h; |
| } |
|
|
| int entropy_sniffer_detect(EntropySniffer* es) { |
| |
| return (es->entropy > 7200) ? 1 : 0; |
| } |
|
|
| |
| |
| |
| |
| |
| typedef struct { u32 structs, defines, typedefs, inlines, loops, ifs, returns; u32 N; } CodeGrammar; |
|
|
| int grammar_analyze(const char* code, CodeGrammar* cg) { |
| |
| u32 s=0,d=0,t=0,i=0,lp=0,ifs=0,r=0; |
| for(const char* c=code;*c;c++) { |
| if(c[0]=='s'&&c[1]=='t'&&c[2]=='r'&&c[3]=='u') { s++; c+=5; } |
| else if(c[0]=='#'&&c[1]=='d') { d++; while(*c&&*c!='\n')c++; } |
| else if(c[0]=='t'&&c[1]=='y'&&c[2]=='p') { t++; c+=6; } |
| else if(c[0]=='f'&&c[1]=='o'&&c[2]=='r') { lp++; c+=2; } |
| else if(c[0]=='w'&&c[1]=='h'&&c[2]=='i') { lp++; c+=3; } |
| else if(c[0]=='i'&&c[1]=='f') { ifs++; c+=1; } |
| else if(c[0]=='r'&&c[1]=='e'&&c[2]=='t') { r++; c+=3; } |
| } |
| cg->structs=s;cg->defines=d;cg->typedefs=t;cg->inlines=i;cg->loops=lp;cg->ifs=ifs;cg->returns=r; |
| u32 n=(s>0)+(d>0)+(t>0)+(i>0)+(lp>0)+(ifs>0)+(r>0); |
| cg->N = n; |
| |
| if(n < 4) return -1; |
| if(n > 12) return -2; |
| if(ifs > 0 && r == 0) return -3; |
| if(lp > 10 && ifs < 3) return -4; |
| return 0; |
| } |
|
|
| |
| |
| |
| |
| |
| typedef struct { double x,y,z; u32 ip; u32 critical; } ChaosNode; |
| static ChaosNode chaos_nodes[64]; |
| static u32 chaos_count; |
|
|
| void chaos_init(void) { chaos_count=0; for(u32 i=0;i<64;i++){chaos_nodes[i].critical=0;} } |
|
|
| void chaos_add_node(u32 ip, u32 open_ports, u32 vuln_count) { |
| if(chaos_count >= 64) return; |
| ChaosNode* cn = &chaos_nodes[chaos_count++]; |
| cn->ip = ip; |
| |
| cn->x = (double)open_ports / 10.0; |
| cn->y = (double)vuln_count; |
| cn->z = (cn->x + cn->y) / 2.0; |
| |
| cn->critical = (open_ports > 5 && vuln_count > 0) ? 1 : 0; |
| } |
|
|
| typedef struct { u32 ip; u32 risk; } CriticalNode; |
| static CriticalNode criticals[16]; static u32 critical_count; |
|
|
| void chaos_find_critical(void) { |
| critical_count = 0; |
| for(u32 i=0;i<chaos_count && critical_count<16;i++) { |
| if(chaos_nodes[i].critical) { |
| criticals[critical_count].ip = chaos_nodes[i].ip; |
| |
| u32 risk = (u32)(chaos_nodes[i].x * 10 + chaos_nodes[i].y * 5); |
| criticals[critical_count].risk = risk; |
| critical_count++; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| #define KOLMO_WINDOW 64 |
| typedef struct { u8 patterns[KOLMO_WINDOW]; u32 pos; u32 compress_ratio; } KolmogorovSniffer; |
|
|
| void kolmogorov_init(KolmogorovSniffer* ks) { ks->pos=0;ks->compress_ratio=0;for(u32 i=0;i<KOLMO_WINDOW;i++)ks->patterns[i]=0; } |
|
|
| int kolmogorov_feed(KolmogorovSniffer* ks, u8 byte) { |
| ks->patterns[ks->pos++ % KOLMO_WINDOW] = byte; |
| |
| u32 repeats = 0; |
| for(u32 i=0;i<ks->pos-1;i++) { |
| if(ks->patterns[i] == byte) repeats++; |
| } |
| |
| u32 ratio = (KOLMO_WINDOW - repeats) * 100 / KOLMO_WINDOW; |
| ks->compress_ratio = ratio; |
| |
| return (ratio > 80) ? 1 : 0; |
| } |
|
|
| |
| |
| |
| |
| |
| #define GENOME_SZ 32 |
| #define POPULATION 16 |
| typedef struct { u8 genes[GENOME_SZ]; u32 fitness; u32 generation; } Genome; |
| static Genome population[POPULATION]; |
|
|
| static inline u32 mutate_gene(u32 g) { return logistic_map(g) ^ (g >> 5); } |
|
|
| void evolution_init(void) { |
| u32 seed = 42; |
| for(u32 i=0;i<POPULATION;i++) { |
| for(u32 j=0;j<GENOME_SZ;j++) { population[i].genes[j] = seed & 0xFF; seed = logistic_map(seed); } |
| population[i].fitness = 0; population[i].generation = 0; |
| } |
| } |
|
|
| Genome* evolution_evolve(u32 target, u32 iterations) { |
| |
| Genome* best = &population[0]; |
| for(u32 iter=0;iter<iterations;iter++) { |
| |
| for(u32 i=POPULATION/2;i<POPULATION;i++) { |
| for(u32 j=0;j<GENOME_SZ;j++) population[i].genes[j] = mutate_gene(population[i%8].genes[j]); |
| |
| u32 h = 0; for(u32 j=0;j<GENOME_SZ;j++) h = h*31 + population[i].genes[j]; |
| population[i].fitness = (h > target) ? 0xFFFFFFFF - (h-target) : 0xFFFFFFFF - (target-h); |
| } |
| |
| for(u32 i=0;i<POPULATION;i++) { |
| if(population[i].fitness > best->fitness) best = &population[i]; |
| } |
| best->generation++; |
| for(u32 i=0;i<POPULATION;i++) population[i].generation = iter; |
| } |
| return best; |
| } |
|
|
| |
| |
| |
| |
| |
| #define MAX_GRAPH_NODES 64 |
| typedef struct { u32 ip; u32 parent; u32 depth; u32 children[16]; u32 child_count; } GraphNode; |
| static GraphNode graph[MAX_GRAPH_NODES]; |
| static u32 graph_node_count; |
|
|
| void graph_init(void) { graph_node_count=0; for(u32 i=0;i<MAX_GRAPH_NODES;i++){graph[i].child_count=0;graph[i].parent=0xFFFFFFFF;} } |
|
|
| GraphNode* graph_add_node(u32 ip) { |
| if(graph_node_count >= MAX_GRAPH_NODES) return NULL; |
| GraphNode* gn = &graph[graph_node_count++]; |
| gn->ip = ip; gn->depth = 0; gn->parent = 0xFFFFFFFF; |
| |
| if(graph_node_count <= 4) { |
| if(graph_node_count > 1) { |
| gn->parent = graph_node_count - 2; |
| u32 p = gn->parent; |
| if(p < MAX_GRAPH_NODES && graph[p].child_count < 16) |
| graph[p].children[graph[p].child_count++] = graph_node_count - 1; |
| } |
| } else { |
| |
| u32 best = 0; u32 min_c = 16; |
| for(u32 i=0;i<graph_node_count-1;i++) { |
| if(graph[i].child_count < min_c) { min_c = graph[i].child_count; best = i; } |
| } |
| gn->parent = best; |
| if(graph[best].child_count < 16) graph[best].children[graph[best].child_count++] = graph_node_count-1; |
| } |
| return gn; |
| } |
|
|
| void graph_trace_path(u32 from, u32 to, u32* path, u32* len) { |
| |
| GraphNode* g = &graph[to]; |
| u32 l = 0; |
| while(g && g->parent != 0xFFFFFFFF && l < 64) { |
| path[l++] = g->ip; |
| if(g->ip == from) break; |
| u32 p = g->parent; |
| g = (p < MAX_GRAPH_NODES) ? &graph[p] : NULL; |
| } |
| *len = l; |
| } |
|
|