📄 gentest.c
字号:
static int gen_int_range(uint64_t min, uint64_t max){ uint_t r = random(); return min + (r % (1+max-min));}/* return a fnum for use as a root fid be careful to call GEN_SET_FNUM() when you use this!*/static uint16_t gen_root_fid(int instance){ if (gen_chance(5)) return gen_fnum(instance); return 0;}/* generate a file offset*/static int gen_offset(void){ if (gen_chance(20)) return 0;// if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF); return gen_int_range(0, 1024*1024);}/* generate a io count*/static int gen_io_count(void){ if (gen_chance(20)) return 0;// if (gen_chance(5)) return gen_int_range(0, 0xFFFFFFFF); return gen_int_range(0, 4096);}/* generate a filename*/static const char *gen_fname(void){ const char *names[] = {"gentest\\gentest.dat", "gentest\\foo", "gentest\\foo2.sym", "gentest\\foo3.dll", "gentest\\foo4", "gentest\\foo4:teststream1", "gentest\\foo4:teststream2", "gentest\\foo5.exe", "gentest\\foo5.exe:teststream3", "gentest\\foo5.exe:teststream4", "gentest\\foo6.com", "gentest\\blah", "gentest\\blah\\blergh.txt", "gentest\\blah\\blergh2", "gentest\\blah\\blergh3.txt", "gentest\\blah\\blergh4", "gentest\\blah\\blergh5.txt", "gentest\\blah\\blergh5", "gentest\\blah\\.", "gentest\\blah\\..", "gentest\\a_very_long_name.bin", "gentest\\x.y", "gentest\\blah"}; int i; do { i = gen_int_range(0, ARRAY_SIZE(names)-1); } while (ignore_pattern(names[i])); return names[i];}/* generate a filename with a higher chance of choosing an already open file*/static const char *gen_fname_open(int instance){ uint16_t h; h = gen_fnum(instance); if (h == BAD_HANDLE) { return gen_fname(); } return open_handles[h].name;}/* generate a wildcard pattern*/static const char *gen_pattern(void){ int i; const char *names[] = {"gentest\\*.dat", "gentest\\*", "gentest\\*.*", "gentest\\blah\\*.*", "gentest\\blah\\*", "gentest\\?"}; if (gen_chance(50)) return gen_fname(); do { i = gen_int_range(0, ARRAY_SIZE(names)-1); } while (ignore_pattern(names[i])); return names[i];}static uint32_t gen_bits_levels(int nlevels, ...){ va_list ap; uint32_t pct; uint32_t mask; int i; va_start(ap, nlevels); for (i=0;i<nlevels;i++) { pct = va_arg(ap, uint32_t); mask = va_arg(ap, uint32_t); if (pct == 100 || gen_chance(pct)) { va_end(ap); return mask & random(); } } va_end(ap); return 0;}/* generate a bitmask*/static uint32_t gen_bits_mask(uint_t mask){ uint_t ret = random(); return ret & mask;}/* generate a bitmask with high probability of the first mask and low of the second*/static uint32_t gen_bits_mask2(uint32_t mask1, uint32_t mask2){ if (!options.valid && gen_chance(10)) return gen_bits_mask(mask2); return gen_bits_mask(mask1);}/* generate reserved values */static uint64_t gen_reserved8(void){ if (options.valid) return 0; return gen_bits_mask(0xFF);}static uint64_t gen_reserved16(void){ if (options.valid) return 0; return gen_bits_mask(0xFFFF);}static uint64_t gen_reserved32(void){ if (options.valid) return 0; return gen_bits_mask(0xFFFFFFFF);}static uint64_t gen_reserved64(void){ if (options.valid) return 0; return gen_bits_mask(0xFFFFFFFF) | (((uint64_t)gen_bits_mask(0xFFFFFFFF))<<32);}/* generate a boolean*/static bool gen_bool(void){ return gen_bits_mask2(0x1, 0xFF);}/* generate ntrename flags*/static uint16_t gen_rename_flags(void){ if (gen_chance(30)) return RENAME_FLAG_RENAME; if (gen_chance(30)) return RENAME_FLAG_HARD_LINK; if (gen_chance(30)) return RENAME_FLAG_COPY; return gen_bits_mask(0xFFFF);}/* generate a pid */static uint16_t gen_pid(void){ if (gen_chance(10)) return gen_bits_mask(0xFFFF); return getpid();}/* return a set of lock flags*/static uint16_t gen_lock_flags_smb2(void){ if (!options.valid && gen_chance(5)) return gen_bits_mask(0xFFFF); if (gen_chance(20)) return gen_bits_mask(0x1F); if (gen_chance(50)) return SMB2_LOCK_FLAG_UNLOCK; return gen_bits_mask(SMB2_LOCK_FLAG_SHARED | SMB2_LOCK_FLAG_EXCLUSIVE | SMB2_LOCK_FLAG_FAIL_IMMEDIATELY);}/* generate a lock count*/static off_t gen_lock_count(void){ return gen_int_range(0, 3);}/* generate a NT access mask*/static uint32_t gen_access_mask(void){ uint32_t ret; if (gen_chance(70)) return SEC_FLAG_MAXIMUM_ALLOWED; if (gen_chance(70)) return SEC_FILE_ALL; ret = gen_bits_mask(0xFFFFFFFF); if (options.valid) ret &= ~SEC_MASK_INVALID; return ret;}/* return a lockingx lock mode*/static uint16_t gen_lock_mode(void){ if (!options.valid && gen_chance(5)) return gen_bits_mask(0xFFFF); if (gen_chance(20)) return gen_bits_mask(0x1F); return gen_bits_mask(LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES);}/* generate a ntcreatex flags field*/static uint32_t gen_ntcreatex_flags(void){ if (gen_chance(70)) return NTCREATEX_FLAGS_EXTENDED; return gen_bits_mask2(0x1F, 0xFFFFFFFF);}/* generate a ntcreatex create options bitfield*/static uint32_t gen_create_options(void){ if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFFFFFF); if (gen_chance(50)) return 0; return gen_bits_mask(NTCREATEX_OPTIONS_DELETE_ON_CLOSE | NTCREATEX_OPTIONS_DIRECTORY);}/* generate a ntcreatex open disposition*/static uint32_t gen_open_disp(void){ if (gen_chance(50)) return NTCREATEX_DISP_OPEN_IF; if (!options.valid && gen_chance(10)) return gen_bits_mask(0xFFFFFFFF); return gen_int_range(0, 5);}/* generate an openx open mode*/static uint16_t gen_openx_mode(void){ if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF); if (gen_chance(20)) return gen_bits_mask(0xFF); return OPENX_MODE_DENY_NONE | gen_bits_mask(0x3);}/* generate an openx flags field*/static uint16_t gen_openx_flags(void){ if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF); return gen_bits_mask(0x7);}/* generate an openx open function*/static uint16_t gen_openx_func(void){ if (!options.valid && gen_chance(20)) return gen_bits_mask(0xFFFF); return gen_bits_mask(0x13);}/* generate a file attrib combination*/static uint32_t gen_attrib(void){ uint32_t ret; if (gen_chance(20)) { ret = gen_bits_mask(0xFFFFFFFF); if (options.valid) ret &= FILE_ATTRIBUTE_ALL_MASK; return ret; } return gen_bits_mask(FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY);}/* generate a unix timestamp*/static time_t gen_timet(void){ if (gen_chance(30)) return 0; return (time_t)random();}/* generate a milliseconds protocol timeout*/static uint32_t gen_timeout(void){ if (gen_chance(98)) return 0; return random() % 50;}/* generate a timestamp*/static NTTIME gen_nttime(void){ NTTIME ret; unix_to_nt_time(&ret, gen_timet()); return ret;}/* generate a timewarp value*/static NTTIME gen_timewarp(void){ NTTIME ret = gen_nttime(); if (gen_chance(98)) ret = 0; return ret;}/* generate a file allocation size*/static uint_t gen_alloc_size(void){ uint_t ret; if (gen_chance(30)) return 0; ret = random() % 4*1024*1024; /* give a high chance of a round number */ if (gen_chance(60)) { ret &= ~(1024*1024 - 1); } return ret;}/* generate an ea_struct*/static struct ea_struct gen_ea_struct(void){ struct ea_struct ea; const char *names[] = {"EAONE", "", "FOO!", " WITH SPACES ", ".", "AVERYLONGATTRIBUTENAME"}; const char *values[] = {"VALUE1", "", "NOT MUCH FOO", " LEADING SPACES ", ":", "ASOMEWHATLONGERATTRIBUTEVALUE"}; int i; ZERO_STRUCT(ea); do { i = gen_int_range(0, ARRAY_SIZE(names)-1); } while (ignore_pattern(names[i])); ea.name.s = names[i]; do { i = gen_int_range(0, ARRAY_SIZE(values)-1); } while (ignore_pattern(values[i])); ea.value = data_blob(values[i], strlen(values[i])); if (gen_chance(10)) ea.flags = gen_bits_mask(0xFF); ea.flags = 0; return ea;}/* generate an ea_struct*/static struct smb_ea_list gen_ea_list(void){ struct smb_ea_list eas; int i; if (options.no_eas) { ZERO_STRUCT(eas); return eas; } eas.num_eas = gen_int_range(0, 3); eas.eas = talloc_array(current_op.mem_ctx, struct ea_struct, eas.num_eas); for (i=0;i<eas.num_eas;i++) { eas.eas[i] = gen_ea_struct(); } return eas;}/* generate a security descriptor */static struct security_descriptor *gen_sec_desc(void){ struct security_descriptor *sd; if (options.no_acls || gen_chance(90)) return NULL; sd = security_descriptor_dacl_create(current_op.mem_ctx, 0, NULL, NULL, NULL, SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_FILE_WRITE_DATA | SEC_STD_WRITE_DAC, SEC_ACE_FLAG_OBJECT_INHERIT, SID_WORLD, SEC_ACE_TYPE_ACCESS_ALLOWED, SEC_FILE_ALL | SEC_STD_ALL, 0, NULL); return sd;}static void oplock_handler_close_recv_smb(struct smbcli_request *req){ NTSTATUS status; status = smbcli_request_simple_recv(req); if (!NT_STATUS_IS_OK(status)) { printf("close failed in oplock_handler\n"); smb_panic("close failed in oplock_handler"); }}/* the oplock handler will either ack the break or close the file*/static bool oplock_handler_smb(struct smbcli_transport *transport, uint16_t tid, uint16_t fnum, uint8_t level, void *private){ union smb_close io; int i, j; bool do_close; struct smbcli_tree *tree = NULL; struct smbcli_request *req; srandom(current_op.seed); do_close = gen_chance(50); for (i=0;i<NSERVERS;i++) { for (j=0;j<NINSTANCES;j++) { if (transport == servers[i].smb_tree[j]->session->transport && tid == servers[i].smb_tree[j]->tid) { oplocks[i][j].got_break = true; oplocks[i][j].smb_handle = fnum; oplocks[i][j].handle = fnum_to_handle_smb(i, j, fnum); oplocks[i][j].level = level; oplocks[i][j].do_close = do_close; tree = servers[i].smb_tree[j]; } } } if (!tree) { printf("Oplock break not for one of our trees!?\n"); return false; } if (!do_close) { printf("oplock ack fnum=%d\n", fnum); return smbcli_oplock_ack(tree, fnum, level); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -