[Draft] Add audit analyzer tool for demonstration for/drafts/demo
authorseolheui, kim <s414.kim@samsung.com>
Fri, 3 Aug 2018 08:51:52 +0000 (17:51 +0900)
committerseolheui, kim <s414.kim@samsung.com>
Fri, 3 Aug 2018 09:13:28 +0000 (18:13 +0900)
Change-Id: I717cb6b33d040a242a47a1a3e17ad0e8dfb2fc55
Signed-off-by: seolheui, kim <s414.kim@samsung.com>
packaging/audit-trail.spec
tools/CMakeLists.txt
tools/sample/analysis/CMakeLists.txt [new file with mode: 0644]
tools/sample/analysis/analyzer.cpp [new file with mode: 0644]
tools/sample/analysis/analyzer.h [new file with mode: 0644]
tools/sample/analysis/main.cpp [new file with mode: 0644]
tools/sample/analysis/type.h [new file with mode: 0644]

index 5437848f9d4aafecea57e392f7812624fd548526..e8264f63bdaaf95b48bb6c6aeb2fd195b1fefd37 100755 (executable)
@@ -146,3 +146,16 @@ The audit-trail-tests package contains the testcases needed to test audit functi
 %attr(700,root,root) %{_sbindir}/audit-trail-rules-test
 %attr(700,root,root) %{_sbindir}/audit-trail-overhead-test
 %{audit_base_dir}/test_module.ko
+
+%package -n audit-trail-sample
+Summary: Sample tools for audit trail demonstration
+Group: Security/Testing
+Requires: %{name} = %{version}-%{release}
+
+%description -n audit-trail-sample
+The audit-trail-sample package contains test tools for demonstration
+
+%files -n audit-trail-sample
+%manifest audit-trail.manifest
+%defattr(644,root,root,755)
+%attr(700,root,root) %{_sbindir}/audit-analyzer
index 416c4b2837556b36d1c41b37ec23e82dafcfab5a..f149581d3199f302fbaeb1f13305e31f7b63ec7c 100755 (executable)
@@ -15,6 +15,8 @@
 #
 SET(AUDIT_TRAIL_CLI  ${AUDIT_TRAIL_TOOLS}/cli)
 SET(AUDIT_TRAIL_TEST  ${AUDIT_TRAIL_TOOLS}/tests)
+SET(AUDIT_TRAIL_SAMPLE ${AUDIT_TRAIL_TOOLS}/sample/analysis)
 
 ADD_SUBDIRECTORY(${AUDIT_TRAIL_CLI})
 ADD_SUBDIRECTORY(${AUDIT_TRAIL_TEST})
