# -*- Fundamental -*- # smb_nt.inc # $Revision$ # function kb_smb_name() { return string(get_kb_item("SMB/name")); } function kb_smb_domain() { return string(get_kb_item("SMB/domain")); } function kb_smb_login() { return string(get_kb_item("SMB/login")); } function kb_smb_password() { return string(get_kb_item("SMB/password")); } function kb_smb_transport() { return get_kb_item("SMB/transport"); } #-----------------------------------------------------------------# # Reads a SMB packet # #-----------------------------------------------------------------# function smb_recv(socket, length) { ____msg_header = recv(socket:socket, length:4, min:4); if (strlen(____msg_header) < 4)return(NULL); ____msg_trailer_length = 256 * ord(____msg_header[2]); ____msg_trailer_length = ____msg_trailer_length + ord(____msg_header[3]); if (____msg_trailer_length == 0)return(____msg_header); ____msg_trailer = recv(socket:socket, length:____msg_trailer_length, min:____msg_trailer_length); if(strlen(____msg_trailer) < ____msg_trailer_length)return(NULL); return(____msg_header+____msg_trailer); } #-----------------------------------------------------------------# # Convert a netbios name to the netbios network format # #-----------------------------------------------------------------# function netbios_name(orig) { ret = ""; len = strlen(orig); for(i=0;i<16;i=i+1) { if(i >= len) { c = "CA"; } else { o = ord(orig[i]); odiv = o/16; odiv = odiv + ord("A"); omod = o%16; omod = omod + ord("A"); c = raw_string(odiv, omod); } ret = ret+c; } return(ret); } #--------------------------------------------------------------# # Returns the netbios name of a redirector # #--------------------------------------------------------------# function netbios_redirector_name() { ret = crap(data:"CA", length:30); ret = ret+"AA"; return(ret); } #-------------------------------------------------------------# # return a 28 + strlen(data) + (odd(data)?0:1) long string # #-------------------------------------------------------------# function unicode(data) { len = strlen(data); ret = raw_string(ord(data[0])); for(i=1;i is writeable # #---------------------------------------------------------------------# function registry_get_acl(key) { local_var name, domain, _smb_port, login, pass, soc, r, uid, tid, pipe, ret, prot; name = kb_smb_name(); if(!name)exit(0); domain = kb_smb_domain(); _smb_port = kb_smb_transport(); if(!_smb_port)exit(0); if(!get_port_state(_smb_port))return(FALSE); login = kb_smb_login(); pass = kb_smb_password(); if(!login)login = ""; if(!pass) pass = ""; soc = open_sock_tcp(_smb_port); # # Request the session # r = smb_session_request(soc:soc, remote:name); if(!r)return(FALSE); # # Negociate the protocol # prot = smb_neg_prot(soc:soc); if(!prot)return(FALSE); # # Set up our session # r = smb_session_setup(soc:soc, login:login, password:pass, domain:domain, prot:prot); if(!r)return(FALSE); # and extract our uid uid = session_extract_uid(reply:r); # # Connect to the remote IPC and extract the TID # we are attributed # r = smb_tconx(soc:soc, name:name, uid:uid, share:"IPC$"); # and extract our tree id tid = tconx_extract_tid(reply:r); if(!tid)return(NULL); # # Create a pipe to \winreg # r = smbntcreatex(soc:soc, uid:uid, tid:tid); if(!r)return(NULL); # and extract its ID pipe = smbntcreatex_extract_pipe(reply:r); # # Setup things # r = pipe_accessible_registry(soc:soc, uid:uid, tid:tid, pipe:pipe); if(!r)return(FALSE); r = registry_access_step_1(soc:soc, uid:uid, tid:tid, pipe:pipe); if(strlen(key)) { r2 = registry_get_key(soc:soc, uid:uid, tid:tid, pipe:pipe, key:key, reply:r); } else r2 = r; if(r2) { r3 = registry_get_key_security(soc:soc, uid:uid, tid:tid, pipe:pipe, reply:r2); close(soc); if(strlen(r3) < 100)return(NULL); return(r3); } return(NULL); } #---------------------------------------------------------------------# # Get an item of type reg_sz from the key # #---------------------------------------------------------------------# function unicode2(data) { len = strlen(data); ret = raw_string(0, ord(data[0])); for(i=1;i0;i=i-1) { t *= 256; t += ord(data[index+i-1]); } return(t); } #---------------------------------------------------------------------# # registry_get_dword() # #---------------------------------------------------------------------# function registry_get_dword(key, item) { local_var name, port, login, pass, soc, dom, r, prot, value; port = kb_smb_transport(); if(!port)exit(0); name = kb_smb_name(); if(!name)exit(0); if(!get_port_state(port))return(FALSE); login = kb_smb_login(); pass = kb_smb_password(); if(!login)login = ""; if(!pass) pass = ""; dom = kb_smb_domain(); soc = open_sock_tcp(port); if(!soc)exit(0); # # Request the session # r = smb_session_request(soc:soc, remote:name); if(!r)return NULL; # # Negociate the protocol # prot = smb_neg_prot(soc:soc); if(!prot) return NULL; # # Set up our session # r = smb_session_setup(soc:soc, login:login, password:pass, domain:dom, prot:prot); if(!r)return NULL; # and extract our uid uid = session_extract_uid(reply:r); # # Connect to the remote IPC and extract the TID # we are attributed # r = smb_tconx(soc:soc, name:name, uid:uid, share:"IPC$"); # and extract our tree id tid = tconx_extract_tid(reply:r); # # Create a pipe to \winreg # r = smbntcreatex(soc:soc, uid:uid, tid:tid); if(!r)return(NULL); # and extract its ID pipe = smbntcreatex_extract_pipe(reply:r); # # Setup things # r = pipe_accessible_registry(soc:soc, uid:uid, tid:tid, pipe:pipe); if(!r)return(NULL); r = registry_access_step_1(soc:soc, uid:uid, tid:tid, pipe:pipe); r2 = registry_get_key(soc:soc, uid:uid, tid:tid, pipe:pipe, key:key, reply:r); if(r2) { r3 = registry_get_item_dword(soc:soc, uid:uid, tid:tid, pipe:pipe, item:item, reply:r2); value = registry_decode_dword(data:r3); return(value); } } #---------------------------------------------------------------------# # registry_get_sz() # #---------------------------------------------------------------------# function registry_get_sz(key, item) { name = kb_smb_name(); if(!name)exit(0); _smb_port = kb_smb_transport(); if(!_smb_port)exit(0); if(!get_port_state(_smb_port))return(FALSE); login = kb_smb_login(); pass = kb_smb_password(); domain = kb_smb_domain(); if(!login)login = ""; if(!pass) pass = ""; soc = open_sock_tcp(_smb_port); if(!soc)return(FALSE); # # Request the session # r = smb_session_request(soc:soc, remote:name); if(!r)return(FALSE); # # Negociate the protocol # prot = smb_neg_prot(soc:soc); if(!prot)return(FALSE); # # Set up our session # r = smb_session_setup(soc:soc, login:login, password:pass, domain:domain, prot:prot); if(!r)return(FALSE); # and extract our uid uid = session_extract_uid(reply:r); # # Connect to the remote IPC and extract the TID # we are attributed # r = smb_tconx(soc:soc, name:name, uid:uid, share:"IPC$"); # and extract our tree id tid = tconx_extract_tid(reply:r); if(!tid)return(FALSE); # # Create a pipe to \winreg # r = smbntcreatex(soc:soc, uid:uid, tid:tid); if(!r)return(FALSE); # and extract its ID pipe = smbntcreatex_extract_pipe(reply:r); # # Setup things # r = pipe_accessible_registry(soc:soc, uid:uid, tid:tid, pipe:pipe); if(!r)return(FALSE); r = registry_access_step_1(soc:soc, uid:uid, tid:tid, pipe:pipe); r2 = registry_get_key(soc:soc, uid:uid, tid:tid, pipe:pipe, key:key, reply:r); if(r2) { r3 = registry_get_item_sz(soc:soc, uid:uid, tid:tid, pipe:pipe, item:item, reply:r2); value = registry_decode_sz(data:r3); close(soc); return(value); } return(FALSE); } #---------------------------------------------------------------------------# # SAM related functions # #---------------------------------------------------------------------------# #------------------------------------------------------# # Open a pipe to \samr # #------------------------------------------------------# function OpenPipeToSamr(soc, uid, tid) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; req = raw_string(0x00, 0x00, 0x00, 0x60, 0xFF, 0x53, 0x4D, 0x42, 0xA2, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x18, 0xFF, 0x00, 0xDE, 0xDE, 0x00, 0x0A, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9F, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x0D, 0x00, 0x00, 0x5C, 0x00, 0x73, 0x00, 0x61, 0x00, 0x6D, 0x00, 0x72, 0x00, 0x00, 0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 42) return(FALSE); else { low = ord(r[42]); hi = ord(r[43]); ret = hi * 256; ret = ret + low; return(ret); } } function samr_smbwritex(soc, tid, uid, pipe) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; req = raw_string(0x00, 0x00, 0x00, 0x88, 0xFF, 0x53, 0x4D, 0x42, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x0E, 0xFF, 0x00, 0xDE, 0xDE, pipe_lo, pipe_hi, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x00, 0x48, 0x00, 0x00, 0x00, 0x48, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x00, 0xEE, 0x05, 0x00, 0x0B, 0x03, 0x10, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xB8, 0x10, 0xB8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x78, 0x57, 0x34, 0x12, 0x34, 0x12, 0xCD, 0xAB, 0xEF, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAC, 0x01, 0x00, 0x00, 0x00, 0x04, 0x5D, 0x88, 0x8A, 0xEB, 0x1C, 0xC9, 0x11, 0x9F, 0xE8, 0x08, 0x00, 0x2B, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); } function samr_smbreadx(soc, tid, uid, pipe) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; req = raw_string(0x00, 0x00, 0x00, 0x3B, 0xFF, 0x53, 0x4D, 0x42, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x0C, 0xFF, 0x00, 0xDE, 0xDE, pipe_lo, pipe_hi, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); } #------------------------------------------------------# # Returns the unicode representation of # #------------------------------------------------------# function samr_uc(name) { ret = ""; for(i=0;i", strlen(r), "\n"); # # We return a handle to the remote SAM # samrhdl = ""; _len = strlen(r); if(_len < 24) return(FALSE); _len = _len - 24; for(i=0;i<20;i=i+1) { samrhdl = samrhdl + raw_string(ord(r[i+_len])); #display(hex(ord(r[i+_len])), " "); } #display("\n"); #display("samhdl : ", strlen(samrhdl), "\n"); return(samrhdl); } #--------------------------------------------------------------# # This function is probably SamrEnumerateDomainsInSamServer() # # but I'm not sure of that, so I changed its name to # # _SamrEnumDomains() # # # # This function only returns the first domain it obtains # #--------------------------------------------------------------# function _SamrEnumDomains(soc, uid, tid, pipe, samrhdl) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; req = raw_string(0x00, 0x00, 0x00, 0x88, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x10, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x34, 0x00, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, 0x45, 0x00, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0xAF, 0x47, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00) + samrhdl + raw_string(0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 137)return(FALSE); len_lo = ord(r[136]); len_hi = ord(r[137]); len = len_hi * 256; len = len + len_lo; dom = ""; len = len*2; maxlen = strlen(r); if(maxlen < len)return(FALSE); for(i=0;i # #------------------------------------------------------# function SamrDom2Sid(soc, tid, uid, pipe, samrhdl, dom) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; #display(strlen(dom), "<-dom\n"); tot_len = 148 + strlen(dom); tot_len_hi = tot_len / 256; tot_len_lo = tot_len % 256; bcc = 81 + strlen(dom); bcc_lo = bcc % 256; bcc_hi = bcc / 256; tot_dat_count = 64 + strlen(dom); tot_dat_count_lo = tot_dat_count % 256; tot_dat_count_hi = tot_dat_count / 256; dom_len = strlen(dom); dom_len = dom_len / 2; dom_len_lo = dom_len % 256; dom_len_hi = dom_len / 256; dom_t_len = dom_len + 1; dom_t_len_lo = dom_t_len % 256; dom_t_len_hi = dom_t_len / 256; dom_m_len = dom_len * 2; dom_m_len_lo = dom_m_len % 256; dom_m_len_hi = dom_m_len / 256; dom_mm_len = dom_m_len + 2; dom_mm_len_lo = dom_mm_len % 256; dom_mm_len_hi = dom_mm_len / 256; req = raw_string(0x00, 0x00, tot_len_hi, tot_len_lo, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x10, 0x00, 0x00, tot_dat_count_lo, tot_dat_count_hi, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, tot_dat_count_lo, tot_dat_count_hi, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, bcc_lo, bcc_hi, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0xAF, 0x47, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, tot_dat_count_lo, tot_dat_count_hi, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00) + samrhdl + raw_string( dom_m_len_lo, dom_m_len_hi, dom_mm_len_lo, dom_mm_len_hi, 0x40, 0x7B, 0x13, 0x00, dom_t_len_lo, dom_t_len_hi, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, dom_len_lo, dom_len_hi, 0x00) + dom + raw_string(0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 88)return(FALSE); #display(ord(r[88]), "\n"); _sid = ""; for(i=0;i<28;i=i+1) { _sid = _sid + raw_string(ord(r[88+i])); #display(hex(ord(r[88+i])), " "); } #display("\n"); return(_sid); } #------------------------------------------------------# # Opens a policy handle to a given domain # #------------------------------------------------------# function SamrOpenDomain(soc, tid, uid, pipe, samrhdl, sid) { #display("sid = ", strlen(sid), "\n"); tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; len = 132 + strlen(sid); len_h = len / 256; len_l = len % 256; tdc = 48 + strlen(sid); tdc_l = tdc % 256; tdc_h = tdc / 256; bcc = tdc + 17; bcc_l = bcc % 256; bcc_h = bcc / 256; req = raw_string(0x00, 0x00, 0x00, 0xA0, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x10, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x4C, 0x00, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, 0x5D, 0x00, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x33, 0x00, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, 0x4C, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00) + samrhdl + raw_string(0x00, 0x02, 0x00, 0x00) + sid; send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 30) return(FALSE); #display(strlen(r),"\n"); samrhdl = ""; _len = strlen(r); _len = _len - 24; _z = 0; for(i=0;i<20;i=i+1) { if(ord(r[i+_len]) == 0)_z = _z + 1; samrhdl = samrhdl + raw_string(ord(r[i+_len])); #display(hex(ord(r[i+_len])), " "); } #display("\n"); #display("samhdl : ", strlen(samrhdl), "\n"); if(_z == 20)return(NULL); return(samrhdl); } function SamrOpenBuiltin(soc, tid, uid, pipe, samrhdl) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; req = raw_string(0x00, 0x00, 0x00, 0x94, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x80, 0x03, 0x10, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x40, 0x00, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, 0x51, 0x00, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00) + samrhdl + raw_string( 0x80, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); builtinhdl = ""; _len = strlen(r); _len = _len - 24; _z = 0; for(i=0;i<20;i=i+1) { if(ord(r[i+_len]) == 0)_z = _z + 1; builtinhdl = builtinhdl + raw_string(ord(r[i+_len])); #display(hex(ord(r[i+_len])), " "); } if(_z == 20)return(NULL); #display("\n"); #display("builtinhdl : ", strlen(builtinhdl), "\n"); return(builtinhdl); } #------------------------------------------------------# # Converts a username to its rid # #------------------------------------------------------# function SamrLookupNames(soc, uid, tid, pipe, name, domhdl) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; usr = samr_uc(name:name); len = 164 + strlen(usr); len_hi = len / 256; len_lo = len % 256; tdc = 80 + strlen(usr); tdc_l = tdc % 256; tdc_h = tdc / 256; bcc = tdc + 17; bcc_l = bcc % 256; bcc_h = bcc / 256; x = strlen(usr) / 2; x_h = x / 256; x_l = x % 256; y = x + 1; y_h = y / 256; y_l = y % 256; z = strlen(usr); z_l = z % 256; z_h = z / 256; t = z + 2; t_l = t % 256; t_h = t / 256; req = raw_string(0x00, 0x00, len_hi, len_lo, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x10, 0x00, 0x00, tdc_l, tdc_h, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, tdc_l, tdc_h, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, bcc_l, bcc_h, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0xAF, 0x47, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, tdc_l, tdc_h, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00) + domhdl + raw_string(0x01, 0x00, 0x00, 0x00, 0xE8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, z_l, z_h, t_l, t_h, 0xD8, 0x0E, 0x41, 0x00, y_l, y_h, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, x_l, x_h, 0x00) + usr + raw_string(0x00); send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 100)return(FALSE); _rid = ""; ##display("RID : "); _z = 0; for(i=0;i<4;i=i+1) { if(ord(r[96+i]) == 0)_z = _z + 1; # ##display(hex(ord(r[96+i])), " "); _rid = _rid + raw_string(ord(r[96+i])); } ##display("\n"); if(_z == 4)return(NULL); return(_rid); } #--------------------------------------------------------# # Opens a policy handle to a given user # #--------------------------------------------------------# function SamrOpenUser(soc, uid, tid, pipe, samrhdl, rid) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; len = 176; len_hi = len / 256; len_lo = len % 256; req = raw_string(0x00, 0x00, 0x00, 0x88, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x10, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x34, 0x00, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, 0x45, 0x00, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x33, 0x00, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22, 0x00) + samrhdl + raw_string(0x1B, 0x01, 0x02, 0x00) + rid; send(socket:soc, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 100)return(FALSE); _usrhdl = ""; _len = strlen(r); _len = _len - 24; #display("usrhdl = "); _z = 0; for(i=0;i<20;i=i+1) { if(ord(r[i+_len]) == 0)_z = _z + 1; _usrhdl = _usrhdl + raw_string(ord(r[i+_len])); #display(hex(ord(r[i+_len])), " "); } if(_z == 20)return(NULL); #display("\n"); return(_usrhdl); } #-------------------------------------------------------# # Requests the list of groups to which the user belongs # # to # #-------------------------------------------------------# function SamrQueryUserGroups(soc, uid, tid, pipe, usrhdl) { tid_hi = tid / 256; tid_lo = tid % 256; uid_hi = uid / 256; uid_lo = uid % 256; pipe_hi = pipe / 256; pipe_lo = pipe % 256; req = raw_string(0x00, 0x00, 0x00, 0x80, 0xFF, 0x53, 0x4D, 0x42, 0x25, 0x00, 0x00, 0x00, 0x00, 0x18, 0x07, 0xC8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x10, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x2C, 0x00, 0x54, 0x00, 0x02, 0x00, 0x26, 0x00, pipe_lo, pipe_hi, 0x3D, 0x00, 0x00, 0x5C, 0x00, 0x50, 0x00, 0x49, 0x00, 0x50, 0x00, 0x45, 0x00, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x00) + usrhdl; send(socket:soc, data:req); r = recv(socket:soc, length:4096); num_lo = ord(r[88]); num_hi = ord(r[89]); num = num_hi * 256; num = num + num_lo; # # Ok. Our user is in groups. Let's decode their RID # if(strlen(r) < 103) return(FALSE); base = 100; rids = ""; for(i=0;i", strlen(r), "<====\n"); return(FALSE); } num_lo = ord(r[92]); num_hi = ord(r[93]); num = num_hi * 256; num = num + num_lo; #display("NUM EGAL : ", num, "\n"); base = 96; rids = ""; for(i=0;i # function OpenAndX(socket, uid, tid, file) { local_var req, tid_lo, tid_hi, uid_lo, uid_hi, len_lo, len_hi, rep; local_var fid_lo, fid_hi; len_lo = (66 + strlen(file)) % 256; len_hi = (66 + strlen(file)) / 256; tid_lo = tid % 256; tid_hi = tid / 256; uid_lo = uid % 256; uid_hi = uid / 256; bcc_lo = strlen(file) % 256; bcc_hi = strlen(file) / 256; req = raw_string(0x00, 0x00, len_hi, len_lo, 0xFF, 0x53, 0x4D, 0x42, 0x2D, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x0F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, bcc_lo, bcc_hi) + file + raw_string(0x00); send(socket:soc, data:req); rep = smb_recv(socket:socket, length:4096); if(strlen(rep) < 65)return(NULL); else { fid_lo = ord(rep[41]); fid_hi = ord(rep[42]); return(fid_lo + (fid_hi * 256)); } } # # Read bytes at offset # function ReadAndX(socket, uid, tid, fid, count, off) { local_var r, req, uid_lo, uid_hi, tid_lo, tid_hi, fid_lo, fid_hi, off_hi, off_lo, ret, i; uid_lo = uid % 256; uid_hi = uid / 256; tid_lo = tid % 256; tid_hi = tid / 256; fid_lo = fid % 256; fid_hi = fid / 256; cnt_lo = count % 256; cnt_hi = count / 256; off_lo_lo = off % 256; off /= 256; off_lo_hi = off % 256; off /= 256; off_hi_lo = off % 256; off /= 256; off_hi_hi = off; req = raw_string(0x00, 0x00, 0x00, 0x37, 0xFF, 0x53, 0x4D, 0x42, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x0A, 0xFF, 0x00, 0x00, 0x00, fid_lo, fid_hi, off_lo_lo, off_lo_hi, off_hi_lo, off_hi_hi, cnt_lo, cnt_hi, cnt_lo, cnt_hi, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00); send(socket:socket, data:req); r = smb_recv(socket:socket, length:65535); ret = ""; if(strlen(r) < 36 + 28)return(NULL); return substr(r, 35+28, strlen(r) - 1); } # Returns the size of the file pointed by function smb_get_file_size(socket, uid, tid, fid) { local_var r, req, uid_lo, uid_hi, tid_lo, tid_hi, fid_lo, fid_hi, ret; uid_lo = uid % 256; uid_hi = uid / 256; tid_lo = tid % 256; tid_hi = tid / 256; fid_lo = fid % 256; fid_hi = fid / 256; req = raw_string(0x00, 0x00, 0x00, 0x48, 0xFF, 0x53, 0x4D, 0x42, 0x32, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, tid_lo, tid_hi, 0x00, 0x28, uid_lo, uid_hi, 0x00, 0x00, 0x0F, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x44, 0x00, 0x00, 0x00, 0x48, 0x00, 0x01, 0x00, 0x07, 0x00, 0x07, 0x00, 0x00, 0x44, 0x20, fid_lo, fid_hi, 0x07, 0x01); send(socket:socket, data:req); r = smb_recv(socket:soc, length:4096); if(strlen(r) < 112) return -1; ret = ord(r[115]); ret = ret * 256 + ord(r[114]); ret = ret * 256 + ord(r[113]); ret = ret * 256 + ord(r[112]); return ret; } # # Gives the listing in the pattern # If pattern is set to NULL, then we return the # content of the root (\*) # function FindFirst2(socket, uid, tid, pattern) { local_var uid_lo, uid_hi, tid_lo, tid_hi, r; local_var t, nxt, off, name, ret, bcc, bcc_lo, bcc_hi; local_var len, len_lo, len_hi; local_var unicode_pattern, i; local_var data_off, data_off_lo, data_off_hi, bcc2, bcc2_lo, bcc2_hi; if(isnull(pattern))pattern = "\*"; for(i=0;i strlen(r))break; for(i=0;i<4;i++) { nxt += ord(r[off+i]) * t; t *= 256; } t = 1; len = 0; if( off+4+4+8+8+8+8+8+8+4+i+4 > strlen(r))break; for(i=0;i<4;i++) { len += ord(r[off+4+4+8+8+8+8+8+8+4+i]) * t; t *= 256; } if(len > strlen(r))break; name = NULL; if(off+4+4+8+8+8+8+8+8+4+4+4+1+1+24+i+len > strlen(r)) break; for(i=0;i= strlen(r)) || off < 0 )return ret; } return ret; }