2009-11-10 18:57:59 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2015-12-11 12:21:29 +00:00
|
|
|
###############################################################
|
|
|
|
# This program creates a cowrie file system pickle file.
|
|
|
|
#
|
|
|
|
# This is meant to build a brand new filesystem.
|
2016-07-14 07:04:12 +00:00
|
|
|
# To edit the file structure, please use 'bin/fsctl'
|
2015-12-11 12:21:29 +00:00
|
|
|
#
|
|
|
|
##############################################################
|
|
|
|
|
|
|
|
import os, pickle, sys, locale, getopt, fnmatch
|
2009-11-10 18:57:59 +00:00
|
|
|
from stat import *
|
|
|
|
|
2015-12-11 12:21:29 +00:00
|
|
|
|
2015-03-16 14:23:30 +00:00
|
|
|
A_NAME, A_TYPE, A_UID, A_GID, A_SIZE, A_MODE, \
|
|
|
|
A_CTIME, A_CONTENTS, A_TARGET, A_REALFILE = range(0, 10)
|
|
|
|
T_LINK, T_DIR, T_FILE, T_BLK, T_CHR, T_SOCK, T_FIFO = range(0, 7)
|
|
|
|
PROC = False
|
|
|
|
VERBOSE = False
|
2009-11-10 18:57:59 +00:00
|
|
|
|
2015-12-11 12:21:29 +00:00
|
|
|
blacklist_files = [
|
|
|
|
'/root/fs.pickle',
|
2016-07-14 07:04:12 +00:00
|
|
|
'/root/createfs',
|
2015-12-11 12:21:29 +00:00
|
|
|
'*cowrie*',
|
|
|
|
'*kippo*',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2015-03-16 14:23:30 +00:00
|
|
|
def logit(ftxt):
|
|
|
|
if VERBOSE:
|
|
|
|
sys.stderr.write(ftxt)
|
|
|
|
|
2015-12-11 12:21:29 +00:00
|
|
|
def checkblacklist(ftxt):
|
|
|
|
for value in blacklist_files:
|
|
|
|
if fnmatch.fnmatch(ftxt, value):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2015-03-16 14:23:30 +00:00
|
|
|
def recurse(localroot, root, tree, maxdepth = sys.maxint):
|
|
|
|
if maxdepth == 0: return
|
|
|
|
|
|
|
|
localpath = os.path.join(localroot, root[1:])
|
|
|
|
|
|
|
|
logit(' %s\n' % (localpath))
|
|
|
|
|
|
|
|
if not os.access(localpath, os.R_OK):
|
|
|
|
logit(' Cannot access %s\n' % localpath)
|
|
|
|
return
|
|
|
|
|
|
|
|
for name in os.listdir(localpath):
|
|
|
|
fspath = os.path.join(root, name)
|
2015-12-11 12:21:29 +00:00
|
|
|
if checkblacklist(fspath):
|
2009-11-10 18:57:59 +00:00
|
|
|
continue
|
|
|
|
|
2015-12-11 12:21:29 +00:00
|
|
|
|
2015-03-16 14:23:30 +00:00
|
|
|
path = os.path.join(localpath, name)
|
|
|
|
|
2010-10-25 15:11:50 +00:00
|
|
|
try:
|
|
|
|
if os.path.islink(path):
|
|
|
|
s = os.lstat(path)
|
|
|
|
else:
|
|
|
|
s = os.stat(path)
|
|
|
|
except OSError:
|
|
|
|
continue
|
2009-11-10 18:57:59 +00:00
|
|
|
|
|
|
|
entry = [name, T_FILE, s.st_uid, s.st_gid, s.st_size, s.st_mode, \
|
2014-10-04 19:22:27 +00:00
|
|
|
int(s.st_mtime), [], None, None]
|
2009-11-10 18:57:59 +00:00
|
|
|
|
2014-06-14 10:11:03 +00:00
|
|
|
if S_ISLNK(s[ST_MODE]):
|
2015-03-16 14:23:30 +00:00
|
|
|
if not os.access(path, os.R_OK):
|
|
|
|
logit(' Cannot access link: %s\n' % path)
|
|
|
|
continue
|
|
|
|
realpath = os.path.realpath(path)
|
|
|
|
if not realpath.startswith(localroot):
|
|
|
|
logit(' Link "%s" has real path "%s" outside local root "%s"\n' \
|
|
|
|
% (path, realpath, localroot))
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
entry[A_TYPE] = T_LINK
|
|
|
|
entry[A_TARGET] = realpath[len(localroot):]
|
2009-11-10 18:57:59 +00:00
|
|
|
elif S_ISDIR(s[ST_MODE]):
|
|
|
|
entry[A_TYPE] = T_DIR
|
2015-03-16 14:23:30 +00:00
|
|
|
if (PROC or not localpath.startswith('/proc/')) and maxdepth > 0:
|
|
|
|
recurse(localroot, fspath, entry[A_CONTENTS], maxdepth - 1)
|
2009-11-10 18:57:59 +00:00
|
|
|
elif S_ISREG(s[ST_MODE]):
|
|
|
|
entry[A_TYPE] = T_FILE
|
|
|
|
elif S_ISBLK(s[ST_MODE]):
|
|
|
|
entry[A_TYPE] = T_BLK
|
|
|
|
elif S_ISCHR(s[ST_MODE]):
|
|
|
|
entry[A_TYPE] = T_CHR
|
|
|
|
elif S_ISSOCK(s[ST_MODE]):
|
|
|
|
entry[A_TYPE] = T_SOCK
|
|
|
|
elif S_ISFIFO(s[ST_MODE]):
|
|
|
|
entry[A_TYPE] = T_FIFO
|
|
|
|
else:
|
|
|
|
sys.stderr.write('We should handle %s' % path)
|
2014-06-14 10:11:03 +00:00
|
|
|
sys.exit(1)
|
2009-11-10 18:57:59 +00:00
|
|
|
|
|
|
|
tree.append(entry)
|
|
|
|
|
2015-03-16 14:23:30 +00:00
|
|
|
def help(brief = False):
|
|
|
|
print 'Usage: %s [-h] [-v] [-p] [-l dir] [-d maxdepth] [-o file]\n' % \
|
|
|
|
os.path.basename(sys.argv[0])
|
|
|
|
|
|
|
|
if not brief:
|
|
|
|
print ' -v verbose'
|
|
|
|
print ' -p include /proc'
|
|
|
|
print ' -l <dir> local root directory (default is current working directory)'
|
|
|
|
print ' -d <depth> maximum depth (default is full depth)'
|
|
|
|
print ' -o <file> write output to file instead of stdout'
|
|
|
|
print ' -h display this help\n'
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
2009-11-10 18:57:59 +00:00
|
|
|
if __name__ == '__main__':
|
2015-03-16 14:23:30 +00:00
|
|
|
maxdepth = sys.maxint
|
|
|
|
localroot = os.getcwd()
|
|
|
|
output = ''
|
|
|
|
|
|
|
|
try:
|
|
|
|
optlist, args = getopt.getopt(sys.argv[1:], 'hvpl:d:o:', ['help'])
|
|
|
|
except getopt.GetoptError, error:
|
|
|
|
sys.stderr.write('Error: %s\n' % error)
|
|
|
|
help()
|
|
|
|
|
|
|
|
for o, a in optlist:
|
|
|
|
if o == '-v': VERBOSE = True
|
|
|
|
elif o == '-p': PROC = True
|
|
|
|
elif o == '-l': localroot = a
|
|
|
|
elif o == '-d': maxdepth = int(a)
|
|
|
|
elif o == '-o': output = a
|
|
|
|
elif o in ['-h', '--help']: help()
|
|
|
|
|
|
|
|
if output and os.path.isfile(output):
|
|
|
|
sys.stderr.write('File: %s exists!\n' % output)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
logit('Processing:\n')
|
2009-11-10 18:57:59 +00:00
|
|
|
|
|
|
|
tree = ['/', T_DIR, 0, 0, 0, 0, 0, [], '']
|
2015-03-16 14:23:30 +00:00
|
|
|
recurse(localroot, '/', tree[A_CONTENTS], maxdepth)
|
2009-11-10 18:57:59 +00:00
|
|
|
|
2015-03-16 14:23:30 +00:00
|
|
|
if output:
|
|
|
|
pickle.dump(tree, open(output, 'wb'))
|
|
|
|
else:
|
|
|
|
print pickle.dumps(tree)
|