MISP (core software) - Open Source Threat Intelligence and Sharing Platform (formely known as Malware Information Sharing Platform) https://www.misp-project.org/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

315 lines
11 KiB

  1. --- app/Controller/AppController.php.orig 2012-09-01 10:28:02.000000000 +0200
  2. +++ app/Controller/AppController.php 2012-09-25 13:49:19.670674039 +0200
  3. @@ -20,7 +20,10 @@
  4. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  5. */
  6. +// TODO GPG encryption has issues when keys are expired
  7. +
  8. App::uses('Controller', 'Controller');
  9. +App::uses('Sanitize', 'Utility');
  10. /**
  11. * Application Controller
  12. @@ -32,4 +35,300 @@
  13. * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
  14. */
  15. class AppController extends Controller {
  16. +
  17. + public $components = array(
  18. + 'Session',
  19. + 'Auth' => array(
  20. + 'className' => 'SecureAuth',
  21. + 'authenticate' => array(
  22. + 'Form' => array(
  23. + 'fields' => array('username' => 'email')
  24. + )
  25. + ),
  26. + 'authError' => 'Did you really think you are allowed to see that?',
  27. + 'loginRedirect' => array('controller' => 'users', 'action' => 'routeafterlogin'),
  28. + 'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
  29. + 'authorize' => array('Controller') // Added this line
  30. + )
  31. + );
  32. +
  33. + public function isAuthorized($user) {
  34. + if (self::_isAdmin()) {
  35. + return true; // admin can access every action on every controller
  36. + }
  37. + return false; // The rest don't
  38. + }
  39. +
  40. + public function beforeFilter() {
  41. + // REST things
  42. + if ($this->_isRest()) {
  43. + // disable CSRF for REST access
  44. + if (array_key_exists('Security', $this->components))
  45. + $this->Security->csrfCheck = false;
  46. +
  47. + // Authenticate user with authkey in Authorization HTTP header
  48. + if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
  49. + $authkey = $_SERVER['HTTP_AUTHORIZATION'];
  50. + $this->loadModel('User');
  51. + $params = array(
  52. + 'conditions' => array('User.authkey' => $authkey),
  53. + 'recursive' => 0,
  54. + );
  55. + $user = $this->User->find('first', $params);
  56. +
  57. + if ($user) {
  58. + // User found in the db, add the user info to the session
  59. + $this->Session->renew();
  60. + $this->Session->write(AuthComponent::$sessionKey, $user['User']);
  61. + } else {
  62. + // User not authenticated correctly
  63. + // reset the session information
  64. + $this->Session->destroy();
  65. + throw new ForbiddenException('Incorrect authentication key');
  66. + }
  67. + }
  68. + }
  69. +
  70. + // These variables are required for every view
  71. + $this->set('me', $this->Auth->user());
  72. + $this->set('isAdmin', $this->_isAdmin());
  73. + }
  74. +
  75. + protected function _isRest() {
  76. + return (isset($this->RequestHandler) && $this->RequestHandler->isXml());
  77. + }
  78. +
  79. +/**
  80. + * Convert an array to the same array but with the values also as index instead of an interface_exists
  81. + */
  82. + protected function _arrayToValuesIndexArray($oldArray) {
  83. + $newArray = Array();
  84. + foreach ($oldArray as $value)
  85. + $newArray[$value] = $value;
  86. + return $newArray;
  87. + }
  88. +
  89. +/**
  90. + * checks if the currently logged user is an administrator
  91. + */
  92. + protected function _isAdmin() {
  93. + $org = $this->Auth->user('org');
  94. + if (isset($org) && $org === 'ADMIN') {
  95. + return true;
  96. + }
  97. + return false;
  98. + }
  99. +
  100. +/**
  101. + * Refreshes the Auth session with new/updated data
  102. + * @return void
  103. + */
  104. + protected function _refreshAuth() {
  105. + if (isset($this->User)) {
  106. + $user = $this->User->read(false, $this->Auth->user('id'));
  107. + } else {
  108. + $user = ClassRegistry::init('User')->findById($this->Auth->user('id'));
  109. + }
  110. + $this->Auth->login($user['User']);
  111. + }
  112. +
  113. +/**
  114. + * Updates the missing fields from v0.1 to v0.2 of CyDefSIG
  115. + * First you will need to manually update the database to the new schema.
  116. + * Log in as admin user and
  117. + * Then run this function by setting debug = 1 (or more) and call /events/migrate01to02
  118. + *
  119. + * @throws NotFoundException
  120. + */
  121. + public function migrate01to02() {
  122. + if (!self::_isAdmin()) throw new NotFoundException();
  123. +
  124. + // generate uuids for events who have no uuid
  125. + $this->loadModel('Event');
  126. + $params = array(
  127. + 'conditions' => array('Event.uuid' => ''),
  128. + 'recursive' => 0,
  129. + 'fields' => array('Event.id'),
  130. + );
  131. + $events = $this->Event->find('all', $params);
  132. +
  133. + echo '<p>Generating UUID for events: ';
  134. + foreach ($events as $event) {
  135. + $this->Event->id = $event['Event']['id'];
  136. + $this->Event->saveField('uuid', String::uuid());
  137. + echo $event['Event']['id'] . ' ';
  138. + }
  139. + echo "</p>";
  140. + // generate uuids for attributes who have no uuid
  141. + $this->loadModel('Attribute');
  142. + $params = array(
  143. + 'conditions' => array('Attribute.uuid' => ''),
  144. + 'recursive' => 0,
  145. + 'fields' => array('Attribute.id'),
  146. + );
  147. + $attributes = $this->Attribute->find('all', $params);
  148. + echo '<p>Generating UUID for attributes: ';
  149. + foreach ($attributes as $attribute) {
  150. + $this->Attribute->id = $attribute['Attribute']['id'];
  151. + $this->Attribute->saveField('uuid', String::uuid());
  152. + echo $attribute['Attribute']['id'] . ' ';
  153. + }
  154. + echo "</p>";
  155. + }
  156. +
  157. +/**
  158. + * Updates the missing fields from v0.2 to v0.2.1 of CyDefSIG
  159. + * First you will need to manually update the database to the new schema.
  160. + * Log in as admin user and
  161. + * Then run this function by setting debug = 1 (or more) and call /events/migrate02to021
  162. + */
  163. + private function __explodeValueToValues() {
  164. + // search for composite value1 fields and explode it to value1 and value2
  165. + $this->loadModel('Attribute');
  166. + $params = array(
  167. + 'conditions' => array(
  168. + 'OR' => array(
  169. + 'Attribute.type' => $this->Attribute->getCompositeTypes()
  170. + )
  171. + ),
  172. + 'recursive' => 0,
  173. + 'fields' => array('Attribute.id', 'Attribute.value1'),
  174. + );
  175. + $attributes = $this->Attribute->find('all', $params);
  176. + echo '<h2>Exploding composite fields in 2 columns: </h2><ul>';
  177. + foreach ($attributes as $attribute) {
  178. + $pieces = explode('|', $attribute['Attribute']['value1']);
  179. + if (2 != count($pieces)) continue; // do nothing if not 2 pieces
  180. +
  181. + $this->Attribute->id = $attribute['Attribute']['id'];
  182. + echo '<li>' . $attribute['Attribute']['id'] . ' --> ' . $attribute['Attribute']['value1'] . ' --> ' . $pieces[0] . ' --> ' . $pieces[1] . '</li> ';
  183. + $this->Attribute->saveField('value1', $pieces[0]);
  184. + $this->Attribute->id = $attribute['Attribute']['id'];
  185. + $this->Attribute->saveField('value2', $pieces[1]);
  186. + }
  187. + echo "</ul> DONE.";
  188. + }
  189. +
  190. + public function migrate02to021() {
  191. + if (!self::_isAdmin()) {
  192. + throw new NotFoundException();
  193. + }
  194. +
  195. + // search for composite value1 fields and explode it to value1 and value2
  196. + $this->__explodeValueToValues();
  197. + }
  198. +
  199. + public function migrate021to022() {
  200. + if (!self::_isAdmin()) throw new NotFoundException();
  201. +
  202. + // replace description by comment
  203. +
  204. + // replace empty category
  205. + // not easy as we have to guess the category from the type
  206. + //$this->loadModel('Attribute');
  207. + // $params = array(
  208. + // 'conditions' => array('Attribute.type' => ''),
  209. + // 'recursive' => 0,
  210. + // 'fields' => array('Attribute.id'),
  211. + // );
  212. + // $attributes = $this->Attribute->find('all', $params);
  213. + // echo '<p>Replacing empty categories by OtherExploding composite fields in 2 columns: </p><ul>';
  214. + // foreach ($attributes as $attribute) {
  215. + // $pieces = explode('|', $attribute['Attribute']['value1']);
  216. + // if (2 != sizeof($pieces)) continue; // do nothing if not 2 pieces
  217. +
  218. + // $this->Attribute->id = $attribute['Attribute']['id'];
  219. + // echo '<li>'.$attribute['Attribute']['id'].' --> '.$attribute['Attribute']['value1'].' --> '.$pieces[0].' --> '.$pieces[1].'</li> ';
  220. + // $this->Attribute->saveField('value1', $pieces[0]);
  221. + // $this->Attribute->id = $attribute['Attribute']['id'];
  222. + // $this->Attribute->saveField('value2', $pieces[1]);
  223. + // }
  224. + // echo "</ul> DONE</p>";
  225. +
  226. + // search for incompatible combination of category / type
  227. + }
  228. +
  229. + public function migratemisp02to10() {
  230. + if (!self::_isAdmin()) {
  231. + throw new NotFoundException();
  232. + }
  233. +
  234. + // add missing columns, rename other columns
  235. + $queries = array(
  236. + // ATTRIBUTES
  237. + // rename value to value1
  238. + "ALTER TABLE `attributes` CHANGE `value` `value1` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL "
  239. + // add value2
  240. + ,"ALTER TABLE `attributes` ADD `value2` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL AFTER `value1` "
  241. + // fix the keys
  242. + ,"ALTER TABLE `attributes` DROP INDEX `uuid`;"
  243. + ,"ALTER TABLE `attributes` ADD INDEX `value1_key` ( `value1` ( 5 ) ) ;"
  244. + ,"ALTER TABLE `attributes` ADD INDEX `value2_key` ( `value2` ( 5 ) ) ;"
  245. + // EVENTS
  246. + // remove useless things
  247. + ,"ALTER TABLE `events` DROP `user_id`"
  248. + ,"ALTER TABLE `events` DROP `alerted`"
  249. + ,"ALTER TABLE `events` ADD `revision` INT( 10 ) NOT NULL DEFAULT '0' AFTER `uuid` "
  250. + // fix the keys
  251. + ,"ALTER TABLE events DROP INDEX uuid"
  252. + ,"ALTER TABLE events DROP INDEX info"
  253. + // SERVERS
  254. + // rename lastfetchedid to lastpushedid
  255. + ,"ALTER TABLE `servers` CHANGE `lastfetchedid` `lastpushedid` INT( 11 ) NOT NULL "
  256. + // add lastpulledid
  257. + ,"ALTER TABLE `servers` ADD `lastpulledid` INT( 11 ) NOT NULL AFTER `lastpushedid` "
  258. + // USERS
  259. + // fix keys
  260. + ,"ALTER TABLE `users` DROP INDEX `username`"
  261. + ,"ALTER TABLE `users` ADD INDEX `email` ( `email` ) "
  262. + );
  263. + // execute the queries
  264. + foreach ($queries as &$query) {
  265. + $result = $this->{$this->modelClass}->query($query);
  266. + }
  267. + }
  268. +
  269. + public function migratemisp10to11() {
  270. + if (!self::_isAdmin()) {
  271. + throw new NotFoundException();
  272. + }
  273. +
  274. + // add missing columns, rename other columns
  275. + $queries = array(
  276. + // EVENTS
  277. + // bring user_id back in
  278. + "ALTER TABLE `events` ADD `user_id` INT( 11 ) NOT NULL AFTER `info` "
  279. + );
  280. + // execute the queries
  281. + foreach ($queries as &$query) {
  282. + $result = $this->{$this->modelClass}->query($query);
  283. + }
  284. + }
  285. +
  286. + public function generateCorrelation() {
  287. + if (!self::_isAdmin()) throw new NotFoundException();
  288. +
  289. + $this->loadModel('Correlation');
  290. + $this->loadModel('Attribute');
  291. + $fields = array('Attribute.id', 'Attribute.event_id', 'Event.date');
  292. + // get all attributes..
  293. + $attributes = $this->Attribute->find('all',array('recursive' => 0));
  294. + // for all attributes..
  295. + foreach ($attributes as $attribute) {
  296. + $this->Attribute->setRelatedAttributes($attribute['Attribute'], $fields = array());
  297. +
  298. + //// i want to keep this in repo for a moment
  299. + //$relatedAttributes = $this->Attribute->getRelatedAttributes($attribute['Attribute'], $fields);
  300. + //if ($relatedAttributes) {
  301. + // foreach ($relatedAttributes as $relatedAttribute) {
  302. + // // and store into table
  303. + // $this->Correlation->create();
  304. + // $this->Correlation->save(array('Correlation' => array(
  305. + // '1_event_id' => $attribute['Attribute']['event_id'], '1_attribute_id' => $attribute['Attribute']['id'],
  306. + // 'event_id' => $relatedAttribute['Attribute']['event_id'], 'attribute_id' => $relatedAttribute['Attribute']['id'],
  307. + // 'date' => $relatedAttribute['Event']['date'])));
  308. + // }
  309. + //}
  310. + }
  311. + }
  312. }