+ADD_SUBDIRECTORY(${AUDIT_TRAIL_SAMPLE})
diff --git a/tools/sample/analysis/CMakeLists.txt b/tools/sample/analysis/CMakeLists.txt
new file mode 100644 (file)
index 0000000..54203de
--- /dev/null
@@ -0,0 +1,31 @@
+#
+# Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+SET(SERVER_SRCS        main.cpp analyzer.cpp)
+SET(DEPENDENCY klay
+                               glib-2.0
+)
+SET(SERVER_NAME                audit-analyzer)
+
+ADD_EXECUTABLE(${SERVER_NAME} ${SERVER_SRCS})
+PKG_CHECK_MODULES(SERVER_DEPS REQUIRED ${DEPENDENCY})
+
+SET_TARGET_PROPERTIES(${SERVER_NAME} PROPERTIES COMPILE_FLAGS "-fPIE")
+SET_TARGET_PROPERTIES(${SERVER_NAME} PROPERTIES LINK_FLAGS "-pie")
+
+INCLUDE_DIRECTORIES(SYSTEM ${SERVER_DEPS_INCLUDE_DIRS} ${AUDIT_TRAIL_LIB})
+TARGET_LINK_LIBRARIES(${SERVER_NAME} ${SERVER_DEPS_LIBRARIES} ${PROJECT_NAME} audit-trail)
+
+INSTALL(TARGETS ${SERVER_NAME} DESTINATION sbin)
diff --git a/tools/sample/analysis/analyzer.cpp b/tools/sample/analysis/analyzer.cpp
new file mode 100644 (file)
index 0000000..c900bcf
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <iostream> //TBD: removed
+#include "analyzer.h"
+
+AuditAnalyzer::AuditAnalyzer()
+       : auditTrail(nullptr), callbackId(0)
+{
+}
+
+AuditAnalyzer::~AuditAnalyzer()
+{
+       terminate();
+}
+
+void AuditAnalyzer::run()
+{
+       //start analyzer
+       runMonitor();
+       mainloop.run();
+}
+
+void AuditAnalyzer::terminate()
+{
+       //terminate analyzer
+       ::audit_trail_remove_system_log_cb(auditTrail, callbackId);
+       ::audit_trail_destroy(auditTrail);
+       mainloop.stop();
+}
+
+void AuditAnalyzer::runMonitor()
+{
+       //add callback to get system logs
+       audit_trail_create(&auditTrail);
+       if (auditTrail == nullptr)
+               throw runtime::Exception("Failed to create audit context");
+
+       ::audit_trail_add_system_log_cb(auditTrail, auditCallbackDispatcher,
+                       reinterpret_cast<void*>(this), &callbackId);
+}
+
+void AuditAnalyzer::auditCallbackDispatcher(void *log, void *data)
+{
+       AuditAnalyzer *analyzer = nullptr;
+       analyzer = reinterpret_cast<AuditAnalyzer *>(data);
+       analyzer->analysis(reinterpret_cast<SystemLog>(log));
+}
+
+void AuditAnalyzer::analysis(SystemLog log)
+{
+       //parse system logs and check suspicious elements
+       int ret = parseLog(log);
+       //if parseLog's return value is not -1,
+       if (ret != -1)
+               sendNotification(ret);
+}
+
+int AuditAnalyzer::parseLog(SystemLog log)
+{
+       int ret = -1;
+       unsigned int syscall = 0;
+       //test code
+       std::cout << "[Parse Log] : ";
+       ::audit_system_log_get_action_systemcall(log, &syscall);
+       std::cout << "syscall = " << syscall << std::endl;
+
+       //check logs
+       //return suspicious activity type. if not suspicious, return -1
+       return ret;
+}
+
+void AuditAnalyzer::sendNotification(int result)
+{
+       //call noti app
+}
diff --git a/tools/sample/analysis/analyzer.h b/tools/sample/analysis/analyzer.h
new file mode 100644 (file)
index 0000000..9712ffa
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_ANALYZER_H__
+#define __AUDIT_ANALYZER_H__
+
+#include <audit-trail/system-log.h>
+#include <klay/mainloop.h>
+#include <klay/exception.h>
+
+#include "type.h"
+
+class AuditAnalyzer final {
+public:
+       using AuditTrail = audit_trail_h;
+       using SystemLog = audit_system_log_h;
+       AuditAnalyzer();
+       ~AuditAnalyzer();
+
+       void run();
+       void terminate();
+
+private:
+       void runMonitor();
+       void analysis(SystemLog log);
+
+       int parseLog(SystemLog log);
+       void sendNotification(int result);
+       static void auditCallbackDispatcher(void *log, void *data);
+
+private:
+       runtime::Mainloop mainloop;
+       AuditTrail auditTrail;
+       int callbackId;
+};
+
+#endif /*__AUDIT_ANALYZER_H__*/
diff --git a/tools/sample/analysis/main.cpp b/tools/sample/analysis/main.cpp
new file mode 100644 (file)
index 0000000..3ab471b
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+#include <iostream>
+#include <sys/stat.h>
+#include <signal.h>
+#include <klay/gmainloop.h>
+#include "analyzer.h"
+
+void signalHandler(int signal)
+{
+       exit(0);
+}
+
+class AnalyzerGMainLoop {
+public:
+       AnalyzerGMainLoop() :
+               mainloop(::g_main_loop_new(NULL, FALSE), ::g_main_loop_unref)
+       {
+               handle = std::thread(g_main_loop_run, mainloop.get());
+       }
+       ~AnalyzerGMainLoop()
+       {
+               while (!g_main_loop_is_running(mainloop.get())) {
+                       std::this_thread::yield();
+               }
+               ::g_main_loop_quit(mainloop.get());
+               handle.join();
+       }
+private:
+       std::unique_ptr<GMainLoop, void(*)(GMainLoop*)> mainloop;
+       std::thread handle;
+};
+
+int main(int argc, char *argv[])
+{
+       ::signal(SIGINT, signalHandler);
+       ::umask(0);
+
+       try {
+               AnalyzerGMainLoop gmainloop;
+               AuditAnalyzer analyzer;
+               analyzer.run();
+       } catch (runtime::Exception &e) {
+               std::cout << "Error : " << e.what() << std::endl;
+               return -1;
+       }
+       return EXIT_SUCCESS;
+}
diff --git a/tools/sample/analysis/type.h b/tools/sample/analysis/type.h
new file mode 100644 (file)
index 0000000..3a64084
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#ifndef __AUDIT_SUSPICIOUS_TYPE_H__
+#define __AUDIT_SUSPICIOUS_TYPE_H__
+
+enum SuspiciousActivity {
+       UnixSocketHijacking = 0,
+       BruteForceForDMCrypt,
+       ModifyFilePermForDAC,
+       ModifyFilePermForMAC,
+       ModifyMACPolicy,
+       MountDevice,
+       ModifyLibraries,
+       UseKernelModule,
+       Debugging,
+       PrivilegeEscalation,
+       Invalid,
+};
+
+#endif /*__AUDIT_SUSPICIOUS_TYPE_H__*/