mirror of
https://github.com/aljazceru/cowrie.git
synced 2026-02-23 07:14:24 +01:00
Added some assertEquals to shell commands (RegExp) (#199)
This commit is contained in:
committed by
Michel Oosterhof
parent
4075e55c3d
commit
fc8620a2da
97
cowrie/test/expected_results.json
Normal file
97
cowrie/test/expected_results.json
Normal file
@@ -0,0 +1,97 @@
|
||||
{
|
||||
"results": {
|
||||
"whoami": "root\nroot@unitTest:~# ",
|
||||
"users": "root\nroot@unitTest:~# ",
|
||||
"alias": [ "root@unitTest:~# " ],
|
||||
"export": [ "root@unitTest:~# " ],
|
||||
"chattr": [ "root@unitTest:~# " ],
|
||||
"chgrp": [ "root@unitTest:~# " ],
|
||||
"chown": [ "root@unitTest:~# " ],
|
||||
"jobs": [ "root@unitTest:~# " ],
|
||||
"kill": [ "root@unitTest:~# " ],
|
||||
"su": [ "root@unitTest:~# " ],
|
||||
"jobs": [ "root@unitTest:~# " ],
|
||||
"php": [ "root@unitTest:~# " ],
|
||||
"python": [ "root@unitTest:~# " ],
|
||||
"unset": [ "root@unitTest:~# " ],
|
||||
"set": [ "root@unitTest:~# " ],
|
||||
"umask": [ "root@unitTest:~# " ],
|
||||
"id" : [ "uid=0(root) gid=0(root) groups=0(root)\nroot@unitTest:~# " ],
|
||||
"help": [
|
||||
"GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)",
|
||||
"These shell commands are defined internally. Type `help' to see this list.",
|
||||
"Type `help name' to find out more about the function `name'.",
|
||||
"Use `info bash' to find out more about the shell in general.",
|
||||
"Use `man -k' or `info' to find out more about commands not in this list.",
|
||||
"",
|
||||
"A star (*) next to a name means that the command is disabled.",
|
||||
"",
|
||||
" job_spec [&] history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]",
|
||||
" (( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi",
|
||||
" . filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x command [args]",
|
||||
" : kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]",
|
||||
" [ arg... ] let arg [arg ...]",
|
||||
" [[ expression ]] local [option] name[=value] ...",
|
||||
" alias [-p] [name[=value] ... ] logout [n]",
|
||||
" bg [job_spec ...] mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]",
|
||||
" bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-c> popd [-n] [+N | -N]",
|
||||
" break [n] printf [-v var] format [arguments]",
|
||||
" builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]",
|
||||
" caller [expr] pwd [-LP]",
|
||||
" case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout>",
|
||||
" cd [-L|[-P [-e]]] [dir] readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]>",
|
||||
" command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] or readonly -p",
|
||||
" compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [> return [n]",
|
||||
" complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist] [-F> select NAME [in WORDS ... ;] do COMMANDS; done",
|
||||
" compopt [-o|+o option] [-DE] [name ...] set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]",
|
||||
" continue [n] shift [n]",
|
||||
" coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]",
|
||||
" declare [-aAfFgilrtux] [-p] [name[=value] ...] source filename [arguments]",
|
||||
" dirs [-clpv] [+N] [-N] suspend [-f]",
|
||||
" disown [-h] [-ar] [jobspec ...] test [expr]",
|
||||
" echo [-neE] [arg ...] time [-p] pipeline",
|
||||
" enable [-a] [-dnps] [-f filename] [name ...] times",
|
||||
" eval [arg ...] trap [-lp] [[arg] signal_spec ...]",
|
||||
" exec [-cl] [-a name] [command [arguments ...]] [redirection ...] true",
|
||||
" exit [n] type [-afptP] name [name ...]",
|
||||
" export [-fn] [name[=value] ...] or export -p typeset [-aAfFgilrtux] [-p] name[=value] ...",
|
||||
" false ulimit [-SHacdefilmnpqrstuvx] [limit]",
|
||||
" fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command] umask [-p] [-S] [mode]",
|
||||
" fg [job_spec] unalias [-a] name [name ...]",
|
||||
" for NAME [in WORDS ... ] ; do COMMANDS; done unset [-f] [-v] [name ...]",
|
||||
" for (( exp1; exp2; exp3 )); do COMMANDS; done until COMMANDS; do COMMANDS; done",
|
||||
" function name { COMMANDS ; } or name () { COMMANDS ; } variables - Names and meanings of some shell variables",
|
||||
" getopts optstring name [arg] wait [id]",
|
||||
" hash [-lr] [-p pathname] [-dt] [name ...] while COMMANDS; do COMMANDS; done",
|
||||
" help [-dms] [pattern ...] { COMMANDS ; }",
|
||||
"root@unitTest:~# "
|
||||
],
|
||||
"date": [ "[A-Za-z][A-Za-z][A-Za-z] [A-Za-z][A-Za-z][A-Za-z] [0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] UTC [0-9][0-9][0-9][0-9]" ],
|
||||
"w": [
|
||||
" [0-9][0-9]:[0-9][0-9]:[0-9][0-9] up [0-9 ]+[a-zA-Z]+, +[0-9: ]+, [0-9 ]+user, +load average: [0-9., ]+",
|
||||
"USER +TTY +FROM + LOGIN@ +IDLE +JCPU +PCPU +WHAT",
|
||||
"[a-zA-Z]+ +pts\/[0-9] +[0-9.]+ +[0-9:]+ +[0-9s. ]+ +[a-zA-Z0-9 ]+",
|
||||
"root@unitTest:~#"
|
||||
],
|
||||
"who": [
|
||||
"root +pts\/0 +2[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-3][0-9]:[0-5][0-9][0-9(). ]+",
|
||||
"root@unitTest:~# "
|
||||
],
|
||||
"echo": [
|
||||
"\"test worked correctly\"",
|
||||
"root@unitTest:~# "
|
||||
],
|
||||
"hostname": [
|
||||
"root@unitChanged:~# "
|
||||
],
|
||||
"ps": [
|
||||
" PID TTY TIME COMMAND",
|
||||
"5673 pts/0 0:00 -bash",
|
||||
"5679 pts/0 0:00 ps ",
|
||||
"root@unitTest:~# "
|
||||
],
|
||||
"passwd": [ "Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully\nroot@unitTest:~# "
|
||||
],
|
||||
"history": [ " 1 history"]
|
||||
}
|
||||
}
|
||||
@@ -7,194 +7,194 @@ from twisted.trial import unittest
|
||||
|
||||
from cowrie.core import protocol
|
||||
from cowrie.core import config
|
||||
from . import fake_server
|
||||
from . import fake_transport
|
||||
import fake_server
|
||||
import fake_transport
|
||||
import json
|
||||
|
||||
|
||||
|
||||
class ShellBaseCommandsTests(unittest.TestCase):
|
||||
|
||||
|
||||
def setUp(self):
|
||||
with open('../cowrie/test/expected_results.json') as data_file:
|
||||
self.data = json.load(data_file)
|
||||
self.cfg = config.readConfigFile("../cowrie/test/unittests.cfg")
|
||||
self.proto = protocol.HoneyPotInteractiveProtocol \
|
||||
(fake_server.FakeAvatar(fake_server.FakeServer(self.cfg)))
|
||||
self.tr = fake_transport.FakeTransport("1.1.1.1", "1111")
|
||||
self.proto.makeConnection(self.tr)
|
||||
self.tr.clear()
|
||||
|
||||
|
||||
def test_whoami_command(self):
|
||||
self.proto.lineReceived('whoami \n')
|
||||
print(self.tr.value())
|
||||
self.assertEqual(self.tr.value(),self.data['results']['whoami'])
|
||||
|
||||
|
||||
def test_users_command(self):
|
||||
self.proto.lineReceived('users \n')
|
||||
print(self.tr.value())
|
||||
self.assertEqual(self.tr.value(),self.data['results']['users'])
|
||||
|
||||
|
||||
def test_help_command(self):
|
||||
self.proto.lineReceived('help \n')
|
||||
print(self.tr.value())
|
||||
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['help']))
|
||||
|
||||
def test_w_command(self):
|
||||
self.proto.lineReceived('w \n')
|
||||
print(self.tr.value())
|
||||
self.assertRegexpMatches(self.tr.value(),("\n").join(self.data['results']['w']))
|
||||
|
||||
|
||||
def test_who_command(self):
|
||||
self.proto.lineReceived('who \n')
|
||||
print(self.tr.value())
|
||||
self.assertRegexpMatches(self.tr.value(),"\n".join(self.data['results']['who']))
|
||||
|
||||
|
||||
def test_echo_command(self):
|
||||
self.proto.lineReceived('echo "test worked correctly" \n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['echo']))
|
||||
|
||||
|
||||
def test_exit_command(self):
|
||||
self.proto.lineReceived('exit \n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_logout_command(self):
|
||||
self.proto.lineReceived('logout \n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_clear_command(self):
|
||||
self.proto.lineReceived('clear \n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_reset_command(self):
|
||||
self.proto.lineReceived('hostname unitChanged\n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_hostname_command(self):
|
||||
self.proto.lineReceived('hostname unitChanged\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['hostname']))
|
||||
|
||||
|
||||
def test_reset_command(self):
|
||||
self.proto.lineReceived('reset')
|
||||
|
||||
|
||||
def test_ps_command(self):
|
||||
self.proto.lineReceived('ps\n')
|
||||
print(self.tr.value())
|
||||
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['ps']))
|
||||
|
||||
def test_id_command(self):
|
||||
self.proto.lineReceived('id\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['id']))
|
||||
|
||||
|
||||
def test_passwd_command(self):
|
||||
self.proto.lineReceived('passwd\n')
|
||||
self.proto.lineReceived('changeme\n')
|
||||
self.proto.lineReceived('changeme\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['passwd']))
|
||||
|
||||
|
||||
def test_shutdown_command(self):
|
||||
self.proto.lineReceived('shutdown\n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_poweroff_command(self):
|
||||
self.proto.lineReceived('poweroff\n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_history_command(self):
|
||||
self.proto.lineReceived('history\n')
|
||||
print(self.tr.value())
|
||||
self.proto.lineReceived("history\n")
|
||||
self.proto.lineReceived("history\n")
|
||||
# Not Sure HOW TO TEST THIS!!
|
||||
print self.tr.value()
|
||||
|
||||
|
||||
def test_date_command(self):
|
||||
self.proto.lineReceived('date\n')
|
||||
print(self.tr.value())
|
||||
self.assertRegexpMatches(self.tr.value(),("\n").join(self.data['results']['date']))
|
||||
|
||||
|
||||
def test_bash_command(self):
|
||||
self.proto.lineReceived('bash\n')
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
def test_sh_command(self):
|
||||
self.proto.lineReceived('sh\n')
|
||||
print(self.tr.value())
|
||||
self.proto.lineReceived('sh -c who\n')
|
||||
self.assertRegexpMatches(self.tr.value(),"\n".join(self.data['results']['who']))
|
||||
|
||||
|
||||
def test_php_command(self):
|
||||
self.proto.lineReceived('php\n')
|
||||
self.proto.lineReceived('php -h');
|
||||
print(self.tr.value())
|
||||
|
||||
|
||||
|
||||
def test_chattr_command(self):
|
||||
self.proto.lineReceived('chattr\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['chattr']))
|
||||
|
||||
|
||||
def test_umask_command(self):
|
||||
self.proto.lineReceived('umask\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['umask']))
|
||||
|
||||
|
||||
def test_set_command(self):
|
||||
self.proto.lineReceived('set\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['set']))
|
||||
|
||||
|
||||
def test_unset_command(self):
|
||||
self.proto.lineReceived('unset\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['unset']))
|
||||
|
||||
|
||||
def test_export_command(self):
|
||||
self.proto.lineReceived('export\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['export']))
|
||||
|
||||
|
||||
def test_alias_command(self):
|
||||
self.proto.lineReceived('alias\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['alias']))
|
||||
|
||||
|
||||
def test_jobs_command(self):
|
||||
self.proto.lineReceived('jobs\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['jobs']))
|
||||
|
||||
|
||||
def test_kill_command(self):
|
||||
self.proto.lineReceived('/bin/kill\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['kill']))
|
||||
|
||||
|
||||
def test_pkill_command(self):
|
||||
self.proto.lineReceived('/bin/pkill\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['kill']))
|
||||
|
||||
|
||||
def test_killall_command(self):
|
||||
self.proto.lineReceived('/bin/killall\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['kill']))
|
||||
|
||||
|
||||
def test_killall5_command(self):
|
||||
self.proto.lineReceived('/bin/killall5\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['kill']))
|
||||
|
||||
|
||||
def test_su_command(self):
|
||||
self.proto.lineReceived('su\n')
|
||||
print(self.tr.value())
|
||||
|
||||
self.assertEquals(self.tr.value(),"\67n".join(self.data['results']['su']))
|
||||
|
||||
def test_chown_command(self):
|
||||
self.proto.lineReceived('chown\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['chown']))
|
||||
|
||||
|
||||
def test_chgrp_command(self):
|
||||
self.proto.lineReceived('chgrp\n')
|
||||
print(self.tr.value())
|
||||
self.assertEquals(self.tr.value(),"\n".join(self.data['results']['chgrp']))
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
|
||||
Reference in New Issue
Block a user