| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.util; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.RandomAccessFile; |
| 10 |
import java.nio.MappedByteBuffer; |
| 11 |
import java.nio.channels.FileChannel.MapMode; |
| 12 |
import java.nio.charset.StandardCharsets; |
| 13 |
|
| 14 |
import org.h2.engine.Constants; |
| 15 |
import org.h2.security.SHA256; |
| 16 |
import org.h2.store.fs.FileUtils; |
| 17 |
import org.h2.util.MathUtils; |
| 18 |
import org.h2.util.StringUtils; |
| 19 |
import org.h2.util.Utils; |
| 20 |
|
| 21 |
/** |
| 22 |
* A tool that removes passwords from an unencrypted database. |
| 23 |
*/ |
| 24 |
public class RemovePasswords { |
| 25 |
|
| 26 |
/** |
| 27 |
* Run the tool. |
| 28 |
* |
| 29 |
* @param args the command line arguments |
| 30 |
*/ |
| 31 |
public static void main(String... args) throws Exception { |
| 32 |
execute(args[0]); |
| 33 |
} |
| 34 |
|
| 35 |
private static void execute(String fileName) throws IOException { |
| 36 |
fileName = FileUtils.toRealPath(fileName); |
| 37 |
RandomAccessFile f = new RandomAccessFile(fileName, "rw"); |
| 38 |
long length = f.length(); |
| 39 |
MappedByteBuffer buff = f.getChannel() |
| 40 |
.map(MapMode.READ_WRITE, 0, length); |
| 41 |
byte[] data = new byte[200]; |
| 42 |
for (int i = 0; i < length - 200; i++) { |
| 43 |
if (buff.get(i) != 'C' || buff.get(i + 1) != 'R' || |
| 44 |
buff.get(i + 7) != 'U' || buff.get(i + 8) != 'S') { |
| 45 |
continue; |
| 46 |
} |
| 47 |
buff.position(i); |
| 48 |
buff.get(data); |
| 49 |
String s = new String(data, StandardCharsets.UTF_8); |
| 50 |
if (!s.startsWith("CREATE USER ")) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
int saltIndex = Utils.indexOf(s.getBytes(), "SALT ".getBytes(), 0); |
| 54 |
if (saltIndex < 0) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
String userName = s.substring("CREATE USER ".length(), |
| 58 |
s.indexOf("SALT ") - 1); |
| 59 |
if (userName.startsWith("IF NOT EXISTS ")) { |
| 60 |
userName = userName.substring("IF NOT EXISTS ".length()); |
| 61 |
} |
| 62 |
if (userName.startsWith("\"")) { |
| 63 |
// TODO doesn't work for all cases ("" inside |
| 64 |
// user name) |
| 65 |
userName = userName.substring(1, userName.length() - 1); |
| 66 |
} |
| 67 |
System.out.println("User: " + userName); |
| 68 |
byte[] userPasswordHash = SHA256.getKeyPasswordHash(userName, |
| 69 |
"".toCharArray()); |
| 70 |
byte[] salt = MathUtils.secureRandomBytes(Constants.SALT_LEN); |
| 71 |
byte[] passwordHash = SHA256 |
| 72 |
.getHashWithSalt(userPasswordHash, salt); |
| 73 |
StringBuilder b = new StringBuilder(); |
| 74 |
b.append("SALT '").append(StringUtils.convertBytesToHex(salt)) |
| 75 |
.append("' HASH '") |
| 76 |
.append(StringUtils.convertBytesToHex(passwordHash)) |
| 77 |
.append('\''); |
| 78 |
byte[] replacement = b.toString().getBytes(); |
| 79 |
buff.position(i + saltIndex); |
| 80 |
buff.put(replacement, 0, replacement.length); |
| 81 |
} |
| 82 |
f.close(); |
| 83 |
} |
| 84 |
|
| 85 |
} |