1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
| #!/usr/bin/env perl
# 言語: perl
# バージョン: 5.36以上
# 依存: Moo(cpanmでインストール)
use v5.36;
package IntrusionEvent;
use Moo;
has timestamp => (
is => 'ro',
required => 1,
);
has source_ip => (
is => 'ro',
required => 1,
);
has attack_type => (
is => 'ro',
required => 1,
);
package IntrusionObserver;
use Moo::Role;
requires 'update';
package RadarLogObserver;
use Moo;
with 'IntrusionObserver';
sub update ($self, $event) {
say "[ログ] " . $event->attack_type . " from " . $event->source_ip;
}
package ThreatScoreObserver;
use Moo;
with 'IntrusionObserver';
my %threat_scores = (
'SSH Brute Force' => 80,
'Port Scan' => 30,
'SQL Injection Attempt' => 90,
);
has total_score => (
is => 'rw',
default => 0,
);
sub update ($self, $event) {
my $score = $threat_scores{$event->attack_type} // 50;
$self->total_score($self->total_score + $score);
say "[スコア] 累計: " . $self->total_score;
}
# 新しいObserver(追加)
package RiskLevelObserver;
use Moo;
with 'IntrusionObserver';
my %risk_levels = (
'SSH Brute Force' => 'HIGH',
'Port Scan' => 'LOW',
'SQL Injection Attempt' => 'HIGH',
'XSS Attack' => 'MEDIUM',
);
sub update ($self, $event) {
my $level = $risk_levels{$event->attack_type} // 'MEDIUM';
my $indicator = $level eq 'HIGH' ? '!!!'
: $level eq 'MEDIUM' ? '!!'
: '!';
say "[$indicator リスクレベル: $level] " . $event->attack_type;
}
package IntrusionHub;
use Moo;
has observers => (
is => 'ro',
default => sub { [] },
);
sub attach ($self, $observer) {
unless ($observer->does('IntrusionObserver')) {
die "Error: IntrusionObserverロールを実装していないオブジェクトは登録できません";
}
push $self->observers->@*, $observer;
}
sub detach ($self, $observer) {
$self->observers->@* = grep { $_ != $observer } $self->observers->@*;
}
sub notify ($self, $event) {
for my $observer ($self->observers->@*) {
$observer->update($event);
}
}
package main;
my $hub = IntrusionHub->new;
# 既存のObserver
$hub->attach(RadarLogObserver->new);
$hub->attach(ThreatScoreObserver->new);
# 新しいObserverを追加
$hub->attach(RiskLevelObserver->new);
my @events = (
IntrusionEvent->new(
timestamp => '2026-01-18T06:00:00+09:00',
source_ip => '192.168.1.100',
attack_type => 'SSH Brute Force',
),
IntrusionEvent->new(
timestamp => '2026-01-18T06:01:15+09:00',
source_ip => '10.0.0.55',
attack_type => 'Port Scan',
),
);
say "=== 侵入イベント処理 ===";
say "";
for my $event (@events) {
$hub->notify($event);
say "";
}
|